blob: 0d6c9f80f28395d7e42de72cf70699f6399c6cfb [file] [log] [blame]
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001//===- InstCombineCalls.cpp -----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the visitCall and visitInvoke functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/None.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/ADT/Statistic.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000022#include "llvm/ADT/Twine.h"
David Majnemer15032582015-05-22 03:56:46 +000023#include "llvm/Analysis/InstructionSimplify.h"
Chris Lattner7a9e47a2010-01-05 07:32:13 +000024#include "llvm/Analysis/MemoryBuiltins.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000025#include "llvm/Analysis/ValueTracking.h"
26#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000027#include "llvm/IR/CallSite.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000028#include "llvm/IR/Constant.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/InstrTypes.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Intrinsics.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Metadata.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000040#include "llvm/IR/PatternMatch.h"
Philip Reames1a1bdb22014-12-02 18:50:36 +000041#include "llvm/IR/Statepoint.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000042#include "llvm/IR/Type.h"
43#include "llvm/IR/Value.h"
44#include "llvm/IR/ValueHandle.h"
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/Debug.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000047#include "llvm/Support/KnownBits.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000048#include "llvm/Support/MathExtras.h"
Chris Lattner6fcd32e2010-12-25 20:37:57 +000049#include "llvm/Transforms/Utils/Local.h"
Chandler Carruthba4c5172015-01-21 11:23:40 +000050#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000051#include <algorithm>
52#include <cassert>
53#include <cstdint>
54#include <cstring>
55#include <vector>
56
Chris Lattner7a9e47a2010-01-05 07:32:13 +000057using namespace llvm;
Michael Ilseman536cc322012-12-13 03:13:36 +000058using namespace PatternMatch;
Chris Lattner7a9e47a2010-01-05 07:32:13 +000059
Chandler Carruth964daaa2014-04-22 02:55:47 +000060#define DEBUG_TYPE "instcombine"
61
Meador Ingee3f2b262012-11-30 04:05:06 +000062STATISTIC(NumSimplified, "Number of library calls simplified");
63
Igor Laevskya9b68722017-02-08 15:21:48 +000064static cl::opt<unsigned> UnfoldElementAtomicMemcpyMaxElements(
Igor Laevsky900ffa32017-02-08 14:32:04 +000065 "unfold-element-atomic-memcpy-max-elements",
66 cl::init(16),
67 cl::desc("Maximum number of elements in atomic memcpy the optimizer is "
68 "allowed to unfold"));
69
Sanjay Patelcd4377c2016-01-20 22:24:38 +000070/// Return the specified type promoted as it would be to pass though a va_arg
71/// area.
Chris Lattner229907c2011-07-18 04:54:35 +000072static Type *getPromotedType(Type *Ty) {
73 if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +000074 if (ITy->getBitWidth() < 32)
75 return Type::getInt32Ty(Ty->getContext());
76 }
77 return Ty;
78}
79
Sanjay Patel368ac5d2016-02-21 17:29:33 +000080/// Return a constant boolean vector that has true elements in all positions
Sanjay Patel24401302016-02-21 17:33:31 +000081/// where the input constant data vector has an element with the sign bit set.
Sanjay Patel368ac5d2016-02-21 17:29:33 +000082static Constant *getNegativeIsTrueBoolVec(ConstantDataVector *V) {
83 SmallVector<Constant *, 32> BoolVec;
84 IntegerType *BoolTy = Type::getInt1Ty(V->getContext());
85 for (unsigned I = 0, E = V->getNumElements(); I != E; ++I) {
86 Constant *Elt = V->getElementAsConstant(I);
87 assert((isa<ConstantInt>(Elt) || isa<ConstantFP>(Elt)) &&
88 "Unexpected constant data vector element type");
89 bool Sign = V->getElementType()->isIntegerTy()
90 ? cast<ConstantInt>(Elt)->isNegative()
91 : cast<ConstantFP>(Elt)->isNegative();
92 BoolVec.push_back(ConstantInt::get(BoolTy, Sign));
93 }
94 return ConstantVector::get(BoolVec);
95}
96
Daniel Neilson3faabbb2017-06-16 14:43:59 +000097Instruction *InstCombiner::SimplifyElementUnorderedAtomicMemCpy(
98 ElementUnorderedAtomicMemCpyInst *AMI) {
Igor Laevsky900ffa32017-02-08 14:32:04 +000099 // Try to unfold this intrinsic into sequence of explicit atomic loads and
100 // stores.
101 // First check that number of elements is compile time constant.
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000102 auto *LengthCI = dyn_cast<ConstantInt>(AMI->getLength());
103 if (!LengthCI)
Igor Laevsky900ffa32017-02-08 14:32:04 +0000104 return nullptr;
105
106 // Check that there are not too many elements.
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000107 uint64_t LengthInBytes = LengthCI->getZExtValue();
108 uint32_t ElementSizeInBytes = AMI->getElementSizeInBytes();
109 uint64_t NumElements = LengthInBytes / ElementSizeInBytes;
Igor Laevsky900ffa32017-02-08 14:32:04 +0000110 if (NumElements >= UnfoldElementAtomicMemcpyMaxElements)
111 return nullptr;
112
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000113 // Only expand if there are elements to copy.
114 if (NumElements > 0) {
115 // Don't unfold into illegal integers
116 uint64_t ElementSizeInBits = ElementSizeInBytes * 8;
117 if (!getDataLayout().isLegalInteger(ElementSizeInBits))
118 return nullptr;
Igor Laevsky900ffa32017-02-08 14:32:04 +0000119
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000120 // Cast source and destination to the correct type. Intrinsic input
121 // arguments are usually represented as i8*. Often operands will be
122 // explicitly casted to i8* and we can just strip those casts instead of
123 // inserting new ones. However it's easier to rely on other InstCombine
124 // rules which will cover trivial cases anyway.
125 Value *Src = AMI->getRawSource();
126 Value *Dst = AMI->getRawDest();
127 Type *ElementPointerType =
128 Type::getIntNPtrTy(AMI->getContext(), ElementSizeInBits,
129 Src->getType()->getPointerAddressSpace());
Igor Laevsky900ffa32017-02-08 14:32:04 +0000130
Craig Topperbb4069e2017-07-07 23:16:26 +0000131 Value *SrcCasted = Builder.CreatePointerCast(Src, ElementPointerType,
132 "memcpy_unfold.src_casted");
133 Value *DstCasted = Builder.CreatePointerCast(Dst, ElementPointerType,
134 "memcpy_unfold.dst_casted");
Igor Laevsky900ffa32017-02-08 14:32:04 +0000135
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000136 for (uint64_t i = 0; i < NumElements; ++i) {
137 // Get current element addresses
138 ConstantInt *ElementIdxCI =
139 ConstantInt::get(AMI->getContext(), APInt(64, i));
140 Value *SrcElementAddr =
Craig Topperbb4069e2017-07-07 23:16:26 +0000141 Builder.CreateGEP(SrcCasted, ElementIdxCI, "memcpy_unfold.src_addr");
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000142 Value *DstElementAddr =
Craig Topperbb4069e2017-07-07 23:16:26 +0000143 Builder.CreateGEP(DstCasted, ElementIdxCI, "memcpy_unfold.dst_addr");
Igor Laevsky900ffa32017-02-08 14:32:04 +0000144
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000145 // Load from the source. Transfer alignment information and mark load as
146 // unordered atomic.
Craig Topperbb4069e2017-07-07 23:16:26 +0000147 LoadInst *Load = Builder.CreateLoad(SrcElementAddr, "memcpy_unfold.val");
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000148 Load->setOrdering(AtomicOrdering::Unordered);
149 // We know alignment of the first element. It is also guaranteed by the
150 // verifier that element size is less or equal than first element
151 // alignment and both of this values are powers of two. This means that
152 // all subsequent accesses are at least element size aligned.
153 // TODO: We can infer better alignment but there is no evidence that this
154 // will matter.
155 Load->setAlignment(i == 0 ? AMI->getParamAlignment(1)
156 : ElementSizeInBytes);
157 Load->setDebugLoc(AMI->getDebugLoc());
Igor Laevsky900ffa32017-02-08 14:32:04 +0000158
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000159 // Store loaded value via unordered atomic store.
Craig Topperbb4069e2017-07-07 23:16:26 +0000160 StoreInst *Store = Builder.CreateStore(Load, DstElementAddr);
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000161 Store->setOrdering(AtomicOrdering::Unordered);
162 Store->setAlignment(i == 0 ? AMI->getParamAlignment(0)
163 : ElementSizeInBytes);
164 Store->setDebugLoc(AMI->getDebugLoc());
165 }
Igor Laevsky900ffa32017-02-08 14:32:04 +0000166 }
167
168 // Set the number of elements of the copy to 0, it will be deleted on the
169 // next iteration.
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000170 AMI->setLength(Constant::getNullValue(LengthCI->getType()));
Igor Laevsky900ffa32017-02-08 14:32:04 +0000171 return AMI;
172}
173
Pete Cooper67cf9a72015-11-19 05:56:52 +0000174Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000175 unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), DL, MI, &AC, &DT);
176 unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), DL, MI, &AC, &DT);
Pete Cooper67cf9a72015-11-19 05:56:52 +0000177 unsigned MinAlign = std::min(DstAlign, SrcAlign);
178 unsigned CopyAlign = MI->getAlignment();
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000179
Pete Cooper67cf9a72015-11-19 05:56:52 +0000180 if (CopyAlign < MinAlign) {
181 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), MinAlign, false));
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000182 return MI;
183 }
Jim Grosbach7815f562012-02-03 00:07:04 +0000184
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000185 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
186 // load/store.
Gabor Greif0a136c92010-06-24 13:54:33 +0000187 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +0000188 if (!MemOpLength) return nullptr;
Jim Grosbach7815f562012-02-03 00:07:04 +0000189
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000190 // Source and destination pointer types are always "i8*" for intrinsic. See
191 // if the size is something we can handle with a single primitive load/store.
192 // A single load+store correctly handles overlapping memory in the memmove
193 // case.
Michael Liao69e172a2012-08-15 03:49:59 +0000194 uint64_t Size = MemOpLength->getLimitedValue();
Alp Tokercb402912014-01-24 17:20:08 +0000195 assert(Size && "0-sized memory transferring should be removed already.");
Jim Grosbach7815f562012-02-03 00:07:04 +0000196
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000197 if (Size > 8 || (Size&(Size-1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000198 return nullptr; // If not 1/2/4/8 bytes, exit.
Jim Grosbach7815f562012-02-03 00:07:04 +0000199
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000200 // Use an integer load+store unless we can find something better.
Mon P Wangc576ee92010-04-04 03:10:48 +0000201 unsigned SrcAddrSp =
Gabor Greif0a136c92010-06-24 13:54:33 +0000202 cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
Gabor Greiff3755202010-04-16 15:33:14 +0000203 unsigned DstAddrSp =
Gabor Greif0a136c92010-06-24 13:54:33 +0000204 cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
Mon P Wangc576ee92010-04-04 03:10:48 +0000205
Chris Lattner229907c2011-07-18 04:54:35 +0000206 IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
Mon P Wangc576ee92010-04-04 03:10:48 +0000207 Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
208 Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
Jim Grosbach7815f562012-02-03 00:07:04 +0000209
Mikael Holmen760dc9a2017-03-01 06:45:20 +0000210 // If the memcpy has metadata describing the members, see if we can get the
211 // TBAA tag describing our copy.
Craig Topperf40110f2014-04-25 05:29:35 +0000212 MDNode *CopyMD = nullptr;
Mikael Holmen760dc9a2017-03-01 06:45:20 +0000213 if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
214 if (M->getNumOperands() == 3 && M->getOperand(0) &&
215 mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
Craig Topper79ab6432017-07-06 18:39:47 +0000216 mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
Mikael Holmen760dc9a2017-03-01 06:45:20 +0000217 M->getOperand(1) &&
218 mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
219 mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
220 Size &&
221 M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
222 CopyMD = cast<MDNode>(M->getOperand(2));
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000223 }
Jim Grosbach7815f562012-02-03 00:07:04 +0000224
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000225 // If the memcpy/memmove provides better alignment info than we can
226 // infer, use it.
Pete Cooper67cf9a72015-11-19 05:56:52 +0000227 SrcAlign = std::max(SrcAlign, CopyAlign);
228 DstAlign = std::max(DstAlign, CopyAlign);
Jim Grosbach7815f562012-02-03 00:07:04 +0000229
Craig Topperbb4069e2017-07-07 23:16:26 +0000230 Value *Src = Builder.CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
231 Value *Dest = Builder.CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
232 LoadInst *L = Builder.CreateLoad(Src, MI->isVolatile());
Eli Friedman49346012011-05-18 19:57:14 +0000233 L->setAlignment(SrcAlign);
Dan Gohman3f553c22012-09-13 21:51:01 +0000234 if (CopyMD)
235 L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
Dorit Nuzmanabd15f62016-09-04 07:49:39 +0000236 MDNode *LoopMemParallelMD =
237 MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
238 if (LoopMemParallelMD)
239 L->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
Dorit Nuzman7673ba72016-09-04 07:06:00 +0000240
Craig Topperbb4069e2017-07-07 23:16:26 +0000241 StoreInst *S = Builder.CreateStore(L, Dest, MI->isVolatile());
Eli Friedman49346012011-05-18 19:57:14 +0000242 S->setAlignment(DstAlign);
Dan Gohman3f553c22012-09-13 21:51:01 +0000243 if (CopyMD)
244 S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
Dorit Nuzmanabd15f62016-09-04 07:49:39 +0000245 if (LoopMemParallelMD)
246 S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000247
248 // Set the size of the copy to 0, it will be deleted on the next iteration.
Gabor Greif5b1370e2010-06-28 16:50:57 +0000249 MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType()));
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000250 return MI;
251}
252
253Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000254 unsigned Alignment = getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT);
Pete Cooper67cf9a72015-11-19 05:56:52 +0000255 if (MI->getAlignment() < Alignment) {
256 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
257 Alignment, false));
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000258 return MI;
259 }
Jim Grosbach7815f562012-02-03 00:07:04 +0000260
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000261 // Extract the length and alignment and fill if they are constant.
262 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
263 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
Duncan Sands9dff9be2010-02-15 16:12:20 +0000264 if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
Craig Topperf40110f2014-04-25 05:29:35 +0000265 return nullptr;
Michael Liao69e172a2012-08-15 03:49:59 +0000266 uint64_t Len = LenC->getLimitedValue();
Pete Cooper67cf9a72015-11-19 05:56:52 +0000267 Alignment = MI->getAlignment();
Michael Liao69e172a2012-08-15 03:49:59 +0000268 assert(Len && "0-sized memory setting should be removed already.");
Jim Grosbach7815f562012-02-03 00:07:04 +0000269
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000270 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
271 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000272 Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
Jim Grosbach7815f562012-02-03 00:07:04 +0000273
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000274 Value *Dest = MI->getDest();
Mon P Wang1991c472010-12-20 01:05:30 +0000275 unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
276 Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
Craig Topperbb4069e2017-07-07 23:16:26 +0000277 Dest = Builder.CreateBitCast(Dest, NewDstPtrTy);
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000278
279 // Alignment 0 is identity for alignment 1 for memset, but not store.
280 if (Alignment == 0) Alignment = 1;
Jim Grosbach7815f562012-02-03 00:07:04 +0000281
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000282 // Extract the fill value and store.
283 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
Craig Topperbb4069e2017-07-07 23:16:26 +0000284 StoreInst *S = Builder.CreateStore(ConstantInt::get(ITy, Fill), Dest,
285 MI->isVolatile());
Eli Friedman49346012011-05-18 19:57:14 +0000286 S->setAlignment(Alignment);
Jim Grosbach7815f562012-02-03 00:07:04 +0000287
Chris Lattner7a9e47a2010-01-05 07:32:13 +0000288 // Set the size of the copy to 0, it will be deleted on the next iteration.
289 MI->setLength(Constant::getNullValue(LenC->getType()));
290 return MI;
291 }
292
Simon Pilgrim18617d12015-08-05 08:18:00 +0000293 return nullptr;
294}
295
Sanjay Patel6038d3e2016-01-29 23:27:03 +0000296static Value *simplifyX86immShift(const IntrinsicInst &II,
Simon Pilgrimbecd5e82015-08-13 07:39:03 +0000297 InstCombiner::BuilderTy &Builder) {
298 bool LogicalShift = false;
299 bool ShiftLeft = false;
300
301 switch (II.getIntrinsicID()) {
Craig Topperb4173a52016-11-13 07:26:19 +0000302 default: llvm_unreachable("Unexpected intrinsic!");
Simon Pilgrimbecd5e82015-08-13 07:39:03 +0000303 case Intrinsic::x86_sse2_psra_d:
304 case Intrinsic::x86_sse2_psra_w:
305 case Intrinsic::x86_sse2_psrai_d:
306 case Intrinsic::x86_sse2_psrai_w:
307 case Intrinsic::x86_avx2_psra_d:
308 case Intrinsic::x86_avx2_psra_w:
309 case Intrinsic::x86_avx2_psrai_d:
310 case Intrinsic::x86_avx2_psrai_w:
Craig Topper8b831cb2016-11-13 01:51:55 +0000311 case Intrinsic::x86_avx512_psra_q_128:
312 case Intrinsic::x86_avx512_psrai_q_128:
313 case Intrinsic::x86_avx512_psra_q_256:
314 case Intrinsic::x86_avx512_psrai_q_256:
315 case Intrinsic::x86_avx512_psra_d_512:
316 case Intrinsic::x86_avx512_psra_q_512:
317 case Intrinsic::x86_avx512_psra_w_512:
318 case Intrinsic::x86_avx512_psrai_d_512:
319 case Intrinsic::x86_avx512_psrai_q_512:
320 case Intrinsic::x86_avx512_psrai_w_512:
Simon Pilgrimbecd5e82015-08-13 07:39:03 +0000321 LogicalShift = false; ShiftLeft = false;
322 break;
323 case Intrinsic::x86_sse2_psrl_d:
324 case Intrinsic::x86_sse2_psrl_q:
325 case Intrinsic::x86_sse2_psrl_w:
326 case Intrinsic::x86_sse2_psrli_d:
327 case Intrinsic::x86_sse2_psrli_q:
328 case Intrinsic::x86_sse2_psrli_w:
329 case Intrinsic::x86_avx2_psrl_d:
330 case Intrinsic::x86_avx2_psrl_q:
331 case Intrinsic::x86_avx2_psrl_w:
332 case Intrinsic::x86_avx2_psrli_d:
333 case Intrinsic::x86_avx2_psrli_q:
334 case Intrinsic::x86_avx2_psrli_w:
Craig Topper8b831cb2016-11-13 01:51:55 +0000335 case Intrinsic::x86_avx512_psrl_d_512:
336 case Intrinsic::x86_avx512_psrl_q_512:
337 case Intrinsic::x86_avx512_psrl_w_512:
338 case Intrinsic::x86_avx512_psrli_d_512:
339 case Intrinsic::x86_avx512_psrli_q_512:
340 case Intrinsic::x86_avx512_psrli_w_512:
Simon Pilgrimbecd5e82015-08-13 07:39:03 +0000341 LogicalShift = true; ShiftLeft = false;
342 break;
343 case Intrinsic::x86_sse2_psll_d:
344 case Intrinsic::x86_sse2_psll_q:
345 case Intrinsic::x86_sse2_psll_w:
346 case Intrinsic::x86_sse2_pslli_d:
347 case Intrinsic::x86_sse2_pslli_q:
348 case Intrinsic::x86_sse2_pslli_w:
349 case Intrinsic::x86_avx2_psll_d:
350 case Intrinsic::x86_avx2_psll_q:
351 case Intrinsic::x86_avx2_psll_w:
352 case Intrinsic::x86_avx2_pslli_d:
353 case Intrinsic::x86_avx2_pslli_q:
354 case Intrinsic::x86_avx2_pslli_w:
Craig Topper8b831cb2016-11-13 01:51:55 +0000355 case Intrinsic::x86_avx512_psll_d_512:
356 case Intrinsic::x86_avx512_psll_q_512:
357 case Intrinsic::x86_avx512_psll_w_512:
358 case Intrinsic::x86_avx512_pslli_d_512:
359 case Intrinsic::x86_avx512_pslli_q_512:
360 case Intrinsic::x86_avx512_pslli_w_512:
Simon Pilgrimbecd5e82015-08-13 07:39:03 +0000361 LogicalShift = true; ShiftLeft = true;
362 break;
363 }
Simon Pilgrima3a72b42015-08-10 20:21:15 +0000364 assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left");
365
Simon Pilgrim3815c162015-08-07 18:22:50 +0000366 // Simplify if count is constant.
367 auto Arg1 = II.getArgOperand(1);
368 auto CAZ = dyn_cast<ConstantAggregateZero>(Arg1);
369 auto CDV = dyn_cast<ConstantDataVector>(Arg1);
370 auto CInt = dyn_cast<ConstantInt>(Arg1);
371 if (!CAZ && !CDV && !CInt)
Simon Pilgrim18617d12015-08-05 08:18:00 +0000372 return nullptr;
Simon Pilgrim3815c162015-08-07 18:22:50 +0000373
374 APInt Count(64, 0);
375 if (CDV) {
376 // SSE2/AVX2 uses all the first 64-bits of the 128-bit vector
377 // operand to compute the shift amount.
378 auto VT = cast<VectorType>(CDV->getType());
379 unsigned BitWidth = VT->getElementType()->getPrimitiveSizeInBits();
380 assert((64 % BitWidth) == 0 && "Unexpected packed shift size");
381 unsigned NumSubElts = 64 / BitWidth;
382
383 // Concatenate the sub-elements to create the 64-bit value.
384 for (unsigned i = 0; i != NumSubElts; ++i) {
385 unsigned SubEltIdx = (NumSubElts - 1) - i;
386 auto SubElt = cast<ConstantInt>(CDV->getElementAsConstant(SubEltIdx));
Craig Topper24e71012017-04-28 03:36:24 +0000387 Count <<= BitWidth;
Simon Pilgrim3815c162015-08-07 18:22:50 +0000388 Count |= SubElt->getValue().zextOrTrunc(64);
389 }
390 }
391 else if (CInt)
392 Count = CInt->getValue();
Simon Pilgrim18617d12015-08-05 08:18:00 +0000393
394 auto Vec = II.getArgOperand(0);
395 auto VT = cast<VectorType>(Vec->getType());
396 auto SVT = VT->getElementType();
Simon Pilgrim3815c162015-08-07 18:22:50 +0000397 unsigned VWidth = VT->getNumElements();
398 unsigned BitWidth = SVT->getPrimitiveSizeInBits();
399
400 // If shift-by-zero then just return the original value.
Craig Topper73ba1c82017-06-07 07:40:37 +0000401 if (Count.isNullValue())
Simon Pilgrim3815c162015-08-07 18:22:50 +0000402 return Vec;
403
Simon Pilgrima3a72b42015-08-10 20:21:15 +0000404 // Handle cases when Shift >= BitWidth.
405 if (Count.uge(BitWidth)) {
406 // If LogicalShift - just return zero.
407 if (LogicalShift)
408 return ConstantAggregateZero::get(VT);
409
410 // If ArithmeticShift - clamp Shift to (BitWidth - 1).
411 Count = APInt(64, BitWidth - 1);
412 }
Simon Pilgrim18617d12015-08-05 08:18:00 +0000413
Simon Pilgrim18617d12015-08-05 08:18:00 +0000414 // Get a constant vector of the same type as the first operand.
Simon Pilgrim3815c162015-08-07 18:22:50 +0000415 auto ShiftAmt = ConstantInt::get(SVT, Count.zextOrTrunc(BitWidth));
416 auto ShiftVec = Builder.CreateVectorSplat(VWidth, ShiftAmt);
Simon Pilgrim18617d12015-08-05 08:18:00 +0000417
418 if (ShiftLeft)
Simon Pilgrim3815c162015-08-07 18:22:50 +0000419 return Builder.CreateShl(Vec, ShiftVec);
Simon Pilgrim18617d12015-08-05 08:18:00 +0000420
Simon Pilgrima3a72b42015-08-10 20:21:15 +0000421 if (LogicalShift)
422 return Builder.CreateLShr(Vec, ShiftVec);
423
424 return Builder.CreateAShr(Vec, ShiftVec);
Simon Pilgrim18617d12015-08-05 08:18:00 +0000425}
426
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000427// Attempt to simplify AVX2 per-element shift intrinsics to a generic IR shift.
428// Unlike the generic IR shifts, the intrinsics have defined behaviour for out
429// of range shift amounts (logical - set to zero, arithmetic - splat sign bit).
430static Value *simplifyX86varShift(const IntrinsicInst &II,
431 InstCombiner::BuilderTy &Builder) {
432 bool LogicalShift = false;
433 bool ShiftLeft = false;
434
435 switch (II.getIntrinsicID()) {
Craig Topperb4173a52016-11-13 07:26:19 +0000436 default: llvm_unreachable("Unexpected intrinsic!");
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000437 case Intrinsic::x86_avx2_psrav_d:
438 case Intrinsic::x86_avx2_psrav_d_256:
Craig Topperb4173a52016-11-13 07:26:19 +0000439 case Intrinsic::x86_avx512_psrav_q_128:
440 case Intrinsic::x86_avx512_psrav_q_256:
441 case Intrinsic::x86_avx512_psrav_d_512:
442 case Intrinsic::x86_avx512_psrav_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +0000443 case Intrinsic::x86_avx512_psrav_w_128:
444 case Intrinsic::x86_avx512_psrav_w_256:
445 case Intrinsic::x86_avx512_psrav_w_512:
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000446 LogicalShift = false;
447 ShiftLeft = false;
448 break;
449 case Intrinsic::x86_avx2_psrlv_d:
450 case Intrinsic::x86_avx2_psrlv_d_256:
451 case Intrinsic::x86_avx2_psrlv_q:
452 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topperb4173a52016-11-13 07:26:19 +0000453 case Intrinsic::x86_avx512_psrlv_d_512:
454 case Intrinsic::x86_avx512_psrlv_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +0000455 case Intrinsic::x86_avx512_psrlv_w_128:
456 case Intrinsic::x86_avx512_psrlv_w_256:
457 case Intrinsic::x86_avx512_psrlv_w_512:
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000458 LogicalShift = true;
459 ShiftLeft = false;
460 break;
461 case Intrinsic::x86_avx2_psllv_d:
462 case Intrinsic::x86_avx2_psllv_d_256:
463 case Intrinsic::x86_avx2_psllv_q:
464 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topperb4173a52016-11-13 07:26:19 +0000465 case Intrinsic::x86_avx512_psllv_d_512:
466 case Intrinsic::x86_avx512_psllv_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +0000467 case Intrinsic::x86_avx512_psllv_w_128:
468 case Intrinsic::x86_avx512_psllv_w_256:
469 case Intrinsic::x86_avx512_psllv_w_512:
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000470 LogicalShift = true;
471 ShiftLeft = true;
472 break;
473 }
474 assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left");
475
476 // Simplify if all shift amounts are constant/undef.
477 auto *CShift = dyn_cast<Constant>(II.getArgOperand(1));
478 if (!CShift)
479 return nullptr;
480
481 auto Vec = II.getArgOperand(0);
482 auto VT = cast<VectorType>(II.getType());
483 auto SVT = VT->getVectorElementType();
484 int NumElts = VT->getNumElements();
485 int BitWidth = SVT->getIntegerBitWidth();
486
487 // Collect each element's shift amount.
488 // We also collect special cases: UNDEF = -1, OUT-OF-RANGE = BitWidth.
489 bool AnyOutOfRange = false;
490 SmallVector<int, 8> ShiftAmts;
491 for (int I = 0; I < NumElts; ++I) {
492 auto *CElt = CShift->getAggregateElement(I);
493 if (CElt && isa<UndefValue>(CElt)) {
494 ShiftAmts.push_back(-1);
495 continue;
496 }
497
498 auto *COp = dyn_cast_or_null<ConstantInt>(CElt);
499 if (!COp)
500 return nullptr;
501
502 // Handle out of range shifts.
503 // If LogicalShift - set to BitWidth (special case).
504 // If ArithmeticShift - set to (BitWidth - 1) (sign splat).
505 APInt ShiftVal = COp->getValue();
506 if (ShiftVal.uge(BitWidth)) {
507 AnyOutOfRange = LogicalShift;
508 ShiftAmts.push_back(LogicalShift ? BitWidth : BitWidth - 1);
509 continue;
510 }
511
512 ShiftAmts.push_back((int)ShiftVal.getZExtValue());
513 }
514
515 // If all elements out of range or UNDEF, return vector of zeros/undefs.
516 // ArithmeticShift should only hit this if they are all UNDEF.
517 auto OutOfRange = [&](int Idx) { return (Idx < 0) || (BitWidth <= Idx); };
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000518 if (all_of(ShiftAmts, OutOfRange)) {
Simon Pilgrimdb9893f2016-06-07 10:27:15 +0000519 SmallVector<Constant *, 8> ConstantVec;
520 for (int Idx : ShiftAmts) {
521 if (Idx < 0) {
522 ConstantVec.push_back(UndefValue::get(SVT));
523 } else {
524 assert(LogicalShift && "Logical shift expected");
525 ConstantVec.push_back(ConstantInt::getNullValue(SVT));
526 }
527 }
528 return ConstantVector::get(ConstantVec);
529 }
530
531 // We can't handle only some out of range values with generic logical shifts.
532 if (AnyOutOfRange)
533 return nullptr;
534
535 // Build the shift amount constant vector.
536 SmallVector<Constant *, 8> ShiftVecAmts;
537 for (int Idx : ShiftAmts) {
538 if (Idx < 0)
539 ShiftVecAmts.push_back(UndefValue::get(SVT));
540 else
541 ShiftVecAmts.push_back(ConstantInt::get(SVT, Idx));
542 }
543 auto ShiftVec = ConstantVector::get(ShiftVecAmts);
544
545 if (ShiftLeft)
546 return Builder.CreateShl(Vec, ShiftVec);
547
548 if (LogicalShift)
549 return Builder.CreateLShr(Vec, ShiftVec);
550
551 return Builder.CreateAShr(Vec, ShiftVec);
552}
553
Simon Pilgrimf6f3a3612017-01-23 15:22:59 +0000554static Value *simplifyX86muldq(const IntrinsicInst &II,
555 InstCombiner::BuilderTy &Builder) {
Simon Pilgrima50a93f2017-01-20 18:20:30 +0000556 Value *Arg0 = II.getArgOperand(0);
557 Value *Arg1 = II.getArgOperand(1);
558 Type *ResTy = II.getType();
Simon Pilgrimf6f3a3612017-01-23 15:22:59 +0000559 assert(Arg0->getType()->getScalarSizeInBits() == 32 &&
560 Arg1->getType()->getScalarSizeInBits() == 32 &&
561 ResTy->getScalarSizeInBits() == 64 && "Unexpected muldq/muludq types");
Simon Pilgrima50a93f2017-01-20 18:20:30 +0000562
Simon Pilgrimbb13fda2017-01-23 12:07:32 +0000563 // muldq/muludq(undef, undef) -> zero (matches generic mul behavior)
Simon Pilgrim78f86302017-01-24 11:07:41 +0000564 if (isa<UndefValue>(Arg0) || isa<UndefValue>(Arg1))
Simon Pilgrimbb13fda2017-01-23 12:07:32 +0000565 return ConstantAggregateZero::get(ResTy);
Simon Pilgrima50a93f2017-01-20 18:20:30 +0000566
Simon Pilgrimf6f3a3612017-01-23 15:22:59 +0000567 // Constant folding.
568 // PMULDQ = (mul(vXi64 sext(shuffle<0,2,..>(Arg0)),
569 // vXi64 sext(shuffle<0,2,..>(Arg1))))
570 // PMULUDQ = (mul(vXi64 zext(shuffle<0,2,..>(Arg0)),
571 // vXi64 zext(shuffle<0,2,..>(Arg1))))
572 if (!isa<Constant>(Arg0) || !isa<Constant>(Arg1))
573 return nullptr;
574
575 unsigned NumElts = ResTy->getVectorNumElements();
576 assert(Arg0->getType()->getVectorNumElements() == (2 * NumElts) &&
577 Arg1->getType()->getVectorNumElements() == (2 * NumElts) &&
578 "Unexpected muldq/muludq types");
579
580 unsigned IntrinsicID = II.getIntrinsicID();
581 bool IsSigned = (Intrinsic::x86_sse41_pmuldq == IntrinsicID ||
582 Intrinsic::x86_avx2_pmul_dq == IntrinsicID ||
583 Intrinsic::x86_avx512_pmul_dq_512 == IntrinsicID);
584
585 SmallVector<unsigned, 16> ShuffleMask;
586 for (unsigned i = 0; i != NumElts; ++i)
587 ShuffleMask.push_back(i * 2);
588
589 auto *LHS = Builder.CreateShuffleVector(Arg0, Arg0, ShuffleMask);
590 auto *RHS = Builder.CreateShuffleVector(Arg1, Arg1, ShuffleMask);
591
592 if (IsSigned) {
593 LHS = Builder.CreateSExt(LHS, ResTy);
594 RHS = Builder.CreateSExt(RHS, ResTy);
595 } else {
596 LHS = Builder.CreateZExt(LHS, ResTy);
597 RHS = Builder.CreateZExt(RHS, ResTy);
598 }
599
600 return Builder.CreateMul(LHS, RHS);
Simon Pilgrima50a93f2017-01-20 18:20:30 +0000601}
602
Craig Topper4853c432017-07-06 23:18:42 +0000603static Value *simplifyX86pack(IntrinsicInst &II, bool IsSigned) {
Simon Pilgrim6f6b2792017-01-25 14:37:24 +0000604 Value *Arg0 = II.getArgOperand(0);
605 Value *Arg1 = II.getArgOperand(1);
606 Type *ResTy = II.getType();
607
608 // Fast all undef handling.
609 if (isa<UndefValue>(Arg0) && isa<UndefValue>(Arg1))
610 return UndefValue::get(ResTy);
611
612 Type *ArgTy = Arg0->getType();
613 unsigned NumLanes = ResTy->getPrimitiveSizeInBits() / 128;
614 unsigned NumDstElts = ResTy->getVectorNumElements();
615 unsigned NumSrcElts = ArgTy->getVectorNumElements();
616 assert(NumDstElts == (2 * NumSrcElts) && "Unexpected packing types");
617
618 unsigned NumDstEltsPerLane = NumDstElts / NumLanes;
619 unsigned NumSrcEltsPerLane = NumSrcElts / NumLanes;
620 unsigned DstScalarSizeInBits = ResTy->getScalarSizeInBits();
621 assert(ArgTy->getScalarSizeInBits() == (2 * DstScalarSizeInBits) &&
622 "Unexpected packing types");
623
624 // Constant folding.
625 auto *Cst0 = dyn_cast<Constant>(Arg0);
626 auto *Cst1 = dyn_cast<Constant>(Arg1);
627 if (!Cst0 || !Cst1)
628 return nullptr;
629
630 SmallVector<Constant *, 32> Vals;
631 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
632 for (unsigned Elt = 0; Elt != NumDstEltsPerLane; ++Elt) {
633 unsigned SrcIdx = Lane * NumSrcEltsPerLane + Elt % NumSrcEltsPerLane;
634 auto *Cst = (Elt >= NumSrcEltsPerLane) ? Cst1 : Cst0;
635 auto *COp = Cst->getAggregateElement(SrcIdx);
636 if (COp && isa<UndefValue>(COp)) {
637 Vals.push_back(UndefValue::get(ResTy->getScalarType()));
638 continue;
639 }
640
641 auto *CInt = dyn_cast_or_null<ConstantInt>(COp);
642 if (!CInt)
643 return nullptr;
644
645 APInt Val = CInt->getValue();
646 assert(Val.getBitWidth() == ArgTy->getScalarSizeInBits() &&
647 "Unexpected constant bitwidth");
648
649 if (IsSigned) {
650 // PACKSS: Truncate signed value with signed saturation.
651 // Source values less than dst minint are saturated to minint.
652 // Source values greater than dst maxint are saturated to maxint.
653 if (Val.isSignedIntN(DstScalarSizeInBits))
654 Val = Val.trunc(DstScalarSizeInBits);
655 else if (Val.isNegative())
656 Val = APInt::getSignedMinValue(DstScalarSizeInBits);
657 else
658 Val = APInt::getSignedMaxValue(DstScalarSizeInBits);
659 } else {
660 // PACKUS: Truncate signed value with unsigned saturation.
661 // Source values less than zero are saturated to zero.
662 // Source values greater than dst maxuint are saturated to maxuint.
663 if (Val.isIntN(DstScalarSizeInBits))
664 Val = Val.trunc(DstScalarSizeInBits);
665 else if (Val.isNegative())
666 Val = APInt::getNullValue(DstScalarSizeInBits);
667 else
668 Val = APInt::getAllOnesValue(DstScalarSizeInBits);
669 }
670
671 Vals.push_back(ConstantInt::get(ResTy->getScalarType(), Val));
672 }
673 }
674
675 return ConstantVector::get(Vals);
676}
677
Craig Topper4853c432017-07-06 23:18:42 +0000678static Value *simplifyX86movmsk(const IntrinsicInst &II) {
Simon Pilgrim91e3ac82016-06-07 08:18:35 +0000679 Value *Arg = II.getArgOperand(0);
680 Type *ResTy = II.getType();
681 Type *ArgTy = Arg->getType();
682
683 // movmsk(undef) -> zero as we must ensure the upper bits are zero.
684 if (isa<UndefValue>(Arg))
685 return Constant::getNullValue(ResTy);
686
687 // We can't easily peek through x86_mmx types.
688 if (!ArgTy->isVectorTy())
689 return nullptr;
690
691 auto *C = dyn_cast<Constant>(Arg);
692 if (!C)
693 return nullptr;
694
695 // Extract signbits of the vector input and pack into integer result.
696 APInt Result(ResTy->getPrimitiveSizeInBits(), 0);
697 for (unsigned I = 0, E = ArgTy->getVectorNumElements(); I != E; ++I) {
698 auto *COp = C->getAggregateElement(I);
699 if (!COp)
700 return nullptr;
701 if (isa<UndefValue>(COp))
702 continue;
703
704 auto *CInt = dyn_cast<ConstantInt>(COp);
705 auto *CFp = dyn_cast<ConstantFP>(COp);
706 if (!CInt && !CFp)
707 return nullptr;
708
709 if ((CInt && CInt->isNegative()) || (CFp && CFp->isNegative()))
710 Result.setBit(I);
711 }
712
713 return Constant::getIntegerValue(ResTy, Result);
714}
715
Sanjay Patel6038d3e2016-01-29 23:27:03 +0000716static Value *simplifyX86insertps(const IntrinsicInst &II,
Sanjay Patelc86867c2015-04-16 17:52:13 +0000717 InstCombiner::BuilderTy &Builder) {
Sanjay Patel03c03f52016-01-28 00:03:16 +0000718 auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2));
719 if (!CInt)
720 return nullptr;
Simon Pilgrim54fcd622015-07-25 20:41:00 +0000721
Sanjay Patel03c03f52016-01-28 00:03:16 +0000722 VectorType *VecTy = cast<VectorType>(II.getType());
723 assert(VecTy->getNumElements() == 4 && "insertps with wrong vector type");
Sanjay Patelc86867c2015-04-16 17:52:13 +0000724
Sanjay Patel03c03f52016-01-28 00:03:16 +0000725 // The immediate permute control byte looks like this:
726 // [3:0] - zero mask for each 32-bit lane
727 // [5:4] - select one 32-bit destination lane
728 // [7:6] - select one 32-bit source lane
Sanjay Patelc86867c2015-04-16 17:52:13 +0000729
Sanjay Patel03c03f52016-01-28 00:03:16 +0000730 uint8_t Imm = CInt->getZExtValue();
731 uint8_t ZMask = Imm & 0xf;
732 uint8_t DestLane = (Imm >> 4) & 0x3;
733 uint8_t SourceLane = (Imm >> 6) & 0x3;
Sanjay Patelc1d20a32015-04-25 20:55:25 +0000734
Sanjay Patel03c03f52016-01-28 00:03:16 +0000735 ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy);
Sanjay Patelc86867c2015-04-16 17:52:13 +0000736
Sanjay Patel03c03f52016-01-28 00:03:16 +0000737 // If all zero mask bits are set, this was just a weird way to
738 // generate a zero vector.
739 if (ZMask == 0xf)
740 return ZeroVector;
Sanjay Patelc1d20a32015-04-25 20:55:25 +0000741
Sanjay Patel03c03f52016-01-28 00:03:16 +0000742 // Initialize by passing all of the first source bits through.
Craig Topper99d1eab2016-06-12 00:41:19 +0000743 uint32_t ShuffleMask[4] = { 0, 1, 2, 3 };
Sanjay Patelc1d20a32015-04-25 20:55:25 +0000744
Sanjay Patel03c03f52016-01-28 00:03:16 +0000745 // We may replace the second operand with the zero vector.
746 Value *V1 = II.getArgOperand(1);
747
748 if (ZMask) {
749 // If the zero mask is being used with a single input or the zero mask
750 // overrides the destination lane, this is a shuffle with the zero vector.
751 if ((II.getArgOperand(0) == II.getArgOperand(1)) ||
752 (ZMask & (1 << DestLane))) {
753 V1 = ZeroVector;
754 // We may still move 32-bits of the first source vector from one lane
755 // to another.
756 ShuffleMask[DestLane] = SourceLane;
757 // The zero mask may override the previous insert operation.
758 for (unsigned i = 0; i < 4; ++i)
759 if ((ZMask >> i) & 0x1)
760 ShuffleMask[i] = i + 4;
Sanjay Patelc1d20a32015-04-25 20:55:25 +0000761 } else {
Sanjay Patel03c03f52016-01-28 00:03:16 +0000762 // TODO: Model this case as 2 shuffles or a 'logical and' plus shuffle?
763 return nullptr;
Sanjay Patelc1d20a32015-04-25 20:55:25 +0000764 }
Sanjay Patel03c03f52016-01-28 00:03:16 +0000765 } else {
766 // Replace the selected destination lane with the selected source lane.
767 ShuffleMask[DestLane] = SourceLane + 4;
Sanjay Patelc86867c2015-04-16 17:52:13 +0000768 }
Sanjay Patel03c03f52016-01-28 00:03:16 +0000769
770 return Builder.CreateShuffleVector(II.getArgOperand(0), V1, ShuffleMask);
Sanjay Patelc86867c2015-04-16 17:52:13 +0000771}
772
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000773/// Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding
774/// or conversion to a shuffle vector.
Sanjay Patel6038d3e2016-01-29 23:27:03 +0000775static Value *simplifyX86extrq(IntrinsicInst &II, Value *Op0,
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000776 ConstantInt *CILength, ConstantInt *CIIndex,
777 InstCombiner::BuilderTy &Builder) {
778 auto LowConstantHighUndef = [&](uint64_t Val) {
779 Type *IntTy64 = Type::getInt64Ty(II.getContext());
780 Constant *Args[] = {ConstantInt::get(IntTy64, Val),
781 UndefValue::get(IntTy64)};
782 return ConstantVector::get(Args);
783 };
784
785 // See if we're dealing with constant values.
786 Constant *C0 = dyn_cast<Constant>(Op0);
787 ConstantInt *CI0 =
Andrea Di Biagio8df5b9c2016-09-07 12:03:03 +0000788 C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000789 : nullptr;
790
791 // Attempt to constant fold.
792 if (CILength && CIIndex) {
793 // From AMD documentation: "The bit index and field length are each six
794 // bits in length other bits of the field are ignored."
795 APInt APIndex = CIIndex->getValue().zextOrTrunc(6);
796 APInt APLength = CILength->getValue().zextOrTrunc(6);
797
798 unsigned Index = APIndex.getZExtValue();
799
800 // From AMD documentation: "a value of zero in the field length is
801 // defined as length of 64".
802 unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue();
803
804 // From AMD documentation: "If the sum of the bit index + length field
805 // is greater than 64, the results are undefined".
806 unsigned End = Index + Length;
807
808 // Note that both field index and field length are 8-bit quantities.
809 // Since variables 'Index' and 'Length' are unsigned values
810 // obtained from zero-extending field index and field length
811 // respectively, their sum should never wrap around.
812 if (End > 64)
813 return UndefValue::get(II.getType());
814
815 // If we are inserting whole bytes, we can convert this to a shuffle.
816 // Lowering can recognize EXTRQI shuffle masks.
817 if ((Length % 8) == 0 && (Index % 8) == 0) {
818 // Convert bit indices to byte indices.
819 Length /= 8;
820 Index /= 8;
821
822 Type *IntTy8 = Type::getInt8Ty(II.getContext());
823 Type *IntTy32 = Type::getInt32Ty(II.getContext());
824 VectorType *ShufTy = VectorType::get(IntTy8, 16);
825
826 SmallVector<Constant *, 16> ShuffleMask;
827 for (int i = 0; i != (int)Length; ++i)
828 ShuffleMask.push_back(
829 Constant::getIntegerValue(IntTy32, APInt(32, i + Index)));
830 for (int i = Length; i != 8; ++i)
831 ShuffleMask.push_back(
832 Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
833 for (int i = 8; i != 16; ++i)
834 ShuffleMask.push_back(UndefValue::get(IntTy32));
835
836 Value *SV = Builder.CreateShuffleVector(
837 Builder.CreateBitCast(Op0, ShufTy),
838 ConstantAggregateZero::get(ShufTy), ConstantVector::get(ShuffleMask));
839 return Builder.CreateBitCast(SV, II.getType());
840 }
841
842 // Constant Fold - shift Index'th bit to lowest position and mask off
843 // Length bits.
844 if (CI0) {
845 APInt Elt = CI0->getValue();
Craig Topperfc947bc2017-04-18 17:14:21 +0000846 Elt.lshrInPlace(Index);
847 Elt = Elt.zextOrTrunc(Length);
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000848 return LowConstantHighUndef(Elt.getZExtValue());
849 }
850
851 // If we were an EXTRQ call, we'll save registers if we convert to EXTRQI.
852 if (II.getIntrinsicID() == Intrinsic::x86_sse4a_extrq) {
853 Value *Args[] = {Op0, CILength, CIIndex};
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000854 Module *M = II.getModule();
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000855 Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_extrqi);
856 return Builder.CreateCall(F, Args);
857 }
858 }
859
860 // Constant Fold - extraction from zero is always {zero, undef}.
Craig Topperca2c8762017-07-06 18:39:49 +0000861 if (CI0 && CI0->isZero())
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000862 return LowConstantHighUndef(0);
863
864 return nullptr;
865}
866
867/// Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant
868/// folding or conversion to a shuffle vector.
Sanjay Patel6038d3e2016-01-29 23:27:03 +0000869static Value *simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1,
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000870 APInt APLength, APInt APIndex,
871 InstCombiner::BuilderTy &Builder) {
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000872 // From AMD documentation: "The bit index and field length are each six bits
873 // in length other bits of the field are ignored."
874 APIndex = APIndex.zextOrTrunc(6);
875 APLength = APLength.zextOrTrunc(6);
876
877 // Attempt to constant fold.
878 unsigned Index = APIndex.getZExtValue();
879
880 // From AMD documentation: "a value of zero in the field length is
881 // defined as length of 64".
882 unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue();
883
884 // From AMD documentation: "If the sum of the bit index + length field
885 // is greater than 64, the results are undefined".
886 unsigned End = Index + Length;
887
888 // Note that both field index and field length are 8-bit quantities.
889 // Since variables 'Index' and 'Length' are unsigned values
890 // obtained from zero-extending field index and field length
891 // respectively, their sum should never wrap around.
892 if (End > 64)
893 return UndefValue::get(II.getType());
894
895 // If we are inserting whole bytes, we can convert this to a shuffle.
896 // Lowering can recognize INSERTQI shuffle masks.
897 if ((Length % 8) == 0 && (Index % 8) == 0) {
898 // Convert bit indices to byte indices.
899 Length /= 8;
900 Index /= 8;
901
902 Type *IntTy8 = Type::getInt8Ty(II.getContext());
903 Type *IntTy32 = Type::getInt32Ty(II.getContext());
904 VectorType *ShufTy = VectorType::get(IntTy8, 16);
905
906 SmallVector<Constant *, 16> ShuffleMask;
907 for (int i = 0; i != (int)Index; ++i)
908 ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
909 for (int i = 0; i != (int)Length; ++i)
910 ShuffleMask.push_back(
911 Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
912 for (int i = Index + Length; i != 8; ++i)
913 ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
914 for (int i = 8; i != 16; ++i)
915 ShuffleMask.push_back(UndefValue::get(IntTy32));
916
917 Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy),
918 Builder.CreateBitCast(Op1, ShufTy),
919 ConstantVector::get(ShuffleMask));
920 return Builder.CreateBitCast(SV, II.getType());
921 }
922
923 // See if we're dealing with constant values.
924 Constant *C0 = dyn_cast<Constant>(Op0);
925 Constant *C1 = dyn_cast<Constant>(Op1);
926 ConstantInt *CI00 =
Andrea Di Biagiof3fd3162016-09-07 12:47:53 +0000927 C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000928 : nullptr;
929 ConstantInt *CI10 =
Andrea Di Biagiof3fd3162016-09-07 12:47:53 +0000930 C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000931 : nullptr;
932
933 // Constant Fold - insert bottom Length bits starting at the Index'th bit.
934 if (CI00 && CI10) {
935 APInt V00 = CI00->getValue();
936 APInt V10 = CI10->getValue();
937 APInt Mask = APInt::getLowBitsSet(64, Length).shl(Index);
938 V00 = V00 & ~Mask;
939 V10 = V10.zextOrTrunc(Length).zextOrTrunc(64).shl(Index);
940 APInt Val = V00 | V10;
941 Type *IntTy64 = Type::getInt64Ty(II.getContext());
942 Constant *Args[] = {ConstantInt::get(IntTy64, Val.getZExtValue()),
943 UndefValue::get(IntTy64)};
944 return ConstantVector::get(Args);
945 }
946
947 // If we were an INSERTQ call, we'll save demanded elements if we convert to
948 // INSERTQI.
949 if (II.getIntrinsicID() == Intrinsic::x86_sse4a_insertq) {
950 Type *IntTy8 = Type::getInt8Ty(II.getContext());
951 Constant *CILength = ConstantInt::get(IntTy8, Length, false);
952 Constant *CIIndex = ConstantInt::get(IntTy8, Index, false);
953
954 Value *Args[] = {Op0, Op1, CILength, CIIndex};
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000955 Module *M = II.getModule();
Simon Pilgrim216b1bf2015-10-17 11:40:05 +0000956 Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_insertqi);
957 return Builder.CreateCall(F, Args);
958 }
959
960 return nullptr;
961}
962
Simon Pilgrimc0c56e72016-04-24 17:00:34 +0000963/// Attempt to convert pshufb* to shufflevector if the mask is constant.
964static Value *simplifyX86pshufb(const IntrinsicInst &II,
965 InstCombiner::BuilderTy &Builder) {
Simon Pilgrimbf60cc42016-04-29 21:34:54 +0000966 Constant *V = dyn_cast<Constant>(II.getArgOperand(1));
967 if (!V)
968 return nullptr;
969
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +0000970 auto *VecTy = cast<VectorType>(II.getType());
971 auto *MaskEltTy = Type::getInt32Ty(II.getContext());
972 unsigned NumElts = VecTy->getNumElements();
Craig Topper9a63d7a2016-12-11 00:23:50 +0000973 assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
Simon Pilgrimc0c56e72016-04-24 17:00:34 +0000974 "Unexpected number of elements in shuffle mask!");
Simon Pilgrimbf60cc42016-04-29 21:34:54 +0000975
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +0000976 // Construct a shuffle mask from constant integers or UNDEFs.
Craig Topper9a63d7a2016-12-11 00:23:50 +0000977 Constant *Indexes[64] = {nullptr};
Simon Pilgrimc0c56e72016-04-24 17:00:34 +0000978
Simon Pilgrimbf60cc42016-04-29 21:34:54 +0000979 // Each byte in the shuffle control mask forms an index to permute the
980 // corresponding byte in the destination operand.
981 for (unsigned I = 0; I < NumElts; ++I) {
982 Constant *COp = V->getAggregateElement(I);
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +0000983 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
Simon Pilgrimbf60cc42016-04-29 21:34:54 +0000984 return nullptr;
Simon Pilgrimc0c56e72016-04-24 17:00:34 +0000985
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +0000986 if (isa<UndefValue>(COp)) {
987 Indexes[I] = UndefValue::get(MaskEltTy);
988 continue;
989 }
990
Simon Pilgrimbf60cc42016-04-29 21:34:54 +0000991 int8_t Index = cast<ConstantInt>(COp)->getValue().getZExtValue();
992
993 // If the most significant bit (bit[7]) of each byte of the shuffle
994 // control mask is set, then zero is written in the result byte.
995 // The zero vector is in the right-hand side of the resulting
996 // shufflevector.
997
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +0000998 // The value of each index for the high 128-bit lane is the least
999 // significant 4 bits of the respective shuffle control byte.
1000 Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0);
1001 Indexes[I] = ConstantInt::get(MaskEltTy, Index);
Simon Pilgrimbf60cc42016-04-29 21:34:54 +00001002 }
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00001003
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +00001004 auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00001005 auto V1 = II.getArgOperand(0);
Simon Pilgrime5e8c2f2016-05-01 19:26:21 +00001006 auto V2 = Constant::getNullValue(VecTy);
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00001007 return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1008}
1009
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00001010/// Attempt to convert vpermilvar* to shufflevector if the mask is constant.
1011static Value *simplifyX86vpermilvar(const IntrinsicInst &II,
1012 InstCombiner::BuilderTy &Builder) {
Simon Pilgrim640f9962016-04-30 07:23:30 +00001013 Constant *V = dyn_cast<Constant>(II.getArgOperand(1));
1014 if (!V)
1015 return nullptr;
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00001016
Craig Topper58917f32016-12-11 01:59:36 +00001017 auto *VecTy = cast<VectorType>(II.getType());
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001018 auto *MaskEltTy = Type::getInt32Ty(II.getContext());
Craig Topper58917f32016-12-11 01:59:36 +00001019 unsigned NumElts = VecTy->getVectorNumElements();
1020 bool IsPD = VecTy->getScalarType()->isDoubleTy();
1021 unsigned NumLaneElts = IsPD ? 2 : 4;
1022 assert(NumElts == 16 || NumElts == 8 || NumElts == 4 || NumElts == 2);
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00001023
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001024 // Construct a shuffle mask from constant integers or UNDEFs.
Craig Topper58917f32016-12-11 01:59:36 +00001025 Constant *Indexes[16] = {nullptr};
Simon Pilgrim640f9962016-04-30 07:23:30 +00001026
1027 // The intrinsics only read one or two bits, clear the rest.
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001028 for (unsigned I = 0; I < NumElts; ++I) {
Simon Pilgrim640f9962016-04-30 07:23:30 +00001029 Constant *COp = V->getAggregateElement(I);
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001030 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
Simon Pilgrim640f9962016-04-30 07:23:30 +00001031 return nullptr;
1032
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001033 if (isa<UndefValue>(COp)) {
1034 Indexes[I] = UndefValue::get(MaskEltTy);
1035 continue;
1036 }
1037
1038 APInt Index = cast<ConstantInt>(COp)->getValue();
1039 Index = Index.zextOrTrunc(32).getLoBits(2);
Simon Pilgrim640f9962016-04-30 07:23:30 +00001040
1041 // The PD variants uses bit 1 to select per-lane element index, so
1042 // shift down to convert to generic shuffle mask index.
Craig Topper58917f32016-12-11 01:59:36 +00001043 if (IsPD)
Craig Topperfc947bc2017-04-18 17:14:21 +00001044 Index.lshrInPlace(1);
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001045
1046 // The _256 variants are a bit trickier since the mask bits always index
1047 // into the corresponding 128 half. In order to convert to a generic
1048 // shuffle, we have to make that explicit.
Craig Topper58917f32016-12-11 01:59:36 +00001049 Index += APInt(32, (I / NumLaneElts) * NumLaneElts);
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001050
1051 Indexes[I] = ConstantInt::get(MaskEltTy, Index);
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00001052 }
1053
Simon Pilgrimeeacc402016-05-01 20:22:42 +00001054 auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00001055 auto V1 = II.getArgOperand(0);
1056 auto V2 = UndefValue::get(V1->getType());
1057 return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1058}
1059
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001060/// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant.
1061static Value *simplifyX86vpermv(const IntrinsicInst &II,
1062 InstCombiner::BuilderTy &Builder) {
1063 auto *V = dyn_cast<Constant>(II.getArgOperand(1));
1064 if (!V)
1065 return nullptr;
1066
Simon Pilgrimca140b12016-05-01 20:43:02 +00001067 auto *VecTy = cast<VectorType>(II.getType());
1068 auto *MaskEltTy = Type::getInt32Ty(II.getContext());
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001069 unsigned Size = VecTy->getNumElements();
Craig Toppere3280452016-12-25 23:58:57 +00001070 assert((Size == 4 || Size == 8 || Size == 16 || Size == 32 || Size == 64) &&
1071 "Unexpected shuffle mask size");
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001072
Simon Pilgrimca140b12016-05-01 20:43:02 +00001073 // Construct a shuffle mask from constant integers or UNDEFs.
Craig Toppere3280452016-12-25 23:58:57 +00001074 Constant *Indexes[64] = {nullptr};
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001075
1076 for (unsigned I = 0; I < Size; ++I) {
1077 Constant *COp = V->getAggregateElement(I);
Simon Pilgrimca140b12016-05-01 20:43:02 +00001078 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001079 return nullptr;
1080
Simon Pilgrimca140b12016-05-01 20:43:02 +00001081 if (isa<UndefValue>(COp)) {
1082 Indexes[I] = UndefValue::get(MaskEltTy);
1083 continue;
1084 }
1085
Craig Toppere3280452016-12-25 23:58:57 +00001086 uint32_t Index = cast<ConstantInt>(COp)->getZExtValue();
1087 Index &= Size - 1;
Simon Pilgrimca140b12016-05-01 20:43:02 +00001088 Indexes[I] = ConstantInt::get(MaskEltTy, Index);
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001089 }
1090
Simon Pilgrimca140b12016-05-01 20:43:02 +00001091 auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, Size));
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00001092 auto V1 = II.getArgOperand(0);
1093 auto V2 = UndefValue::get(VecTy);
1094 return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1095}
1096
Sanjay Patelccf5f242015-03-20 21:47:56 +00001097/// The shuffle mask for a perm2*128 selects any two halves of two 256-bit
1098/// source vectors, unless a zero bit is set. If a zero bit is set,
1099/// then ignore that half of the mask and clear that half of the vector.
Sanjay Patel6038d3e2016-01-29 23:27:03 +00001100static Value *simplifyX86vperm2(const IntrinsicInst &II,
Sanjay Patelccf5f242015-03-20 21:47:56 +00001101 InstCombiner::BuilderTy &Builder) {
Sanjay Patel03c03f52016-01-28 00:03:16 +00001102 auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2));
1103 if (!CInt)
1104 return nullptr;
Sanjay Patelccf5f242015-03-20 21:47:56 +00001105
Sanjay Patel03c03f52016-01-28 00:03:16 +00001106 VectorType *VecTy = cast<VectorType>(II.getType());
1107 ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy);
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001108
Sanjay Patel03c03f52016-01-28 00:03:16 +00001109 // The immediate permute control byte looks like this:
1110 // [1:0] - select 128 bits from sources for low half of destination
1111 // [2] - ignore
1112 // [3] - zero low half of destination
1113 // [5:4] - select 128 bits from sources for high half of destination
1114 // [6] - ignore
1115 // [7] - zero high half of destination
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001116
Sanjay Patel03c03f52016-01-28 00:03:16 +00001117 uint8_t Imm = CInt->getZExtValue();
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001118
Sanjay Patel03c03f52016-01-28 00:03:16 +00001119 bool LowHalfZero = Imm & 0x08;
1120 bool HighHalfZero = Imm & 0x80;
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001121
Sanjay Patel03c03f52016-01-28 00:03:16 +00001122 // If both zero mask bits are set, this was just a weird way to
1123 // generate a zero vector.
1124 if (LowHalfZero && HighHalfZero)
1125 return ZeroVector;
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001126
Sanjay Patel03c03f52016-01-28 00:03:16 +00001127 // If 0 or 1 zero mask bits are set, this is a simple shuffle.
1128 unsigned NumElts = VecTy->getNumElements();
1129 unsigned HalfSize = NumElts / 2;
Craig Topper99d1eab2016-06-12 00:41:19 +00001130 SmallVector<uint32_t, 8> ShuffleMask(NumElts);
Simon Pilgrim54fcd622015-07-25 20:41:00 +00001131
Sanjay Patel03c03f52016-01-28 00:03:16 +00001132 // The high bit of the selection field chooses the 1st or 2nd operand.
1133 bool LowInputSelect = Imm & 0x02;
1134 bool HighInputSelect = Imm & 0x20;
Sanjay Patelccf5f242015-03-20 21:47:56 +00001135
Sanjay Patel03c03f52016-01-28 00:03:16 +00001136 // The low bit of the selection field chooses the low or high half
1137 // of the selected operand.
1138 bool LowHalfSelect = Imm & 0x01;
1139 bool HighHalfSelect = Imm & 0x10;
Simon Pilgrim54fcd622015-07-25 20:41:00 +00001140
Sanjay Patel03c03f52016-01-28 00:03:16 +00001141 // Determine which operand(s) are actually in use for this instruction.
1142 Value *V0 = LowInputSelect ? II.getArgOperand(1) : II.getArgOperand(0);
1143 Value *V1 = HighInputSelect ? II.getArgOperand(1) : II.getArgOperand(0);
Simon Pilgrim54fcd622015-07-25 20:41:00 +00001144
Sanjay Patel03c03f52016-01-28 00:03:16 +00001145 // If needed, replace operands based on zero mask.
1146 V0 = LowHalfZero ? ZeroVector : V0;
1147 V1 = HighHalfZero ? ZeroVector : V1;
Sanjay Patelccf5f242015-03-20 21:47:56 +00001148
Sanjay Patel03c03f52016-01-28 00:03:16 +00001149 // Permute low half of result.
1150 unsigned StartIndex = LowHalfSelect ? HalfSize : 0;
1151 for (unsigned i = 0; i < HalfSize; ++i)
1152 ShuffleMask[i] = StartIndex + i;
Sanjay Patel43a87fd2015-03-24 20:36:42 +00001153
Sanjay Patel03c03f52016-01-28 00:03:16 +00001154 // Permute high half of result.
1155 StartIndex = HighHalfSelect ? HalfSize : 0;
1156 StartIndex += NumElts;
1157 for (unsigned i = 0; i < HalfSize; ++i)
1158 ShuffleMask[i + HalfSize] = StartIndex + i;
1159
1160 return Builder.CreateShuffleVector(V0, V1, ShuffleMask);
Sanjay Patelccf5f242015-03-20 21:47:56 +00001161}
1162
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00001163/// Decode XOP integer vector comparison intrinsics.
Sanjay Patel6038d3e2016-01-29 23:27:03 +00001164static Value *simplifyX86vpcom(const IntrinsicInst &II,
Sanjay Patelf9f5d3c2016-01-29 23:14:58 +00001165 InstCombiner::BuilderTy &Builder,
1166 bool IsSigned) {
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00001167 if (auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2))) {
1168 uint64_t Imm = CInt->getZExtValue() & 0x7;
1169 VectorType *VecTy = cast<VectorType>(II.getType());
1170 CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1171
1172 switch (Imm) {
1173 case 0x0:
1174 Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1175 break;
1176 case 0x1:
1177 Pred = IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
1178 break;
1179 case 0x2:
1180 Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1181 break;
1182 case 0x3:
1183 Pred = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
1184 break;
1185 case 0x4:
1186 Pred = ICmpInst::ICMP_EQ; break;
1187 case 0x5:
1188 Pred = ICmpInst::ICMP_NE; break;
1189 case 0x6:
1190 return ConstantInt::getSigned(VecTy, 0); // FALSE
1191 case 0x7:
1192 return ConstantInt::getSigned(VecTy, -1); // TRUE
1193 }
1194
Sanjay Patelf9f5d3c2016-01-29 23:14:58 +00001195 if (Value *Cmp = Builder.CreateICmp(Pred, II.getArgOperand(0),
1196 II.getArgOperand(1)))
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00001197 return Builder.CreateSExtOrTrunc(Cmp, VecTy);
1198 }
1199 return nullptr;
1200}
1201
Craig Toppere3280452016-12-25 23:58:57 +00001202// Emit a select instruction and appropriate bitcasts to help simplify
1203// masked intrinsics.
1204static Value *emitX86MaskSelect(Value *Mask, Value *Op0, Value *Op1,
1205 InstCombiner::BuilderTy &Builder) {
Craig Topper99163632016-12-30 23:06:28 +00001206 unsigned VWidth = Op0->getType()->getVectorNumElements();
1207
1208 // If the mask is all ones we don't need the select. But we need to check
1209 // only the bit thats will be used in case VWidth is less than 8.
1210 if (auto *C = dyn_cast<ConstantInt>(Mask))
1211 if (C->getValue().zextOrTrunc(VWidth).isAllOnesValue())
1212 return Op0;
1213
Craig Toppere3280452016-12-25 23:58:57 +00001214 auto *MaskTy = VectorType::get(Builder.getInt1Ty(),
1215 cast<IntegerType>(Mask->getType())->getBitWidth());
1216 Mask = Builder.CreateBitCast(Mask, MaskTy);
1217
1218 // If we have less than 8 elements, then the starting mask was an i8 and
1219 // we need to extract down to the right number of elements.
Craig Toppere3280452016-12-25 23:58:57 +00001220 if (VWidth < 8) {
1221 uint32_t Indices[4];
1222 for (unsigned i = 0; i != VWidth; ++i)
1223 Indices[i] = i;
1224 Mask = Builder.CreateShuffleVector(Mask, Mask,
1225 makeArrayRef(Indices, VWidth),
1226 "extract");
1227 }
1228
1229 return Builder.CreateSelect(Mask, Op0, Op1);
1230}
1231
Sanjay Patel0069f562016-01-31 16:35:23 +00001232static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) {
1233 Value *Arg0 = II.getArgOperand(0);
1234 Value *Arg1 = II.getArgOperand(1);
1235
1236 // fmin(x, x) -> x
1237 if (Arg0 == Arg1)
1238 return Arg0;
1239
1240 const auto *C1 = dyn_cast<ConstantFP>(Arg1);
1241
1242 // fmin(x, nan) -> x
1243 if (C1 && C1->isNaN())
1244 return Arg0;
1245
1246 // This is the value because if undef were NaN, we would return the other
1247 // value and cannot return a NaN unless both operands are.
1248 //
1249 // fmin(undef, x) -> x
1250 if (isa<UndefValue>(Arg0))
1251 return Arg1;
1252
1253 // fmin(x, undef) -> x
1254 if (isa<UndefValue>(Arg1))
1255 return Arg0;
1256
1257 Value *X = nullptr;
1258 Value *Y = nullptr;
1259 if (II.getIntrinsicID() == Intrinsic::minnum) {
1260 // fmin(x, fmin(x, y)) -> fmin(x, y)
1261 // fmin(y, fmin(x, y)) -> fmin(x, y)
1262 if (match(Arg1, m_FMin(m_Value(X), m_Value(Y)))) {
1263 if (Arg0 == X || Arg0 == Y)
1264 return Arg1;
1265 }
1266
1267 // fmin(fmin(x, y), x) -> fmin(x, y)
1268 // fmin(fmin(x, y), y) -> fmin(x, y)
1269 if (match(Arg0, m_FMin(m_Value(X), m_Value(Y)))) {
1270 if (Arg1 == X || Arg1 == Y)
1271 return Arg0;
1272 }
1273
1274 // TODO: fmin(nnan x, inf) -> x
1275 // TODO: fmin(nnan ninf x, flt_max) -> x
1276 if (C1 && C1->isInfinity()) {
1277 // fmin(x, -inf) -> -inf
1278 if (C1->isNegative())
1279 return Arg1;
1280 }
1281 } else {
1282 assert(II.getIntrinsicID() == Intrinsic::maxnum);
1283 // fmax(x, fmax(x, y)) -> fmax(x, y)
1284 // fmax(y, fmax(x, y)) -> fmax(x, y)
1285 if (match(Arg1, m_FMax(m_Value(X), m_Value(Y)))) {
1286 if (Arg0 == X || Arg0 == Y)
1287 return Arg1;
1288 }
1289
1290 // fmax(fmax(x, y), x) -> fmax(x, y)
1291 // fmax(fmax(x, y), y) -> fmax(x, y)
1292 if (match(Arg0, m_FMax(m_Value(X), m_Value(Y)))) {
1293 if (Arg1 == X || Arg1 == Y)
1294 return Arg0;
1295 }
1296
1297 // TODO: fmax(nnan x, -inf) -> x
1298 // TODO: fmax(nnan ninf x, -flt_max) -> x
1299 if (C1 && C1->isInfinity()) {
1300 // fmax(x, inf) -> inf
1301 if (!C1->isNegative())
1302 return Arg1;
1303 }
1304 }
1305 return nullptr;
1306}
1307
David Majnemer666aa942016-07-14 06:58:42 +00001308static bool maskIsAllOneOrUndef(Value *Mask) {
1309 auto *ConstMask = dyn_cast<Constant>(Mask);
1310 if (!ConstMask)
1311 return false;
1312 if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
1313 return true;
1314 for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
1315 ++I) {
1316 if (auto *MaskElt = ConstMask->getAggregateElement(I))
1317 if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
1318 continue;
1319 return false;
1320 }
1321 return true;
1322}
1323
Sanjay Patelb695c552016-02-01 17:00:10 +00001324static Value *simplifyMaskedLoad(const IntrinsicInst &II,
1325 InstCombiner::BuilderTy &Builder) {
David Majnemer666aa942016-07-14 06:58:42 +00001326 // If the mask is all ones or undefs, this is a plain vector load of the 1st
1327 // argument.
1328 if (maskIsAllOneOrUndef(II.getArgOperand(2))) {
Sanjay Patelb695c552016-02-01 17:00:10 +00001329 Value *LoadPtr = II.getArgOperand(0);
1330 unsigned Alignment = cast<ConstantInt>(II.getArgOperand(1))->getZExtValue();
1331 return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload");
1332 }
1333
1334 return nullptr;
1335}
1336
Sanjay Patel04f792b2016-02-01 19:39:52 +00001337static Instruction *simplifyMaskedStore(IntrinsicInst &II, InstCombiner &IC) {
1338 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
1339 if (!ConstMask)
1340 return nullptr;
1341
1342 // If the mask is all zeros, this instruction does nothing.
1343 if (ConstMask->isNullValue())
Sanjay Patel4b198802016-02-01 22:23:39 +00001344 return IC.eraseInstFromFunction(II);
Sanjay Patel04f792b2016-02-01 19:39:52 +00001345
1346 // If the mask is all ones, this is a plain vector store of the 1st argument.
1347 if (ConstMask->isAllOnesValue()) {
1348 Value *StorePtr = II.getArgOperand(1);
1349 unsigned Alignment = cast<ConstantInt>(II.getArgOperand(2))->getZExtValue();
1350 return new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
1351 }
1352
1353 return nullptr;
1354}
1355
Sanjay Patel103ab7d2016-02-01 22:10:26 +00001356static Instruction *simplifyMaskedGather(IntrinsicInst &II, InstCombiner &IC) {
1357 // If the mask is all zeros, return the "passthru" argument of the gather.
1358 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
1359 if (ConstMask && ConstMask->isNullValue())
Sanjay Patel4b198802016-02-01 22:23:39 +00001360 return IC.replaceInstUsesWith(II, II.getArgOperand(3));
Sanjay Patel103ab7d2016-02-01 22:10:26 +00001361
1362 return nullptr;
1363}
1364
1365static Instruction *simplifyMaskedScatter(IntrinsicInst &II, InstCombiner &IC) {
1366 // If the mask is all zeros, a scatter does nothing.
1367 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
1368 if (ConstMask && ConstMask->isNullValue())
Sanjay Patel4b198802016-02-01 22:23:39 +00001369 return IC.eraseInstFromFunction(II);
Sanjay Patel103ab7d2016-02-01 22:10:26 +00001370
1371 return nullptr;
1372}
1373
Amaury Sechet763c59d2016-08-18 20:43:50 +00001374static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombiner &IC) {
1375 assert((II.getIntrinsicID() == Intrinsic::cttz ||
1376 II.getIntrinsicID() == Intrinsic::ctlz) &&
1377 "Expected cttz or ctlz intrinsic");
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001378 Value *Op0 = II.getArgOperand(0);
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001379
Craig Topper8205a1a2017-05-24 16:53:07 +00001380 KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001381
1382 // Create a mask for bits above (ctlz) or below (cttz) the first known one.
1383 bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
Craig Topper8df66c62017-05-12 17:20:30 +00001384 unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
1385 : Known.countMaxLeadingZeros();
1386 unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
1387 : Known.countMinLeadingZeros();
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001388
1389 // If all bits above (ctlz) or below (cttz) the first known one are known
1390 // zero, this value is constant.
1391 // FIXME: This should be in InstSimplify because we're replacing an
1392 // instruction with a constant.
Craig Topper9474e9b2017-04-27 04:51:25 +00001393 if (PossibleZeros == DefiniteZeros) {
Craig Topper0799ff92017-06-03 18:50:32 +00001394 auto *C = ConstantInt::get(Op0->getType(), DefiniteZeros);
Amaury Sechet763c59d2016-08-18 20:43:50 +00001395 return IC.replaceInstUsesWith(II, C);
1396 }
1397
1398 // If the input to cttz/ctlz is known to be non-zero,
1399 // then change the 'ZeroIsUndef' parameter to 'true'
1400 // because we know the zero behavior can't affect the result.
Craig Topper73ba1c82017-06-07 07:40:37 +00001401 if (!Known.One.isNullValue() ||
Craig Topperd45185f2017-05-26 18:23:57 +00001402 isKnownNonZero(Op0, IC.getDataLayout(), 0, &IC.getAssumptionCache(), &II,
1403 &IC.getDominatorTree())) {
Amaury Sechet763c59d2016-08-18 20:43:50 +00001404 if (!match(II.getArgOperand(1), m_One())) {
Craig Topperbb4069e2017-07-07 23:16:26 +00001405 II.setOperand(1, IC.Builder.getTrue());
Amaury Sechet763c59d2016-08-18 20:43:50 +00001406 return &II;
1407 }
1408 }
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001409
Craig Topper5b173f22017-06-21 16:32:35 +00001410 // Add range metadata since known bits can't completely reflect what we know.
1411 // TODO: Handle splat vectors.
1412 auto *IT = dyn_cast<IntegerType>(Op0->getType());
1413 if (IT && IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
1414 Metadata *LowAndHigh[] = {
1415 ConstantAsMetadata::get(ConstantInt::get(IT, DefiniteZeros)),
1416 ConstantAsMetadata::get(ConstantInt::get(IT, PossibleZeros + 1))};
1417 II.setMetadata(LLVMContext::MD_range,
1418 MDNode::get(II.getContext(), LowAndHigh));
1419 return &II;
1420 }
1421
1422 return nullptr;
1423}
1424
1425static Instruction *foldCtpop(IntrinsicInst &II, InstCombiner &IC) {
1426 assert(II.getIntrinsicID() == Intrinsic::ctpop &&
1427 "Expected ctpop intrinsic");
1428 Value *Op0 = II.getArgOperand(0);
1429 // FIXME: Try to simplify vectors of integers.
1430 auto *IT = dyn_cast<IntegerType>(Op0->getType());
1431 if (!IT)
1432 return nullptr;
1433
1434 unsigned BitWidth = IT->getBitWidth();
1435 KnownBits Known(BitWidth);
1436 IC.computeKnownBits(Op0, Known, 0, &II);
1437
1438 unsigned MinCount = Known.countMinPopulation();
1439 unsigned MaxCount = Known.countMaxPopulation();
1440
1441 // Add range metadata since known bits can't completely reflect what we know.
1442 if (IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
1443 Metadata *LowAndHigh[] = {
1444 ConstantAsMetadata::get(ConstantInt::get(IT, MinCount)),
1445 ConstantAsMetadata::get(ConstantInt::get(IT, MaxCount + 1))};
1446 II.setMetadata(LLVMContext::MD_range,
1447 MDNode::get(II.getContext(), LowAndHigh));
1448 return &II;
1449 }
1450
Sanjay Patel8e3ab172016-08-05 22:42:46 +00001451 return nullptr;
1452}
1453
Sanjay Patel1ace9932016-02-26 21:04:14 +00001454// TODO: If the x86 backend knew how to convert a bool vector mask back to an
1455// XMM register mask efficiently, we could transform all x86 masked intrinsics
1456// to LLVM masked intrinsics and remove the x86 masked intrinsic defs.
Sanjay Patel98a71502016-02-29 23:16:48 +00001457static Instruction *simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC) {
1458 Value *Ptr = II.getOperand(0);
1459 Value *Mask = II.getOperand(1);
Sanjay Patel5e5056d2016-04-12 23:16:23 +00001460 Constant *ZeroVec = Constant::getNullValue(II.getType());
Sanjay Patel98a71502016-02-29 23:16:48 +00001461
1462 // Special case a zero mask since that's not a ConstantDataVector.
Sanjay Patel5e5056d2016-04-12 23:16:23 +00001463 // This masked load instruction creates a zero vector.
Sanjay Patel98a71502016-02-29 23:16:48 +00001464 if (isa<ConstantAggregateZero>(Mask))
Sanjay Patel5e5056d2016-04-12 23:16:23 +00001465 return IC.replaceInstUsesWith(II, ZeroVec);
Sanjay Patel98a71502016-02-29 23:16:48 +00001466
1467 auto *ConstMask = dyn_cast<ConstantDataVector>(Mask);
1468 if (!ConstMask)
1469 return nullptr;
1470
1471 // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic
1472 // to allow target-independent optimizations.
1473
1474 // First, cast the x86 intrinsic scalar pointer to a vector pointer to match
1475 // the LLVM intrinsic definition for the pointer argument.
1476 unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
1477 PointerType *VecPtrTy = PointerType::get(II.getType(), AddrSpace);
Craig Topperbb4069e2017-07-07 23:16:26 +00001478 Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec");
Sanjay Patel98a71502016-02-29 23:16:48 +00001479
1480 // Second, convert the x86 XMM integer vector mask to a vector of bools based
1481 // on each element's most significant bit (the sign bit).
1482 Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask);
1483
Sanjay Patel5e5056d2016-04-12 23:16:23 +00001484 // The pass-through vector for an x86 masked load is a zero vector.
1485 CallInst *NewMaskedLoad =
Craig Topperbb4069e2017-07-07 23:16:26 +00001486 IC.Builder.CreateMaskedLoad(PtrCast, 1, BoolMask, ZeroVec);
Sanjay Patel98a71502016-02-29 23:16:48 +00001487 return IC.replaceInstUsesWith(II, NewMaskedLoad);
1488}
1489
1490// TODO: If the x86 backend knew how to convert a bool vector mask back to an
1491// XMM register mask efficiently, we could transform all x86 masked intrinsics
1492// to LLVM masked intrinsics and remove the x86 masked intrinsic defs.
Sanjay Patel1ace9932016-02-26 21:04:14 +00001493static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC) {
1494 Value *Ptr = II.getOperand(0);
1495 Value *Mask = II.getOperand(1);
1496 Value *Vec = II.getOperand(2);
1497
1498 // Special case a zero mask since that's not a ConstantDataVector:
1499 // this masked store instruction does nothing.
1500 if (isa<ConstantAggregateZero>(Mask)) {
1501 IC.eraseInstFromFunction(II);
1502 return true;
1503 }
1504
Sanjay Patelc4acbae2016-03-12 15:16:59 +00001505 // The SSE2 version is too weird (eg, unaligned but non-temporal) to do
1506 // anything else at this level.
1507 if (II.getIntrinsicID() == Intrinsic::x86_sse2_maskmov_dqu)
1508 return false;
1509
Sanjay Patel1ace9932016-02-26 21:04:14 +00001510 auto *ConstMask = dyn_cast<ConstantDataVector>(Mask);
1511 if (!ConstMask)
1512 return false;
1513
1514 // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic
1515 // to allow target-independent optimizations.
1516
1517 // First, cast the x86 intrinsic scalar pointer to a vector pointer to match
1518 // the LLVM intrinsic definition for the pointer argument.
1519 unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
1520 PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace);
Craig Topperbb4069e2017-07-07 23:16:26 +00001521 Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec");
Sanjay Patel1ace9932016-02-26 21:04:14 +00001522
1523 // Second, convert the x86 XMM integer vector mask to a vector of bools based
1524 // on each element's most significant bit (the sign bit).
1525 Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask);
1526
Craig Topperbb4069e2017-07-07 23:16:26 +00001527 IC.Builder.CreateMaskedStore(Vec, PtrCast, 1, BoolMask);
Sanjay Patel1ace9932016-02-26 21:04:14 +00001528
1529 // 'Replace uses' doesn't work for stores. Erase the original masked store.
1530 IC.eraseInstFromFunction(II);
1531 return true;
1532}
1533
Matt Arsenaultcdb468c2017-02-27 23:08:49 +00001534// Constant fold llvm.amdgcn.fmed3 intrinsics for standard inputs.
1535//
1536// A single NaN input is folded to minnum, so we rely on that folding for
1537// handling NaNs.
1538static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1,
1539 const APFloat &Src2) {
1540 APFloat Max3 = maxnum(maxnum(Src0, Src1), Src2);
1541
1542 APFloat::cmpResult Cmp0 = Max3.compare(Src0);
1543 assert(Cmp0 != APFloat::cmpUnordered && "nans handled separately");
1544 if (Cmp0 == APFloat::cmpEqual)
1545 return maxnum(Src1, Src2);
1546
1547 APFloat::cmpResult Cmp1 = Max3.compare(Src1);
1548 assert(Cmp1 != APFloat::cmpUnordered && "nans handled separately");
1549 if (Cmp1 == APFloat::cmpEqual)
1550 return maxnum(Src0, Src2);
1551
1552 return maxnum(Src0, Src1);
1553}
1554
Arnaud A. de Grandmaison333ef382016-05-10 09:24:49 +00001555// Returns true iff the 2 intrinsics have the same operands, limiting the
1556// comparison to the first NumOperands.
1557static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E,
1558 unsigned NumOperands) {
1559 assert(I.getNumArgOperands() >= NumOperands && "Not enough operands");
1560 assert(E.getNumArgOperands() >= NumOperands && "Not enough operands");
1561 for (unsigned i = 0; i < NumOperands; i++)
1562 if (I.getArgOperand(i) != E.getArgOperand(i))
1563 return false;
1564 return true;
1565}
1566
1567// Remove trivially empty start/end intrinsic ranges, i.e. a start
1568// immediately followed by an end (ignoring debuginfo or other
1569// start/end intrinsics in between). As this handles only the most trivial
1570// cases, tracking the nesting level is not needed:
1571//
1572// call @llvm.foo.start(i1 0) ; &I
1573// call @llvm.foo.start(i1 0)
1574// call @llvm.foo.end(i1 0) ; This one will not be skipped: it will be removed
1575// call @llvm.foo.end(i1 0)
1576static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID,
1577 unsigned EndID, InstCombiner &IC) {
1578 assert(I.getIntrinsicID() == StartID &&
1579 "Start intrinsic does not have expected ID");
1580 BasicBlock::iterator BI(I), BE(I.getParent()->end());
1581 for (++BI; BI != BE; ++BI) {
1582 if (auto *E = dyn_cast<IntrinsicInst>(BI)) {
1583 if (isa<DbgInfoIntrinsic>(E) || E->getIntrinsicID() == StartID)
1584 continue;
1585 if (E->getIntrinsicID() == EndID &&
1586 haveSameOperands(I, *E, E->getNumArgOperands())) {
1587 IC.eraseInstFromFunction(*E);
1588 IC.eraseInstFromFunction(I);
1589 return true;
1590 }
1591 }
1592 break;
1593 }
1594
1595 return false;
1596}
1597
Justin Lebar698c31b2017-01-27 00:58:58 +00001598// Convert NVVM intrinsics to target-generic LLVM code where possible.
1599static Instruction *SimplifyNVVMIntrinsic(IntrinsicInst *II, InstCombiner &IC) {
1600 // Each NVVM intrinsic we can simplify can be replaced with one of:
1601 //
1602 // * an LLVM intrinsic,
1603 // * an LLVM cast operation,
1604 // * an LLVM binary operation, or
1605 // * ad-hoc LLVM IR for the particular operation.
1606
1607 // Some transformations are only valid when the module's
1608 // flush-denormals-to-zero (ftz) setting is true/false, whereas other
1609 // transformations are valid regardless of the module's ftz setting.
1610 enum FtzRequirementTy {
1611 FTZ_Any, // Any ftz setting is ok.
1612 FTZ_MustBeOn, // Transformation is valid only if ftz is on.
1613 FTZ_MustBeOff, // Transformation is valid only if ftz is off.
1614 };
1615 // Classes of NVVM intrinsics that can't be replaced one-to-one with a
1616 // target-generic intrinsic, cast op, or binary op but that we can nonetheless
1617 // simplify.
1618 enum SpecialCase {
1619 SPC_Reciprocal,
1620 };
1621
1622 // SimplifyAction is a poor-man's variant (plus an additional flag) that
1623 // represents how to replace an NVVM intrinsic with target-generic LLVM IR.
1624 struct SimplifyAction {
1625 // Invariant: At most one of these Optionals has a value.
1626 Optional<Intrinsic::ID> IID;
1627 Optional<Instruction::CastOps> CastOp;
1628 Optional<Instruction::BinaryOps> BinaryOp;
1629 Optional<SpecialCase> Special;
1630
1631 FtzRequirementTy FtzRequirement = FTZ_Any;
1632
1633 SimplifyAction() = default;
1634
1635 SimplifyAction(Intrinsic::ID IID, FtzRequirementTy FtzReq)
1636 : IID(IID), FtzRequirement(FtzReq) {}
1637
1638 // Cast operations don't have anything to do with FTZ, so we skip that
1639 // argument.
1640 SimplifyAction(Instruction::CastOps CastOp) : CastOp(CastOp) {}
1641
1642 SimplifyAction(Instruction::BinaryOps BinaryOp, FtzRequirementTy FtzReq)
1643 : BinaryOp(BinaryOp), FtzRequirement(FtzReq) {}
1644
1645 SimplifyAction(SpecialCase Special, FtzRequirementTy FtzReq)
1646 : Special(Special), FtzRequirement(FtzReq) {}
1647 };
1648
1649 // Try to generate a SimplifyAction describing how to replace our
1650 // IntrinsicInstr with target-generic LLVM IR.
1651 const SimplifyAction Action = [II]() -> SimplifyAction {
1652 switch (II->getIntrinsicID()) {
1653
1654 // NVVM intrinsics that map directly to LLVM intrinsics.
1655 case Intrinsic::nvvm_ceil_d:
1656 return {Intrinsic::ceil, FTZ_Any};
1657 case Intrinsic::nvvm_ceil_f:
1658 return {Intrinsic::ceil, FTZ_MustBeOff};
1659 case Intrinsic::nvvm_ceil_ftz_f:
1660 return {Intrinsic::ceil, FTZ_MustBeOn};
1661 case Intrinsic::nvvm_fabs_d:
1662 return {Intrinsic::fabs, FTZ_Any};
1663 case Intrinsic::nvvm_fabs_f:
1664 return {Intrinsic::fabs, FTZ_MustBeOff};
1665 case Intrinsic::nvvm_fabs_ftz_f:
1666 return {Intrinsic::fabs, FTZ_MustBeOn};
1667 case Intrinsic::nvvm_floor_d:
1668 return {Intrinsic::floor, FTZ_Any};
1669 case Intrinsic::nvvm_floor_f:
1670 return {Intrinsic::floor, FTZ_MustBeOff};
1671 case Intrinsic::nvvm_floor_ftz_f:
1672 return {Intrinsic::floor, FTZ_MustBeOn};
1673 case Intrinsic::nvvm_fma_rn_d:
1674 return {Intrinsic::fma, FTZ_Any};
1675 case Intrinsic::nvvm_fma_rn_f:
1676 return {Intrinsic::fma, FTZ_MustBeOff};
1677 case Intrinsic::nvvm_fma_rn_ftz_f:
1678 return {Intrinsic::fma, FTZ_MustBeOn};
1679 case Intrinsic::nvvm_fmax_d:
1680 return {Intrinsic::maxnum, FTZ_Any};
1681 case Intrinsic::nvvm_fmax_f:
1682 return {Intrinsic::maxnum, FTZ_MustBeOff};
1683 case Intrinsic::nvvm_fmax_ftz_f:
1684 return {Intrinsic::maxnum, FTZ_MustBeOn};
1685 case Intrinsic::nvvm_fmin_d:
1686 return {Intrinsic::minnum, FTZ_Any};
1687 case Intrinsic::nvvm_fmin_f:
1688 return {Intrinsic::minnum, FTZ_MustBeOff};
1689 case Intrinsic::nvvm_fmin_ftz_f:
1690 return {Intrinsic::minnum, FTZ_MustBeOn};
1691 case Intrinsic::nvvm_round_d:
1692 return {Intrinsic::round, FTZ_Any};
1693 case Intrinsic::nvvm_round_f:
1694 return {Intrinsic::round, FTZ_MustBeOff};
1695 case Intrinsic::nvvm_round_ftz_f:
1696 return {Intrinsic::round, FTZ_MustBeOn};
1697 case Intrinsic::nvvm_sqrt_rn_d:
1698 return {Intrinsic::sqrt, FTZ_Any};
1699 case Intrinsic::nvvm_sqrt_f:
1700 // nvvm_sqrt_f is a special case. For most intrinsics, foo_ftz_f is the
1701 // ftz version, and foo_f is the non-ftz version. But nvvm_sqrt_f adopts
1702 // the ftz-ness of the surrounding code. sqrt_rn_f and sqrt_rn_ftz_f are
1703 // the versions with explicit ftz-ness.
1704 return {Intrinsic::sqrt, FTZ_Any};
1705 case Intrinsic::nvvm_sqrt_rn_f:
1706 return {Intrinsic::sqrt, FTZ_MustBeOff};
1707 case Intrinsic::nvvm_sqrt_rn_ftz_f:
1708 return {Intrinsic::sqrt, FTZ_MustBeOn};
1709 case Intrinsic::nvvm_trunc_d:
1710 return {Intrinsic::trunc, FTZ_Any};
1711 case Intrinsic::nvvm_trunc_f:
1712 return {Intrinsic::trunc, FTZ_MustBeOff};
1713 case Intrinsic::nvvm_trunc_ftz_f:
1714 return {Intrinsic::trunc, FTZ_MustBeOn};
1715
1716 // NVVM intrinsics that map to LLVM cast operations.
1717 //
1718 // Note that llvm's target-generic conversion operators correspond to the rz
1719 // (round to zero) versions of the nvvm conversion intrinsics, even though
1720 // most everything else here uses the rn (round to nearest even) nvvm ops.
1721 case Intrinsic::nvvm_d2i_rz:
1722 case Intrinsic::nvvm_f2i_rz:
1723 case Intrinsic::nvvm_d2ll_rz:
1724 case Intrinsic::nvvm_f2ll_rz:
1725 return {Instruction::FPToSI};
1726 case Intrinsic::nvvm_d2ui_rz:
1727 case Intrinsic::nvvm_f2ui_rz:
1728 case Intrinsic::nvvm_d2ull_rz:
1729 case Intrinsic::nvvm_f2ull_rz:
1730 return {Instruction::FPToUI};
1731 case Intrinsic::nvvm_i2d_rz:
1732 case Intrinsic::nvvm_i2f_rz:
1733 case Intrinsic::nvvm_ll2d_rz:
1734 case Intrinsic::nvvm_ll2f_rz:
1735 return {Instruction::SIToFP};
1736 case Intrinsic::nvvm_ui2d_rz:
1737 case Intrinsic::nvvm_ui2f_rz:
1738 case Intrinsic::nvvm_ull2d_rz:
1739 case Intrinsic::nvvm_ull2f_rz:
1740 return {Instruction::UIToFP};
1741
1742 // NVVM intrinsics that map to LLVM binary ops.
1743 case Intrinsic::nvvm_add_rn_d:
1744 return {Instruction::FAdd, FTZ_Any};
1745 case Intrinsic::nvvm_add_rn_f:
1746 return {Instruction::FAdd, FTZ_MustBeOff};
1747 case Intrinsic::nvvm_add_rn_ftz_f:
1748 return {Instruction::FAdd, FTZ_MustBeOn};
1749 case Intrinsic::nvvm_mul_rn_d:
1750 return {Instruction::FMul, FTZ_Any};
1751 case Intrinsic::nvvm_mul_rn_f:
1752 return {Instruction::FMul, FTZ_MustBeOff};
1753 case Intrinsic::nvvm_mul_rn_ftz_f:
1754 return {Instruction::FMul, FTZ_MustBeOn};
1755 case Intrinsic::nvvm_div_rn_d:
1756 return {Instruction::FDiv, FTZ_Any};
1757 case Intrinsic::nvvm_div_rn_f:
1758 return {Instruction::FDiv, FTZ_MustBeOff};
1759 case Intrinsic::nvvm_div_rn_ftz_f:
1760 return {Instruction::FDiv, FTZ_MustBeOn};
1761
1762 // The remainder of cases are NVVM intrinsics that map to LLVM idioms, but
1763 // need special handling.
1764 //
1765 // We seem to be mising intrinsics for rcp.approx.{ftz.}f32, which is just
1766 // as well.
1767 case Intrinsic::nvvm_rcp_rn_d:
1768 return {SPC_Reciprocal, FTZ_Any};
1769 case Intrinsic::nvvm_rcp_rn_f:
1770 return {SPC_Reciprocal, FTZ_MustBeOff};
1771 case Intrinsic::nvvm_rcp_rn_ftz_f:
1772 return {SPC_Reciprocal, FTZ_MustBeOn};
1773
1774 // We do not currently simplify intrinsics that give an approximate answer.
1775 // These include:
1776 //
1777 // - nvvm_cos_approx_{f,ftz_f}
1778 // - nvvm_ex2_approx_{d,f,ftz_f}
1779 // - nvvm_lg2_approx_{d,f,ftz_f}
1780 // - nvvm_sin_approx_{f,ftz_f}
1781 // - nvvm_sqrt_approx_{f,ftz_f}
1782 // - nvvm_rsqrt_approx_{d,f,ftz_f}
1783 // - nvvm_div_approx_{ftz_d,ftz_f,f}
1784 // - nvvm_rcp_approx_ftz_d
1785 //
1786 // Ideally we'd encode them as e.g. "fast call @llvm.cos", where "fast"
1787 // means that fastmath is enabled in the intrinsic. Unfortunately only
1788 // binary operators (currently) have a fastmath bit in SelectionDAG, so this
1789 // information gets lost and we can't select on it.
1790 //
1791 // TODO: div and rcp are lowered to a binary op, so these we could in theory
1792 // lower them to "fast fdiv".
1793
1794 default:
1795 return {};
1796 }
1797 }();
1798
1799 // If Action.FtzRequirementTy is not satisfied by the module's ftz state, we
1800 // can bail out now. (Notice that in the case that IID is not an NVVM
1801 // intrinsic, we don't have to look up any module metadata, as
1802 // FtzRequirementTy will be FTZ_Any.)
1803 if (Action.FtzRequirement != FTZ_Any) {
1804 bool FtzEnabled =
1805 II->getFunction()->getFnAttribute("nvptx-f32ftz").getValueAsString() ==
1806 "true";
1807
1808 if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn))
1809 return nullptr;
1810 }
1811
1812 // Simplify to target-generic intrinsic.
1813 if (Action.IID) {
1814 SmallVector<Value *, 4> Args(II->arg_operands());
1815 // All the target-generic intrinsics currently of interest to us have one
1816 // type argument, equal to that of the nvvm intrinsic's argument.
Justin Lebare3ac0fb2017-01-27 01:49:39 +00001817 Type *Tys[] = {II->getArgOperand(0)->getType()};
Justin Lebar698c31b2017-01-27 00:58:58 +00001818 return CallInst::Create(
1819 Intrinsic::getDeclaration(II->getModule(), *Action.IID, Tys), Args);
1820 }
1821
1822 // Simplify to target-generic binary op.
1823 if (Action.BinaryOp)
1824 return BinaryOperator::Create(*Action.BinaryOp, II->getArgOperand(0),
1825 II->getArgOperand(1), II->getName());
1826
1827 // Simplify to target-generic cast op.
1828 if (Action.CastOp)
1829 return CastInst::Create(*Action.CastOp, II->getArgOperand(0), II->getType(),
1830 II->getName());
1831
1832 // All that's left are the special cases.
1833 if (!Action.Special)
1834 return nullptr;
1835
1836 switch (*Action.Special) {
1837 case SPC_Reciprocal:
1838 // Simplify reciprocal.
1839 return BinaryOperator::Create(
1840 Instruction::FDiv, ConstantFP::get(II->getArgOperand(0)->getType(), 1),
1841 II->getArgOperand(0), II->getName());
1842 }
Justin Lebar25ebe2d2017-01-27 02:04:07 +00001843 llvm_unreachable("All SpecialCase enumerators should be handled in switch.");
Justin Lebar698c31b2017-01-27 00:58:58 +00001844}
1845
Arnaud A. de Grandmaison333ef382016-05-10 09:24:49 +00001846Instruction *InstCombiner::visitVAStartInst(VAStartInst &I) {
1847 removeTriviallyEmptyRange(I, Intrinsic::vastart, Intrinsic::vaend, *this);
1848 return nullptr;
1849}
1850
1851Instruction *InstCombiner::visitVACopyInst(VACopyInst &I) {
1852 removeTriviallyEmptyRange(I, Intrinsic::vacopy, Intrinsic::vaend, *this);
1853 return nullptr;
1854}
1855
Sanjay Patelcd4377c2016-01-20 22:24:38 +00001856/// CallInst simplification. This mostly only handles folding of intrinsic
1857/// instructions. For normal calls, it allows visitCallSite to do the heavy
1858/// lifting.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001859Instruction *InstCombiner::visitCallInst(CallInst &CI) {
David Majnemer15032582015-05-22 03:56:46 +00001860 auto Args = CI.arg_operands();
Andrew Kaylor647025f2017-06-09 23:18:11 +00001861 if (Value *V = SimplifyCall(&CI, CI.getCalledValue(), Args.begin(),
1862 Args.end(), SQ.getWithInstruction(&CI)))
Sanjay Patel4b198802016-02-01 22:23:39 +00001863 return replaceInstUsesWith(CI, V);
David Majnemer15032582015-05-22 03:56:46 +00001864
Justin Bogner99798402016-08-05 01:06:44 +00001865 if (isFreeCall(&CI, &TLI))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001866 return visitFree(CI);
1867
1868 // If the caller function is nounwind, mark the call as nounwind, even if the
1869 // callee isn't.
Sanjay Patel5a470952016-08-11 15:16:06 +00001870 if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001871 CI.setDoesNotThrow();
1872 return &CI;
1873 }
Jim Grosbach7815f562012-02-03 00:07:04 +00001874
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001875 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
1876 if (!II) return visitCallSite(&CI);
Gabor Greif589a0b92010-06-24 12:58:35 +00001877
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001878 // Intrinsics cannot occur in an invoke, so handle them here instead of in
1879 // visitCallSite.
1880 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
1881 bool Changed = false;
1882
1883 // memmove/cpy/set of zero bytes is a noop.
1884 if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
Chris Lattnerc663a672010-10-01 05:51:02 +00001885 if (NumBytes->isNullValue())
Sanjay Patel4b198802016-02-01 22:23:39 +00001886 return eraseInstFromFunction(CI);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001887
1888 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
1889 if (CI->getZExtValue() == 1) {
1890 // Replace the instruction with just byte operations. We would
1891 // transform other cases to loads/stores, but we don't know if
1892 // alignment is sufficient.
1893 }
1894 }
Jim Grosbach7815f562012-02-03 00:07:04 +00001895
Chris Lattnerc663a672010-10-01 05:51:02 +00001896 // No other transformations apply to volatile transfers.
1897 if (MI->isVolatile())
Craig Topperf40110f2014-04-25 05:29:35 +00001898 return nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001899
1900 // If we have a memmove and the source operation is a constant global,
1901 // then the source and dest pointers can't alias, so we can change this
1902 // into a call to memcpy.
1903 if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
1904 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
1905 if (GVSrc->isConstant()) {
Sanjay Patelaf674fb2015-12-14 17:24:23 +00001906 Module *M = CI.getModule();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001907 Intrinsic::ID MemCpyID = Intrinsic::memcpy;
Jay Foadb804a2b2011-07-12 14:06:48 +00001908 Type *Tys[3] = { CI.getArgOperand(0)->getType(),
1909 CI.getArgOperand(1)->getType(),
1910 CI.getArgOperand(2)->getType() };
Benjamin Kramere6e19332011-07-14 17:45:39 +00001911 CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001912 Changed = true;
1913 }
1914 }
1915
1916 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
1917 // memmove(x,x,size) -> noop.
1918 if (MTI->getSource() == MTI->getDest())
Sanjay Patel4b198802016-02-01 22:23:39 +00001919 return eraseInstFromFunction(CI);
Eric Christopher7258dcd2010-04-16 23:37:20 +00001920 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001921
Eric Christopher7258dcd2010-04-16 23:37:20 +00001922 // If we can determine a pointer alignment that is bigger than currently
1923 // set, update the alignment.
Pete Cooper67cf9a72015-11-19 05:56:52 +00001924 if (isa<MemTransferInst>(MI)) {
1925 if (Instruction *I = SimplifyMemTransfer(MI))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001926 return I;
1927 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
1928 if (Instruction *I = SimplifyMemSet(MSI))
1929 return I;
1930 }
Gabor Greif590d95e2010-06-24 13:42:49 +00001931
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001932 if (Changed) return II;
1933 }
Jim Grosbach7815f562012-02-03 00:07:04 +00001934
Daniel Neilson3faabbb2017-06-16 14:43:59 +00001935 if (auto *AMI = dyn_cast<ElementUnorderedAtomicMemCpyInst>(II)) {
1936 if (Constant *C = dyn_cast<Constant>(AMI->getLength()))
Igor Laevsky4b317fa2017-02-08 14:23:47 +00001937 if (C->isNullValue())
1938 return eraseInstFromFunction(*AMI);
Igor Laevsky900ffa32017-02-08 14:32:04 +00001939
Daniel Neilson3faabbb2017-06-16 14:43:59 +00001940 if (Instruction *I = SimplifyElementUnorderedAtomicMemCpy(AMI))
Igor Laevsky900ffa32017-02-08 14:32:04 +00001941 return I;
Igor Laevsky4b317fa2017-02-08 14:23:47 +00001942 }
1943
Justin Lebar698c31b2017-01-27 00:58:58 +00001944 if (Instruction *I = SimplifyNVVMIntrinsic(II, *this))
1945 return I;
1946
Sanjay Patel1c600c62016-01-20 16:41:43 +00001947 auto SimplifyDemandedVectorEltsLow = [this](Value *Op, unsigned Width,
1948 unsigned DemandedWidth) {
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001949 APInt UndefElts(Width, 0);
1950 APInt DemandedElts = APInt::getLowBitsSet(Width, DemandedWidth);
1951 return SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts);
1952 };
1953
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001954 switch (II->getIntrinsicID()) {
1955 default: break;
George Burgess IV3f089142016-12-20 23:46:36 +00001956 case Intrinsic::objectsize:
1957 if (ConstantInt *N =
1958 lowerObjectSizeCall(II, DL, &TLI, /*MustSucceed=*/false))
1959 return replaceInstUsesWith(CI, N);
Craig Topperf40110f2014-04-25 05:29:35 +00001960 return nullptr;
George Burgess IV3f089142016-12-20 23:46:36 +00001961
Michael Ilseman536cc322012-12-13 03:13:36 +00001962 case Intrinsic::bswap: {
1963 Value *IIOperand = II->getArgOperand(0);
Craig Topperf40110f2014-04-25 05:29:35 +00001964 Value *X = nullptr;
Michael Ilseman536cc322012-12-13 03:13:36 +00001965
Craig Topper0f746c22017-07-04 06:50:48 +00001966 // TODO should this be in InstSimplify?
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001967 // bswap(bswap(x)) -> x
Michael Ilseman536cc322012-12-13 03:13:36 +00001968 if (match(IIOperand, m_BSwap(m_Value(X))))
Craig Topper0f746c22017-07-04 06:50:48 +00001969 return replaceInstUsesWith(CI, X);
Jim Grosbach7815f562012-02-03 00:07:04 +00001970
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001971 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
Michael Ilseman536cc322012-12-13 03:13:36 +00001972 if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
1973 unsigned C = X->getType()->getPrimitiveSizeInBits() -
1974 IIOperand->getType()->getPrimitiveSizeInBits();
1975 Value *CV = ConstantInt::get(X->getType(), C);
Craig Topperbb4069e2017-07-07 23:16:26 +00001976 Value *V = Builder.CreateLShr(X, CV);
Michael Ilseman536cc322012-12-13 03:13:36 +00001977 return new TruncInst(V, IIOperand->getType());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001978 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00001979 break;
Michael Ilseman536cc322012-12-13 03:13:36 +00001980 }
1981
James Molloy2d09c002015-11-12 12:39:41 +00001982 case Intrinsic::bitreverse: {
1983 Value *IIOperand = II->getArgOperand(0);
1984 Value *X = nullptr;
1985
Craig Topper0f746c22017-07-04 06:50:48 +00001986 // TODO should this be in InstSimplify?
James Molloy2d09c002015-11-12 12:39:41 +00001987 // bitreverse(bitreverse(x)) -> x
Simon Pilgrim77c3c5f2017-06-30 18:58:29 +00001988 if (match(IIOperand, m_BitReverse(m_Value(X))))
Sanjay Patel4b198802016-02-01 22:23:39 +00001989 return replaceInstUsesWith(CI, X);
James Molloy2d09c002015-11-12 12:39:41 +00001990 break;
1991 }
1992
Sanjay Patelb695c552016-02-01 17:00:10 +00001993 case Intrinsic::masked_load:
Craig Topperbb4069e2017-07-07 23:16:26 +00001994 if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00001995 return replaceInstUsesWith(CI, SimplifiedMaskedOp);
Sanjay Patelb695c552016-02-01 17:00:10 +00001996 break;
Sanjay Patel04f792b2016-02-01 19:39:52 +00001997 case Intrinsic::masked_store:
1998 return simplifyMaskedStore(*II, *this);
Sanjay Patel103ab7d2016-02-01 22:10:26 +00001999 case Intrinsic::masked_gather:
2000 return simplifyMaskedGather(*II, *this);
2001 case Intrinsic::masked_scatter:
2002 return simplifyMaskedScatter(*II, *this);
Sanjay Patelb695c552016-02-01 17:00:10 +00002003
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002004 case Intrinsic::powi:
Gabor Greif589a0b92010-06-24 12:58:35 +00002005 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002006 // powi(x, 0) -> 1.0
2007 if (Power->isZero())
Sanjay Patel4b198802016-02-01 22:23:39 +00002008 return replaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002009 // powi(x, 1) -> x
2010 if (Power->isOne())
Sanjay Patel4b198802016-02-01 22:23:39 +00002011 return replaceInstUsesWith(CI, II->getArgOperand(0));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002012 // powi(x, -1) -> 1/x
Craig Topper79ab6432017-07-06 18:39:47 +00002013 if (Power->isMinusOne())
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002014 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
Gabor Greif589a0b92010-06-24 12:58:35 +00002015 II->getArgOperand(0));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002016 }
2017 break;
Jim Grosbach7815f562012-02-03 00:07:04 +00002018
Sanjay Patel8e3ab172016-08-05 22:42:46 +00002019 case Intrinsic::cttz:
2020 case Intrinsic::ctlz:
Amaury Sechet763c59d2016-08-18 20:43:50 +00002021 if (auto *I = foldCttzCtlz(*II, *this))
2022 return I;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002023 break;
Sanjoy Dasb0984472015-04-08 04:27:22 +00002024
Craig Topper5b173f22017-06-21 16:32:35 +00002025 case Intrinsic::ctpop:
2026 if (auto *I = foldCtpop(*II, *this))
2027 return I;
2028 break;
2029
Nick Lewyckyabe2cc12015-04-13 19:17:37 +00002030 case Intrinsic::uadd_with_overflow:
2031 case Intrinsic::sadd_with_overflow:
2032 case Intrinsic::umul_with_overflow:
2033 case Intrinsic::smul_with_overflow:
Gabor Greif5b1370e2010-06-28 16:50:57 +00002034 if (isa<Constant>(II->getArgOperand(0)) &&
2035 !isa<Constant>(II->getArgOperand(1))) {
Sanjoy Dasb0984472015-04-08 04:27:22 +00002036 // Canonicalize constants into the RHS.
Gabor Greif5b1370e2010-06-28 16:50:57 +00002037 Value *LHS = II->getArgOperand(0);
2038 II->setArgOperand(0, II->getArgOperand(1));
2039 II->setArgOperand(1, LHS);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002040 return II;
2041 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +00002042 LLVM_FALLTHROUGH;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002043
Nick Lewyckyabe2cc12015-04-13 19:17:37 +00002044 case Intrinsic::usub_with_overflow:
2045 case Intrinsic::ssub_with_overflow: {
Sanjoy Dasb0984472015-04-08 04:27:22 +00002046 OverflowCheckFlavor OCF =
2047 IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID());
2048 assert(OCF != OCF_INVALID && "unexpected!");
Jim Grosbach7815f562012-02-03 00:07:04 +00002049
Sanjoy Dasb0984472015-04-08 04:27:22 +00002050 Value *OperationResult = nullptr;
2051 Constant *OverflowResult = nullptr;
2052 if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1),
2053 *II, OperationResult, OverflowResult))
2054 return CreateOverflowTuple(II, OperationResult, OverflowResult);
Benjamin Kramera420df22014-07-04 10:22:21 +00002055
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002056 break;
Erik Eckstein096ff7d2014-12-11 08:02:30 +00002057 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002058
Matt Arsenaultd6511b42014-10-21 23:00:20 +00002059 case Intrinsic::minnum:
2060 case Intrinsic::maxnum: {
2061 Value *Arg0 = II->getArgOperand(0);
2062 Value *Arg1 = II->getArgOperand(1);
Sanjay Patel0069f562016-01-31 16:35:23 +00002063 // Canonicalize constants to the RHS.
2064 if (isa<ConstantFP>(Arg0) && !isa<ConstantFP>(Arg1)) {
Matt Arsenaultd6511b42014-10-21 23:00:20 +00002065 II->setArgOperand(0, Arg1);
2066 II->setArgOperand(1, Arg0);
2067 return II;
2068 }
Sanjay Patel0069f562016-01-31 16:35:23 +00002069 if (Value *V = simplifyMinnumMaxnum(*II))
Sanjay Patel4b198802016-02-01 22:23:39 +00002070 return replaceInstUsesWith(*II, V);
Matt Arsenaultd6511b42014-10-21 23:00:20 +00002071 break;
2072 }
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002073 case Intrinsic::fmuladd: {
Matt Arsenault92057602017-02-16 18:46:24 +00002074 // Canonicalize fast fmuladd to the separate fmul + fadd.
2075 if (II->hasUnsafeAlgebra()) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002076 BuilderTy::FastMathFlagGuard Guard(Builder);
2077 Builder.setFastMathFlags(II->getFastMathFlags());
2078 Value *Mul = Builder.CreateFMul(II->getArgOperand(0),
2079 II->getArgOperand(1));
2080 Value *Add = Builder.CreateFAdd(Mul, II->getArgOperand(2));
Matt Arsenault92057602017-02-16 18:46:24 +00002081 Add->takeName(II);
2082 return replaceInstUsesWith(*II, Add);
2083 }
2084
2085 LLVM_FALLTHROUGH;
2086 }
2087 case Intrinsic::fma: {
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002088 Value *Src0 = II->getArgOperand(0);
2089 Value *Src1 = II->getArgOperand(1);
2090
Matt Arsenaultb264c942017-01-03 04:32:35 +00002091 // Canonicalize constants into the RHS.
2092 if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
2093 II->setArgOperand(0, Src1);
2094 II->setArgOperand(1, Src0);
2095 std::swap(Src0, Src1);
2096 }
2097
2098 Value *LHS = nullptr;
2099 Value *RHS = nullptr;
2100
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002101 // fma fneg(x), fneg(y), z -> fma x, y, z
2102 if (match(Src0, m_FNeg(m_Value(LHS))) &&
2103 match(Src1, m_FNeg(m_Value(RHS)))) {
Matt Arsenault3f509042017-01-10 23:17:52 +00002104 II->setArgOperand(0, LHS);
2105 II->setArgOperand(1, RHS);
2106 return II;
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002107 }
2108
2109 // fma fabs(x), fabs(x), z -> fma x, x, z
2110 if (match(Src0, m_Intrinsic<Intrinsic::fabs>(m_Value(LHS))) &&
2111 match(Src1, m_Intrinsic<Intrinsic::fabs>(m_Value(RHS))) && LHS == RHS) {
Matt Arsenault3f509042017-01-10 23:17:52 +00002112 II->setArgOperand(0, LHS);
2113 II->setArgOperand(1, RHS);
2114 return II;
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002115 }
2116
Matt Arsenaultb264c942017-01-03 04:32:35 +00002117 // fma x, 1, z -> fadd x, z
2118 if (match(Src1, m_FPOne())) {
2119 Instruction *RI = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2));
2120 RI->copyFastMathFlags(II);
2121 return RI;
2122 }
2123
Matt Arsenault1cc294c2017-01-03 04:32:31 +00002124 break;
2125 }
Matt Arsenault56ff4832017-01-03 22:40:34 +00002126 case Intrinsic::fabs: {
2127 Value *Cond;
2128 Constant *LHS, *RHS;
2129 if (match(II->getArgOperand(0),
2130 m_Select(m_Value(Cond), m_Constant(LHS), m_Constant(RHS)))) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002131 CallInst *Call0 = Builder.CreateCall(II->getCalledFunction(), {LHS});
2132 CallInst *Call1 = Builder.CreateCall(II->getCalledFunction(), {RHS});
Matt Arsenault56ff4832017-01-03 22:40:34 +00002133 return SelectInst::Create(Cond, Call0, Call1);
2134 }
2135
Matt Arsenault954a6242017-01-23 23:55:08 +00002136 LLVM_FALLTHROUGH;
2137 }
2138 case Intrinsic::ceil:
2139 case Intrinsic::floor:
2140 case Intrinsic::round:
2141 case Intrinsic::nearbyint:
Joerg Sonnenberger28bed102017-03-31 19:58:07 +00002142 case Intrinsic::rint:
Matt Arsenault954a6242017-01-23 23:55:08 +00002143 case Intrinsic::trunc: {
Matt Arsenault72333442017-01-17 00:10:40 +00002144 Value *ExtSrc;
2145 if (match(II->getArgOperand(0), m_FPExt(m_Value(ExtSrc))) &&
2146 II->getArgOperand(0)->hasOneUse()) {
2147 // fabs (fpext x) -> fpext (fabs x)
Matt Arsenault954a6242017-01-23 23:55:08 +00002148 Value *F = Intrinsic::getDeclaration(II->getModule(), II->getIntrinsicID(),
Matt Arsenault72333442017-01-17 00:10:40 +00002149 { ExtSrc->getType() });
Craig Topperbb4069e2017-07-07 23:16:26 +00002150 CallInst *NewFabs = Builder.CreateCall(F, ExtSrc);
Matt Arsenault72333442017-01-17 00:10:40 +00002151 NewFabs->copyFastMathFlags(II);
2152 NewFabs->takeName(II);
2153 return new FPExtInst(NewFabs, II->getType());
2154 }
2155
Matt Arsenault56ff4832017-01-03 22:40:34 +00002156 break;
2157 }
Matt Arsenault3bdd75d2017-01-04 22:49:03 +00002158 case Intrinsic::cos:
2159 case Intrinsic::amdgcn_cos: {
2160 Value *SrcSrc;
2161 Value *Src = II->getArgOperand(0);
2162 if (match(Src, m_FNeg(m_Value(SrcSrc))) ||
2163 match(Src, m_Intrinsic<Intrinsic::fabs>(m_Value(SrcSrc)))) {
2164 // cos(-x) -> cos(x)
2165 // cos(fabs(x)) -> cos(x)
2166 II->setArgOperand(0, SrcSrc);
2167 return II;
2168 }
2169
2170 break;
2171 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002172 case Intrinsic::ppc_altivec_lvx:
2173 case Intrinsic::ppc_altivec_lvxl:
Bill Wendlingb902f1d2011-04-13 00:36:11 +00002174 // Turn PPC lvx -> load if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002175 if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002176 &DT) >= 16) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002177 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002178 PointerType::getUnqual(II->getType()));
2179 return new LoadInst(Ptr);
2180 }
2181 break;
Bill Schmidt72954782014-11-12 04:19:40 +00002182 case Intrinsic::ppc_vsx_lxvw4x:
2183 case Intrinsic::ppc_vsx_lxvd2x: {
2184 // Turn PPC VSX loads into normal loads.
Craig Topperbb4069e2017-07-07 23:16:26 +00002185 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
2186 PointerType::getUnqual(II->getType()));
Bill Schmidt72954782014-11-12 04:19:40 +00002187 return new LoadInst(Ptr, Twine(""), false, 1);
2188 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002189 case Intrinsic::ppc_altivec_stvx:
2190 case Intrinsic::ppc_altivec_stvxl:
2191 // Turn stvx -> store if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002192 if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002193 &DT) >= 16) {
Jim Grosbach7815f562012-02-03 00:07:04 +00002194 Type *OpPtrTy =
Gabor Greifa6d75e22010-06-24 15:51:11 +00002195 PointerType::getUnqual(II->getArgOperand(0)->getType());
Craig Topperbb4069e2017-07-07 23:16:26 +00002196 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
Gabor Greifa6d75e22010-06-24 15:51:11 +00002197 return new StoreInst(II->getArgOperand(0), Ptr);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002198 }
2199 break;
Bill Schmidt72954782014-11-12 04:19:40 +00002200 case Intrinsic::ppc_vsx_stxvw4x:
2201 case Intrinsic::ppc_vsx_stxvd2x: {
2202 // Turn PPC VSX stores into normal stores.
2203 Type *OpPtrTy = PointerType::getUnqual(II->getArgOperand(0)->getType());
Craig Topperbb4069e2017-07-07 23:16:26 +00002204 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
Bill Schmidt72954782014-11-12 04:19:40 +00002205 return new StoreInst(II->getArgOperand(0), Ptr, false, 1);
2206 }
Hal Finkel221f4672015-02-26 18:56:03 +00002207 case Intrinsic::ppc_qpx_qvlfs:
2208 // Turn PPC QPX qvlfs -> load if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002209 if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002210 &DT) >= 16) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002211 Type *VTy = VectorType::get(Builder.getFloatTy(),
Hal Finkelf0d68d72015-05-11 06:37:03 +00002212 II->getType()->getVectorNumElements());
Craig Topperbb4069e2017-07-07 23:16:26 +00002213 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
Hal Finkelf0d68d72015-05-11 06:37:03 +00002214 PointerType::getUnqual(VTy));
Craig Topperbb4069e2017-07-07 23:16:26 +00002215 Value *Load = Builder.CreateLoad(Ptr);
Hal Finkelf0d68d72015-05-11 06:37:03 +00002216 return new FPExtInst(Load, II->getType());
Hal Finkel221f4672015-02-26 18:56:03 +00002217 }
2218 break;
2219 case Intrinsic::ppc_qpx_qvlfd:
2220 // Turn PPC QPX qvlfd -> load if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002221 if (getOrEnforceKnownAlignment(II->getArgOperand(0), 32, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002222 &DT) >= 32) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002223 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
Hal Finkel221f4672015-02-26 18:56:03 +00002224 PointerType::getUnqual(II->getType()));
2225 return new LoadInst(Ptr);
2226 }
2227 break;
2228 case Intrinsic::ppc_qpx_qvstfs:
2229 // Turn PPC QPX qvstfs -> store if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002230 if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002231 &DT) >= 16) {
Craig Topperbb4069e2017-07-07 23:16:26 +00002232 Type *VTy = VectorType::get(Builder.getFloatTy(),
Hal Finkelf0d68d72015-05-11 06:37:03 +00002233 II->getArgOperand(0)->getType()->getVectorNumElements());
Craig Topperbb4069e2017-07-07 23:16:26 +00002234 Value *TOp = Builder.CreateFPTrunc(II->getArgOperand(0), VTy);
Hal Finkelf0d68d72015-05-11 06:37:03 +00002235 Type *OpPtrTy = PointerType::getUnqual(VTy);
Craig Topperbb4069e2017-07-07 23:16:26 +00002236 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
Hal Finkelf0d68d72015-05-11 06:37:03 +00002237 return new StoreInst(TOp, Ptr);
Hal Finkel221f4672015-02-26 18:56:03 +00002238 }
2239 break;
2240 case Intrinsic::ppc_qpx_qvstfd:
2241 // Turn PPC QPX qvstfd -> store if the pointer is known aligned.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002242 if (getOrEnforceKnownAlignment(II->getArgOperand(1), 32, DL, II, &AC,
Justin Bogner99798402016-08-05 01:06:44 +00002243 &DT) >= 32) {
Hal Finkel221f4672015-02-26 18:56:03 +00002244 Type *OpPtrTy =
2245 PointerType::getUnqual(II->getArgOperand(0)->getType());
Craig Topperbb4069e2017-07-07 23:16:26 +00002246 Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
Hal Finkel221f4672015-02-26 18:56:03 +00002247 return new StoreInst(II->getArgOperand(0), Ptr);
2248 }
2249 break;
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002250
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002251 case Intrinsic::x86_vcvtph2ps_128:
2252 case Intrinsic::x86_vcvtph2ps_256: {
2253 auto Arg = II->getArgOperand(0);
2254 auto ArgType = cast<VectorType>(Arg->getType());
2255 auto RetType = cast<VectorType>(II->getType());
2256 unsigned ArgWidth = ArgType->getNumElements();
2257 unsigned RetWidth = RetType->getNumElements();
2258 assert(RetWidth <= ArgWidth && "Unexpected input/return vector widths");
2259 assert(ArgType->isIntOrIntVectorTy() &&
2260 ArgType->getScalarSizeInBits() == 16 &&
2261 "CVTPH2PS input type should be 16-bit integer vector");
2262 assert(RetType->getScalarType()->isFloatTy() &&
2263 "CVTPH2PS output type should be 32-bit float vector");
2264
2265 // Constant folding: Convert to generic half to single conversion.
Simon Pilgrim48ffca02015-09-12 14:00:17 +00002266 if (isa<ConstantAggregateZero>(Arg))
Sanjay Patel4b198802016-02-01 22:23:39 +00002267 return replaceInstUsesWith(*II, ConstantAggregateZero::get(RetType));
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002268
Simon Pilgrim48ffca02015-09-12 14:00:17 +00002269 if (isa<ConstantDataVector>(Arg)) {
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002270 auto VectorHalfAsShorts = Arg;
2271 if (RetWidth < ArgWidth) {
Craig Topper99d1eab2016-06-12 00:41:19 +00002272 SmallVector<uint32_t, 8> SubVecMask;
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002273 for (unsigned i = 0; i != RetWidth; ++i)
2274 SubVecMask.push_back((int)i);
Craig Topperbb4069e2017-07-07 23:16:26 +00002275 VectorHalfAsShorts = Builder.CreateShuffleVector(
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002276 Arg, UndefValue::get(ArgType), SubVecMask);
2277 }
2278
2279 auto VectorHalfType =
2280 VectorType::get(Type::getHalfTy(II->getContext()), RetWidth);
2281 auto VectorHalfs =
Craig Topperbb4069e2017-07-07 23:16:26 +00002282 Builder.CreateBitCast(VectorHalfAsShorts, VectorHalfType);
2283 auto VectorFloats = Builder.CreateFPExt(VectorHalfs, RetType);
Sanjay Patel4b198802016-02-01 22:23:39 +00002284 return replaceInstUsesWith(*II, VectorFloats);
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002285 }
2286
2287 // We only use the lowest lanes of the argument.
Simon Pilgrim996725e2015-09-19 11:41:53 +00002288 if (Value *V = SimplifyDemandedVectorEltsLow(Arg, ArgWidth, RetWidth)) {
Simon Pilgrim20c607b2015-09-12 13:39:53 +00002289 II->setArgOperand(0, V);
2290 return II;
2291 }
2292 break;
2293 }
2294
Chandler Carruthcf414cf2011-01-10 07:19:37 +00002295 case Intrinsic::x86_sse_cvtss2si:
2296 case Intrinsic::x86_sse_cvtss2si64:
2297 case Intrinsic::x86_sse_cvttss2si:
2298 case Intrinsic::x86_sse_cvttss2si64:
2299 case Intrinsic::x86_sse2_cvtsd2si:
2300 case Intrinsic::x86_sse2_cvtsd2si64:
2301 case Intrinsic::x86_sse2_cvttsd2si:
Craig Topperaeaa52c2016-12-14 07:46:12 +00002302 case Intrinsic::x86_sse2_cvttsd2si64:
2303 case Intrinsic::x86_avx512_vcvtss2si32:
2304 case Intrinsic::x86_avx512_vcvtss2si64:
2305 case Intrinsic::x86_avx512_vcvtss2usi32:
2306 case Intrinsic::x86_avx512_vcvtss2usi64:
2307 case Intrinsic::x86_avx512_vcvtsd2si32:
2308 case Intrinsic::x86_avx512_vcvtsd2si64:
2309 case Intrinsic::x86_avx512_vcvtsd2usi32:
2310 case Intrinsic::x86_avx512_vcvtsd2usi64:
2311 case Intrinsic::x86_avx512_cvttss2si:
2312 case Intrinsic::x86_avx512_cvttss2si64:
2313 case Intrinsic::x86_avx512_cvttss2usi:
2314 case Intrinsic::x86_avx512_cvttss2usi64:
2315 case Intrinsic::x86_avx512_cvttsd2si:
2316 case Intrinsic::x86_avx512_cvttsd2si64:
2317 case Intrinsic::x86_avx512_cvttsd2usi:
2318 case Intrinsic::x86_avx512_cvttsd2usi64: {
Chandler Carruthcf414cf2011-01-10 07:19:37 +00002319 // These intrinsics only demand the 0th element of their input vectors. If
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002320 // we can simplify the input based on that, do so now.
Simon Pilgrim996725e2015-09-19 11:41:53 +00002321 Value *Arg = II->getArgOperand(0);
2322 unsigned VWidth = Arg->getType()->getVectorNumElements();
2323 if (Value *V = SimplifyDemandedVectorEltsLow(Arg, VWidth, 1)) {
Gabor Greif5b1370e2010-06-28 16:50:57 +00002324 II->setArgOperand(0, V);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00002325 return II;
2326 }
Simon Pilgrim18617d12015-08-05 08:18:00 +00002327 break;
2328 }
2329
Simon Pilgrim91e3ac82016-06-07 08:18:35 +00002330 case Intrinsic::x86_mmx_pmovmskb:
2331 case Intrinsic::x86_sse_movmsk_ps:
2332 case Intrinsic::x86_sse2_movmsk_pd:
2333 case Intrinsic::x86_sse2_pmovmskb_128:
2334 case Intrinsic::x86_avx_movmsk_pd_256:
2335 case Intrinsic::x86_avx_movmsk_ps_256:
2336 case Intrinsic::x86_avx2_pmovmskb: {
Craig Topper4853c432017-07-06 23:18:42 +00002337 if (Value *V = simplifyX86movmsk(*II))
Simon Pilgrim91e3ac82016-06-07 08:18:35 +00002338 return replaceInstUsesWith(*II, V);
2339 break;
2340 }
2341
Simon Pilgrim471efd22016-02-20 23:17:35 +00002342 case Intrinsic::x86_sse_comieq_ss:
2343 case Intrinsic::x86_sse_comige_ss:
2344 case Intrinsic::x86_sse_comigt_ss:
2345 case Intrinsic::x86_sse_comile_ss:
2346 case Intrinsic::x86_sse_comilt_ss:
2347 case Intrinsic::x86_sse_comineq_ss:
2348 case Intrinsic::x86_sse_ucomieq_ss:
2349 case Intrinsic::x86_sse_ucomige_ss:
2350 case Intrinsic::x86_sse_ucomigt_ss:
2351 case Intrinsic::x86_sse_ucomile_ss:
2352 case Intrinsic::x86_sse_ucomilt_ss:
2353 case Intrinsic::x86_sse_ucomineq_ss:
2354 case Intrinsic::x86_sse2_comieq_sd:
2355 case Intrinsic::x86_sse2_comige_sd:
2356 case Intrinsic::x86_sse2_comigt_sd:
2357 case Intrinsic::x86_sse2_comile_sd:
2358 case Intrinsic::x86_sse2_comilt_sd:
2359 case Intrinsic::x86_sse2_comineq_sd:
2360 case Intrinsic::x86_sse2_ucomieq_sd:
2361 case Intrinsic::x86_sse2_ucomige_sd:
2362 case Intrinsic::x86_sse2_ucomigt_sd:
2363 case Intrinsic::x86_sse2_ucomile_sd:
2364 case Intrinsic::x86_sse2_ucomilt_sd:
Craig Topperd9639532016-12-11 07:42:04 +00002365 case Intrinsic::x86_sse2_ucomineq_sd:
Craig Topperd00db692016-12-31 00:45:06 +00002366 case Intrinsic::x86_avx512_vcomi_ss:
2367 case Intrinsic::x86_avx512_vcomi_sd:
Craig Topperd9639532016-12-11 07:42:04 +00002368 case Intrinsic::x86_avx512_mask_cmp_ss:
2369 case Intrinsic::x86_avx512_mask_cmp_sd: {
Simon Pilgrim471efd22016-02-20 23:17:35 +00002370 // These intrinsics only demand the 0th element of their input vectors. If
2371 // we can simplify the input based on that, do so now.
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002372 bool MadeChange = false;
Simon Pilgrim471efd22016-02-20 23:17:35 +00002373 Value *Arg0 = II->getArgOperand(0);
2374 Value *Arg1 = II->getArgOperand(1);
2375 unsigned VWidth = Arg0->getType()->getVectorNumElements();
2376 if (Value *V = SimplifyDemandedVectorEltsLow(Arg0, VWidth, 1)) {
2377 II->setArgOperand(0, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002378 MadeChange = true;
Simon Pilgrim471efd22016-02-20 23:17:35 +00002379 }
2380 if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) {
2381 II->setArgOperand(1, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002382 MadeChange = true;
Simon Pilgrim471efd22016-02-20 23:17:35 +00002383 }
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002384 if (MadeChange)
2385 return II;
Simon Pilgrim471efd22016-02-20 23:17:35 +00002386 break;
2387 }
Michael Zuckerman16b20d22017-04-16 13:26:08 +00002388 case Intrinsic::x86_avx512_mask_cmp_pd_128:
2389 case Intrinsic::x86_avx512_mask_cmp_pd_256:
2390 case Intrinsic::x86_avx512_mask_cmp_pd_512:
2391 case Intrinsic::x86_avx512_mask_cmp_ps_128:
2392 case Intrinsic::x86_avx512_mask_cmp_ps_256:
2393 case Intrinsic::x86_avx512_mask_cmp_ps_512: {
2394 // Folding cmp(sub(a,b),0) -> cmp(a,b) and cmp(0,sub(a,b)) -> cmp(b,a)
2395 Value *Arg0 = II->getArgOperand(0);
2396 Value *Arg1 = II->getArgOperand(1);
2397 bool Arg0IsZero = match(Arg0, m_Zero());
2398 if (Arg0IsZero)
2399 std::swap(Arg0, Arg1);
2400 Value *A, *B;
2401 // This fold requires only the NINF(not +/- inf) since inf minus
2402 // inf is nan.
2403 // NSZ(No Signed Zeros) is not needed because zeros of any sign are
2404 // equal for both compares.
2405 // NNAN is not needed because nans compare the same for both compares.
2406 // The compare intrinsic uses the above assumptions and therefore
2407 // doesn't require additional flags.
2408 if ((match(Arg0, m_OneUse(m_FSub(m_Value(A), m_Value(B)))) &&
2409 match(Arg1, m_Zero()) &&
2410 cast<Instruction>(Arg0)->getFastMathFlags().noInfs())) {
2411 if (Arg0IsZero)
2412 std::swap(A, B);
2413 II->setArgOperand(0, A);
2414 II->setArgOperand(1, B);
2415 return II;
2416 }
2417 break;
2418 }
Simon Pilgrim471efd22016-02-20 23:17:35 +00002419
Craig Topper020b2282016-12-27 00:23:16 +00002420 case Intrinsic::x86_avx512_mask_add_ps_512:
2421 case Intrinsic::x86_avx512_mask_div_ps_512:
2422 case Intrinsic::x86_avx512_mask_mul_ps_512:
2423 case Intrinsic::x86_avx512_mask_sub_ps_512:
2424 case Intrinsic::x86_avx512_mask_add_pd_512:
2425 case Intrinsic::x86_avx512_mask_div_pd_512:
2426 case Intrinsic::x86_avx512_mask_mul_pd_512:
2427 case Intrinsic::x86_avx512_mask_sub_pd_512:
2428 // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular
2429 // IR operations.
2430 if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(4))) {
2431 if (R->getValue() == 4) {
2432 Value *Arg0 = II->getArgOperand(0);
2433 Value *Arg1 = II->getArgOperand(1);
2434
2435 Value *V;
2436 switch (II->getIntrinsicID()) {
2437 default: llvm_unreachable("Case stmts out of sync!");
2438 case Intrinsic::x86_avx512_mask_add_ps_512:
2439 case Intrinsic::x86_avx512_mask_add_pd_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002440 V = Builder.CreateFAdd(Arg0, Arg1);
Craig Topper020b2282016-12-27 00:23:16 +00002441 break;
2442 case Intrinsic::x86_avx512_mask_sub_ps_512:
2443 case Intrinsic::x86_avx512_mask_sub_pd_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002444 V = Builder.CreateFSub(Arg0, Arg1);
Craig Topper020b2282016-12-27 00:23:16 +00002445 break;
2446 case Intrinsic::x86_avx512_mask_mul_ps_512:
2447 case Intrinsic::x86_avx512_mask_mul_pd_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002448 V = Builder.CreateFMul(Arg0, Arg1);
Craig Topper020b2282016-12-27 00:23:16 +00002449 break;
2450 case Intrinsic::x86_avx512_mask_div_ps_512:
2451 case Intrinsic::x86_avx512_mask_div_pd_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002452 V = Builder.CreateFDiv(Arg0, Arg1);
Craig Topper020b2282016-12-27 00:23:16 +00002453 break;
2454 }
2455
2456 // Create a select for the masking.
2457 V = emitX86MaskSelect(II->getArgOperand(3), V, II->getArgOperand(2),
Craig Topperbb4069e2017-07-07 23:16:26 +00002458 Builder);
Craig Topper020b2282016-12-27 00:23:16 +00002459 return replaceInstUsesWith(*II, V);
2460 }
2461 }
2462 break;
2463
Craig Topper790d0fa2016-12-11 07:42:01 +00002464 case Intrinsic::x86_avx512_mask_add_ss_round:
2465 case Intrinsic::x86_avx512_mask_div_ss_round:
2466 case Intrinsic::x86_avx512_mask_mul_ss_round:
2467 case Intrinsic::x86_avx512_mask_sub_ss_round:
Craig Topper790d0fa2016-12-11 07:42:01 +00002468 case Intrinsic::x86_avx512_mask_add_sd_round:
2469 case Intrinsic::x86_avx512_mask_div_sd_round:
2470 case Intrinsic::x86_avx512_mask_mul_sd_round:
2471 case Intrinsic::x86_avx512_mask_sub_sd_round:
Craig Topper7b788ada2016-12-26 06:33:19 +00002472 // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular
2473 // IR operations.
2474 if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(4))) {
2475 if (R->getValue() == 4) {
Craig Topper7f8540b2016-12-27 01:56:30 +00002476 // Extract the element as scalars.
2477 Value *Arg0 = II->getArgOperand(0);
2478 Value *Arg1 = II->getArgOperand(1);
Craig Topperbb4069e2017-07-07 23:16:26 +00002479 Value *LHS = Builder.CreateExtractElement(Arg0, (uint64_t)0);
2480 Value *RHS = Builder.CreateExtractElement(Arg1, (uint64_t)0);
Craig Topper7b788ada2016-12-26 06:33:19 +00002481
Craig Topper7f8540b2016-12-27 01:56:30 +00002482 Value *V;
2483 switch (II->getIntrinsicID()) {
2484 default: llvm_unreachable("Case stmts out of sync!");
2485 case Intrinsic::x86_avx512_mask_add_ss_round:
2486 case Intrinsic::x86_avx512_mask_add_sd_round:
Craig Topperbb4069e2017-07-07 23:16:26 +00002487 V = Builder.CreateFAdd(LHS, RHS);
Craig Topper7f8540b2016-12-27 01:56:30 +00002488 break;
2489 case Intrinsic::x86_avx512_mask_sub_ss_round:
2490 case Intrinsic::x86_avx512_mask_sub_sd_round:
Craig Topperbb4069e2017-07-07 23:16:26 +00002491 V = Builder.CreateFSub(LHS, RHS);
Craig Topper7f8540b2016-12-27 01:56:30 +00002492 break;
2493 case Intrinsic::x86_avx512_mask_mul_ss_round:
2494 case Intrinsic::x86_avx512_mask_mul_sd_round:
Craig Topperbb4069e2017-07-07 23:16:26 +00002495 V = Builder.CreateFMul(LHS, RHS);
Craig Topper7f8540b2016-12-27 01:56:30 +00002496 break;
2497 case Intrinsic::x86_avx512_mask_div_ss_round:
2498 case Intrinsic::x86_avx512_mask_div_sd_round:
Craig Topperbb4069e2017-07-07 23:16:26 +00002499 V = Builder.CreateFDiv(LHS, RHS);
Craig Topper7f8540b2016-12-27 01:56:30 +00002500 break;
Craig Topper7b788ada2016-12-26 06:33:19 +00002501 }
Craig Topper7f8540b2016-12-27 01:56:30 +00002502
2503 // Handle the masking aspect of the intrinsic.
Craig Topper7f8540b2016-12-27 01:56:30 +00002504 Value *Mask = II->getArgOperand(3);
Craig Topper99163632016-12-30 23:06:28 +00002505 auto *C = dyn_cast<ConstantInt>(Mask);
2506 // We don't need a select if we know the mask bit is a 1.
2507 if (!C || !C->getValue()[0]) {
2508 // Cast the mask to an i1 vector and then extract the lowest element.
Craig Topperbb4069e2017-07-07 23:16:26 +00002509 auto *MaskTy = VectorType::get(Builder.getInt1Ty(),
Craig Topper7f8540b2016-12-27 01:56:30 +00002510 cast<IntegerType>(Mask->getType())->getBitWidth());
Craig Topperbb4069e2017-07-07 23:16:26 +00002511 Mask = Builder.CreateBitCast(Mask, MaskTy);
2512 Mask = Builder.CreateExtractElement(Mask, (uint64_t)0);
Craig Topper99163632016-12-30 23:06:28 +00002513 // Extract the lowest element from the passthru operand.
Craig Topperbb4069e2017-07-07 23:16:26 +00002514 Value *Passthru = Builder.CreateExtractElement(II->getArgOperand(2),
Craig Topper99163632016-12-30 23:06:28 +00002515 (uint64_t)0);
Craig Topperbb4069e2017-07-07 23:16:26 +00002516 V = Builder.CreateSelect(Mask, V, Passthru);
Craig Topper99163632016-12-30 23:06:28 +00002517 }
Craig Topper7f8540b2016-12-27 01:56:30 +00002518
2519 // Insert the result back into the original argument 0.
Craig Topperbb4069e2017-07-07 23:16:26 +00002520 V = Builder.CreateInsertElement(Arg0, V, (uint64_t)0);
Craig Topper7f8540b2016-12-27 01:56:30 +00002521
2522 return replaceInstUsesWith(*II, V);
Craig Topper7b788ada2016-12-26 06:33:19 +00002523 }
2524 }
2525 LLVM_FALLTHROUGH;
2526
2527 // X86 scalar intrinsics simplified with SimplifyDemandedVectorElts.
2528 case Intrinsic::x86_avx512_mask_max_ss_round:
2529 case Intrinsic::x86_avx512_mask_min_ss_round:
Craig Topper790d0fa2016-12-11 07:42:01 +00002530 case Intrinsic::x86_avx512_mask_max_sd_round:
Craig Topper268b3ab2016-12-14 06:06:58 +00002531 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topperab5f3552016-12-15 03:49:45 +00002532 case Intrinsic::x86_avx512_mask_vfmadd_ss:
2533 case Intrinsic::x86_avx512_mask_vfmadd_sd:
2534 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
2535 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
2536 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
2537 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
2538 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
2539 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
2540 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
2541 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
Craig Topperdfd268d2016-12-14 05:43:05 +00002542 case Intrinsic::x86_fma_vfmadd_ss:
2543 case Intrinsic::x86_fma_vfmsub_ss:
2544 case Intrinsic::x86_fma_vfnmadd_ss:
2545 case Intrinsic::x86_fma_vfnmsub_ss:
2546 case Intrinsic::x86_fma_vfmadd_sd:
2547 case Intrinsic::x86_fma_vfmsub_sd:
2548 case Intrinsic::x86_fma_vfnmadd_sd:
2549 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00002550 case Intrinsic::x86_sse_cmp_ss:
2551 case Intrinsic::x86_sse_min_ss:
2552 case Intrinsic::x86_sse_max_ss:
2553 case Intrinsic::x86_sse2_cmp_sd:
2554 case Intrinsic::x86_sse2_min_sd:
2555 case Intrinsic::x86_sse2_max_sd:
Craig Toppereb6a20e2016-12-14 03:17:30 +00002556 case Intrinsic::x86_sse41_round_ss:
2557 case Intrinsic::x86_sse41_round_sd:
Craig Topperac75bca2016-12-13 07:45:45 +00002558 case Intrinsic::x86_xop_vfrcz_ss:
2559 case Intrinsic::x86_xop_vfrcz_sd: {
2560 unsigned VWidth = II->getType()->getVectorNumElements();
2561 APInt UndefElts(VWidth, 0);
2562 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
2563 if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, UndefElts)) {
2564 if (V != II)
2565 return replaceInstUsesWith(*II, V);
2566 return II;
2567 }
2568 break;
2569 }
2570
Simon Pilgrima3a72b42015-08-10 20:21:15 +00002571 // Constant fold ashr( <A x Bi>, Ci ).
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002572 // Constant fold lshr( <A x Bi>, Ci ).
2573 // Constant fold shl( <A x Bi>, Ci ).
Simon Pilgrima3a72b42015-08-10 20:21:15 +00002574 case Intrinsic::x86_sse2_psrai_d:
2575 case Intrinsic::x86_sse2_psrai_w:
Simon Pilgrima3a72b42015-08-10 20:21:15 +00002576 case Intrinsic::x86_avx2_psrai_d:
2577 case Intrinsic::x86_avx2_psrai_w:
Craig Topper8b831cb2016-11-13 01:51:55 +00002578 case Intrinsic::x86_avx512_psrai_q_128:
2579 case Intrinsic::x86_avx512_psrai_q_256:
2580 case Intrinsic::x86_avx512_psrai_d_512:
2581 case Intrinsic::x86_avx512_psrai_q_512:
2582 case Intrinsic::x86_avx512_psrai_w_512:
Simon Pilgrim18617d12015-08-05 08:18:00 +00002583 case Intrinsic::x86_sse2_psrli_d:
2584 case Intrinsic::x86_sse2_psrli_q:
2585 case Intrinsic::x86_sse2_psrli_w:
Simon Pilgrim18617d12015-08-05 08:18:00 +00002586 case Intrinsic::x86_avx2_psrli_d:
2587 case Intrinsic::x86_avx2_psrli_q:
2588 case Intrinsic::x86_avx2_psrli_w:
Craig Topper8b831cb2016-11-13 01:51:55 +00002589 case Intrinsic::x86_avx512_psrli_d_512:
2590 case Intrinsic::x86_avx512_psrli_q_512:
2591 case Intrinsic::x86_avx512_psrli_w_512:
Michael J. Spencerdee4b2c2014-04-24 00:58:18 +00002592 case Intrinsic::x86_sse2_pslli_d:
2593 case Intrinsic::x86_sse2_pslli_q:
2594 case Intrinsic::x86_sse2_pslli_w:
Simon Pilgrim18617d12015-08-05 08:18:00 +00002595 case Intrinsic::x86_avx2_pslli_d:
2596 case Intrinsic::x86_avx2_pslli_q:
2597 case Intrinsic::x86_avx2_pslli_w:
Craig Topper8b831cb2016-11-13 01:51:55 +00002598 case Intrinsic::x86_avx512_pslli_d_512:
2599 case Intrinsic::x86_avx512_pslli_q_512:
2600 case Intrinsic::x86_avx512_pslli_w_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002601 if (Value *V = simplifyX86immShift(*II, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002602 return replaceInstUsesWith(*II, V);
Simon Pilgrim18617d12015-08-05 08:18:00 +00002603 break;
2604
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002605 case Intrinsic::x86_sse2_psra_d:
2606 case Intrinsic::x86_sse2_psra_w:
2607 case Intrinsic::x86_avx2_psra_d:
2608 case Intrinsic::x86_avx2_psra_w:
Craig Topper8b831cb2016-11-13 01:51:55 +00002609 case Intrinsic::x86_avx512_psra_q_128:
2610 case Intrinsic::x86_avx512_psra_q_256:
2611 case Intrinsic::x86_avx512_psra_d_512:
2612 case Intrinsic::x86_avx512_psra_q_512:
2613 case Intrinsic::x86_avx512_psra_w_512:
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002614 case Intrinsic::x86_sse2_psrl_d:
2615 case Intrinsic::x86_sse2_psrl_q:
2616 case Intrinsic::x86_sse2_psrl_w:
2617 case Intrinsic::x86_avx2_psrl_d:
2618 case Intrinsic::x86_avx2_psrl_q:
2619 case Intrinsic::x86_avx2_psrl_w:
Craig Topper8b831cb2016-11-13 01:51:55 +00002620 case Intrinsic::x86_avx512_psrl_d_512:
2621 case Intrinsic::x86_avx512_psrl_q_512:
2622 case Intrinsic::x86_avx512_psrl_w_512:
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002623 case Intrinsic::x86_sse2_psll_d:
2624 case Intrinsic::x86_sse2_psll_q:
2625 case Intrinsic::x86_sse2_psll_w:
2626 case Intrinsic::x86_avx2_psll_d:
2627 case Intrinsic::x86_avx2_psll_q:
Craig Topper8b831cb2016-11-13 01:51:55 +00002628 case Intrinsic::x86_avx2_psll_w:
2629 case Intrinsic::x86_avx512_psll_d_512:
2630 case Intrinsic::x86_avx512_psll_q_512:
2631 case Intrinsic::x86_avx512_psll_w_512: {
Craig Topperbb4069e2017-07-07 23:16:26 +00002632 if (Value *V = simplifyX86immShift(*II, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002633 return replaceInstUsesWith(*II, V);
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002634
2635 // SSE2/AVX2 uses only the first 64-bits of the 128-bit vector
2636 // operand to compute the shift amount.
Simon Pilgrim996725e2015-09-19 11:41:53 +00002637 Value *Arg1 = II->getArgOperand(1);
2638 assert(Arg1->getType()->getPrimitiveSizeInBits() == 128 &&
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002639 "Unexpected packed shift size");
Simon Pilgrim996725e2015-09-19 11:41:53 +00002640 unsigned VWidth = Arg1->getType()->getVectorNumElements();
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002641
Simon Pilgrim996725e2015-09-19 11:41:53 +00002642 if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, VWidth / 2)) {
Simon Pilgrimbecd5e82015-08-13 07:39:03 +00002643 II->setArgOperand(1, V);
2644 return II;
2645 }
2646 break;
2647 }
2648
Simon Pilgrimdb9893f2016-06-07 10:27:15 +00002649 case Intrinsic::x86_avx2_psllv_d:
2650 case Intrinsic::x86_avx2_psllv_d_256:
2651 case Intrinsic::x86_avx2_psllv_q:
2652 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topperb4173a52016-11-13 07:26:19 +00002653 case Intrinsic::x86_avx512_psllv_d_512:
2654 case Intrinsic::x86_avx512_psllv_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +00002655 case Intrinsic::x86_avx512_psllv_w_128:
2656 case Intrinsic::x86_avx512_psllv_w_256:
2657 case Intrinsic::x86_avx512_psllv_w_512:
Simon Pilgrimdb9893f2016-06-07 10:27:15 +00002658 case Intrinsic::x86_avx2_psrav_d:
2659 case Intrinsic::x86_avx2_psrav_d_256:
Craig Topperb4173a52016-11-13 07:26:19 +00002660 case Intrinsic::x86_avx512_psrav_q_128:
2661 case Intrinsic::x86_avx512_psrav_q_256:
2662 case Intrinsic::x86_avx512_psrav_d_512:
2663 case Intrinsic::x86_avx512_psrav_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +00002664 case Intrinsic::x86_avx512_psrav_w_128:
2665 case Intrinsic::x86_avx512_psrav_w_256:
2666 case Intrinsic::x86_avx512_psrav_w_512:
Simon Pilgrimdb9893f2016-06-07 10:27:15 +00002667 case Intrinsic::x86_avx2_psrlv_d:
2668 case Intrinsic::x86_avx2_psrlv_d_256:
2669 case Intrinsic::x86_avx2_psrlv_q:
2670 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topperb4173a52016-11-13 07:26:19 +00002671 case Intrinsic::x86_avx512_psrlv_d_512:
2672 case Intrinsic::x86_avx512_psrlv_q_512:
Craig Topper1de753f2016-11-18 06:04:33 +00002673 case Intrinsic::x86_avx512_psrlv_w_128:
2674 case Intrinsic::x86_avx512_psrlv_w_256:
2675 case Intrinsic::x86_avx512_psrlv_w_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002676 if (Value *V = simplifyX86varShift(*II, Builder))
Simon Pilgrimdb9893f2016-06-07 10:27:15 +00002677 return replaceInstUsesWith(*II, V);
2678 break;
2679
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00002680 case Intrinsic::x86_sse2_pmulu_dq:
2681 case Intrinsic::x86_sse41_pmuldq:
2682 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00002683 case Intrinsic::x86_avx2_pmulu_dq:
2684 case Intrinsic::x86_avx512_pmul_dq_512:
2685 case Intrinsic::x86_avx512_pmulu_dq_512: {
Craig Topperbb4069e2017-07-07 23:16:26 +00002686 if (Value *V = simplifyX86muldq(*II, Builder))
Simon Pilgrima50a93f2017-01-20 18:20:30 +00002687 return replaceInstUsesWith(*II, V);
2688
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00002689 unsigned VWidth = II->getType()->getVectorNumElements();
2690 APInt UndefElts(VWidth, 0);
2691 APInt DemandedElts = APInt::getAllOnesValue(VWidth);
2692 if (Value *V = SimplifyDemandedVectorElts(II, DemandedElts, UndefElts)) {
2693 if (V != II)
2694 return replaceInstUsesWith(*II, V);
2695 return II;
2696 }
2697 break;
2698 }
2699
Simon Pilgrim6f6b2792017-01-25 14:37:24 +00002700 case Intrinsic::x86_sse2_packssdw_128:
2701 case Intrinsic::x86_sse2_packsswb_128:
2702 case Intrinsic::x86_avx2_packssdw:
2703 case Intrinsic::x86_avx2_packsswb:
Craig Topper3731f4d2017-02-16 07:35:23 +00002704 case Intrinsic::x86_avx512_packssdw_512:
2705 case Intrinsic::x86_avx512_packsswb_512:
Craig Topper4853c432017-07-06 23:18:42 +00002706 if (Value *V = simplifyX86pack(*II, true))
Simon Pilgrim6f6b2792017-01-25 14:37:24 +00002707 return replaceInstUsesWith(*II, V);
2708 break;
2709
2710 case Intrinsic::x86_sse2_packuswb_128:
2711 case Intrinsic::x86_sse41_packusdw:
2712 case Intrinsic::x86_avx2_packusdw:
2713 case Intrinsic::x86_avx2_packuswb:
Craig Topper3731f4d2017-02-16 07:35:23 +00002714 case Intrinsic::x86_avx512_packusdw_512:
2715 case Intrinsic::x86_avx512_packuswb_512:
Craig Topper4853c432017-07-06 23:18:42 +00002716 if (Value *V = simplifyX86pack(*II, false))
Simon Pilgrim6f6b2792017-01-25 14:37:24 +00002717 return replaceInstUsesWith(*II, V);
2718 break;
2719
Craig Topperb6122122017-01-26 05:17:13 +00002720 case Intrinsic::x86_pclmulqdq: {
2721 if (auto *C = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
2722 unsigned Imm = C->getZExtValue();
2723
2724 bool MadeChange = false;
2725 Value *Arg0 = II->getArgOperand(0);
2726 Value *Arg1 = II->getArgOperand(1);
2727 unsigned VWidth = Arg0->getType()->getVectorNumElements();
2728 APInt DemandedElts(VWidth, 0);
2729
2730 APInt UndefElts1(VWidth, 0);
2731 DemandedElts = (Imm & 0x01) ? 2 : 1;
2732 if (Value *V = SimplifyDemandedVectorElts(Arg0, DemandedElts,
2733 UndefElts1)) {
2734 II->setArgOperand(0, V);
2735 MadeChange = true;
2736 }
2737
2738 APInt UndefElts2(VWidth, 0);
2739 DemandedElts = (Imm & 0x10) ? 2 : 1;
2740 if (Value *V = SimplifyDemandedVectorElts(Arg1, DemandedElts,
2741 UndefElts2)) {
2742 II->setArgOperand(1, V);
2743 MadeChange = true;
2744 }
2745
2746 // If both input elements are undef, the result is undef.
2747 if (UndefElts1[(Imm & 0x01) ? 1 : 0] ||
2748 UndefElts2[(Imm & 0x10) ? 1 : 0])
2749 return replaceInstUsesWith(*II,
2750 ConstantAggregateZero::get(II->getType()));
2751
2752 if (MadeChange)
2753 return II;
2754 }
2755 break;
2756 }
2757
Sanjay Patelc86867c2015-04-16 17:52:13 +00002758 case Intrinsic::x86_sse41_insertps:
Craig Topperbb4069e2017-07-07 23:16:26 +00002759 if (Value *V = simplifyX86insertps(*II, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002760 return replaceInstUsesWith(*II, V);
Sanjay Patelc86867c2015-04-16 17:52:13 +00002761 break;
Simon Pilgrim54fcd622015-07-25 20:41:00 +00002762
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002763 case Intrinsic::x86_sse4a_extrq: {
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002764 Value *Op0 = II->getArgOperand(0);
2765 Value *Op1 = II->getArgOperand(1);
2766 unsigned VWidth0 = Op0->getType()->getVectorNumElements();
2767 unsigned VWidth1 = Op1->getType()->getVectorNumElements();
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002768 assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
2769 Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 &&
2770 VWidth1 == 16 && "Unexpected operand sizes");
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002771
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002772 // See if we're dealing with constant values.
2773 Constant *C1 = dyn_cast<Constant>(Op1);
2774 ConstantInt *CILength =
Andrea Di Biagio8df5b9c2016-09-07 12:03:03 +00002775 C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002776 : nullptr;
2777 ConstantInt *CIIndex =
Andrea Di Biagio8df5b9c2016-09-07 12:03:03 +00002778 C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002779 : nullptr;
2780
2781 // Attempt to simplify to a constant, shuffle vector or EXTRQI call.
Craig Topperbb4069e2017-07-07 23:16:26 +00002782 if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002783 return replaceInstUsesWith(*II, V);
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002784
2785 // EXTRQ only uses the lowest 64-bits of the first 128-bit vector
2786 // operands and the lowest 16-bits of the second.
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002787 bool MadeChange = false;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002788 if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) {
2789 II->setArgOperand(0, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002790 MadeChange = true;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002791 }
2792 if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 2)) {
2793 II->setArgOperand(1, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002794 MadeChange = true;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002795 }
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002796 if (MadeChange)
2797 return II;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002798 break;
2799 }
2800
2801 case Intrinsic::x86_sse4a_extrqi: {
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002802 // EXTRQI: Extract Length bits starting from Index. Zero pad the remaining
2803 // bits of the lower 64-bits. The upper 64-bits are undefined.
2804 Value *Op0 = II->getArgOperand(0);
2805 unsigned VWidth = Op0->getType()->getVectorNumElements();
2806 assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 &&
2807 "Unexpected operand size");
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002808
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002809 // See if we're dealing with constant values.
2810 ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(1));
2811 ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(2));
2812
2813 // Attempt to simplify to a constant or shuffle vector.
Craig Topperbb4069e2017-07-07 23:16:26 +00002814 if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002815 return replaceInstUsesWith(*II, V);
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002816
2817 // EXTRQI only uses the lowest 64-bits of the first 128-bit vector
2818 // operand.
2819 if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) {
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002820 II->setArgOperand(0, V);
2821 return II;
2822 }
2823 break;
2824 }
2825
2826 case Intrinsic::x86_sse4a_insertq: {
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002827 Value *Op0 = II->getArgOperand(0);
2828 Value *Op1 = II->getArgOperand(1);
2829 unsigned VWidth = Op0->getType()->getVectorNumElements();
2830 assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
2831 Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 &&
2832 Op1->getType()->getVectorNumElements() == 2 &&
2833 "Unexpected operand size");
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002834
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002835 // See if we're dealing with constant values.
2836 Constant *C1 = dyn_cast<Constant>(Op1);
2837 ConstantInt *CI11 =
Andrea Di Biagiof3fd3162016-09-07 12:47:53 +00002838 C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1))
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002839 : nullptr;
2840
2841 // Attempt to simplify to a constant, shuffle vector or INSERTQI call.
2842 if (CI11) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +00002843 const APInt &V11 = CI11->getValue();
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002844 APInt Len = V11.zextOrTrunc(6);
2845 APInt Idx = V11.lshr(8).zextOrTrunc(6);
Craig Topperbb4069e2017-07-07 23:16:26 +00002846 if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002847 return replaceInstUsesWith(*II, V);
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002848 }
2849
2850 // INSERTQ only uses the lowest 64-bits of the first 128-bit vector
2851 // operand.
2852 if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) {
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002853 II->setArgOperand(0, V);
2854 return II;
2855 }
2856 break;
2857 }
2858
Filipe Cabecinhas1a805952014-04-24 00:38:14 +00002859 case Intrinsic::x86_sse4a_insertqi: {
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002860 // INSERTQI: Extract lowest Length bits from lower half of second source and
2861 // insert over first source starting at Index bit. The upper 64-bits are
2862 // undefined.
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002863 Value *Op0 = II->getArgOperand(0);
2864 Value *Op1 = II->getArgOperand(1);
2865 unsigned VWidth0 = Op0->getType()->getVectorNumElements();
2866 unsigned VWidth1 = Op1->getType()->getVectorNumElements();
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002867 assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
2868 Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 &&
2869 VWidth1 == 2 && "Unexpected operand sizes");
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002870
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002871 // See if we're dealing with constant values.
2872 ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(2));
2873 ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(3));
2874
2875 // Attempt to simplify to a constant or shuffle vector.
2876 if (CILength && CIIndex) {
2877 APInt Len = CILength->getValue().zextOrTrunc(6);
2878 APInt Idx = CIIndex->getValue().zextOrTrunc(6);
Craig Topperbb4069e2017-07-07 23:16:26 +00002879 if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002880 return replaceInstUsesWith(*II, V);
Simon Pilgrim216b1bf2015-10-17 11:40:05 +00002881 }
2882
2883 // INSERTQI only uses the lowest 64-bits of the first two 128-bit vector
2884 // operands.
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002885 bool MadeChange = false;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002886 if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) {
2887 II->setArgOperand(0, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002888 MadeChange = true;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002889 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002890 if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 1)) {
2891 II->setArgOperand(1, V);
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002892 MadeChange = true;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00002893 }
Simon Pilgrim1c9a9f22016-04-24 17:57:27 +00002894 if (MadeChange)
2895 return II;
Filipe Cabecinhas1a805952014-04-24 00:38:14 +00002896 break;
2897 }
2898
Filipe Cabecinhas82ac07c2014-05-27 03:42:20 +00002899 case Intrinsic::x86_sse41_pblendvb:
2900 case Intrinsic::x86_sse41_blendvps:
2901 case Intrinsic::x86_sse41_blendvpd:
2902 case Intrinsic::x86_avx_blendv_ps_256:
2903 case Intrinsic::x86_avx_blendv_pd_256:
2904 case Intrinsic::x86_avx2_pblendvb: {
2905 // Convert blendv* to vector selects if the mask is constant.
2906 // This optimization is convoluted because the intrinsic is defined as
2907 // getting a vector of floats or doubles for the ps and pd versions.
2908 // FIXME: That should be changed.
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002909
2910 Value *Op0 = II->getArgOperand(0);
2911 Value *Op1 = II->getArgOperand(1);
Filipe Cabecinhas82ac07c2014-05-27 03:42:20 +00002912 Value *Mask = II->getArgOperand(2);
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002913
2914 // fold (blend A, A, Mask) -> A
2915 if (Op0 == Op1)
Sanjay Patel4b198802016-02-01 22:23:39 +00002916 return replaceInstUsesWith(CI, Op0);
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002917
2918 // Zero Mask - select 1st argument.
Simon Pilgrim93f59f52015-08-12 08:23:36 +00002919 if (isa<ConstantAggregateZero>(Mask))
Sanjay Patel4b198802016-02-01 22:23:39 +00002920 return replaceInstUsesWith(CI, Op0);
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002921
2922 // Constant Mask - select 1st/2nd argument lane based on top bit of mask.
Sanjay Patel368ac5d2016-02-21 17:29:33 +00002923 if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) {
2924 Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask);
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002925 return SelectInst::Create(NewSelector, Op1, Op0, "blendv");
Filipe Cabecinhas82ac07c2014-05-27 03:42:20 +00002926 }
Simon Pilgrim8c049d52015-08-12 08:08:56 +00002927 break;
Filipe Cabecinhas82ac07c2014-05-27 03:42:20 +00002928 }
2929
Andrea Di Biagio0594e2a2015-09-30 16:44:39 +00002930 case Intrinsic::x86_ssse3_pshuf_b_128:
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00002931 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrima22c3a12017-01-18 13:44:04 +00002932 case Intrinsic::x86_avx512_pshuf_b_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002933 if (Value *V = simplifyX86pshufb(*II, Builder))
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00002934 return replaceInstUsesWith(*II, V);
2935 break;
Andrea Di Biagio0594e2a2015-09-30 16:44:39 +00002936
Rafael Espindolabad3f772014-04-21 22:06:04 +00002937 case Intrinsic::x86_avx_vpermilvar_ps:
2938 case Intrinsic::x86_avx_vpermilvar_ps_256:
Craig Topper58917f32016-12-11 01:59:36 +00002939 case Intrinsic::x86_avx512_vpermilvar_ps_512:
Rafael Espindolabad3f772014-04-21 22:06:04 +00002940 case Intrinsic::x86_avx_vpermilvar_pd:
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00002941 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrima22c3a12017-01-18 13:44:04 +00002942 case Intrinsic::x86_avx512_vpermilvar_pd_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002943 if (Value *V = simplifyX86vpermilvar(*II, Builder))
Simon Pilgrim2f6097d2016-04-24 17:23:46 +00002944 return replaceInstUsesWith(*II, V);
2945 break;
Rafael Espindolabad3f772014-04-21 22:06:04 +00002946
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00002947 case Intrinsic::x86_avx2_permd:
2948 case Intrinsic::x86_avx2_permps:
Craig Topperbb4069e2017-07-07 23:16:26 +00002949 if (Value *V = simplifyX86vpermv(*II, Builder))
Simon Pilgrim8cddf8b2016-05-01 16:41:22 +00002950 return replaceInstUsesWith(*II, V);
2951 break;
2952
Craig Toppere3280452016-12-25 23:58:57 +00002953 case Intrinsic::x86_avx512_mask_permvar_df_256:
2954 case Intrinsic::x86_avx512_mask_permvar_df_512:
2955 case Intrinsic::x86_avx512_mask_permvar_di_256:
2956 case Intrinsic::x86_avx512_mask_permvar_di_512:
2957 case Intrinsic::x86_avx512_mask_permvar_hi_128:
2958 case Intrinsic::x86_avx512_mask_permvar_hi_256:
2959 case Intrinsic::x86_avx512_mask_permvar_hi_512:
2960 case Intrinsic::x86_avx512_mask_permvar_qi_128:
2961 case Intrinsic::x86_avx512_mask_permvar_qi_256:
2962 case Intrinsic::x86_avx512_mask_permvar_qi_512:
2963 case Intrinsic::x86_avx512_mask_permvar_sf_256:
2964 case Intrinsic::x86_avx512_mask_permvar_sf_512:
2965 case Intrinsic::x86_avx512_mask_permvar_si_256:
2966 case Intrinsic::x86_avx512_mask_permvar_si_512:
Craig Topperbb4069e2017-07-07 23:16:26 +00002967 if (Value *V = simplifyX86vpermv(*II, Builder)) {
Craig Toppere3280452016-12-25 23:58:57 +00002968 // We simplified the permuting, now create a select for the masking.
2969 V = emitX86MaskSelect(II->getArgOperand(3), V, II->getArgOperand(2),
Craig Topperbb4069e2017-07-07 23:16:26 +00002970 Builder);
Craig Toppere3280452016-12-25 23:58:57 +00002971 return replaceInstUsesWith(*II, V);
2972 }
2973 break;
2974
Sanjay Patelccf5f242015-03-20 21:47:56 +00002975 case Intrinsic::x86_avx_vperm2f128_pd_256:
2976 case Intrinsic::x86_avx_vperm2f128_ps_256:
2977 case Intrinsic::x86_avx_vperm2f128_si_256:
Sanjay Patele304bea2015-03-24 22:39:29 +00002978 case Intrinsic::x86_avx2_vperm2i128:
Craig Topperbb4069e2017-07-07 23:16:26 +00002979 if (Value *V = simplifyX86vperm2(*II, Builder))
Sanjay Patel4b198802016-02-01 22:23:39 +00002980 return replaceInstUsesWith(*II, V);
Sanjay Patelccf5f242015-03-20 21:47:56 +00002981 break;
2982
Sanjay Patel98a71502016-02-29 23:16:48 +00002983 case Intrinsic::x86_avx_maskload_ps:
Sanjay Patel6f2c01f2016-02-29 23:59:00 +00002984 case Intrinsic::x86_avx_maskload_pd:
2985 case Intrinsic::x86_avx_maskload_ps_256:
2986 case Intrinsic::x86_avx_maskload_pd_256:
2987 case Intrinsic::x86_avx2_maskload_d:
2988 case Intrinsic::x86_avx2_maskload_q:
2989 case Intrinsic::x86_avx2_maskload_d_256:
2990 case Intrinsic::x86_avx2_maskload_q_256:
Sanjay Patel98a71502016-02-29 23:16:48 +00002991 if (Instruction *I = simplifyX86MaskedLoad(*II, *this))
2992 return I;
2993 break;
2994
Sanjay Patelc4acbae2016-03-12 15:16:59 +00002995 case Intrinsic::x86_sse2_maskmov_dqu:
Sanjay Patel1ace9932016-02-26 21:04:14 +00002996 case Intrinsic::x86_avx_maskstore_ps:
2997 case Intrinsic::x86_avx_maskstore_pd:
2998 case Intrinsic::x86_avx_maskstore_ps_256:
2999 case Intrinsic::x86_avx_maskstore_pd_256:
Sanjay Patelfc7e7eb2016-02-26 21:51:44 +00003000 case Intrinsic::x86_avx2_maskstore_d:
3001 case Intrinsic::x86_avx2_maskstore_q:
3002 case Intrinsic::x86_avx2_maskstore_d_256:
3003 case Intrinsic::x86_avx2_maskstore_q_256:
Sanjay Patel1ace9932016-02-26 21:04:14 +00003004 if (simplifyX86MaskedStore(*II, *this))
3005 return nullptr;
3006 break;
3007
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00003008 case Intrinsic::x86_xop_vpcomb:
3009 case Intrinsic::x86_xop_vpcomd:
3010 case Intrinsic::x86_xop_vpcomq:
3011 case Intrinsic::x86_xop_vpcomw:
Craig Topperbb4069e2017-07-07 23:16:26 +00003012 if (Value *V = simplifyX86vpcom(*II, Builder, true))
Sanjay Patel4b198802016-02-01 22:23:39 +00003013 return replaceInstUsesWith(*II, V);
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00003014 break;
3015
3016 case Intrinsic::x86_xop_vpcomub:
3017 case Intrinsic::x86_xop_vpcomud:
3018 case Intrinsic::x86_xop_vpcomuq:
3019 case Intrinsic::x86_xop_vpcomuw:
Craig Topperbb4069e2017-07-07 23:16:26 +00003020 if (Value *V = simplifyX86vpcom(*II, Builder, false))
Sanjay Patel4b198802016-02-01 22:23:39 +00003021 return replaceInstUsesWith(*II, V);
Simon Pilgrim1d1c56e22015-10-11 14:38:34 +00003022 break;
3023
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003024 case Intrinsic::ppc_altivec_vperm:
3025 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
Bill Schmidta1184632014-06-05 19:46:04 +00003026 // Note that ppc_altivec_vperm has a big-endian bias, so when creating
3027 // a vectorshuffle for little endian, we must undo the transformation
3028 // performed on vec_perm in altivec.h. That is, we must complement
3029 // the permutation mask with respect to 31 and reverse the order of
3030 // V1 and V2.
Chris Lattner0256be92012-01-27 03:08:05 +00003031 if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
3032 assert(Mask->getType()->getVectorNumElements() == 16 &&
3033 "Bad type for intrinsic!");
Jim Grosbach7815f562012-02-03 00:07:04 +00003034
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003035 // Check that all of the elements are integer constants or undefs.
3036 bool AllEltsOk = true;
3037 for (unsigned i = 0; i != 16; ++i) {
Chris Lattner0256be92012-01-27 03:08:05 +00003038 Constant *Elt = Mask->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00003039 if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003040 AllEltsOk = false;
3041 break;
3042 }
3043 }
Jim Grosbach7815f562012-02-03 00:07:04 +00003044
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003045 if (AllEltsOk) {
3046 // Cast the input vectors to byte vectors.
Craig Topperbb4069e2017-07-07 23:16:26 +00003047 Value *Op0 = Builder.CreateBitCast(II->getArgOperand(0),
3048 Mask->getType());
3049 Value *Op1 = Builder.CreateBitCast(II->getArgOperand(1),
3050 Mask->getType());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003051 Value *Result = UndefValue::get(Op0->getType());
Jim Grosbach7815f562012-02-03 00:07:04 +00003052
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003053 // Only extract each element once.
3054 Value *ExtractedElts[32];
3055 memset(ExtractedElts, 0, sizeof(ExtractedElts));
Jim Grosbach7815f562012-02-03 00:07:04 +00003056
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003057 for (unsigned i = 0; i != 16; ++i) {
Chris Lattner0256be92012-01-27 03:08:05 +00003058 if (isa<UndefValue>(Mask->getAggregateElement(i)))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003059 continue;
Jim Grosbach7815f562012-02-03 00:07:04 +00003060 unsigned Idx =
Chris Lattner0256be92012-01-27 03:08:05 +00003061 cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003062 Idx &= 31; // Match the hardware behavior.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003063 if (DL.isLittleEndian())
Bill Schmidta1184632014-06-05 19:46:04 +00003064 Idx = 31 - Idx;
Jim Grosbach7815f562012-02-03 00:07:04 +00003065
Craig Topperf40110f2014-04-25 05:29:35 +00003066 if (!ExtractedElts[Idx]) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003067 Value *Op0ToUse = (DL.isLittleEndian()) ? Op1 : Op0;
3068 Value *Op1ToUse = (DL.isLittleEndian()) ? Op0 : Op1;
Jim Grosbach7815f562012-02-03 00:07:04 +00003069 ExtractedElts[Idx] =
Craig Topperbb4069e2017-07-07 23:16:26 +00003070 Builder.CreateExtractElement(Idx < 16 ? Op0ToUse : Op1ToUse,
3071 Builder.getInt32(Idx&15));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003072 }
Jim Grosbach7815f562012-02-03 00:07:04 +00003073
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003074 // Insert this value into the result vector.
Craig Topperbb4069e2017-07-07 23:16:26 +00003075 Result = Builder.CreateInsertElement(Result, ExtractedElts[Idx],
3076 Builder.getInt32(i));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003077 }
3078 return CastInst::Create(Instruction::BitCast, Result, CI.getType());
3079 }
3080 }
3081 break;
3082
Bob Wilsona4e231c2010-10-22 21:41:48 +00003083 case Intrinsic::arm_neon_vld1:
3084 case Intrinsic::arm_neon_vld2:
3085 case Intrinsic::arm_neon_vld3:
3086 case Intrinsic::arm_neon_vld4:
3087 case Intrinsic::arm_neon_vld2lane:
3088 case Intrinsic::arm_neon_vld3lane:
3089 case Intrinsic::arm_neon_vld4lane:
3090 case Intrinsic::arm_neon_vst1:
3091 case Intrinsic::arm_neon_vst2:
3092 case Intrinsic::arm_neon_vst3:
3093 case Intrinsic::arm_neon_vst4:
3094 case Intrinsic::arm_neon_vst2lane:
3095 case Intrinsic::arm_neon_vst3lane:
3096 case Intrinsic::arm_neon_vst4lane: {
Justin Bogner99798402016-08-05 01:06:44 +00003097 unsigned MemAlign =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00003098 getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT);
Bob Wilsona4e231c2010-10-22 21:41:48 +00003099 unsigned AlignArg = II->getNumArgOperands() - 1;
3100 ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
3101 if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
3102 II->setArgOperand(AlignArg,
3103 ConstantInt::get(Type::getInt32Ty(II->getContext()),
3104 MemAlign, false));
3105 return II;
3106 }
3107 break;
3108 }
3109
Lang Hames3a90fab2012-05-01 00:20:38 +00003110 case Intrinsic::arm_neon_vmulls:
Tim Northover00ed9962014-03-29 10:18:08 +00003111 case Intrinsic::arm_neon_vmullu:
Tim Northover3b0846e2014-05-24 12:50:23 +00003112 case Intrinsic::aarch64_neon_smull:
3113 case Intrinsic::aarch64_neon_umull: {
Lang Hames3a90fab2012-05-01 00:20:38 +00003114 Value *Arg0 = II->getArgOperand(0);
3115 Value *Arg1 = II->getArgOperand(1);
3116
3117 // Handle mul by zero first:
3118 if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
Sanjay Patel4b198802016-02-01 22:23:39 +00003119 return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
Lang Hames3a90fab2012-05-01 00:20:38 +00003120 }
3121
3122 // Check for constant LHS & RHS - in this case we just simplify.
Tim Northover00ed9962014-03-29 10:18:08 +00003123 bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu ||
Tim Northover3b0846e2014-05-24 12:50:23 +00003124 II->getIntrinsicID() == Intrinsic::aarch64_neon_umull);
Lang Hames3a90fab2012-05-01 00:20:38 +00003125 VectorType *NewVT = cast<VectorType>(II->getType());
Benjamin Kramer92040952014-02-13 18:23:24 +00003126 if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
3127 if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
3128 CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext);
3129 CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext);
3130
Sanjay Patel4b198802016-02-01 22:23:39 +00003131 return replaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1));
Lang Hames3a90fab2012-05-01 00:20:38 +00003132 }
3133
Alp Tokercb402912014-01-24 17:20:08 +00003134 // Couldn't simplify - canonicalize constant to the RHS.
Lang Hames3a90fab2012-05-01 00:20:38 +00003135 std::swap(Arg0, Arg1);
3136 }
3137
3138 // Handle mul by one:
Benjamin Kramer92040952014-02-13 18:23:24 +00003139 if (Constant *CV1 = dyn_cast<Constant>(Arg1))
Lang Hames3a90fab2012-05-01 00:20:38 +00003140 if (ConstantInt *Splat =
Benjamin Kramer92040952014-02-13 18:23:24 +00003141 dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
3142 if (Splat->isOne())
3143 return CastInst::CreateIntegerCast(Arg0, II->getType(),
3144 /*isSigned=*/!Zext);
Lang Hames3a90fab2012-05-01 00:20:38 +00003145
3146 break;
3147 }
Matt Arsenaultbef34e22016-01-22 21:30:34 +00003148 case Intrinsic::amdgcn_rcp: {
Matt Arsenault4c7795d2017-03-24 19:04:57 +00003149 Value *Src = II->getArgOperand(0);
3150
3151 // TODO: Move to ConstantFolding/InstSimplify?
3152 if (isa<UndefValue>(Src))
3153 return replaceInstUsesWith(CI, Src);
3154
3155 if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003156 const APFloat &ArgVal = C->getValueAPF();
3157 APFloat Val(ArgVal.getSemantics(), 1.0);
3158 APFloat::opStatus Status = Val.divide(ArgVal,
3159 APFloat::rmNearestTiesToEven);
3160 // Only do this if it was exact and therefore not dependent on the
3161 // rounding mode.
3162 if (Status == APFloat::opOK)
Sanjay Patel4b198802016-02-01 22:23:39 +00003163 return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), Val));
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003164 }
3165
3166 break;
3167 }
Matt Arsenault4c7795d2017-03-24 19:04:57 +00003168 case Intrinsic::amdgcn_rsq: {
3169 Value *Src = II->getArgOperand(0);
3170
3171 // TODO: Move to ConstantFolding/InstSimplify?
3172 if (isa<UndefValue>(Src))
3173 return replaceInstUsesWith(CI, Src);
3174 break;
3175 }
Matt Arsenault2fe4fbc2016-03-30 22:28:52 +00003176 case Intrinsic::amdgcn_frexp_mant:
3177 case Intrinsic::amdgcn_frexp_exp: {
Matt Arsenault5cd4f8f2016-03-30 22:28:26 +00003178 Value *Src = II->getArgOperand(0);
3179 if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
3180 int Exp;
3181 APFloat Significand = frexp(C->getValueAPF(), Exp,
3182 APFloat::rmNearestTiesToEven);
3183
Matt Arsenault2fe4fbc2016-03-30 22:28:52 +00003184 if (II->getIntrinsicID() == Intrinsic::amdgcn_frexp_mant) {
3185 return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(),
3186 Significand));
3187 }
3188
3189 // Match instruction special case behavior.
3190 if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf)
3191 Exp = 0;
3192
3193 return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Exp));
3194 }
3195
3196 if (isa<UndefValue>(Src))
3197 return replaceInstUsesWith(CI, UndefValue::get(II->getType()));
Matt Arsenault5cd4f8f2016-03-30 22:28:26 +00003198
3199 break;
3200 }
Matt Arsenault46a03822016-09-03 07:06:58 +00003201 case Intrinsic::amdgcn_class: {
3202 enum {
3203 S_NAN = 1 << 0, // Signaling NaN
3204 Q_NAN = 1 << 1, // Quiet NaN
3205 N_INFINITY = 1 << 2, // Negative infinity
3206 N_NORMAL = 1 << 3, // Negative normal
3207 N_SUBNORMAL = 1 << 4, // Negative subnormal
3208 N_ZERO = 1 << 5, // Negative zero
3209 P_ZERO = 1 << 6, // Positive zero
3210 P_SUBNORMAL = 1 << 7, // Positive subnormal
3211 P_NORMAL = 1 << 8, // Positive normal
3212 P_INFINITY = 1 << 9 // Positive infinity
3213 };
3214
3215 const uint32_t FullMask = S_NAN | Q_NAN | N_INFINITY | N_NORMAL |
3216 N_SUBNORMAL | N_ZERO | P_ZERO | P_SUBNORMAL | P_NORMAL | P_INFINITY;
3217
3218 Value *Src0 = II->getArgOperand(0);
3219 Value *Src1 = II->getArgOperand(1);
3220 const ConstantInt *CMask = dyn_cast<ConstantInt>(Src1);
3221 if (!CMask) {
3222 if (isa<UndefValue>(Src0))
3223 return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3224
3225 if (isa<UndefValue>(Src1))
3226 return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false));
3227 break;
3228 }
3229
3230 uint32_t Mask = CMask->getZExtValue();
3231
3232 // If all tests are made, it doesn't matter what the value is.
3233 if ((Mask & FullMask) == FullMask)
3234 return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), true));
3235
3236 if ((Mask & FullMask) == 0)
3237 return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false));
3238
3239 if (Mask == (S_NAN | Q_NAN)) {
3240 // Equivalent of isnan. Replace with standard fcmp.
Craig Topperbb4069e2017-07-07 23:16:26 +00003241 Value *FCmp = Builder.CreateFCmpUNO(Src0, Src0);
Matt Arsenault46a03822016-09-03 07:06:58 +00003242 FCmp->takeName(II);
3243 return replaceInstUsesWith(*II, FCmp);
3244 }
3245
3246 const ConstantFP *CVal = dyn_cast<ConstantFP>(Src0);
3247 if (!CVal) {
3248 if (isa<UndefValue>(Src0))
3249 return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3250
3251 // Clamp mask to used bits
3252 if ((Mask & FullMask) != Mask) {
Craig Topperbb4069e2017-07-07 23:16:26 +00003253 CallInst *NewCall = Builder.CreateCall(II->getCalledFunction(),
Matt Arsenault46a03822016-09-03 07:06:58 +00003254 { Src0, ConstantInt::get(Src1->getType(), Mask & FullMask) }
3255 );
3256
3257 NewCall->takeName(II);
3258 return replaceInstUsesWith(*II, NewCall);
3259 }
3260
3261 break;
3262 }
3263
3264 const APFloat &Val = CVal->getValueAPF();
3265
3266 bool Result =
3267 ((Mask & S_NAN) && Val.isNaN() && Val.isSignaling()) ||
3268 ((Mask & Q_NAN) && Val.isNaN() && !Val.isSignaling()) ||
3269 ((Mask & N_INFINITY) && Val.isInfinity() && Val.isNegative()) ||
3270 ((Mask & N_NORMAL) && Val.isNormal() && Val.isNegative()) ||
3271 ((Mask & N_SUBNORMAL) && Val.isDenormal() && Val.isNegative()) ||
3272 ((Mask & N_ZERO) && Val.isZero() && Val.isNegative()) ||
3273 ((Mask & P_ZERO) && Val.isZero() && !Val.isNegative()) ||
3274 ((Mask & P_SUBNORMAL) && Val.isDenormal() && !Val.isNegative()) ||
3275 ((Mask & P_NORMAL) && Val.isNormal() && !Val.isNegative()) ||
3276 ((Mask & P_INFINITY) && Val.isInfinity() && !Val.isNegative());
3277
3278 return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), Result));
3279 }
Matt Arsenault1f17c662017-02-22 00:27:34 +00003280 case Intrinsic::amdgcn_cvt_pkrtz: {
3281 Value *Src0 = II->getArgOperand(0);
3282 Value *Src1 = II->getArgOperand(1);
3283 if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
3284 if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
3285 const fltSemantics &HalfSem
3286 = II->getType()->getScalarType()->getFltSemantics();
3287 bool LosesInfo;
3288 APFloat Val0 = C0->getValueAPF();
3289 APFloat Val1 = C1->getValueAPF();
3290 Val0.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
3291 Val1.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
3292
3293 Constant *Folded = ConstantVector::get({
3294 ConstantFP::get(II->getContext(), Val0),
3295 ConstantFP::get(II->getContext(), Val1) });
3296 return replaceInstUsesWith(*II, Folded);
3297 }
3298 }
3299
3300 if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1))
3301 return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3302
3303 break;
3304 }
Matt Arsenaultf5262252017-02-22 23:04:58 +00003305 case Intrinsic::amdgcn_ubfe:
3306 case Intrinsic::amdgcn_sbfe: {
3307 // Decompose simple cases into standard shifts.
3308 Value *Src = II->getArgOperand(0);
3309 if (isa<UndefValue>(Src))
3310 return replaceInstUsesWith(*II, Src);
3311
3312 unsigned Width;
3313 Type *Ty = II->getType();
3314 unsigned IntSize = Ty->getIntegerBitWidth();
3315
3316 ConstantInt *CWidth = dyn_cast<ConstantInt>(II->getArgOperand(2));
3317 if (CWidth) {
3318 Width = CWidth->getZExtValue();
3319 if ((Width & (IntSize - 1)) == 0)
3320 return replaceInstUsesWith(*II, ConstantInt::getNullValue(Ty));
3321
3322 if (Width >= IntSize) {
3323 // Hardware ignores high bits, so remove those.
3324 II->setArgOperand(2, ConstantInt::get(CWidth->getType(),
3325 Width & (IntSize - 1)));
3326 return II;
3327 }
3328 }
3329
3330 unsigned Offset;
3331 ConstantInt *COffset = dyn_cast<ConstantInt>(II->getArgOperand(1));
3332 if (COffset) {
3333 Offset = COffset->getZExtValue();
3334 if (Offset >= IntSize) {
3335 II->setArgOperand(1, ConstantInt::get(COffset->getType(),
3336 Offset & (IntSize - 1)));
3337 return II;
3338 }
3339 }
3340
3341 bool Signed = II->getIntrinsicID() == Intrinsic::amdgcn_sbfe;
3342
3343 // TODO: Also emit sub if only width is constant.
3344 if (!CWidth && COffset && Offset == 0) {
3345 Constant *KSize = ConstantInt::get(COffset->getType(), IntSize);
Craig Topperbb4069e2017-07-07 23:16:26 +00003346 Value *ShiftVal = Builder.CreateSub(KSize, II->getArgOperand(2));
3347 ShiftVal = Builder.CreateZExt(ShiftVal, II->getType());
Matt Arsenaultf5262252017-02-22 23:04:58 +00003348
Craig Topperbb4069e2017-07-07 23:16:26 +00003349 Value *Shl = Builder.CreateShl(Src, ShiftVal);
3350 Value *RightShift = Signed ? Builder.CreateAShr(Shl, ShiftVal)
3351 : Builder.CreateLShr(Shl, ShiftVal);
Matt Arsenaultf5262252017-02-22 23:04:58 +00003352 RightShift->takeName(II);
3353 return replaceInstUsesWith(*II, RightShift);
3354 }
3355
3356 if (!CWidth || !COffset)
3357 break;
3358
3359 // TODO: This allows folding to undef when the hardware has specific
3360 // behavior?
3361 if (Offset + Width < IntSize) {
Craig Topperbb4069e2017-07-07 23:16:26 +00003362 Value *Shl = Builder.CreateShl(Src, IntSize - Offset - Width);
3363 Value *RightShift = Signed ? Builder.CreateAShr(Shl, IntSize - Width)
3364 : Builder.CreateLShr(Shl, IntSize - Width);
Matt Arsenaultf5262252017-02-22 23:04:58 +00003365 RightShift->takeName(II);
3366 return replaceInstUsesWith(*II, RightShift);
3367 }
3368
Craig Topperbb4069e2017-07-07 23:16:26 +00003369 Value *RightShift = Signed ? Builder.CreateAShr(Src, Offset)
3370 : Builder.CreateLShr(Src, Offset);
Matt Arsenaultf5262252017-02-22 23:04:58 +00003371
3372 RightShift->takeName(II);
3373 return replaceInstUsesWith(*II, RightShift);
3374 }
Matt Arsenaultd4bca1e2017-02-23 00:44:03 +00003375 case Intrinsic::amdgcn_exp:
3376 case Intrinsic::amdgcn_exp_compr: {
3377 ConstantInt *En = dyn_cast<ConstantInt>(II->getArgOperand(1));
3378 if (!En) // Illegal.
3379 break;
3380
3381 unsigned EnBits = En->getZExtValue();
3382 if (EnBits == 0xf)
3383 break; // All inputs enabled.
3384
3385 bool IsCompr = II->getIntrinsicID() == Intrinsic::amdgcn_exp_compr;
3386 bool Changed = false;
3387 for (int I = 0; I < (IsCompr ? 2 : 4); ++I) {
3388 if ((!IsCompr && (EnBits & (1 << I)) == 0) ||
3389 (IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) {
3390 Value *Src = II->getArgOperand(I + 2);
3391 if (!isa<UndefValue>(Src)) {
3392 II->setArgOperand(I + 2, UndefValue::get(Src->getType()));
3393 Changed = true;
3394 }
3395 }
3396 }
3397
3398 if (Changed)
3399 return II;
3400
3401 break;
Matt Arsenaultcdb468c2017-02-27 23:08:49 +00003402
3403 }
3404 case Intrinsic::amdgcn_fmed3: {
3405 // Note this does not preserve proper sNaN behavior if IEEE-mode is enabled
3406 // for the shader.
3407
3408 Value *Src0 = II->getArgOperand(0);
3409 Value *Src1 = II->getArgOperand(1);
3410 Value *Src2 = II->getArgOperand(2);
3411
3412 bool Swap = false;
3413 // Canonicalize constants to RHS operands.
3414 //
3415 // fmed3(c0, x, c1) -> fmed3(x, c0, c1)
3416 if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
3417 std::swap(Src0, Src1);
3418 Swap = true;
3419 }
3420
3421 if (isa<Constant>(Src1) && !isa<Constant>(Src2)) {
3422 std::swap(Src1, Src2);
3423 Swap = true;
3424 }
3425
3426 if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
3427 std::swap(Src0, Src1);
3428 Swap = true;
3429 }
3430
3431 if (Swap) {
3432 II->setArgOperand(0, Src0);
3433 II->setArgOperand(1, Src1);
3434 II->setArgOperand(2, Src2);
3435 return II;
3436 }
3437
3438 if (match(Src2, m_NaN()) || isa<UndefValue>(Src2)) {
Craig Topperbb4069e2017-07-07 23:16:26 +00003439 CallInst *NewCall = Builder.CreateMinNum(Src0, Src1);
Matt Arsenaultcdb468c2017-02-27 23:08:49 +00003440 NewCall->copyFastMathFlags(II);
3441 NewCall->takeName(II);
3442 return replaceInstUsesWith(*II, NewCall);
3443 }
3444
3445 if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
3446 if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
3447 if (const ConstantFP *C2 = dyn_cast<ConstantFP>(Src2)) {
3448 APFloat Result = fmed3AMDGCN(C0->getValueAPF(), C1->getValueAPF(),
3449 C2->getValueAPF());
3450 return replaceInstUsesWith(*II,
Craig Topperbb4069e2017-07-07 23:16:26 +00003451 ConstantFP::get(Builder.getContext(), Result));
Matt Arsenaultcdb468c2017-02-27 23:08:49 +00003452 }
3453 }
3454 }
3455
3456 break;
Matt Arsenaultd4bca1e2017-02-23 00:44:03 +00003457 }
Matt Arsenaultd81f5572017-03-13 18:14:02 +00003458 case Intrinsic::amdgcn_icmp:
3459 case Intrinsic::amdgcn_fcmp: {
3460 const ConstantInt *CC = dyn_cast<ConstantInt>(II->getArgOperand(2));
3461 if (!CC)
3462 break;
3463
3464 // Guard against invalid arguments.
3465 int64_t CCVal = CC->getZExtValue();
3466 bool IsInteger = II->getIntrinsicID() == Intrinsic::amdgcn_icmp;
3467 if ((IsInteger && (CCVal < CmpInst::FIRST_ICMP_PREDICATE ||
3468 CCVal > CmpInst::LAST_ICMP_PREDICATE)) ||
3469 (!IsInteger && (CCVal < CmpInst::FIRST_FCMP_PREDICATE ||
3470 CCVal > CmpInst::LAST_FCMP_PREDICATE)))
3471 break;
3472
3473 Value *Src0 = II->getArgOperand(0);
3474 Value *Src1 = II->getArgOperand(1);
3475
3476 if (auto *CSrc0 = dyn_cast<Constant>(Src0)) {
3477 if (auto *CSrc1 = dyn_cast<Constant>(Src1)) {
3478 Constant *CCmp = ConstantExpr::getCompare(CCVal, CSrc0, CSrc1);
Nicolai Haehnle9c661852017-04-24 17:08:43 +00003479 if (CCmp->isNullValue()) {
3480 return replaceInstUsesWith(
3481 *II, ConstantExpr::getSExt(CCmp, II->getType()));
3482 }
3483
3484 // The result of V_ICMP/V_FCMP assembly instructions (which this
3485 // intrinsic exposes) is one bit per thread, masked with the EXEC
3486 // register (which contains the bitmask of live threads). So a
3487 // comparison that always returns true is the same as a read of the
3488 // EXEC register.
3489 Value *NewF = Intrinsic::getDeclaration(
3490 II->getModule(), Intrinsic::read_register, II->getType());
3491 Metadata *MDArgs[] = {MDString::get(II->getContext(), "exec")};
3492 MDNode *MD = MDNode::get(II->getContext(), MDArgs);
3493 Value *Args[] = {MetadataAsValue::get(II->getContext(), MD)};
Craig Topperbb4069e2017-07-07 23:16:26 +00003494 CallInst *NewCall = Builder.CreateCall(NewF, Args);
Nicolai Haehnle9c661852017-04-24 17:08:43 +00003495 NewCall->addAttribute(AttributeList::FunctionIndex,
3496 Attribute::Convergent);
3497 NewCall->takeName(II);
3498 return replaceInstUsesWith(*II, NewCall);
Matt Arsenaultd81f5572017-03-13 18:14:02 +00003499 }
3500
3501 // Canonicalize constants to RHS.
3502 CmpInst::Predicate SwapPred
3503 = CmpInst::getSwappedPredicate(static_cast<CmpInst::Predicate>(CCVal));
3504 II->setArgOperand(0, Src1);
3505 II->setArgOperand(1, Src0);
3506 II->setArgOperand(2, ConstantInt::get(CC->getType(),
3507 static_cast<int>(SwapPred)));
3508 return II;
3509 }
3510
3511 if (CCVal != CmpInst::ICMP_EQ && CCVal != CmpInst::ICMP_NE)
3512 break;
3513
3514 // Canonicalize compare eq with true value to compare != 0
3515 // llvm.amdgcn.icmp(zext (i1 x), 1, eq)
3516 // -> llvm.amdgcn.icmp(zext (i1 x), 0, ne)
3517 // llvm.amdgcn.icmp(sext (i1 x), -1, eq)
3518 // -> llvm.amdgcn.icmp(sext (i1 x), 0, ne)
3519 Value *ExtSrc;
3520 if (CCVal == CmpInst::ICMP_EQ &&
3521 ((match(Src1, m_One()) && match(Src0, m_ZExt(m_Value(ExtSrc)))) ||
3522 (match(Src1, m_AllOnes()) && match(Src0, m_SExt(m_Value(ExtSrc))))) &&
3523 ExtSrc->getType()->isIntegerTy(1)) {
3524 II->setArgOperand(1, ConstantInt::getNullValue(Src1->getType()));
3525 II->setArgOperand(2, ConstantInt::get(CC->getType(), CmpInst::ICMP_NE));
3526 return II;
3527 }
3528
3529 CmpInst::Predicate SrcPred;
3530 Value *SrcLHS;
3531 Value *SrcRHS;
3532
3533 // Fold compare eq/ne with 0 from a compare result as the predicate to the
3534 // intrinsic. The typical use is a wave vote function in the library, which
3535 // will be fed from a user code condition compared with 0. Fold in the
3536 // redundant compare.
3537
3538 // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, ne)
3539 // -> llvm.amdgcn.[if]cmp(a, b, pred)
3540 //
3541 // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, eq)
3542 // -> llvm.amdgcn.[if]cmp(a, b, inv pred)
3543 if (match(Src1, m_Zero()) &&
3544 match(Src0,
3545 m_ZExtOrSExt(m_Cmp(SrcPred, m_Value(SrcLHS), m_Value(SrcRHS))))) {
3546 if (CCVal == CmpInst::ICMP_EQ)
3547 SrcPred = CmpInst::getInversePredicate(SrcPred);
3548
3549 Intrinsic::ID NewIID = CmpInst::isFPPredicate(SrcPred) ?
3550 Intrinsic::amdgcn_fcmp : Intrinsic::amdgcn_icmp;
3551
3552 Value *NewF = Intrinsic::getDeclaration(II->getModule(), NewIID,
3553 SrcLHS->getType());
3554 Value *Args[] = { SrcLHS, SrcRHS,
3555 ConstantInt::get(CC->getType(), SrcPred) };
Craig Topperbb4069e2017-07-07 23:16:26 +00003556 CallInst *NewCall = Builder.CreateCall(NewF, Args);
Matt Arsenaultd81f5572017-03-13 18:14:02 +00003557 NewCall->takeName(II);
3558 return replaceInstUsesWith(*II, NewCall);
3559 }
3560
3561 break;
3562 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003563 case Intrinsic::stackrestore: {
3564 // If the save is right next to the restore, remove the restore. This can
3565 // happen when variable allocas are DCE'd.
Gabor Greif589a0b92010-06-24 12:58:35 +00003566 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003567 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00003568 if (&*++SS->getIterator() == II)
Sanjay Patel4b198802016-02-01 22:23:39 +00003569 return eraseInstFromFunction(CI);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003570 }
3571 }
Jim Grosbach7815f562012-02-03 00:07:04 +00003572
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003573 // Scan down this block to see if there is another stack restore in the
3574 // same block without an intervening call/alloca.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00003575 BasicBlock::iterator BI(II);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003576 TerminatorInst *TI = II->getParent()->getTerminator();
3577 bool CannotRemove = false;
3578 for (++BI; &*BI != TI; ++BI) {
Nuno Lopes55fff832012-06-21 15:45:28 +00003579 if (isa<AllocaInst>(BI)) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003580 CannotRemove = true;
3581 break;
3582 }
3583 if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
3584 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
3585 // If there is a stackrestore below this one, remove this one.
3586 if (II->getIntrinsicID() == Intrinsic::stackrestore)
Sanjay Patel4b198802016-02-01 22:23:39 +00003587 return eraseInstFromFunction(CI);
Reid Kleckner892ae2e2016-02-27 00:53:54 +00003588
3589 // Bail if we cross over an intrinsic with side effects, such as
3590 // llvm.stacksave, llvm.read_register, or llvm.setjmp.
3591 if (II->mayHaveSideEffects()) {
3592 CannotRemove = true;
3593 break;
3594 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003595 } else {
3596 // If we found a non-intrinsic call, we can't remove the stack
3597 // restore.
3598 CannotRemove = true;
3599 break;
3600 }
3601 }
3602 }
Jim Grosbach7815f562012-02-03 00:07:04 +00003603
Bill Wendlingf891bf82011-07-31 06:30:59 +00003604 // If the stack restore is in a return, resume, or unwind block and if there
3605 // are no allocas or calls between the restore and the return, nuke the
3606 // restore.
Bill Wendlingd5d95b02012-02-06 21:16:41 +00003607 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
Sanjay Patel4b198802016-02-01 22:23:39 +00003608 return eraseInstFromFunction(CI);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003609 break;
3610 }
Vitaly Bukaf0500b62016-07-28 22:50:48 +00003611 case Intrinsic::lifetime_start:
Vitaly Buka0ab23cf2016-07-28 22:59:03 +00003612 // Asan needs to poison memory to detect invalid access which is possible
3613 // even for empty lifetime range.
3614 if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress))
3615 break;
3616
Arnaud A. de Grandmaison333ef382016-05-10 09:24:49 +00003617 if (removeTriviallyEmptyRange(*II, Intrinsic::lifetime_start,
3618 Intrinsic::lifetime_end, *this))
3619 return nullptr;
Arnaud A. de Grandmaison849f3bf2015-10-01 14:54:31 +00003620 break;
Hal Finkelf5867a72014-07-25 21:45:17 +00003621 case Intrinsic::assume: {
David Majnemerfcc58112016-04-08 16:37:12 +00003622 Value *IIOperand = II->getArgOperand(0);
3623 // Remove an assume if it is immediately followed by an identical assume.
3624 if (match(II->getNextNode(),
3625 m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand))))
3626 return eraseInstFromFunction(CI);
3627
Hal Finkelf5867a72014-07-25 21:45:17 +00003628 // Canonicalize assume(a && b) -> assume(a); assume(b);
Hal Finkel74c2f352014-09-07 12:44:26 +00003629 // Note: New assumption intrinsics created here are registered by
3630 // the InstCombineIRInserter object.
David Majnemerfcc58112016-04-08 16:37:12 +00003631 Value *AssumeIntrinsic = II->getCalledValue(), *A, *B;
Hal Finkelf5867a72014-07-25 21:45:17 +00003632 if (match(IIOperand, m_And(m_Value(A), m_Value(B)))) {
Craig Topperbb4069e2017-07-07 23:16:26 +00003633 Builder.CreateCall(AssumeIntrinsic, A, II->getName());
3634 Builder.CreateCall(AssumeIntrinsic, B, II->getName());
Sanjay Patel4b198802016-02-01 22:23:39 +00003635 return eraseInstFromFunction(*II);
Hal Finkelf5867a72014-07-25 21:45:17 +00003636 }
3637 // assume(!(a || b)) -> assume(!a); assume(!b);
3638 if (match(IIOperand, m_Not(m_Or(m_Value(A), m_Value(B))))) {
Craig Topperbb4069e2017-07-07 23:16:26 +00003639 Builder.CreateCall(AssumeIntrinsic, Builder.CreateNot(A), II->getName());
3640 Builder.CreateCall(AssumeIntrinsic, Builder.CreateNot(B), II->getName());
Sanjay Patel4b198802016-02-01 22:23:39 +00003641 return eraseInstFromFunction(*II);
Hal Finkelf5867a72014-07-25 21:45:17 +00003642 }
Hal Finkel04a15612014-10-04 21:27:06 +00003643
Philip Reames66c6de62014-11-11 23:33:19 +00003644 // assume( (load addr) != null ) -> add 'nonnull' metadata to load
3645 // (if assume is valid at the load)
Sanjay Patelf0d1e7732017-01-03 22:25:31 +00003646 CmpInst::Predicate Pred;
3647 Instruction *LHS;
3648 if (match(IIOperand, m_ICmp(Pred, m_Instruction(LHS), m_Zero())) &&
3649 Pred == ICmpInst::ICMP_NE && LHS->getOpcode() == Instruction::Load &&
3650 LHS->getType()->isPointerTy() &&
3651 isValidAssumeForContext(II, LHS, &DT)) {
3652 MDNode *MD = MDNode::get(II->getContext(), None);
3653 LHS->setMetadata(LLVMContext::MD_nonnull, MD);
3654 return eraseInstFromFunction(*II);
3655
Chandler Carruth24969102015-02-10 08:07:32 +00003656 // TODO: apply nonnull return attributes to calls and invokes
Philip Reames66c6de62014-11-11 23:33:19 +00003657 // TODO: apply range metadata for range check patterns?
3658 }
Sanjay Patelf0d1e7732017-01-03 22:25:31 +00003659
Hal Finkel04a15612014-10-04 21:27:06 +00003660 // If there is a dominating assume with the same condition as this one,
3661 // then this one is redundant, and should be removed.
Craig Topperb45eabc2017-04-26 16:39:58 +00003662 KnownBits Known(1);
3663 computeKnownBits(IIOperand, Known, 0, II);
Craig Topperf0aeee02017-05-05 17:36:09 +00003664 if (Known.isAllOnes())
Sanjay Patel4b198802016-02-01 22:23:39 +00003665 return eraseInstFromFunction(*II);
Hal Finkel04a15612014-10-04 21:27:06 +00003666
Hal Finkel8a9a7832017-01-11 13:24:24 +00003667 // Update the cache of affected values for this assumption (we might be
3668 // here because we just simplified the condition).
3669 AC.updateAffectedValues(II);
Hal Finkelf5867a72014-07-25 21:45:17 +00003670 break;
3671 }
Philip Reames9db26ff2014-12-29 23:27:30 +00003672 case Intrinsic::experimental_gc_relocate: {
3673 // Translate facts known about a pointer before relocating into
3674 // facts about the relocate value, while being careful to
3675 // preserve relocation semantics.
Manuel Jacob83eefa62016-01-05 04:03:00 +00003676 Value *DerivedPtr = cast<GCRelocateInst>(II)->getDerivedPtr();
Philip Reames9db26ff2014-12-29 23:27:30 +00003677
3678 // Remove the relocation if unused, note that this check is required
3679 // to prevent the cases below from looping forever.
3680 if (II->use_empty())
Sanjay Patel4b198802016-02-01 22:23:39 +00003681 return eraseInstFromFunction(*II);
Philip Reames9db26ff2014-12-29 23:27:30 +00003682
3683 // Undef is undef, even after relocation.
3684 // TODO: provide a hook for this in GCStrategy. This is clearly legal for
3685 // most practical collectors, but there was discussion in the review thread
3686 // about whether it was legal for all possible collectors.
Philip Reamesea4d8e82016-02-09 21:09:22 +00003687 if (isa<UndefValue>(DerivedPtr))
3688 // Use undef of gc_relocate's type to replace it.
3689 return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
Philip Reames9db26ff2014-12-29 23:27:30 +00003690
Philip Reamesea4d8e82016-02-09 21:09:22 +00003691 if (auto *PT = dyn_cast<PointerType>(II->getType())) {
3692 // The relocation of null will be null for most any collector.
3693 // TODO: provide a hook for this in GCStrategy. There might be some
3694 // weird collector this property does not hold for.
3695 if (isa<ConstantPointerNull>(DerivedPtr))
3696 // Use null-pointer of gc_relocate's type to replace it.
3697 return replaceInstUsesWith(*II, ConstantPointerNull::get(PT));
Simon Pilgrimc0c56e72016-04-24 17:00:34 +00003698
Philip Reamesea4d8e82016-02-09 21:09:22 +00003699 // isKnownNonNull -> nonnull attribute
Justin Bogner99798402016-08-05 01:06:44 +00003700 if (isKnownNonNullAt(DerivedPtr, II, &DT))
Reid Klecknerb5180542017-03-21 16:57:19 +00003701 II->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00003702 }
Philip Reames9db26ff2014-12-29 23:27:30 +00003703
3704 // TODO: bitcast(relocate(p)) -> relocate(bitcast(p))
3705 // Canonicalize on the type from the uses to the defs
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00003706
Philip Reames9db26ff2014-12-29 23:27:30 +00003707 // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...)
Philip Reamesea4d8e82016-02-09 21:09:22 +00003708 break;
Philip Reames9db26ff2014-12-29 23:27:30 +00003709 }
Artur Pilipenkoe812ca02017-01-25 14:12:12 +00003710
3711 case Intrinsic::experimental_guard: {
Sanjoy Dase0e57952017-02-01 16:34:55 +00003712 // Is this guard followed by another guard?
3713 Instruction *NextInst = II->getNextNode();
3714 Value *NextCond = nullptr;
3715 if (match(NextInst,
3716 m_Intrinsic<Intrinsic::experimental_guard>(m_Value(NextCond)))) {
3717 Value *CurrCond = II->getArgOperand(0);
Artur Pilipenkoe812ca02017-01-25 14:12:12 +00003718
Simon Pilgrim68168d12017-03-30 12:59:53 +00003719 // Remove a guard that it is immediately preceded by an identical guard.
Sanjoy Dase0e57952017-02-01 16:34:55 +00003720 if (CurrCond == NextCond)
3721 return eraseInstFromFunction(*NextInst);
3722
3723 // Otherwise canonicalize guard(a); guard(b) -> guard(a & b).
Craig Topperbb4069e2017-07-07 23:16:26 +00003724 II->setArgOperand(0, Builder.CreateAnd(CurrCond, NextCond));
Sanjoy Dase0e57952017-02-01 16:34:55 +00003725 return eraseInstFromFunction(*NextInst);
3726 }
Artur Pilipenkoe812ca02017-01-25 14:12:12 +00003727 break;
3728 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003729 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003730 return visitCallSite(II);
3731}
3732
Davide Italianoaec46172017-01-31 18:09:05 +00003733// Fence instruction simplification
3734Instruction *InstCombiner::visitFenceInst(FenceInst &FI) {
3735 // Remove identical consecutive fences.
3736 if (auto *NFI = dyn_cast<FenceInst>(FI.getNextNode()))
3737 if (FI.isIdenticalTo(NFI))
3738 return eraseInstFromFunction(FI);
3739 return nullptr;
3740}
3741
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003742// InvokeInst simplification
3743//
3744Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
3745 return visitCallSite(&II);
3746}
3747
Sanjay Patelcd4377c2016-01-20 22:24:38 +00003748/// If this cast does not affect the value passed through the varargs area, we
3749/// can eliminate the use of the cast.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003750static bool isSafeToEliminateVarargsCast(const CallSite CS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003751 const DataLayout &DL,
3752 const CastInst *const CI,
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003753 const int ix) {
3754 if (!CI->isLosslessCast())
3755 return false;
3756
Philip Reames1a1bdb22014-12-02 18:50:36 +00003757 // If this is a GC intrinsic, avoid munging types. We need types for
3758 // statepoint reconstruction in SelectionDAG.
3759 // TODO: This is probably something which should be expanded to all
3760 // intrinsics since the entire point of intrinsics is that
3761 // they are understandable by the optimizer.
3762 if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS))
3763 return false;
3764
Reid Kleckner26af2ca2014-01-28 02:38:36 +00003765 // The size of ByVal or InAlloca arguments is derived from the type, so we
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003766 // can't change to a type with a different size. If the size were
3767 // passed explicitly we could avoid this check.
Reid Kleckner26af2ca2014-01-28 02:38:36 +00003768 if (!CS.isByValOrInAllocaArgument(ix))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003769 return true;
3770
Jim Grosbach7815f562012-02-03 00:07:04 +00003771 Type* SrcTy =
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003772 cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00003773 Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003774 if (!SrcTy->isSized() || !DstTy->isSized())
3775 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003776 if (DL.getTypeAllocSize(SrcTy) != DL.getTypeAllocSize(DstTy))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003777 return false;
3778 return true;
3779}
3780
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003781Instruction *InstCombiner::tryOptimizeCall(CallInst *CI) {
Craig Topperf40110f2014-04-25 05:29:35 +00003782 if (!CI->getCalledFunction()) return nullptr;
Eric Christophera7fb58f2010-03-06 10:50:38 +00003783
Chandler Carruthba4c5172015-01-21 11:23:40 +00003784 auto InstCombineRAUW = [this](Instruction *From, Value *With) {
Sanjay Patel4b198802016-02-01 22:23:39 +00003785 replaceInstUsesWith(*From, With);
Chandler Carruthba4c5172015-01-21 11:23:40 +00003786 };
Justin Bogner99798402016-08-05 01:06:44 +00003787 LibCallSimplifier Simplifier(DL, &TLI, InstCombineRAUW);
Chandler Carruthba4c5172015-01-21 11:23:40 +00003788 if (Value *With = Simplifier.optimizeCall(CI)) {
Meador Ingee3f2b262012-11-30 04:05:06 +00003789 ++NumSimplified;
Sanjay Patel4b198802016-02-01 22:23:39 +00003790 return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
Meador Ingee3f2b262012-11-30 04:05:06 +00003791 }
Meador Ingedf796f82012-10-13 16:45:24 +00003792
Craig Topperf40110f2014-04-25 05:29:35 +00003793 return nullptr;
Eric Christophera7fb58f2010-03-06 10:50:38 +00003794}
3795
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003796static IntrinsicInst *findInitTrampolineFromAlloca(Value *TrampMem) {
Duncan Sandsa0984362011-09-06 13:37:06 +00003797 // Strip off at most one level of pointer casts, looking for an alloca. This
3798 // is good enough in practice and simpler than handling any number of casts.
3799 Value *Underlying = TrampMem->stripPointerCasts();
3800 if (Underlying != TrampMem &&
Chandler Carruthcdf47882014-03-09 03:16:01 +00003801 (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem))
Craig Topperf40110f2014-04-25 05:29:35 +00003802 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003803 if (!isa<AllocaInst>(Underlying))
Craig Topperf40110f2014-04-25 05:29:35 +00003804 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003805
Craig Topperf40110f2014-04-25 05:29:35 +00003806 IntrinsicInst *InitTrampoline = nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +00003807 for (User *U : TrampMem->users()) {
3808 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
Duncan Sandsa0984362011-09-06 13:37:06 +00003809 if (!II)
Craig Topperf40110f2014-04-25 05:29:35 +00003810 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003811 if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
3812 if (InitTrampoline)
3813 // More than one init_trampoline writes to this value. Give up.
Craig Topperf40110f2014-04-25 05:29:35 +00003814 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003815 InitTrampoline = II;
3816 continue;
3817 }
3818 if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
3819 // Allow any number of calls to adjust.trampoline.
3820 continue;
Craig Topperf40110f2014-04-25 05:29:35 +00003821 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003822 }
3823
3824 // No call to init.trampoline found.
3825 if (!InitTrampoline)
Craig Topperf40110f2014-04-25 05:29:35 +00003826 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003827
3828 // Check that the alloca is being used in the expected way.
3829 if (InitTrampoline->getOperand(0) != TrampMem)
Craig Topperf40110f2014-04-25 05:29:35 +00003830 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003831
3832 return InitTrampoline;
3833}
3834
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003835static IntrinsicInst *findInitTrampolineFromBB(IntrinsicInst *AdjustTramp,
Duncan Sandsa0984362011-09-06 13:37:06 +00003836 Value *TrampMem) {
3837 // Visit all the previous instructions in the basic block, and try to find a
3838 // init.trampoline which has a direct path to the adjust.trampoline.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00003839 for (BasicBlock::iterator I = AdjustTramp->getIterator(),
3840 E = AdjustTramp->getParent()->begin();
3841 I != E;) {
3842 Instruction *Inst = &*--I;
Duncan Sandsa0984362011-09-06 13:37:06 +00003843 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
3844 if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
3845 II->getOperand(0) == TrampMem)
3846 return II;
3847 if (Inst->mayWriteToMemory())
Craig Topperf40110f2014-04-25 05:29:35 +00003848 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003849 }
Craig Topperf40110f2014-04-25 05:29:35 +00003850 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003851}
3852
3853// Given a call to llvm.adjust.trampoline, find and return the corresponding
3854// call to llvm.init.trampoline if the call to the trampoline can be optimized
3855// to a direct call to a function. Otherwise return NULL.
3856//
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003857static IntrinsicInst *findInitTrampoline(Value *Callee) {
Duncan Sandsa0984362011-09-06 13:37:06 +00003858 Callee = Callee->stripPointerCasts();
3859 IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
3860 if (!AdjustTramp ||
3861 AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
Craig Topperf40110f2014-04-25 05:29:35 +00003862 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003863
3864 Value *TrampMem = AdjustTramp->getOperand(0);
3865
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003866 if (IntrinsicInst *IT = findInitTrampolineFromAlloca(TrampMem))
Duncan Sandsa0984362011-09-06 13:37:06 +00003867 return IT;
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003868 if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem))
Duncan Sandsa0984362011-09-06 13:37:06 +00003869 return IT;
Craig Topperf40110f2014-04-25 05:29:35 +00003870 return nullptr;
Duncan Sandsa0984362011-09-06 13:37:06 +00003871}
3872
Sanjay Patelcd4377c2016-01-20 22:24:38 +00003873/// Improvements for call and invoke instructions.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003874Instruction *InstCombiner::visitCallSite(CallSite CS) {
Justin Bogner99798402016-08-05 01:06:44 +00003875 if (isAllocLikeFn(CS.getInstruction(), &TLI))
Nuno Lopes95cc4f32012-07-09 18:38:20 +00003876 return visitAllocSite(*CS.getInstruction());
Nuno Lopesdc6085e2012-06-21 21:25:05 +00003877
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003878 bool Changed = false;
3879
Philip Reamesc25df112015-06-16 20:24:25 +00003880 // Mark any parameters that are known to be non-null with the nonnull
3881 // attribute. This is helpful for inlining calls to functions with null
3882 // checks on their arguments.
Reid Kleckner5fbdd172017-05-31 19:23:09 +00003883 SmallVector<unsigned, 4> ArgNos;
Philip Reamesc25df112015-06-16 20:24:25 +00003884 unsigned ArgNo = 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +00003885
Philip Reamesc25df112015-06-16 20:24:25 +00003886 for (Value *V : CS.args()) {
Sanjay Patelf9f5d3c2016-01-29 23:14:58 +00003887 if (V->getType()->isPointerTy() &&
Reid Klecknerfb502d22017-04-14 20:19:02 +00003888 !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
Justin Bogner99798402016-08-05 01:06:44 +00003889 isKnownNonNullAt(V, CS.getInstruction(), &DT))
Reid Kleckner5fbdd172017-05-31 19:23:09 +00003890 ArgNos.push_back(ArgNo);
Philip Reamesc25df112015-06-16 20:24:25 +00003891 ArgNo++;
3892 }
Akira Hatanaka237916b2015-12-02 06:58:49 +00003893
Philip Reamesc25df112015-06-16 20:24:25 +00003894 assert(ArgNo == CS.arg_size() && "sanity check");
3895
Reid Kleckner5fbdd172017-05-31 19:23:09 +00003896 if (!ArgNos.empty()) {
Reid Klecknerb5180542017-03-21 16:57:19 +00003897 AttributeList AS = CS.getAttributes();
Akira Hatanaka237916b2015-12-02 06:58:49 +00003898 LLVMContext &Ctx = CS.getInstruction()->getContext();
Reid Kleckner5fbdd172017-05-31 19:23:09 +00003899 AS = AS.addParamAttribute(Ctx, ArgNos,
3900 Attribute::get(Ctx, Attribute::NonNull));
Akira Hatanaka237916b2015-12-02 06:58:49 +00003901 CS.setAttributes(AS);
3902 Changed = true;
3903 }
3904
Chris Lattner73989652010-12-20 08:25:06 +00003905 // If the callee is a pointer to a function, attempt to move any casts to the
3906 // arguments of the call/invoke.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003907 Value *Callee = CS.getCalledValue();
Chris Lattner73989652010-12-20 08:25:06 +00003908 if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
Craig Topperf40110f2014-04-25 05:29:35 +00003909 return nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003910
Justin Lebar9d943972016-03-14 20:18:54 +00003911 if (Function *CalleeF = dyn_cast<Function>(Callee)) {
3912 // Remove the convergent attr on calls when the callee is not convergent.
Matt Arsenault802ebcb2016-06-20 19:04:44 +00003913 if (CS.isConvergent() && !CalleeF->isConvergent() &&
3914 !CalleeF->isIntrinsic()) {
Justin Lebar9d943972016-03-14 20:18:54 +00003915 DEBUG(dbgs() << "Removing convergent attr from instr "
3916 << CS.getInstruction() << "\n");
3917 CS.setNotConvergent();
3918 return CS.getInstruction();
3919 }
3920
Chris Lattner846a52e2010-02-01 18:11:34 +00003921 // If the call and callee calling conventions don't match, this call must
3922 // be unreachable, as the call is undefined.
3923 if (CalleeF->getCallingConv() != CS.getCallingConv() &&
3924 // Only do this for calls to a function with a body. A prototype may
3925 // not actually end up matching the implementation's calling conv for a
3926 // variety of reasons (e.g. it may be written in assembly).
3927 !CalleeF->isDeclaration()) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003928 Instruction *OldCall = CS.getInstruction();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003929 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
Jim Grosbach7815f562012-02-03 00:07:04 +00003930 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003931 OldCall);
Chad Rosiere28ae302012-12-13 00:18:46 +00003932 // If OldCall does not return void then replaceAllUsesWith undef.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003933 // This allows ValueHandlers and custom metadata to adjust itself.
3934 if (!OldCall->getType()->isVoidTy())
Sanjay Patel4b198802016-02-01 22:23:39 +00003935 replaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
Chris Lattner2cecedf2010-02-01 18:04:58 +00003936 if (isa<CallInst>(OldCall))
Sanjay Patel4b198802016-02-01 22:23:39 +00003937 return eraseInstFromFunction(*OldCall);
Jim Grosbach7815f562012-02-03 00:07:04 +00003938
Chris Lattner2cecedf2010-02-01 18:04:58 +00003939 // We cannot remove an invoke, because it would change the CFG, just
3940 // change the callee to a null pointer.
Gabor Greiffebf6ab2010-03-20 21:00:25 +00003941 cast<InvokeInst>(OldCall)->setCalledFunction(
Chris Lattner2cecedf2010-02-01 18:04:58 +00003942 Constant::getNullValue(CalleeF->getType()));
Craig Topperf40110f2014-04-25 05:29:35 +00003943 return nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003944 }
Justin Lebar9d943972016-03-14 20:18:54 +00003945 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003946
3947 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
Gabor Greif589a0b92010-06-24 12:58:35 +00003948 // If CS does not return void then replaceAllUsesWith undef.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003949 // This allows ValueHandlers and custom metadata to adjust itself.
3950 if (!CS.getInstruction()->getType()->isVoidTy())
Sanjay Patel4b198802016-02-01 22:23:39 +00003951 replaceInstUsesWith(*CS.getInstruction(),
Eli Friedmanb9ed18f2011-05-18 00:32:01 +00003952 UndefValue::get(CS.getInstruction()->getType()));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003953
Nuno Lopes771e7bd2012-06-21 23:52:14 +00003954 if (isa<InvokeInst>(CS.getInstruction())) {
3955 // Can't remove an invoke because we cannot change the CFG.
Craig Topperf40110f2014-04-25 05:29:35 +00003956 return nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003957 }
Nuno Lopes771e7bd2012-06-21 23:52:14 +00003958
3959 // This instruction is not reachable, just remove it. We insert a store to
3960 // undef so that we know that this code is not reachable, despite the fact
3961 // that we can't modify the CFG here.
3962 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
3963 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
3964 CS.getInstruction());
3965
Sanjay Patel4b198802016-02-01 22:23:39 +00003966 return eraseInstFromFunction(*CS.getInstruction());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003967 }
3968
Sanjay Patel6038d3e2016-01-29 23:27:03 +00003969 if (IntrinsicInst *II = findInitTrampoline(Callee))
Duncan Sandsa0984362011-09-06 13:37:06 +00003970 return transformCallThroughTrampoline(CS, II);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003971
Chris Lattner229907c2011-07-18 04:54:35 +00003972 PointerType *PTy = cast<PointerType>(Callee->getType());
3973 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003974 if (FTy->isVarArg()) {
Eli Friedman7534b4682011-11-29 01:18:23 +00003975 int ix = FTy->getNumParams();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003976 // See if we can optimize any arguments passed through the varargs area of
3977 // the call.
Matt Arsenault5d2e85f2013-06-28 00:25:40 +00003978 for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(),
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003979 E = CS.arg_end(); I != E; ++I, ++ix) {
3980 CastInst *CI = dyn_cast<CastInst>(*I);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003981 if (CI && isSafeToEliminateVarargsCast(CS, DL, CI, ix)) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00003982 *I = CI->getOperand(0);
3983 Changed = true;
3984 }
3985 }
3986 }
3987
3988 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
3989 // Inline asm calls cannot throw - mark them 'nounwind'.
3990 CS.setDoesNotThrow();
3991 Changed = true;
3992 }
3993
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003994 // Try to optimize the call if possible, we require DataLayout for most of
Eric Christophera7fb58f2010-03-06 10:50:38 +00003995 // this. None of these calls are seen as possibly dead so go ahead and
3996 // delete the instruction now.
3997 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003998 Instruction *I = tryOptimizeCall(CI);
Eric Christopher1810d772010-03-06 10:59:25 +00003999 // If we changed something return the result, etc. Otherwise let
4000 // the fallthrough check.
Sanjay Patel4b198802016-02-01 22:23:39 +00004001 if (I) return eraseInstFromFunction(*I);
Eric Christophera7fb58f2010-03-06 10:50:38 +00004002 }
4003
Craig Topperf40110f2014-04-25 05:29:35 +00004004 return Changed ? CS.getInstruction() : nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004005}
4006
Sanjay Patelcd4377c2016-01-20 22:24:38 +00004007/// If the callee is a constexpr cast of a function, attempt to move the cast to
4008/// the arguments of the call/invoke.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004009bool InstCombiner::transformConstExprCastCall(CallSite CS) {
Sanjay Patele3c335c2016-08-11 15:21:21 +00004010 auto *Callee = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
Craig Topperf40110f2014-04-25 05:29:35 +00004011 if (!Callee)
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004012 return false;
Sanjay Patel38ae83d2016-08-11 15:23:56 +00004013
4014 // The prototype of a thunk is a lie. Don't directly call such a function.
David Majnemer4c0a6e92015-01-21 22:32:04 +00004015 if (Callee->hasFnAttribute("thunk"))
4016 return false;
Sanjay Patel38ae83d2016-08-11 15:23:56 +00004017
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004018 Instruction *Caller = CS.getInstruction();
Reid Klecknerb5180542017-03-21 16:57:19 +00004019 const AttributeList &CallerPAL = CS.getAttributes();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004020
4021 // Okay, this is a cast from a function to a different type. Unless doing so
4022 // would cause a type conversion of one of our arguments, change this call to
4023 // be a direct call with arguments casted to the appropriate types.
4024 //
Chris Lattner229907c2011-07-18 04:54:35 +00004025 FunctionType *FT = Callee->getFunctionType();
4026 Type *OldRetTy = Caller->getType();
4027 Type *NewRetTy = FT->getReturnType();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004028
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004029 // Check to see if we are changing the return type...
4030 if (OldRetTy != NewRetTy) {
Nick Lewyckya6a17d72014-01-18 22:47:12 +00004031
4032 if (NewRetTy->isStructTy())
4033 return false; // TODO: Handle multiple return values.
4034
David Majnemer9b6b8222015-01-06 08:41:31 +00004035 if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) {
Matt Arsenaulte6952f22013-09-17 21:10:14 +00004036 if (Callee->isDeclaration())
4037 return false; // Cannot transform this return value.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004038
Matt Arsenaulte6952f22013-09-17 21:10:14 +00004039 if (!Caller->use_empty() &&
4040 // void -> non-void is handled specially
4041 !NewRetTy->isVoidTy())
Frederic Rissc1892e22014-10-23 04:08:42 +00004042 return false; // Cannot transform this return value.
Matt Arsenaulte6952f22013-09-17 21:10:14 +00004043 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004044
4045 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
Reid Klecknerb5180542017-03-21 16:57:19 +00004046 AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
Pete Cooper2777d8872015-05-06 23:19:56 +00004047 if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(NewRetTy)))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004048 return false; // Attribute not compatible with transformed value.
4049 }
4050
4051 // If the callsite is an invoke instruction, and the return value is used by
4052 // a PHI node in a successor, we cannot change the return type of the call
4053 // because there is no place to put the cast instruction (without breaking
4054 // the critical edge). Bail out in this case.
4055 if (!Caller->use_empty())
4056 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
Chandler Carruthcdf47882014-03-09 03:16:01 +00004057 for (User *U : II->users())
4058 if (PHINode *PN = dyn_cast<PHINode>(U))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004059 if (PN->getParent() == II->getNormalDest() ||
4060 PN->getParent() == II->getUnwindDest())
4061 return false;
4062 }
4063
Matt Arsenault5d2e85f2013-06-28 00:25:40 +00004064 unsigned NumActualArgs = CS.arg_size();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004065 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
4066
David Majnemer9b6b8222015-01-06 08:41:31 +00004067 // Prevent us turning:
4068 // declare void @takes_i32_inalloca(i32* inalloca)
4069 // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0)
4070 //
4071 // into:
4072 // call void @takes_i32_inalloca(i32* null)
David Majnemerd61a6fd2015-03-11 18:03:05 +00004073 //
4074 // Similarly, avoid folding away bitcasts of byval calls.
4075 if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
4076 Callee->getAttributes().hasAttrSomewhere(Attribute::ByVal))
David Majnemer9b6b8222015-01-06 08:41:31 +00004077 return false;
4078
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004079 CallSite::arg_iterator AI = CS.arg_begin();
4080 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +00004081 Type *ParamTy = FT->getParamType(i);
4082 Type *ActTy = (*AI)->getType();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004083
David Majnemer9b6b8222015-01-06 08:41:31 +00004084 if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004085 return false; // Cannot transform this parameter value.
4086
Reid Klecknerf021fab2017-04-13 23:12:13 +00004087 if (AttrBuilder(CallerPAL.getParamAttributes(i))
4088 .overlaps(AttributeFuncs::typeIncompatible(ParamTy)))
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004089 return false; // Attribute not compatible with transformed value.
Jim Grosbach7815f562012-02-03 00:07:04 +00004090
Reid Kleckner26af2ca2014-01-28 02:38:36 +00004091 if (CS.isInAllocaArgument(i))
4092 return false; // Cannot transform to and from inalloca.
4093
Chris Lattner27ca8eb2010-12-20 08:36:38 +00004094 // If the parameter is passed as a byval argument, then we have to have a
4095 // sized type and the sized type has to have the same size as the old type.
Reid Klecknerf021fab2017-04-13 23:12:13 +00004096 if (ParamTy != ActTy && CallerPAL.hasParamAttribute(i, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00004097 PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004098 if (!ParamPTy || !ParamPTy->getElementType()->isSized())
Chris Lattner27ca8eb2010-12-20 08:36:38 +00004099 return false;
Jim Grosbach7815f562012-02-03 00:07:04 +00004100
Matt Arsenaultfa252722013-09-27 22:18:51 +00004101 Type *CurElTy = ActTy->getPointerElementType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004102 if (DL.getTypeAllocSize(CurElTy) !=
4103 DL.getTypeAllocSize(ParamPTy->getElementType()))
Chris Lattner27ca8eb2010-12-20 08:36:38 +00004104 return false;
4105 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004106 }
4107
Chris Lattneradf38b32011-02-24 05:10:56 +00004108 if (Callee->isDeclaration()) {
4109 // Do not delete arguments unless we have a function body.
4110 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
4111 return false;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004112
Chris Lattneradf38b32011-02-24 05:10:56 +00004113 // If the callee is just a declaration, don't change the varargsness of the
4114 // call. We don't want to introduce a varargs call where one doesn't
4115 // already exist.
Chris Lattner229907c2011-07-18 04:54:35 +00004116 PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattneradf38b32011-02-24 05:10:56 +00004117 if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
4118 return false;
Jim Grosbache84ae7b2012-02-03 00:00:55 +00004119
4120 // If both the callee and the cast type are varargs, we still have to make
4121 // sure the number of fixed parameters are the same or we have the same
4122 // ABI issues as if we introduce a varargs call.
Jim Grosbach1df8cdc2012-02-03 00:26:07 +00004123 if (FT->isVarArg() &&
4124 cast<FunctionType>(APTy->getElementType())->isVarArg() &&
4125 FT->getNumParams() !=
Jim Grosbache84ae7b2012-02-03 00:00:55 +00004126 cast<FunctionType>(APTy->getElementType())->getNumParams())
4127 return false;
Chris Lattneradf38b32011-02-24 05:10:56 +00004128 }
Jim Grosbach7815f562012-02-03 00:07:04 +00004129
Jim Grosbach0ab54182012-02-03 00:00:50 +00004130 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
Reid Kleckneraa0cec72017-04-19 23:17:47 +00004131 !CallerPAL.isEmpty()) {
Jim Grosbach0ab54182012-02-03 00:00:50 +00004132 // In this case we have more arguments than the new function type, but we
4133 // won't be dropping them. Check that these extra arguments have attributes
4134 // that are compatible with being a vararg call argument.
Reid Kleckneraa0cec72017-04-19 23:17:47 +00004135 unsigned SRetIdx;
4136 if (CallerPAL.hasAttrSomewhere(Attribute::StructRet, &SRetIdx) &&
4137 SRetIdx > FT->getNumParams())
4138 return false;
4139 }
Jim Grosbach7815f562012-02-03 00:07:04 +00004140
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004141 // Okay, we decided that this is a safe thing to do: go ahead and start
Chris Lattneradf38b32011-02-24 05:10:56 +00004142 // inserting cast instructions as necessary.
Reid Klecknerc3fae792017-04-13 18:11:03 +00004143 SmallVector<Value *, 8> Args;
4144 SmallVector<AttributeSet, 8> ArgAttrs;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004145 Args.reserve(NumActualArgs);
Reid Klecknerc3fae792017-04-13 18:11:03 +00004146 ArgAttrs.reserve(NumActualArgs);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004147
4148 // Get any return attributes.
Reid Klecknerb5180542017-03-21 16:57:19 +00004149 AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004150
4151 // If the return value is not being used, the type may not be compatible
4152 // with the existing attributes. Wipe out any problematic attributes.
Pete Cooper2777d8872015-05-06 23:19:56 +00004153 RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004154
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004155 AI = CS.arg_begin();
4156 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +00004157 Type *ParamTy = FT->getParamType(i);
Matt Arsenaultcacbb232013-07-30 20:45:05 +00004158
Reid Klecknerc3fae792017-04-13 18:11:03 +00004159 Value *NewArg = *AI;
4160 if ((*AI)->getType() != ParamTy)
Craig Topperbb4069e2017-07-07 23:16:26 +00004161 NewArg = Builder.CreateBitOrPointerCast(*AI, ParamTy);
Reid Klecknerc3fae792017-04-13 18:11:03 +00004162 Args.push_back(NewArg);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004163
4164 // Add any parameter attributes.
Reid Klecknerf021fab2017-04-13 23:12:13 +00004165 ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004166 }
4167
4168 // If the function takes more arguments than the call was taking, add them
4169 // now.
Reid Klecknerc3fae792017-04-13 18:11:03 +00004170 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004171 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
Reid Klecknerc3fae792017-04-13 18:11:03 +00004172 ArgAttrs.push_back(AttributeSet());
4173 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004174
4175 // If we are removing arguments to the function, emit an obnoxious warning.
4176 if (FT->getNumParams() < NumActualArgs) {
Nick Lewycky90053a12012-12-26 22:00:35 +00004177 // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
4178 if (FT->isVarArg()) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004179 // Add all of the arguments in their promoted form to the arg list.
4180 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +00004181 Type *PTy = getPromotedType((*AI)->getType());
Reid Klecknerc3fae792017-04-13 18:11:03 +00004182 Value *NewArg = *AI;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004183 if (PTy != (*AI)->getType()) {
4184 // Must promote to pass through va_arg area!
4185 Instruction::CastOps opcode =
4186 CastInst::getCastOpcode(*AI, false, PTy, false);
Craig Topperbb4069e2017-07-07 23:16:26 +00004187 NewArg = Builder.CreateCast(opcode, *AI, PTy);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004188 }
Reid Klecknerc3fae792017-04-13 18:11:03 +00004189 Args.push_back(NewArg);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004190
4191 // Add any parameter attributes.
Reid Klecknerf021fab2017-04-13 23:12:13 +00004192 ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004193 }
4194 }
4195 }
4196
Reid Klecknerc2cb5602017-04-12 00:38:00 +00004197 AttributeSet FnAttrs = CallerPAL.getFnAttributes();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004198
4199 if (NewRetTy->isVoidTy())
4200 Caller->setName(""); // Void type should not have a name.
4201
Reid Klecknerc3fae792017-04-13 18:11:03 +00004202 assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
4203 "missing argument attributes");
4204 LLVMContext &Ctx = Callee->getContext();
4205 AttributeList NewCallerPAL = AttributeList::get(
4206 Ctx, FnAttrs, AttributeSet::get(Ctx, RAttrs), ArgAttrs);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004207
Sanjoy Das76293462015-11-25 00:42:19 +00004208 SmallVector<OperandBundleDef, 1> OpBundles;
Sanjoy Dasc521c7b2015-11-25 00:42:24 +00004209 CS.getOperandBundlesAsDefs(OpBundles);
Sanjoy Das76293462015-11-25 00:42:19 +00004210
Reid Kleckner257cb4e2017-04-13 20:26:38 +00004211 CallSite NewCS;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004212 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Craig Topperbb4069e2017-07-07 23:16:26 +00004213 NewCS = Builder.CreateInvoke(Callee, II->getNormalDest(),
4214 II->getUnwindDest(), Args, OpBundles);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004215 } else {
Craig Topperbb4069e2017-07-07 23:16:26 +00004216 NewCS = Builder.CreateCall(Callee, Args, OpBundles);
Reid Kleckner257cb4e2017-04-13 20:26:38 +00004217 cast<CallInst>(NewCS.getInstruction())
4218 ->setTailCallKind(cast<CallInst>(Caller)->getTailCallKind());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004219 }
Reid Kleckner257cb4e2017-04-13 20:26:38 +00004220 NewCS->takeName(Caller);
4221 NewCS.setCallingConv(CS.getCallingConv());
4222 NewCS.setAttributes(NewCallerPAL);
4223
4224 // Preserve the weight metadata for the new call instruction. The metadata
4225 // is used by SamplePGO to check callsite's hotness.
4226 uint64_t W;
4227 if (Caller->extractProfTotalWeight(W))
4228 NewCS->setProfWeight(W);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004229
4230 // Insert a cast of the return type as necessary.
Reid Kleckner257cb4e2017-04-13 20:26:38 +00004231 Instruction *NC = NewCS.getInstruction();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004232 Value *NV = NC;
4233 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
4234 if (!NV->getType()->isVoidTy()) {
David Majnemer9b6b8222015-01-06 08:41:31 +00004235 NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy);
Eli Friedman35211c62011-05-27 00:19:40 +00004236 NC->setDebugLoc(Caller->getDebugLoc());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004237
4238 // If this is an invoke instruction, we should insert it after the first
4239 // non-phi, instruction in the normal successor block.
4240 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Bill Wendling07efd6f2011-08-25 01:08:34 +00004241 BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004242 InsertNewInstBefore(NC, *I);
4243 } else {
Chris Lattner73989652010-12-20 08:25:06 +00004244 // Otherwise, it's a call, just insert cast right after the call.
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004245 InsertNewInstBefore(NC, *Caller);
4246 }
4247 Worklist.AddUsersToWorkList(*Caller);
4248 } else {
4249 NV = UndefValue::get(Caller->getType());
4250 }
4251 }
4252
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004253 if (!Caller->use_empty())
Sanjay Patel4b198802016-02-01 22:23:39 +00004254 replaceInstUsesWith(*Caller, NV);
Frederic Rissc1892e22014-10-23 04:08:42 +00004255 else if (Caller->hasValueHandle()) {
4256 if (OldRetTy == NV->getType())
4257 ValueHandleBase::ValueIsRAUWd(Caller, NV);
4258 else
4259 // We cannot call ValueIsRAUWd with a different type, and the
4260 // actual tracked value will disappear.
4261 ValueHandleBase::ValueIsDeleted(Caller);
4262 }
Eli Friedmanb9ed18f2011-05-18 00:32:01 +00004263
Sanjay Patel4b198802016-02-01 22:23:39 +00004264 eraseInstFromFunction(*Caller);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004265 return true;
4266}
4267
Sanjay Patelcd4377c2016-01-20 22:24:38 +00004268/// Turn a call to a function created by init_trampoline / adjust_trampoline
4269/// intrinsic pair into a direct call to the underlying function.
Duncan Sandsa0984362011-09-06 13:37:06 +00004270Instruction *
4271InstCombiner::transformCallThroughTrampoline(CallSite CS,
4272 IntrinsicInst *Tramp) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004273 Value *Callee = CS.getCalledValue();
Chris Lattner229907c2011-07-18 04:54:35 +00004274 PointerType *PTy = cast<PointerType>(Callee->getType());
4275 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00004276 AttributeList Attrs = CS.getAttributes();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004277
4278 // If the call already has the 'nest' attribute somewhere then give up -
4279 // otherwise 'nest' would occur twice after splicing in the chain.
Bill Wendling6e95ae82012-12-31 00:49:59 +00004280 if (Attrs.hasAttrSomewhere(Attribute::Nest))
Craig Topperf40110f2014-04-25 05:29:35 +00004281 return nullptr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004282
Duncan Sandsa0984362011-09-06 13:37:06 +00004283 assert(Tramp &&
4284 "transformCallThroughTrampoline called with incorrect CallSite.");
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004285
Gabor Greif3e44ea12010-07-22 10:37:47 +00004286 Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00004287 FunctionType *NestFTy = cast<FunctionType>(NestF->getValueType());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004288
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00004289 AttributeList NestAttrs = NestF->getAttributes();
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004290 if (!NestAttrs.isEmpty()) {
Reid Klecknerf021fab2017-04-13 23:12:13 +00004291 unsigned NestArgNo = 0;
Craig Topperf40110f2014-04-25 05:29:35 +00004292 Type *NestTy = nullptr;
Reid Klecknerc2cb5602017-04-12 00:38:00 +00004293 AttributeSet NestAttr;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004294
4295 // Look for a parameter marked with the 'nest' attribute.
4296 for (FunctionType::param_iterator I = NestFTy->param_begin(),
Reid Klecknerf021fab2017-04-13 23:12:13 +00004297 E = NestFTy->param_end();
4298 I != E; ++NestArgNo, ++I) {
4299 AttributeSet AS = NestAttrs.getParamAttributes(NestArgNo);
4300 if (AS.hasAttribute(Attribute::Nest)) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004301 // Record the parameter type and any other attributes.
4302 NestTy = *I;
Reid Klecknerf021fab2017-04-13 23:12:13 +00004303 NestAttr = AS;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004304 break;
4305 }
Reid Klecknerf021fab2017-04-13 23:12:13 +00004306 }
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004307
4308 if (NestTy) {
4309 Instruction *Caller = CS.getInstruction();
4310 std::vector<Value*> NewArgs;
Reid Kleckner7f720332017-04-13 00:58:09 +00004311 std::vector<AttributeSet> NewArgAttrs;
Matt Arsenault5d2e85f2013-06-28 00:25:40 +00004312 NewArgs.reserve(CS.arg_size() + 1);
Reid Kleckner7f720332017-04-13 00:58:09 +00004313 NewArgAttrs.reserve(CS.arg_size());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004314
4315 // Insert the nest argument into the call argument list, which may
4316 // mean appending it. Likewise for attributes.
4317
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004318 {
Reid Klecknerf021fab2017-04-13 23:12:13 +00004319 unsigned ArgNo = 0;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004320 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
4321 do {
Reid Klecknerf021fab2017-04-13 23:12:13 +00004322 if (ArgNo == NestArgNo) {
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004323 // Add the chain argument and attributes.
Gabor Greif589a0b92010-06-24 12:58:35 +00004324 Value *NestVal = Tramp->getArgOperand(2);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004325 if (NestVal->getType() != NestTy)
Craig Topperbb4069e2017-07-07 23:16:26 +00004326 NestVal = Builder.CreateBitCast(NestVal, NestTy, "nest");
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004327 NewArgs.push_back(NestVal);
Reid Kleckner7f720332017-04-13 00:58:09 +00004328 NewArgAttrs.push_back(NestAttr);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004329 }
4330
4331 if (I == E)
4332 break;
4333
4334 // Add the original argument and attributes.
4335 NewArgs.push_back(*I);
Reid Klecknerf021fab2017-04-13 23:12:13 +00004336 NewArgAttrs.push_back(Attrs.getParamAttributes(ArgNo));
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004337
Reid Klecknerf021fab2017-04-13 23:12:13 +00004338 ++ArgNo;
Richard Trieu7a083812016-02-18 22:09:30 +00004339 ++I;
Eugene Zelenkocdc71612016-08-11 17:20:18 +00004340 } while (true);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004341 }
4342
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004343 // The trampoline may have been bitcast to a bogus type (FTy).
4344 // Handle this by synthesizing a new function type, equal to FTy
4345 // with the chain parameter inserted.
4346
Jay Foadb804a2b2011-07-12 14:06:48 +00004347 std::vector<Type*> NewTypes;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004348 NewTypes.reserve(FTy->getNumParams()+1);
4349
4350 // Insert the chain's type into the list of parameter types, which may
4351 // mean appending it.
4352 {
Reid Klecknerf021fab2017-04-13 23:12:13 +00004353 unsigned ArgNo = 0;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004354 FunctionType::param_iterator I = FTy->param_begin(),
4355 E = FTy->param_end();
4356
4357 do {
Reid Klecknerf021fab2017-04-13 23:12:13 +00004358 if (ArgNo == NestArgNo)
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004359 // Add the chain's type.
4360 NewTypes.push_back(NestTy);
4361
4362 if (I == E)
4363 break;
4364
4365 // Add the original type.
4366 NewTypes.push_back(*I);
4367
Reid Klecknerf021fab2017-04-13 23:12:13 +00004368 ++ArgNo;
Richard Trieu7a083812016-02-18 22:09:30 +00004369 ++I;
Eugene Zelenkocdc71612016-08-11 17:20:18 +00004370 } while (true);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004371 }
4372
4373 // Replace the trampoline call with a direct call. Let the generic
4374 // code sort out any function type mismatches.
Jim Grosbach7815f562012-02-03 00:07:04 +00004375 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004376 FTy->isVarArg());
4377 Constant *NewCallee =
4378 NestF->getType() == PointerType::getUnqual(NewFTy) ?
Jim Grosbach7815f562012-02-03 00:07:04 +00004379 NestF : ConstantExpr::getBitCast(NestF,
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004380 PointerType::getUnqual(NewFTy));
Reid Kleckner7f720332017-04-13 00:58:09 +00004381 AttributeList NewPAL =
4382 AttributeList::get(FTy->getContext(), Attrs.getFnAttributes(),
4383 Attrs.getRetAttributes(), NewArgAttrs);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004384
David Majnemer231a68c2016-04-29 08:07:20 +00004385 SmallVector<OperandBundleDef, 1> OpBundles;
4386 CS.getOperandBundlesAsDefs(OpBundles);
4387
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004388 Instruction *NewCaller;
4389 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
4390 NewCaller = InvokeInst::Create(NewCallee,
4391 II->getNormalDest(), II->getUnwindDest(),
David Majnemer231a68c2016-04-29 08:07:20 +00004392 NewArgs, OpBundles);
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004393 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
4394 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
4395 } else {
David Majnemer231a68c2016-04-29 08:07:20 +00004396 NewCaller = CallInst::Create(NewCallee, NewArgs, OpBundles);
David Majnemerd5648c72016-11-25 22:35:09 +00004397 cast<CallInst>(NewCaller)->setTailCallKind(
4398 cast<CallInst>(Caller)->getTailCallKind());
4399 cast<CallInst>(NewCaller)->setCallingConv(
4400 cast<CallInst>(Caller)->getCallingConv());
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004401 cast<CallInst>(NewCaller)->setAttributes(NewPAL);
4402 }
Eli Friedman49346012011-05-18 19:57:14 +00004403
4404 return NewCaller;
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004405 }
4406 }
4407
4408 // Replace the trampoline call with a direct call. Since there is no 'nest'
4409 // parameter, there is no need to adjust the argument list. Let the generic
4410 // code sort out any function type mismatches.
4411 Constant *NewCallee =
Jim Grosbach7815f562012-02-03 00:07:04 +00004412 NestF->getType() == PTy ? NestF :
Chris Lattner7a9e47a2010-01-05 07:32:13 +00004413 ConstantExpr::getBitCast(NestF, PTy);
4414 CS.setCalledFunction(NewCallee);
4415 return CS.getInstruction();
4416}