blob: bd3c8c71f5b221c1b5efc707b85a2a939c29ba39 [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 Lattner588e72d2004-02-15 22:16:39 +000037/// ReplaceCallWith - This function is used when we want to lower an intrinsic
38/// call to a call of an external function. This handles hard cases such as
39/// when there was already a prototype for the external function, and if that
40/// prototype doesn't match the arguments we expect to pass in.
41template <class ArgIt>
42static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000043 ArgIt ArgBegin, ArgIt ArgEnd,
44 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000045 if (!FCache) {
46 // If we haven't already looked up this function, check to see if the
47 // program already contains a function with this name.
48 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000049 // Get or insert the definition now.
50 std::vector<const Type *> ParamTys;
51 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
52 ParamTys.push_back((*I)->getType());
53 FCache = M->getOrInsertFunction(NewFn,
54 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000055 }
Chris Lattner588e72d2004-02-15 22:16:39 +000056
David Greene52eec542007-08-01 03:43:44 +000057 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Gabor Greif051a9502008-04-06 20:25:17 +000058 CallInst *NewCI = CallInst::Create(FCache, Args.begin(), Args.end(),
59 CI->getName(), CI);
Chris Lattnerb76efb72007-01-07 08:12:01 +000060 if (!CI->use_empty())
61 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000062 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000063}
64
Chris Lattnerb71fd782006-11-15 18:00:10 +000065void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000066 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000067 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000068 switch (I->getIntrinsicID()) {
69 default: break;
70 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000071 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000072 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000073 break;
74 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000075 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
76 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000077 break;
78 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000079 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
80 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000081 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000082 case Intrinsic::memcpy_i32:
83 case Intrinsic::memcpy_i64:
Christopher Lamb43ad6b32007-12-17 01:12:55 +000084 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
85 PointerType::getUnqual(Type::Int8Ty),
86 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000087 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000088 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000089 case Intrinsic::memmove_i32:
90 case Intrinsic::memmove_i64:
Christopher Lamb43ad6b32007-12-17 01:12:55 +000091 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
92 PointerType::getUnqual(Type::Int8Ty),
93 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000094 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000095 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000096 case Intrinsic::memset_i32:
97 case Intrinsic::memset_i64:
Christopher Lamb43ad6b32007-12-17 01:12:55 +000098 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
99 PointerType::getUnqual(Type::Int8Ty),
100 Type::Int32Ty,
Reid Spencer6addf2c2007-01-29 17:42:06 +0000101 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000102 break;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000103 case Intrinsic::sqrt:
104 switch((int)I->arg_begin()->getType()->getTypeID()) {
105 case Type::FloatTyID:
Chris Lattner1f243e92005-05-08 19:46:29 +0000106 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
107 Type::FloatTy);
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000108 case Type::DoubleTyID:
Chris Lattner1f243e92005-05-08 19:46:29 +0000109 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
110 Type::DoubleTy);
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000111 case Type::X86_FP80TyID:
112 case Type::FP128TyID:
113 case Type::PPC_FP128TyID:
114 EnsureFunctionExists(M, "sqrtl", I->arg_begin(), I->arg_end(),
115 I->arg_begin()->getType());
116 }
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000117 break;
Dan Gohmanc4c96602007-10-15 22:07:31 +0000118 case Intrinsic::sin:
119 switch((int)I->arg_begin()->getType()->getTypeID()) {
120 case Type::FloatTyID:
121 EnsureFunctionExists(M, "sinf", I->arg_begin(), I->arg_end(),
122 Type::FloatTy);
123 case Type::DoubleTyID:
124 EnsureFunctionExists(M, "sin", I->arg_begin(), I->arg_end(),
125 Type::DoubleTy);
126 case Type::X86_FP80TyID:
127 case Type::FP128TyID:
128 case Type::PPC_FP128TyID:
129 EnsureFunctionExists(M, "sinl", I->arg_begin(), I->arg_end(),
130 I->arg_begin()->getType());
131 }
132 break;
133 case Intrinsic::cos:
134 switch((int)I->arg_begin()->getType()->getTypeID()) {
135 case Type::FloatTyID:
136 EnsureFunctionExists(M, "cosf", I->arg_begin(), I->arg_end(),
137 Type::FloatTy);
138 case Type::DoubleTyID:
139 EnsureFunctionExists(M, "cos", I->arg_begin(), I->arg_end(),
140 Type::DoubleTy);
141 case Type::X86_FP80TyID:
142 case Type::FP128TyID:
143 case Type::PPC_FP128TyID:
144 EnsureFunctionExists(M, "cosl", I->arg_begin(), I->arg_end(),
145 I->arg_begin()->getType());
146 }
147 break;
148 case Intrinsic::pow:
149 switch((int)I->arg_begin()->getType()->getTypeID()) {
150 case Type::FloatTyID:
151 EnsureFunctionExists(M, "powf", I->arg_begin(), I->arg_end(),
152 Type::FloatTy);
153 case Type::DoubleTyID:
154 EnsureFunctionExists(M, "pow", I->arg_begin(), I->arg_end(),
155 Type::DoubleTy);
156 case Type::X86_FP80TyID:
157 case Type::FP128TyID:
158 case Type::PPC_FP128TyID:
159 EnsureFunctionExists(M, "powl", I->arg_begin(), I->arg_end(),
160 I->arg_begin()->getType());
161 }
162 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000163 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000164}
Chris Lattner588e72d2004-02-15 22:16:39 +0000165
Nate Begemane5981812006-01-16 07:57:00 +0000166/// LowerBSWAP - Emit the code to lower bswap of V before the specified
167/// instruction IP.
168static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000169 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000170
Nate Begemane5981812006-01-16 07:57:00 +0000171 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
172
173 switch(BitSize) {
174 default: assert(0 && "Unhandled type size of value to byteswap!");
175 case 16: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000176 Value *Tmp1 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000177 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000178 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000179 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000180 V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000181 break;
182 }
183 case 32: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000184 Value *Tmp4 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000185 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000186 Value *Tmp3 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000187 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000188 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000189 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000190 Value *Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000191 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000192 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000193 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000194 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000195 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000196 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000197 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000198 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP);
199 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP);
200 V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000201 break;
202 }
203 case 64: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000204 Value *Tmp8 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000205 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000206 Value *Tmp7 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000207 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000208 Value *Tmp6 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000209 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000210 Value *Tmp5 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000211 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000212 Value* Tmp4 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000213 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000214 Value* Tmp3 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000215 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000216 Value* Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000217 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000218 Value* Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000219 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000220 Tmp7 = BinaryOperator::CreateAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000221 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000222 0xFF000000000000ULL),
223 "bswap.and7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000224 Tmp6 = BinaryOperator::CreateAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000225 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000226 "bswap.and6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000227 Tmp5 = BinaryOperator::CreateAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000228 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000229 "bswap.and5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000230 Tmp4 = BinaryOperator::CreateAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000231 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000232 "bswap.and4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000233 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000234 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000235 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000236 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000237 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000238 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000239 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP);
240 Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP);
241 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP);
242 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP);
243 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP);
244 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP);
245 V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000246 break;
247 }
248 }
Nate Begemane5981812006-01-16 07:57:00 +0000249 return V;
250}
251
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000252/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000253/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000254static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000255 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000256
257 static const uint64_t MaskValues[6] = {
258 0x5555555555555555ULL, 0x3333333333333333ULL,
259 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
260 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
261 };
262
Chris Lattner98cf45b2005-05-11 20:24:12 +0000263 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000264 unsigned WordSize = (BitSize + 63) / 64;
265 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencer3822ff52006-11-08 06:47:33 +0000266
Zhou Sheng02031c02007-06-02 04:10:33 +0000267 for (unsigned n = 0; n < WordSize; ++n) {
268 Value *PartValue = V;
269 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
270 i <<= 1, ++ct) {
271 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000272 Value *LHS = BinaryOperator::CreateAnd(
Zhou Sheng02031c02007-06-02 04:10:33 +0000273 PartValue, MaskCst, "cppop.and1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000274 Value *VShift = BinaryOperator::CreateLShr(PartValue,
Zhou Sheng02031c02007-06-02 04:10:33 +0000275 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000276 Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP);
277 PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000278 }
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000279 Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000280 if (BitSize > 64) {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000281 V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64),
Zhou Sheng02031c02007-06-02 04:10:33 +0000282 "ctpop.part.sh", IP);
283 BitSize -= 64;
284 }
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000285 }
286
Chris Lattner914ce452007-08-06 16:36:18 +0000287 return Count;
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000288}
289
Chris Lattner98cf45b2005-05-11 20:24:12 +0000290/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000291/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000292static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000293
294 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000295 for (unsigned i = 1; i < BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000296 Value *ShVal = ConstantInt::get(V->getType(), i);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000297 ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP);
298 V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000299 }
300
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000301 V = BinaryOperator::CreateNot(V, "", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000302 return LowerCTPOP(V, IP);
303}
304
Reid Spencerf75b8742007-04-12 02:48:46 +0000305/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
306/// three integer arguments. The first argument is the Value from which the
307/// bits will be selected. It may be of any bit width. The second and third
308/// arguments specify a range of bits to select with the second argument
309/// specifying the low bit and the third argument specifying the high bit. Both
310/// must be type i32. The result is the corresponding selected bits from the
311/// Value in the same width as the Value (first argument). If the low bit index
312/// is higher than the high bit index then the inverse selection is done and
313/// the bits are returned in inverse order.
314/// @brief Lowering of llvm.part.select intrinsic.
315static Instruction *LowerPartSelect(CallInst *CI) {
Reid Spenceraddd11d2007-04-04 23:48:25 +0000316 // Make sure we're dealing with a part select intrinsic here
317 Function *F = CI->getCalledFunction();
318 const FunctionType *FT = F->getFunctionType();
319 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
320 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
321 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
322 return CI;
323
324 // Get the intrinsic implementation function by converting all the . to _
325 // in the intrinsic's function name and then reconstructing the function
326 // declaration.
327 std::string Name(F->getName());
328 for (unsigned i = 4; i < Name.length(); ++i)
329 if (Name[i] == '.')
330 Name[i] = '_';
331 Module* M = F->getParent();
332 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Reid Spencerdf413532007-04-12 21:53:38 +0000333 F->setLinkage(GlobalValue::WeakLinkage);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000334
335 // If we haven't defined the impl function yet, do so now
336 if (F->isDeclaration()) {
337
338 // Get the arguments to the function
Reid Spencereeedcb62007-04-12 13:30:14 +0000339 Function::arg_iterator args = F->arg_begin();
340 Value* Val = args++; Val->setName("Val");
341 Value* Lo = args++; Lo->setName("Lo");
Gabor Greif051a9502008-04-06 20:25:17 +0000342 Value* Hi = args++; Hi->setName("High");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000343
Reid Spencereeedcb62007-04-12 13:30:14 +0000344 // We want to select a range of bits here such that [Hi, Lo] is shifted
345 // down to the low bits. However, it is quite possible that Hi is smaller
346 // than Lo in which case the bits have to be reversed.
Reid Spenceraddd11d2007-04-04 23:48:25 +0000347
348 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greif051a9502008-04-06 20:25:17 +0000349 BasicBlock* CurBB = BasicBlock::Create("entry", F);
350 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
351 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
352 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
353 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
354 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Reid Spenceraddd11d2007-04-04 23:48:25 +0000355
Reid Spencereeedcb62007-04-12 13:30:14 +0000356 // Cast Hi and Lo to the size of Val so the widths are all the same
357 if (Hi->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000358 Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000359 "tmp", CurBB);
Reid Spencereeedcb62007-04-12 13:30:14 +0000360 if (Lo->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000361 Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000362 "tmp", CurBB);
363
364 // Compute a few things that both cases will need, up front.
365 Constant* Zero = ConstantInt::get(Val->getType(), 0);
366 Constant* One = ConstantInt::get(Val->getType(), 1);
367 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
368
Reid Spencereeedcb62007-04-12 13:30:14 +0000369 // Compare the Hi and Lo bit positions. This is used to determine
Reid Spenceraddd11d2007-04-04 23:48:25 +0000370 // which case we have (forward or reverse)
Reid Spencereeedcb62007-04-12 13:30:14 +0000371 ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Hi, Lo, "less",CurBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000372 BranchInst::Create(RevSize, FwdSize, Cmp, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000373
374 // First, copmute the number of bits in the forward case.
375 Instruction* FBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000376 BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000377 BranchInst::Create(Compute, FwdSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000378
379 // Second, compute the number of bits in the reverse case.
380 Instruction* RBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000381 BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000382 BranchInst::Create(Compute, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000383
384 // Now, compute the bit range. Start by getting the bitsize and the shift
Reid Spencereeedcb62007-04-12 13:30:14 +0000385 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
Reid Spenceraddd11d2007-04-04 23:48:25 +0000386 // the number of bits we want in the range. We shift the bits down to the
387 // least significant bits, apply the mask to zero out unwanted high bits,
388 // and we have computed the "forward" result. It may still need to be
389 // reversed.
390
391 // Get the BitSize from one of the two subtractions
Gabor Greif051a9502008-04-06 20:25:17 +0000392 PHINode *BitSize = PHINode::Create(Val->getType(), "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000393 BitSize->reserveOperandSpace(2);
394 BitSize->addIncoming(FBitSize, FwdSize);
395 BitSize->addIncoming(RBitSize, RevSize);
396
Reid Spencereeedcb62007-04-12 13:30:14 +0000397 // Get the ShiftAmount as the smaller of Hi/Lo
Gabor Greif051a9502008-04-06 20:25:17 +0000398 PHINode *ShiftAmt = PHINode::Create(Val->getType(), "shiftamt", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000399 ShiftAmt->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000400 ShiftAmt->addIncoming(Lo, FwdSize);
401 ShiftAmt->addIncoming(Hi, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000402
403 // Increment the bit size
404 Instruction *BitSizePlusOne =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000405 BinaryOperator::CreateAdd(BitSize, One, "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000406
407 // Create a Mask to zero out the high order bits.
408 Instruction* Mask =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000409 BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute);
410 Mask = BinaryOperator::CreateNot(Mask, "mask", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000411
412 // Shift the bits down and apply the mask
413 Instruction* FRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000414 BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute);
415 FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute);
Gabor Greif051a9502008-04-06 20:25:17 +0000416 BranchInst::Create(Reverse, RsltBlk, Cmp, Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000417
418 // In the Reverse block we have the mask already in FRes but we must reverse
419 // it by shifting FRes bits right and putting them in RRes by shifting them
420 // in from left.
421
422 // First set up our loop counters
Gabor Greif051a9502008-04-06 20:25:17 +0000423 PHINode *Count = PHINode::Create(Val->getType(), "count", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000424 Count->reserveOperandSpace(2);
425 Count->addIncoming(BitSizePlusOne, Compute);
426
427 // Next, get the value that we are shifting.
Gabor Greif051a9502008-04-06 20:25:17 +0000428 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000429 BitsToShift->reserveOperandSpace(2);
430 BitsToShift->addIncoming(FRes, Compute);
431
432 // Finally, get the result of the last computation
Gabor Greif051a9502008-04-06 20:25:17 +0000433 PHINode *RRes = PHINode::Create(Val->getType(), "rres", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000434 RRes->reserveOperandSpace(2);
435 RRes->addIncoming(Zero, Compute);
436
437 // Decrement the counter
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000438 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000439 Count->addIncoming(Decr, Reverse);
440
441 // Compute the Bit that we want to move
442 Instruction *Bit =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000443 BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000444
445 // Compute the new value for next iteration.
446 Instruction *NewVal =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000447 BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000448 BitsToShift->addIncoming(NewVal, Reverse);
449
450 // Shift the bit into the low bits of the result.
451 Instruction *NewRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000452 BinaryOperator::CreateShl(RRes, One, "lshift", Reverse);
453 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000454 RRes->addIncoming(NewRes, Reverse);
455
456 // Terminate loop if we've moved all the bits.
457 ICmpInst *Cond =
458 new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000459 BranchInst::Create(RsltBlk, Reverse, Cond, Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000460
461 // Finally, in the result block, select one of the two results with a PHI
462 // node and return the result;
463 CurBB = RsltBlk;
Gabor Greif051a9502008-04-06 20:25:17 +0000464 PHINode *BitSelect = PHINode::Create(Val->getType(), "part_select", CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000465 BitSelect->reserveOperandSpace(2);
466 BitSelect->addIncoming(FRes, Compute);
467 BitSelect->addIncoming(NewRes, Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000468 ReturnInst::Create(BitSelect, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000469 }
470
471 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000472 Value *Args[] = {
473 CI->getOperand(1),
474 CI->getOperand(2),
475 CI->getOperand(3)
476 };
Gabor Greif051a9502008-04-06 20:25:17 +0000477 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000478}
479
Reid Spencerf75b8742007-04-12 02:48:46 +0000480/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
481/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
482/// The first two arguments can be any bit width. The result is the same width
483/// as %Value. The operation replaces bits between %Low and %High with the value
484/// in %Replacement. If %Replacement is not the same width, it is truncated or
485/// zero extended as appropriate to fit the bits being replaced. If %Low is
486/// greater than %High then the inverse set of bits are replaced.
487/// @brief Lowering of llvm.bit.part.set intrinsic.
488static Instruction *LowerPartSet(CallInst *CI) {
489 // Make sure we're dealing with a part select intrinsic here
490 Function *F = CI->getCalledFunction();
491 const FunctionType *FT = F->getFunctionType();
492 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
493 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
494 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
495 !FT->getParamType(3)->isInteger())
496 return CI;
497
498 // Get the intrinsic implementation function by converting all the . to _
499 // in the intrinsic's function name and then reconstructing the function
500 // declaration.
501 std::string Name(F->getName());
502 for (unsigned i = 4; i < Name.length(); ++i)
503 if (Name[i] == '.')
504 Name[i] = '_';
505 Module* M = F->getParent();
506 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Reid Spencerdf413532007-04-12 21:53:38 +0000507 F->setLinkage(GlobalValue::WeakLinkage);
Reid Spencerf75b8742007-04-12 02:48:46 +0000508
509 // If we haven't defined the impl function yet, do so now
510 if (F->isDeclaration()) {
Reid Spencerf75b8742007-04-12 02:48:46 +0000511 // Get the arguments for the function.
512 Function::arg_iterator args = F->arg_begin();
513 Value* Val = args++; Val->setName("Val");
514 Value* Rep = args++; Rep->setName("Rep");
515 Value* Lo = args++; Lo->setName("Lo");
516 Value* Hi = args++; Hi->setName("Hi");
517
518 // Get some types we need
519 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
520 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
521 uint32_t ValBits = ValTy->getBitWidth();
522 uint32_t RepBits = RepTy->getBitWidth();
523
524 // Constant Definitions
525 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
526 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
527 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
Reid Spencer76c94b62007-05-15 02:26:52 +0000528 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
529 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
530 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
531 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000532
Reid Spencer76c94b62007-05-15 02:26:52 +0000533 // Basic blocks we fill in below.
Gabor Greif051a9502008-04-06 20:25:17 +0000534 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
535 BasicBlock* large = BasicBlock::Create("large", F, 0);
536 BasicBlock* small = BasicBlock::Create("small", F, 0);
537 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
538 BasicBlock* result = BasicBlock::Create("result", F, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000539
Reid Spencer76c94b62007-05-15 02:26:52 +0000540 // BASIC BLOCK: entry
Reid Spencereeedcb62007-04-12 13:30:14 +0000541 // First, get the number of bits that we're placing as an i32
542 ICmpInst* is_forward =
543 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000544 SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry);
545 SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000546 BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
547 NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
Reid Spencereeedcb62007-04-12 13:30:14 +0000548 // Now, convert Lo and Hi to ValTy bit width
Reid Spencerf75b8742007-04-12 02:48:46 +0000549 if (ValBits > 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000550 Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000551 } else if (ValBits < 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000552 Lo = new TruncInst(Lo_pn, ValTy, "", entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000553 }
Reid Spencereeedcb62007-04-12 13:30:14 +0000554 // Determine if the replacement bits are larger than the number of bits we
555 // are replacing and deal with it.
Reid Spencerf75b8742007-04-12 02:48:46 +0000556 ICmpInst* is_large =
557 new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000558 BranchInst::Create(large, small, is_large, entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000559
Reid Spencer76c94b62007-05-15 02:26:52 +0000560 // BASIC BLOCK: large
Reid Spencer9a9203b2007-04-16 22:21:14 +0000561 Instruction* MaskBits =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000562 BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large);
563 MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(),
Reid Spencer9a9203b2007-04-16 22:21:14 +0000564 false, "", large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000565 BinaryOperator* Mask1 =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000566 BinaryOperator::CreateLShr(RepMask, MaskBits, "", large);
567 BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large);
Gabor Greif051a9502008-04-06 20:25:17 +0000568 BranchInst::Create(small, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000569
Reid Spencer76c94b62007-05-15 02:26:52 +0000570 // BASIC BLOCK: small
Gabor Greif051a9502008-04-06 20:25:17 +0000571 PHINode* Rep3 = PHINode::Create(RepTy, "", small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000572 Rep3->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000573 Rep3->addIncoming(Rep2, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000574 Rep3->addIncoming(Rep, entry);
Reid Spencer37958092007-04-12 12:46:33 +0000575 Value* Rep4 = Rep3;
576 if (ValBits > RepBits)
577 Rep4 = new ZExtInst(Rep3, ValTy, "", small);
578 else if (ValBits < RepBits)
579 Rep4 = new TruncInst(Rep3, ValTy, "", small);
Gabor Greif051a9502008-04-06 20:25:17 +0000580 BranchInst::Create(result, reverse, is_forward, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000581
Reid Spencer76c94b62007-05-15 02:26:52 +0000582 // BASIC BLOCK: reverse (reverses the bits of the replacement)
583 // Set up our loop counter as a PHI so we can decrement on each iteration.
584 // We will loop for the number of bits in the replacement value.
Gabor Greif051a9502008-04-06 20:25:17 +0000585 PHINode *Count = PHINode::Create(Type::Int32Ty, "count", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000586 Count->reserveOperandSpace(2);
587 Count->addIncoming(NumBits, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000588
Reid Spencer76c94b62007-05-15 02:26:52 +0000589 // Get the value that we are shifting bits out of as a PHI because
590 // we'll change this with each iteration.
Gabor Greif051a9502008-04-06 20:25:17 +0000591 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000592 BitsToShift->reserveOperandSpace(2);
593 BitsToShift->addIncoming(Rep4, small);
594
595 // Get the result of the last computation or zero on first iteration
Gabor Greif051a9502008-04-06 20:25:17 +0000596 PHINode *RRes = PHINode::Create(Val->getType(), "rres", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000597 RRes->reserveOperandSpace(2);
598 RRes->addIncoming(ValZero, small);
599
600 // Decrement the loop counter by one
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000601 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000602 Count->addIncoming(Decr, reverse);
603
604 // Get the bit that we want to move into the result
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000605 Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000606
607 // Compute the new value of the bits to shift for the next iteration.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000608 Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000609 BitsToShift->addIncoming(NewVal, reverse);
610
611 // Shift the bit we extracted into the low bit of the result.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000612 Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse);
613 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000614 RRes->addIncoming(NewRes, reverse);
615
616 // Terminate loop if we've moved all the bits.
617 ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000618 BranchInst::Create(result, reverse, Cond, reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000619
620 // BASIC BLOCK: result
Gabor Greif051a9502008-04-06 20:25:17 +0000621 PHINode *Rplcmnt = PHINode::Create(Val->getType(), "", result);
Reid Spencer76c94b62007-05-15 02:26:52 +0000622 Rplcmnt->reserveOperandSpace(2);
623 Rplcmnt->addIncoming(NewRes, reverse);
624 Rplcmnt->addIncoming(Rep4, small);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000625 Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result);
626 Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result);
627 Value* t2 = BinaryOperator::CreateNot(t1, "", result);
628 Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result);
629 Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result);
630 Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result);
631 Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result);
632 Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result);
Gabor Greif051a9502008-04-06 20:25:17 +0000633 ReturnInst::Create(Rslt, result);
Reid Spencerf75b8742007-04-12 02:48:46 +0000634 }
635
636 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000637 Value *Args[] = {
638 CI->getOperand(1),
639 CI->getOperand(2),
640 CI->getOperand(3),
641 CI->getOperand(4)
642 };
Gabor Greif051a9502008-04-06 20:25:17 +0000643 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spencerf75b8742007-04-12 02:48:46 +0000644}
645
Reid Spenceraddd11d2007-04-04 23:48:25 +0000646
Chris Lattnerb71fd782006-11-15 18:00:10 +0000647void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000648 Function *Callee = CI->getCalledFunction();
649 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000650
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000651 switch (Callee->getIntrinsicID()) {
652 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000653 cerr << "Cannot lower a call to a non-intrinsic function '"
654 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000655 abort();
656 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000657 cerr << "Error: Code generator does not support intrinsic function '"
658 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000659 abort();
660
Chris Lattner588e72d2004-02-15 22:16:39 +0000661 // The setjmp/longjmp intrinsics should only exist in the code if it was
662 // never optimized (ie, right out of the CFE), or if it has been hacked on
663 // by the lowerinvoke pass. In both cases, the right thing to do is to
664 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000665 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000666 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000667 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000668 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000669 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000670 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000671 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000672 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000673 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000674 if (CI->getType() != Type::VoidTy)
675 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
676 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000677
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000678 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000679 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000680 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000681 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000682 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000683 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000684
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000685 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000686 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000687 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000688 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000689 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000690 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000691 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000692 case Intrinsic::ctpop:
Reid Spencer0b118202006-01-16 21:12:35 +0000693 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
694 break;
695
Reid Spencere9391fd2007-04-01 07:35:23 +0000696 case Intrinsic::bswap:
Nate Begemane5981812006-01-16 07:57:00 +0000697 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
698 break;
699
Reid Spencere9391fd2007-04-01 07:35:23 +0000700 case Intrinsic::ctlz:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000701 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000702 break;
Nate Begemane5981812006-01-16 07:57:00 +0000703
Reid Spencere9391fd2007-04-01 07:35:23 +0000704 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000705 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000706 Value *Src = CI->getOperand(1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000707 Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI);
Gabor Greif051a9502008-04-06 20:25:17 +0000708 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000709 SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI);
710 Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000711 CI->replaceAllUsesWith(Src);
712 break;
713 }
Chris Lattner77b13302004-01-05 05:36:30 +0000714
Chris Lattnerc6eb6d72007-04-10 03:20:39 +0000715 case Intrinsic::part_select:
Reid Spencerf75b8742007-04-12 02:48:46 +0000716 CI->replaceAllUsesWith(LowerPartSelect(CI));
717 break;
718
719 case Intrinsic::part_set:
720 CI->replaceAllUsesWith(LowerPartSet(CI));
Reid Spenceraddd11d2007-04-04 23:48:25 +0000721 break;
722
Chris Lattner0c067bc2006-01-13 02:22:08 +0000723 case Intrinsic::stacksave:
724 case Intrinsic::stackrestore: {
725 static bool Warned = false;
726 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000727 cerr << "WARNING: this target does not support the llvm.stack"
728 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
729 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000730 Warned = true;
731 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
732 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
733 break;
734 }
735
Chris Lattnercf899082004-02-14 02:47:17 +0000736 case Intrinsic::returnaddress:
737 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000738 cerr << "WARNING: this target does not support the llvm."
739 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
740 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000741 CI->replaceAllUsesWith(ConstantPointerNull::get(
742 cast<PointerType>(CI->getType())));
743 break;
744
Chris Lattner0942b7c2005-02-28 19:27:23 +0000745 case Intrinsic::prefetch:
746 break; // Simply strip out prefetches on unsupported architectures
747
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000748 case Intrinsic::pcmarker:
749 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000750 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000751 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
752 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000753 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000754 break;
755 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000756
Chris Lattner77b13302004-01-05 05:36:30 +0000757 case Intrinsic::dbg_stoppoint:
758 case Intrinsic::dbg_region_start:
759 case Intrinsic::dbg_region_end:
760 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000761 case Intrinsic::dbg_declare:
Duncan Sandsf664e412007-07-06 14:46:23 +0000762 break; // Simply strip out debugging intrinsics
763
Jim Laskeyb180aa12007-02-21 22:53:45 +0000764 case Intrinsic::eh_exception:
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000765 case Intrinsic::eh_selector_i32:
766 case Intrinsic::eh_selector_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000767 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
768 break;
769
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000770 case Intrinsic::eh_typeid_for_i32:
771 case Intrinsic::eh_typeid_for_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000772 // Return something different to eh_selector.
773 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
774 break;
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000775
Tanya Lattner24e5aad2007-06-15 22:26:58 +0000776 case Intrinsic::var_annotation:
777 break; // Strip out annotate intrinsic
778
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000779 case Intrinsic::memcpy_i32:
Reid Spencer3da59db2006-11-27 01:05:10 +0000780 case Intrinsic::memcpy_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000781 static Constant *MemcpyFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000782 Value *Size = CI->getOperand(3);
783 const Type *IntPtr = TD.getIntPtrType();
784 if (Size->getType()->getPrimitiveSizeInBits() <
785 IntPtr->getPrimitiveSizeInBits())
786 Size = new ZExtInst(Size, IntPtr, "", CI);
787 else if (Size->getType()->getPrimitiveSizeInBits() >
788 IntPtr->getPrimitiveSizeInBits())
789 Size = new TruncInst(Size, IntPtr, "", CI);
790 Value *Ops[3];
791 Ops[0] = CI->getOperand(1);
792 Ops[1] = CI->getOperand(2);
793 Ops[2] = Size;
794 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
795 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000796 break;
797 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000798 case Intrinsic::memmove_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000799 case Intrinsic::memmove_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000800 static Constant *MemmoveFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000801 Value *Size = CI->getOperand(3);
802 const Type *IntPtr = TD.getIntPtrType();
803 if (Size->getType()->getPrimitiveSizeInBits() <
804 IntPtr->getPrimitiveSizeInBits())
805 Size = new ZExtInst(Size, IntPtr, "", CI);
806 else if (Size->getType()->getPrimitiveSizeInBits() >
807 IntPtr->getPrimitiveSizeInBits())
808 Size = new TruncInst(Size, IntPtr, "", CI);
809 Value *Ops[3];
810 Ops[0] = CI->getOperand(1);
811 Ops[1] = CI->getOperand(2);
812 Ops[2] = Size;
813 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
814 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000815 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000816 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000817 case Intrinsic::memset_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000818 case Intrinsic::memset_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000819 static Constant *MemsetFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000820 Value *Size = CI->getOperand(3);
Chris Lattner7d6f77d2007-02-06 06:07:51 +0000821 const Type *IntPtr = TD.getIntPtrType();
822 if (Size->getType()->getPrimitiveSizeInBits() <
823 IntPtr->getPrimitiveSizeInBits())
824 Size = new ZExtInst(Size, IntPtr, "", CI);
825 else if (Size->getType()->getPrimitiveSizeInBits() >
826 IntPtr->getPrimitiveSizeInBits())
827 Size = new TruncInst(Size, IntPtr, "", CI);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000828 Value *Ops[3];
829 Ops[0] = CI->getOperand(1);
830 // Extend the amount to i32.
831 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
832 Ops[2] = Size;
833 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
834 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000835 break;
836 }
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000837 case Intrinsic::sqrt: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000838 static Constant *sqrtfFCache = 0;
Chris Lattnerb76efb72007-01-07 08:12:01 +0000839 static Constant *sqrtFCache = 0;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000840 static Constant *sqrtLDCache = 0;
841 switch (CI->getOperand(1)->getType()->getTypeID()) {
842 default: assert(0 && "Invalid type in sqrt"); abort();
843 case Type::FloatTyID:
844 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
845 Type::FloatTy, sqrtfFCache);
846 break;
847 case Type::DoubleTyID:
848 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000849 Type::DoubleTy, sqrtFCache);
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000850 break;
851 case Type::X86_FP80TyID:
852 case Type::FP128TyID:
853 case Type::PPC_FP128TyID:
854 ReplaceCallWith("sqrtl", CI, CI->op_begin()+1, CI->op_end(),
855 CI->getOperand(1)->getType(), sqrtLDCache);
856 break;
857 }
Dale Johannesen4292d1c2007-09-28 18:06:58 +0000858 break;
859 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000860 case Intrinsic::flt_rounds:
861 // Lower to "round to the nearest"
862 if (CI->getType() != Type::VoidTy)
863 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
864 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000865 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000866
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000867 assert(CI->use_empty() &&
868 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000869 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000870}