blob: 0ef9706668b8260ea1a8a3871c62a7b3e17279bb [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
Dale Johannesenc4342ea2008-09-22 19:51:58 +000037static void EnsureFPIntrinsicsExist(Module &M, Module::iterator I,
38 const char *FName,
39 const char *DName, const char *LDName) {
40 // Insert definitions for all the floating point types.
41 switch((int)I->arg_begin()->getType()->getTypeID()) {
42 case Type::FloatTyID:
43 EnsureFunctionExists(M, FName, I->arg_begin(), I->arg_end(),
44 Type::FloatTy);
45 case Type::DoubleTyID:
46 EnsureFunctionExists(M, DName, I->arg_begin(), I->arg_end(),
47 Type::DoubleTy);
48 case Type::X86_FP80TyID:
49 case Type::FP128TyID:
50 case Type::PPC_FP128TyID:
51 EnsureFunctionExists(M, LDName, I->arg_begin(), I->arg_end(),
52 I->arg_begin()->getType());
53 }
54}
55
Chris Lattner588e72d2004-02-15 22:16:39 +000056/// ReplaceCallWith - This function is used when we want to lower an intrinsic
57/// call to a call of an external function. This handles hard cases such as
58/// when there was already a prototype for the external function, and if that
59/// prototype doesn't match the arguments we expect to pass in.
60template <class ArgIt>
61static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000062 ArgIt ArgBegin, ArgIt ArgEnd,
63 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000064 if (!FCache) {
65 // If we haven't already looked up this function, check to see if the
66 // program already contains a function with this name.
67 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000068 // Get or insert the definition now.
69 std::vector<const Type *> ParamTys;
70 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
71 ParamTys.push_back((*I)->getType());
72 FCache = M->getOrInsertFunction(NewFn,
73 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000074 }
Chris Lattner588e72d2004-02-15 22:16:39 +000075
David Greene52eec542007-08-01 03:43:44 +000076 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Gabor Greif051a9502008-04-06 20:25:17 +000077 CallInst *NewCI = CallInst::Create(FCache, Args.begin(), Args.end(),
78 CI->getName(), CI);
Chris Lattnerb76efb72007-01-07 08:12:01 +000079 if (!CI->use_empty())
80 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000081 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000082}
83
Chris Lattnerb71fd782006-11-15 18:00:10 +000084void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000085 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000086 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000087 switch (I->getIntrinsicID()) {
88 default: break;
89 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000090 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000091 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000092 break;
93 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000094 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
95 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000096 break;
97 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000098 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
99 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000100 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000101 case Intrinsic::memcpy:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000102 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
103 PointerType::getUnqual(Type::Int8Ty),
104 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000105 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000106 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000107 case Intrinsic::memmove:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000108 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
109 PointerType::getUnqual(Type::Int8Ty),
110 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000111 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000112 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000113 case Intrinsic::memset:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000114 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
115 PointerType::getUnqual(Type::Int8Ty),
116 Type::Int32Ty,
Reid Spencer6addf2c2007-01-29 17:42:06 +0000117 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000118 break;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000119 case Intrinsic::sqrt:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000120 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000121 break;
Dan Gohmanc4c96602007-10-15 22:07:31 +0000122 case Intrinsic::sin:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000123 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000124 break;
125 case Intrinsic::cos:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000126 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000127 break;
128 case Intrinsic::pow:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000129 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000130 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000131 case Intrinsic::log:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000133 break;
134 case Intrinsic::log2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000136 break;
137 case Intrinsic::log10:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000139 break;
140 case Intrinsic::exp:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000142 break;
143 case Intrinsic::exp2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000145 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000146 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000147}
Chris Lattner588e72d2004-02-15 22:16:39 +0000148
Nate Begemane5981812006-01-16 07:57:00 +0000149/// LowerBSWAP - Emit the code to lower bswap of V before the specified
150/// instruction IP.
151static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000152 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000153
Nate Begemane5981812006-01-16 07:57:00 +0000154 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
155
156 switch(BitSize) {
157 default: assert(0 && "Unhandled type size of value to byteswap!");
158 case 16: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000159 Value *Tmp1 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000160 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000161 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000162 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000163 V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000164 break;
165 }
166 case 32: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000167 Value *Tmp4 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000168 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000169 Value *Tmp3 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000170 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000171 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000172 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000173 Value *Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000174 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000175 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000176 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000177 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000178 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000179 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000180 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000181 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP);
182 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP);
183 V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000184 break;
185 }
186 case 64: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000187 Value *Tmp8 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000188 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000189 Value *Tmp7 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000190 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000191 Value *Tmp6 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000192 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000193 Value *Tmp5 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000194 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000195 Value* Tmp4 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000196 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000197 Value* Tmp3 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000198 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000199 Value* Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000200 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000201 Value* Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000202 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000203 Tmp7 = BinaryOperator::CreateAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000204 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000205 0xFF000000000000ULL),
206 "bswap.and7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000207 Tmp6 = BinaryOperator::CreateAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000208 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000209 "bswap.and6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000210 Tmp5 = BinaryOperator::CreateAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000211 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000212 "bswap.and5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000213 Tmp4 = BinaryOperator::CreateAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000214 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000215 "bswap.and4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000216 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000217 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000218 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000219 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000220 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000221 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000222 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP);
223 Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP);
224 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP);
225 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP);
226 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP);
227 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP);
228 V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000229 break;
230 }
231 }
Nate Begemane5981812006-01-16 07:57:00 +0000232 return V;
233}
234
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000235/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000236/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000237static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000238 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000239
240 static const uint64_t MaskValues[6] = {
241 0x5555555555555555ULL, 0x3333333333333333ULL,
242 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
243 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
244 };
245
Chris Lattner98cf45b2005-05-11 20:24:12 +0000246 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000247 unsigned WordSize = (BitSize + 63) / 64;
248 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencer3822ff52006-11-08 06:47:33 +0000249
Zhou Sheng02031c02007-06-02 04:10:33 +0000250 for (unsigned n = 0; n < WordSize; ++n) {
251 Value *PartValue = V;
252 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
253 i <<= 1, ++ct) {
254 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000255 Value *LHS = BinaryOperator::CreateAnd(
Zhou Sheng02031c02007-06-02 04:10:33 +0000256 PartValue, MaskCst, "cppop.and1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000257 Value *VShift = BinaryOperator::CreateLShr(PartValue,
Zhou Sheng02031c02007-06-02 04:10:33 +0000258 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000259 Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP);
260 PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000261 }
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000262 Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000263 if (BitSize > 64) {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000264 V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64),
Zhou Sheng02031c02007-06-02 04:10:33 +0000265 "ctpop.part.sh", IP);
266 BitSize -= 64;
267 }
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000268 }
269
Chris Lattner914ce452007-08-06 16:36:18 +0000270 return Count;
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000271}
272
Chris Lattner98cf45b2005-05-11 20:24:12 +0000273/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000274/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000275static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000276
277 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000278 for (unsigned i = 1; i < BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000279 Value *ShVal = ConstantInt::get(V->getType(), i);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000280 ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP);
281 V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000282 }
283
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000284 V = BinaryOperator::CreateNot(V, "", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000285 return LowerCTPOP(V, IP);
286}
287
Reid Spencerf75b8742007-04-12 02:48:46 +0000288/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
289/// three integer arguments. The first argument is the Value from which the
290/// bits will be selected. It may be of any bit width. The second and third
291/// arguments specify a range of bits to select with the second argument
292/// specifying the low bit and the third argument specifying the high bit. Both
293/// must be type i32. The result is the corresponding selected bits from the
294/// Value in the same width as the Value (first argument). If the low bit index
295/// is higher than the high bit index then the inverse selection is done and
296/// the bits are returned in inverse order.
297/// @brief Lowering of llvm.part.select intrinsic.
298static Instruction *LowerPartSelect(CallInst *CI) {
Reid Spenceraddd11d2007-04-04 23:48:25 +0000299 // Make sure we're dealing with a part select intrinsic here
300 Function *F = CI->getCalledFunction();
301 const FunctionType *FT = F->getFunctionType();
302 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
303 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
304 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
305 return CI;
306
307 // Get the intrinsic implementation function by converting all the . to _
308 // in the intrinsic's function name and then reconstructing the function
309 // declaration.
310 std::string Name(F->getName());
311 for (unsigned i = 4; i < Name.length(); ++i)
312 if (Name[i] == '.')
313 Name[i] = '_';
314 Module* M = F->getParent();
315 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Reid Spencerdf413532007-04-12 21:53:38 +0000316 F->setLinkage(GlobalValue::WeakLinkage);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000317
318 // If we haven't defined the impl function yet, do so now
319 if (F->isDeclaration()) {
320
321 // Get the arguments to the function
Reid Spencereeedcb62007-04-12 13:30:14 +0000322 Function::arg_iterator args = F->arg_begin();
323 Value* Val = args++; Val->setName("Val");
324 Value* Lo = args++; Lo->setName("Lo");
Gabor Greif051a9502008-04-06 20:25:17 +0000325 Value* Hi = args++; Hi->setName("High");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000326
Reid Spencereeedcb62007-04-12 13:30:14 +0000327 // We want to select a range of bits here such that [Hi, Lo] is shifted
328 // down to the low bits. However, it is quite possible that Hi is smaller
329 // than Lo in which case the bits have to be reversed.
Reid Spenceraddd11d2007-04-04 23:48:25 +0000330
331 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greif051a9502008-04-06 20:25:17 +0000332 BasicBlock* CurBB = BasicBlock::Create("entry", F);
333 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
334 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
335 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
336 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
337 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Reid Spenceraddd11d2007-04-04 23:48:25 +0000338
Reid Spencereeedcb62007-04-12 13:30:14 +0000339 // Cast Hi and Lo to the size of Val so the widths are all the same
340 if (Hi->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000341 Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000342 "tmp", CurBB);
Reid Spencereeedcb62007-04-12 13:30:14 +0000343 if (Lo->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000344 Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000345 "tmp", CurBB);
346
347 // Compute a few things that both cases will need, up front.
348 Constant* Zero = ConstantInt::get(Val->getType(), 0);
349 Constant* One = ConstantInt::get(Val->getType(), 1);
350 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
351
Reid Spencereeedcb62007-04-12 13:30:14 +0000352 // Compare the Hi and Lo bit positions. This is used to determine
Reid Spenceraddd11d2007-04-04 23:48:25 +0000353 // which case we have (forward or reverse)
Reid Spencereeedcb62007-04-12 13:30:14 +0000354 ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Hi, Lo, "less",CurBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000355 BranchInst::Create(RevSize, FwdSize, Cmp, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000356
357 // First, copmute the number of bits in the forward case.
358 Instruction* FBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000359 BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000360 BranchInst::Create(Compute, FwdSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000361
362 // Second, compute the number of bits in the reverse case.
363 Instruction* RBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000364 BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000365 BranchInst::Create(Compute, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000366
367 // Now, compute the bit range. Start by getting the bitsize and the shift
Reid Spencereeedcb62007-04-12 13:30:14 +0000368 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
Reid Spenceraddd11d2007-04-04 23:48:25 +0000369 // the number of bits we want in the range. We shift the bits down to the
370 // least significant bits, apply the mask to zero out unwanted high bits,
371 // and we have computed the "forward" result. It may still need to be
372 // reversed.
373
374 // Get the BitSize from one of the two subtractions
Gabor Greif051a9502008-04-06 20:25:17 +0000375 PHINode *BitSize = PHINode::Create(Val->getType(), "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000376 BitSize->reserveOperandSpace(2);
377 BitSize->addIncoming(FBitSize, FwdSize);
378 BitSize->addIncoming(RBitSize, RevSize);
379
Reid Spencereeedcb62007-04-12 13:30:14 +0000380 // Get the ShiftAmount as the smaller of Hi/Lo
Gabor Greif051a9502008-04-06 20:25:17 +0000381 PHINode *ShiftAmt = PHINode::Create(Val->getType(), "shiftamt", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000382 ShiftAmt->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000383 ShiftAmt->addIncoming(Lo, FwdSize);
384 ShiftAmt->addIncoming(Hi, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000385
386 // Increment the bit size
387 Instruction *BitSizePlusOne =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000388 BinaryOperator::CreateAdd(BitSize, One, "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000389
390 // Create a Mask to zero out the high order bits.
391 Instruction* Mask =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000392 BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute);
393 Mask = BinaryOperator::CreateNot(Mask, "mask", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000394
395 // Shift the bits down and apply the mask
396 Instruction* FRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000397 BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute);
398 FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute);
Gabor Greif051a9502008-04-06 20:25:17 +0000399 BranchInst::Create(Reverse, RsltBlk, Cmp, Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000400
401 // In the Reverse block we have the mask already in FRes but we must reverse
402 // it by shifting FRes bits right and putting them in RRes by shifting them
403 // in from left.
404
405 // First set up our loop counters
Gabor Greif051a9502008-04-06 20:25:17 +0000406 PHINode *Count = PHINode::Create(Val->getType(), "count", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000407 Count->reserveOperandSpace(2);
408 Count->addIncoming(BitSizePlusOne, Compute);
409
410 // Next, get the value that we are shifting.
Gabor Greif051a9502008-04-06 20:25:17 +0000411 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000412 BitsToShift->reserveOperandSpace(2);
413 BitsToShift->addIncoming(FRes, Compute);
414
415 // Finally, get the result of the last computation
Gabor Greif051a9502008-04-06 20:25:17 +0000416 PHINode *RRes = PHINode::Create(Val->getType(), "rres", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000417 RRes->reserveOperandSpace(2);
418 RRes->addIncoming(Zero, Compute);
419
420 // Decrement the counter
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000421 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000422 Count->addIncoming(Decr, Reverse);
423
424 // Compute the Bit that we want to move
425 Instruction *Bit =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000426 BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000427
428 // Compute the new value for next iteration.
429 Instruction *NewVal =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000430 BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000431 BitsToShift->addIncoming(NewVal, Reverse);
432
433 // Shift the bit into the low bits of the result.
434 Instruction *NewRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000435 BinaryOperator::CreateShl(RRes, One, "lshift", Reverse);
436 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000437 RRes->addIncoming(NewRes, Reverse);
438
439 // Terminate loop if we've moved all the bits.
440 ICmpInst *Cond =
441 new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000442 BranchInst::Create(RsltBlk, Reverse, Cond, Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000443
444 // Finally, in the result block, select one of the two results with a PHI
445 // node and return the result;
446 CurBB = RsltBlk;
Gabor Greif051a9502008-04-06 20:25:17 +0000447 PHINode *BitSelect = PHINode::Create(Val->getType(), "part_select", CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000448 BitSelect->reserveOperandSpace(2);
449 BitSelect->addIncoming(FRes, Compute);
450 BitSelect->addIncoming(NewRes, Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000451 ReturnInst::Create(BitSelect, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000452 }
453
454 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000455 Value *Args[] = {
456 CI->getOperand(1),
457 CI->getOperand(2),
458 CI->getOperand(3)
459 };
Gabor Greif051a9502008-04-06 20:25:17 +0000460 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000461}
462
Reid Spencerf75b8742007-04-12 02:48:46 +0000463/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
464/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
465/// The first two arguments can be any bit width. The result is the same width
466/// as %Value. The operation replaces bits between %Low and %High with the value
467/// in %Replacement. If %Replacement is not the same width, it is truncated or
468/// zero extended as appropriate to fit the bits being replaced. If %Low is
469/// greater than %High then the inverse set of bits are replaced.
470/// @brief Lowering of llvm.bit.part.set intrinsic.
471static Instruction *LowerPartSet(CallInst *CI) {
472 // Make sure we're dealing with a part select intrinsic here
473 Function *F = CI->getCalledFunction();
474 const FunctionType *FT = F->getFunctionType();
475 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
476 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
477 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
478 !FT->getParamType(3)->isInteger())
479 return CI;
480
481 // Get the intrinsic implementation function by converting all the . to _
482 // in the intrinsic's function name and then reconstructing the function
483 // declaration.
484 std::string Name(F->getName());
485 for (unsigned i = 4; i < Name.length(); ++i)
486 if (Name[i] == '.')
487 Name[i] = '_';
488 Module* M = F->getParent();
489 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Reid Spencerdf413532007-04-12 21:53:38 +0000490 F->setLinkage(GlobalValue::WeakLinkage);
Reid Spencerf75b8742007-04-12 02:48:46 +0000491
492 // If we haven't defined the impl function yet, do so now
493 if (F->isDeclaration()) {
Reid Spencerf75b8742007-04-12 02:48:46 +0000494 // Get the arguments for the function.
495 Function::arg_iterator args = F->arg_begin();
496 Value* Val = args++; Val->setName("Val");
497 Value* Rep = args++; Rep->setName("Rep");
498 Value* Lo = args++; Lo->setName("Lo");
499 Value* Hi = args++; Hi->setName("Hi");
500
501 // Get some types we need
502 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
503 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
504 uint32_t ValBits = ValTy->getBitWidth();
505 uint32_t RepBits = RepTy->getBitWidth();
506
507 // Constant Definitions
508 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
509 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
510 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
Reid Spencer76c94b62007-05-15 02:26:52 +0000511 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
512 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
513 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
514 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000515
Reid Spencer76c94b62007-05-15 02:26:52 +0000516 // Basic blocks we fill in below.
Gabor Greif051a9502008-04-06 20:25:17 +0000517 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
518 BasicBlock* large = BasicBlock::Create("large", F, 0);
519 BasicBlock* small = BasicBlock::Create("small", F, 0);
520 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
521 BasicBlock* result = BasicBlock::Create("result", F, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000522
Reid Spencer76c94b62007-05-15 02:26:52 +0000523 // BASIC BLOCK: entry
Reid Spencereeedcb62007-04-12 13:30:14 +0000524 // First, get the number of bits that we're placing as an i32
525 ICmpInst* is_forward =
526 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000527 SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry);
528 SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000529 BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
530 NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
Reid Spencereeedcb62007-04-12 13:30:14 +0000531 // Now, convert Lo and Hi to ValTy bit width
Reid Spencerf75b8742007-04-12 02:48:46 +0000532 if (ValBits > 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000533 Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000534 } else if (ValBits < 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000535 Lo = new TruncInst(Lo_pn, ValTy, "", entry);
Zhou Sheng904ebf92009-01-30 09:02:50 +0000536 } else
537 Lo = Lo_pn;
Reid Spencereeedcb62007-04-12 13:30:14 +0000538 // Determine if the replacement bits are larger than the number of bits we
539 // are replacing and deal with it.
Reid Spencerf75b8742007-04-12 02:48:46 +0000540 ICmpInst* is_large =
541 new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000542 BranchInst::Create(large, small, is_large, entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000543
Reid Spencer76c94b62007-05-15 02:26:52 +0000544 // BASIC BLOCK: large
Reid Spencer9a9203b2007-04-16 22:21:14 +0000545 Instruction* MaskBits =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000546 BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large);
547 MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(),
Reid Spencer9a9203b2007-04-16 22:21:14 +0000548 false, "", large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000549 BinaryOperator* Mask1 =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000550 BinaryOperator::CreateLShr(RepMask, MaskBits, "", large);
551 BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large);
Gabor Greif051a9502008-04-06 20:25:17 +0000552 BranchInst::Create(small, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000553
Reid Spencer76c94b62007-05-15 02:26:52 +0000554 // BASIC BLOCK: small
Gabor Greif051a9502008-04-06 20:25:17 +0000555 PHINode* Rep3 = PHINode::Create(RepTy, "", small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000556 Rep3->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000557 Rep3->addIncoming(Rep2, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000558 Rep3->addIncoming(Rep, entry);
Reid Spencer37958092007-04-12 12:46:33 +0000559 Value* Rep4 = Rep3;
560 if (ValBits > RepBits)
561 Rep4 = new ZExtInst(Rep3, ValTy, "", small);
562 else if (ValBits < RepBits)
563 Rep4 = new TruncInst(Rep3, ValTy, "", small);
Gabor Greif051a9502008-04-06 20:25:17 +0000564 BranchInst::Create(result, reverse, is_forward, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000565
Reid Spencer76c94b62007-05-15 02:26:52 +0000566 // BASIC BLOCK: reverse (reverses the bits of the replacement)
567 // Set up our loop counter as a PHI so we can decrement on each iteration.
568 // We will loop for the number of bits in the replacement value.
Gabor Greif051a9502008-04-06 20:25:17 +0000569 PHINode *Count = PHINode::Create(Type::Int32Ty, "count", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000570 Count->reserveOperandSpace(2);
571 Count->addIncoming(NumBits, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000572
Reid Spencer76c94b62007-05-15 02:26:52 +0000573 // Get the value that we are shifting bits out of as a PHI because
574 // we'll change this with each iteration.
Gabor Greif051a9502008-04-06 20:25:17 +0000575 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000576 BitsToShift->reserveOperandSpace(2);
577 BitsToShift->addIncoming(Rep4, small);
578
579 // Get the result of the last computation or zero on first iteration
Gabor Greif051a9502008-04-06 20:25:17 +0000580 PHINode *RRes = PHINode::Create(Val->getType(), "rres", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000581 RRes->reserveOperandSpace(2);
582 RRes->addIncoming(ValZero, small);
583
584 // Decrement the loop counter by one
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000585 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000586 Count->addIncoming(Decr, reverse);
587
588 // Get the bit that we want to move into the result
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000589 Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000590
591 // Compute the new value of the bits to shift for the next iteration.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000592 Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000593 BitsToShift->addIncoming(NewVal, reverse);
594
595 // Shift the bit we extracted into the low bit of the result.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000596 Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse);
597 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000598 RRes->addIncoming(NewRes, reverse);
599
600 // Terminate loop if we've moved all the bits.
601 ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000602 BranchInst::Create(result, reverse, Cond, reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000603
604 // BASIC BLOCK: result
Gabor Greif051a9502008-04-06 20:25:17 +0000605 PHINode *Rplcmnt = PHINode::Create(Val->getType(), "", result);
Reid Spencer76c94b62007-05-15 02:26:52 +0000606 Rplcmnt->reserveOperandSpace(2);
607 Rplcmnt->addIncoming(NewRes, reverse);
608 Rplcmnt->addIncoming(Rep4, small);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000609 Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result);
610 Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result);
611 Value* t2 = BinaryOperator::CreateNot(t1, "", result);
612 Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result);
613 Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result);
614 Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result);
615 Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result);
616 Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result);
Gabor Greif051a9502008-04-06 20:25:17 +0000617 ReturnInst::Create(Rslt, result);
Reid Spencerf75b8742007-04-12 02:48:46 +0000618 }
619
620 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000621 Value *Args[] = {
622 CI->getOperand(1),
623 CI->getOperand(2),
624 CI->getOperand(3),
625 CI->getOperand(4)
626 };
Gabor Greif051a9502008-04-06 20:25:17 +0000627 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spencerf75b8742007-04-12 02:48:46 +0000628}
629
Dale Johannesenf74185b2008-09-22 20:51:30 +0000630static void ReplaceFPIntrinsicWithCall(CallInst *CI, Constant *FCache,
631 Constant *DCache, Constant *LDCache,
632 const char *Fname, const char *Dname,
633 const char *LDname) {
634 switch (CI->getOperand(1)->getType()->getTypeID()) {
635 default: assert(0 && "Invalid type in intrinsic"); abort();
636 case Type::FloatTyID:
637 ReplaceCallWith(Fname, CI, CI->op_begin()+1, CI->op_end(),
638 Type::FloatTy, FCache);
639 break;
640 case Type::DoubleTyID:
641 ReplaceCallWith(Dname, CI, CI->op_begin()+1, CI->op_end(),
642 Type::DoubleTy, DCache);
643 break;
644 case Type::X86_FP80TyID:
645 case Type::FP128TyID:
646 case Type::PPC_FP128TyID:
647 ReplaceCallWith(LDname, CI, CI->op_begin()+1, CI->op_end(),
648 CI->getOperand(1)->getType(), LDCache);
649 break;
650 }
651}
Reid Spenceraddd11d2007-04-04 23:48:25 +0000652
Chris Lattnerb71fd782006-11-15 18:00:10 +0000653void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000654 Function *Callee = CI->getCalledFunction();
655 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000656
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000657 switch (Callee->getIntrinsicID()) {
658 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000659 cerr << "Cannot lower a call to a non-intrinsic function '"
660 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000661 abort();
662 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000663 cerr << "Error: Code generator does not support intrinsic function '"
664 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000665 abort();
666
Chris Lattner588e72d2004-02-15 22:16:39 +0000667 // The setjmp/longjmp intrinsics should only exist in the code if it was
668 // never optimized (ie, right out of the CFE), or if it has been hacked on
669 // by the lowerinvoke pass. In both cases, the right thing to do is to
670 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000671 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000672 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000673 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000674 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000675 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000676 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000677 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000678 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000679 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000680 if (CI->getType() != Type::VoidTy)
681 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
682 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000683
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000684 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000685 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000686 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000687 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000688 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000689 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000690
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000691 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000692 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000693 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000694 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000695 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000696 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000697 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000698 case Intrinsic::ctpop:
Reid Spencer0b118202006-01-16 21:12:35 +0000699 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
700 break;
701
Reid Spencere9391fd2007-04-01 07:35:23 +0000702 case Intrinsic::bswap:
Nate Begemane5981812006-01-16 07:57:00 +0000703 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
704 break;
705
Reid Spencere9391fd2007-04-01 07:35:23 +0000706 case Intrinsic::ctlz:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000707 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000708 break;
Nate Begemane5981812006-01-16 07:57:00 +0000709
Reid Spencere9391fd2007-04-01 07:35:23 +0000710 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000711 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000712 Value *Src = CI->getOperand(1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000713 Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI);
Gabor Greif051a9502008-04-06 20:25:17 +0000714 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000715 SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI);
716 Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000717 CI->replaceAllUsesWith(Src);
718 break;
719 }
Chris Lattner77b13302004-01-05 05:36:30 +0000720
Chris Lattnerc6eb6d72007-04-10 03:20:39 +0000721 case Intrinsic::part_select:
Reid Spencerf75b8742007-04-12 02:48:46 +0000722 CI->replaceAllUsesWith(LowerPartSelect(CI));
723 break;
724
725 case Intrinsic::part_set:
726 CI->replaceAllUsesWith(LowerPartSet(CI));
Reid Spenceraddd11d2007-04-04 23:48:25 +0000727 break;
728
Chris Lattner0c067bc2006-01-13 02:22:08 +0000729 case Intrinsic::stacksave:
730 case Intrinsic::stackrestore: {
731 static bool Warned = false;
732 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000733 cerr << "WARNING: this target does not support the llvm.stack"
734 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
735 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000736 Warned = true;
737 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
738 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
739 break;
740 }
741
Chris Lattnercf899082004-02-14 02:47:17 +0000742 case Intrinsic::returnaddress:
743 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000744 cerr << "WARNING: this target does not support the llvm."
745 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
746 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000747 CI->replaceAllUsesWith(ConstantPointerNull::get(
748 cast<PointerType>(CI->getType())));
749 break;
750
Chris Lattner0942b7c2005-02-28 19:27:23 +0000751 case Intrinsic::prefetch:
752 break; // Simply strip out prefetches on unsupported architectures
753
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000754 case Intrinsic::pcmarker:
755 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000756 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000757 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
758 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000759 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000760 break;
761 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000762
Chris Lattner77b13302004-01-05 05:36:30 +0000763 case Intrinsic::dbg_stoppoint:
764 case Intrinsic::dbg_region_start:
765 case Intrinsic::dbg_region_end:
766 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000767 case Intrinsic::dbg_declare:
Duncan Sandsf664e412007-07-06 14:46:23 +0000768 break; // Simply strip out debugging intrinsics
769
Jim Laskeyb180aa12007-02-21 22:53:45 +0000770 case Intrinsic::eh_exception:
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000771 case Intrinsic::eh_selector_i32:
772 case Intrinsic::eh_selector_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000773 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
774 break;
775
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000776 case Intrinsic::eh_typeid_for_i32:
777 case Intrinsic::eh_typeid_for_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000778 // Return something different to eh_selector.
779 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
780 break;
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000781
Tanya Lattner24e5aad2007-06-15 22:26:58 +0000782 case Intrinsic::var_annotation:
783 break; // Strip out annotate intrinsic
784
Chris Lattner824b9582008-11-21 16:42:48 +0000785 case Intrinsic::memcpy: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000786 static Constant *MemcpyFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000787 Value *Size = CI->getOperand(3);
788 const Type *IntPtr = TD.getIntPtrType();
789 if (Size->getType()->getPrimitiveSizeInBits() <
790 IntPtr->getPrimitiveSizeInBits())
791 Size = new ZExtInst(Size, IntPtr, "", CI);
792 else if (Size->getType()->getPrimitiveSizeInBits() >
793 IntPtr->getPrimitiveSizeInBits())
794 Size = new TruncInst(Size, IntPtr, "", CI);
795 Value *Ops[3];
796 Ops[0] = CI->getOperand(1);
797 Ops[1] = CI->getOperand(2);
798 Ops[2] = Size;
799 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
800 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000801 break;
802 }
Chris Lattner824b9582008-11-21 16:42:48 +0000803 case Intrinsic::memmove: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000804 static Constant *MemmoveFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000805 Value *Size = CI->getOperand(3);
806 const Type *IntPtr = TD.getIntPtrType();
807 if (Size->getType()->getPrimitiveSizeInBits() <
808 IntPtr->getPrimitiveSizeInBits())
809 Size = new ZExtInst(Size, IntPtr, "", CI);
810 else if (Size->getType()->getPrimitiveSizeInBits() >
811 IntPtr->getPrimitiveSizeInBits())
812 Size = new TruncInst(Size, IntPtr, "", CI);
813 Value *Ops[3];
814 Ops[0] = CI->getOperand(1);
815 Ops[1] = CI->getOperand(2);
816 Ops[2] = Size;
817 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
818 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000819 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000820 }
Chris Lattner824b9582008-11-21 16:42:48 +0000821 case Intrinsic::memset: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000822 static Constant *MemsetFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000823 Value *Size = CI->getOperand(3);
Chris Lattner7d6f77d2007-02-06 06:07:51 +0000824 const Type *IntPtr = TD.getIntPtrType();
825 if (Size->getType()->getPrimitiveSizeInBits() <
826 IntPtr->getPrimitiveSizeInBits())
827 Size = new ZExtInst(Size, IntPtr, "", CI);
828 else if (Size->getType()->getPrimitiveSizeInBits() >
829 IntPtr->getPrimitiveSizeInBits())
830 Size = new TruncInst(Size, IntPtr, "", CI);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000831 Value *Ops[3];
832 Ops[0] = CI->getOperand(1);
833 // Extend the amount to i32.
834 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
835 Ops[2] = Size;
836 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
837 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000838 break;
839 }
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000840 case Intrinsic::sqrt: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000841 static Constant *sqrtFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000842 static Constant *sqrtDCache = 0;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000843 static Constant *sqrtLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000844 ReplaceFPIntrinsicWithCall(CI, sqrtFCache, sqrtDCache, sqrtLDCache,
845 "sqrtf", "sqrt", "sqrtl");
Dale Johannesen4292d1c2007-09-28 18:06:58 +0000846 break;
847 }
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000848 case Intrinsic::log: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000849 static Constant *logFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000850 static Constant *logDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000851 static Constant *logLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000852 ReplaceFPIntrinsicWithCall(CI, logFCache, logDCache, logLDCache,
853 "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000854 break;
855 }
856 case Intrinsic::log2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000857 static Constant *log2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000858 static Constant *log2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000859 static Constant *log2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000860 ReplaceFPIntrinsicWithCall(CI, log2FCache, log2DCache, log2LDCache,
861 "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000862 break;
863 }
864 case Intrinsic::log10: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000865 static Constant *log10FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000866 static Constant *log10DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000867 static Constant *log10LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000868 ReplaceFPIntrinsicWithCall(CI, log10FCache, log10DCache, log10LDCache,
869 "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000870 break;
871 }
872 case Intrinsic::exp: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000873 static Constant *expFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000874 static Constant *expDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000875 static Constant *expLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000876 ReplaceFPIntrinsicWithCall(CI, expFCache, expDCache, expLDCache,
877 "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000878 break;
879 }
880 case Intrinsic::exp2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000881 static Constant *exp2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000882 static Constant *exp2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000883 static Constant *exp2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000884 ReplaceFPIntrinsicWithCall(CI, exp2FCache, exp2DCache, exp2LDCache,
885 "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000886 break;
887 }
888 case Intrinsic::pow: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000889 static Constant *powFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000890 static Constant *powDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000891 static Constant *powLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000892 ReplaceFPIntrinsicWithCall(CI, powFCache, powDCache, powLDCache,
893 "powf", "pow", "powl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000894 break;
895 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000896 case Intrinsic::flt_rounds:
897 // Lower to "round to the nearest"
898 if (CI->getType() != Type::VoidTy)
899 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
900 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000901 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000902
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000903 assert(CI->use_empty() &&
904 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000905 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000906}