Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Loads.h" |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CallSite.h" |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 21 | #include "llvm/IR/PatternMatch.h" |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Statepoint.h" |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Chris Lattner | 6fcd32e | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/SimplifyLibCalls.h" |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 27 | using namespace PatternMatch; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 28 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "instcombine" |
| 30 | |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 31 | STATISTIC(NumSimplified, "Number of library calls simplified"); |
| 32 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 33 | /// Return the specified type promoted as it would be to pass though a va_arg |
| 34 | /// area. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 35 | static Type *getPromotedType(Type *Ty) { |
| 36 | if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 37 | if (ITy->getBitWidth() < 32) |
| 38 | return Type::getInt32Ty(Ty->getContext()); |
| 39 | } |
| 40 | return Ty; |
| 41 | } |
| 42 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 43 | /// Given an aggregate type which ultimately holds a single scalar element, |
| 44 | /// like {{{type}}} or [1 x type], return type. |
Dan Gohman | d0080c4 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 45 | static Type *reduceToSingleValueType(Type *T) { |
| 46 | while (!T->isSingleValueType()) { |
| 47 | if (StructType *STy = dyn_cast<StructType>(T)) { |
| 48 | if (STy->getNumElements() == 1) |
| 49 | T = STy->getElementType(0); |
| 50 | else |
| 51 | break; |
| 52 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(T)) { |
| 53 | if (ATy->getNumElements() == 1) |
| 54 | T = ATy->getElementType(); |
| 55 | else |
| 56 | break; |
| 57 | } else |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | return T; |
| 62 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 63 | |
Sanjay Patel | 368ac5d | 2016-02-21 17:29:33 +0000 | [diff] [blame] | 64 | /// Return a constant boolean vector that has true elements in all positions |
Sanjay Patel | 2440130 | 2016-02-21 17:33:31 +0000 | [diff] [blame] | 65 | /// where the input constant data vector has an element with the sign bit set. |
Sanjay Patel | 368ac5d | 2016-02-21 17:29:33 +0000 | [diff] [blame] | 66 | static Constant *getNegativeIsTrueBoolVec(ConstantDataVector *V) { |
| 67 | SmallVector<Constant *, 32> BoolVec; |
| 68 | IntegerType *BoolTy = Type::getInt1Ty(V->getContext()); |
| 69 | for (unsigned I = 0, E = V->getNumElements(); I != E; ++I) { |
| 70 | Constant *Elt = V->getElementAsConstant(I); |
| 71 | assert((isa<ConstantInt>(Elt) || isa<ConstantFP>(Elt)) && |
| 72 | "Unexpected constant data vector element type"); |
| 73 | bool Sign = V->getElementType()->isIntegerTy() |
| 74 | ? cast<ConstantInt>(Elt)->isNegative() |
| 75 | : cast<ConstantFP>(Elt)->isNegative(); |
| 76 | BoolVec.push_back(ConstantInt::get(BoolTy, Sign)); |
| 77 | } |
| 78 | return ConstantVector::get(BoolVec); |
| 79 | } |
| 80 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 81 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 82 | unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), DL, MI, &AC, &DT); |
| 83 | unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), DL, MI, &AC, &DT); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 84 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 85 | unsigned CopyAlign = MI->getAlignment(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 86 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 87 | if (CopyAlign < MinAlign) { |
| 88 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), MinAlign, false)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 89 | return MI; |
| 90 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 92 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 93 | // load/store. |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 94 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 95 | if (!MemOpLength) return nullptr; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 97 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 98 | // if the size is something we can handle with a single primitive load/store. |
| 99 | // A single load+store correctly handles overlapping memory in the memmove |
| 100 | // case. |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 101 | uint64_t Size = MemOpLength->getLimitedValue(); |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 102 | assert(Size && "0-sized memory transferring should be removed already."); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 104 | if (Size > 8 || (Size&(Size-1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 105 | return nullptr; // If not 1/2/4/8 bytes, exit. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 107 | // Use an integer load+store unless we can find something better. |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 108 | unsigned SrcAddrSp = |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 109 | cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace(); |
Gabor Greif | f375520 | 2010-04-16 15:33:14 +0000 | [diff] [blame] | 110 | unsigned DstAddrSp = |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 111 | cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace(); |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 113 | IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3); |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 114 | Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp); |
| 115 | Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 117 | // Memcpy forces the use of i8* for the source and destination. That means |
| 118 | // that if you're using memcpy to move one double around, you'll get a cast |
| 119 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 120 | // an i64 load+store, here because this improves the odds that the source or |
| 121 | // dest address will be promotable. See if we can find a better type than the |
| 122 | // integer datatype. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 123 | Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 124 | MDNode *CopyMD = nullptr; |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 125 | if (StrippedDest != MI->getArgOperand(0)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 126 | Type *SrcETy = cast<PointerType>(StrippedDest->getType()) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 127 | ->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 128 | if (SrcETy->isSized() && DL.getTypeStoreSize(SrcETy) == Size) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 129 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 130 | // down through these levels if so. |
Dan Gohman | d0080c4 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 131 | SrcETy = reduceToSingleValueType(SrcETy); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 132 | |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 133 | if (SrcETy->isSingleValueType()) { |
| 134 | NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp); |
| 135 | NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 136 | |
| 137 | // If the memcpy has metadata describing the members, see if we can |
| 138 | // get the TBAA tag describing our copy. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 139 | if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 140 | if (M->getNumOperands() == 3 && M->getOperand(0) && |
| 141 | mdconst::hasa<ConstantInt>(M->getOperand(0)) && |
| 142 | mdconst::extract<ConstantInt>(M->getOperand(0))->isNullValue() && |
Nick Lewycky | 49ac81a | 2012-10-11 02:05:23 +0000 | [diff] [blame] | 143 | M->getOperand(1) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 144 | mdconst::hasa<ConstantInt>(M->getOperand(1)) && |
| 145 | mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() == |
| 146 | Size && |
| 147 | M->getOperand(2) && isa<MDNode>(M->getOperand(2))) |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 148 | CopyMD = cast<MDNode>(M->getOperand(2)); |
| 149 | } |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 154 | // If the memcpy/memmove provides better alignment info than we can |
| 155 | // infer, use it. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 156 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 157 | DstAlign = std::max(DstAlign, CopyAlign); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 158 | |
Gabor Greif | 5f3e656 | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 159 | Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy); |
| 160 | Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy); |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 161 | LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile()); |
| 162 | L->setAlignment(SrcAlign); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 163 | if (CopyMD) |
| 164 | L->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 165 | StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile()); |
| 166 | S->setAlignment(DstAlign); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 167 | if (CopyMD) |
| 168 | S->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 169 | |
| 170 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 171 | MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 172 | return MI; |
| 173 | } |
| 174 | |
| 175 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 176 | unsigned Alignment = getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 177 | if (MI->getAlignment() < Alignment) { |
| 178 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
| 179 | Alignment, false)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 180 | return MI; |
| 181 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 183 | // Extract the length and alignment and fill if they are constant. |
| 184 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 185 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 186 | if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 187 | return nullptr; |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 188 | uint64_t Len = LenC->getLimitedValue(); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 189 | Alignment = MI->getAlignment(); |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 190 | assert(Len && "0-sized memory setting should be removed already."); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 192 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 193 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 194 | Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 196 | Value *Dest = MI->getDest(); |
Mon P Wang | 1991c47 | 2010-12-20 01:05:30 +0000 | [diff] [blame] | 197 | unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace(); |
| 198 | Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp); |
| 199 | Dest = Builder->CreateBitCast(Dest, NewDstPtrTy); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 200 | |
| 201 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 202 | if (Alignment == 0) Alignment = 1; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 204 | // Extract the fill value and store. |
| 205 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 206 | StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest, |
| 207 | MI->isVolatile()); |
| 208 | S->setAlignment(Alignment); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 210 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 211 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 212 | return MI; |
| 213 | } |
| 214 | |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 215 | return nullptr; |
| 216 | } |
| 217 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 218 | static Value *simplifyX86immShift(const IntrinsicInst &II, |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 219 | InstCombiner::BuilderTy &Builder) { |
| 220 | bool LogicalShift = false; |
| 221 | bool ShiftLeft = false; |
| 222 | |
| 223 | switch (II.getIntrinsicID()) { |
| 224 | default: |
| 225 | return nullptr; |
| 226 | case Intrinsic::x86_sse2_psra_d: |
| 227 | case Intrinsic::x86_sse2_psra_w: |
| 228 | case Intrinsic::x86_sse2_psrai_d: |
| 229 | case Intrinsic::x86_sse2_psrai_w: |
| 230 | case Intrinsic::x86_avx2_psra_d: |
| 231 | case Intrinsic::x86_avx2_psra_w: |
| 232 | case Intrinsic::x86_avx2_psrai_d: |
| 233 | case Intrinsic::x86_avx2_psrai_w: |
| 234 | LogicalShift = false; ShiftLeft = false; |
| 235 | break; |
| 236 | case Intrinsic::x86_sse2_psrl_d: |
| 237 | case Intrinsic::x86_sse2_psrl_q: |
| 238 | case Intrinsic::x86_sse2_psrl_w: |
| 239 | case Intrinsic::x86_sse2_psrli_d: |
| 240 | case Intrinsic::x86_sse2_psrli_q: |
| 241 | case Intrinsic::x86_sse2_psrli_w: |
| 242 | case Intrinsic::x86_avx2_psrl_d: |
| 243 | case Intrinsic::x86_avx2_psrl_q: |
| 244 | case Intrinsic::x86_avx2_psrl_w: |
| 245 | case Intrinsic::x86_avx2_psrli_d: |
| 246 | case Intrinsic::x86_avx2_psrli_q: |
| 247 | case Intrinsic::x86_avx2_psrli_w: |
| 248 | LogicalShift = true; ShiftLeft = false; |
| 249 | break; |
| 250 | case Intrinsic::x86_sse2_psll_d: |
| 251 | case Intrinsic::x86_sse2_psll_q: |
| 252 | case Intrinsic::x86_sse2_psll_w: |
| 253 | case Intrinsic::x86_sse2_pslli_d: |
| 254 | case Intrinsic::x86_sse2_pslli_q: |
| 255 | case Intrinsic::x86_sse2_pslli_w: |
| 256 | case Intrinsic::x86_avx2_psll_d: |
| 257 | case Intrinsic::x86_avx2_psll_q: |
| 258 | case Intrinsic::x86_avx2_psll_w: |
| 259 | case Intrinsic::x86_avx2_pslli_d: |
| 260 | case Intrinsic::x86_avx2_pslli_q: |
| 261 | case Intrinsic::x86_avx2_pslli_w: |
| 262 | LogicalShift = true; ShiftLeft = true; |
| 263 | break; |
| 264 | } |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 265 | assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); |
| 266 | |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 267 | // Simplify if count is constant. |
| 268 | auto Arg1 = II.getArgOperand(1); |
| 269 | auto CAZ = dyn_cast<ConstantAggregateZero>(Arg1); |
| 270 | auto CDV = dyn_cast<ConstantDataVector>(Arg1); |
| 271 | auto CInt = dyn_cast<ConstantInt>(Arg1); |
| 272 | if (!CAZ && !CDV && !CInt) |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 273 | return nullptr; |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 274 | |
| 275 | APInt Count(64, 0); |
| 276 | if (CDV) { |
| 277 | // SSE2/AVX2 uses all the first 64-bits of the 128-bit vector |
| 278 | // operand to compute the shift amount. |
| 279 | auto VT = cast<VectorType>(CDV->getType()); |
| 280 | unsigned BitWidth = VT->getElementType()->getPrimitiveSizeInBits(); |
| 281 | assert((64 % BitWidth) == 0 && "Unexpected packed shift size"); |
| 282 | unsigned NumSubElts = 64 / BitWidth; |
| 283 | |
| 284 | // Concatenate the sub-elements to create the 64-bit value. |
| 285 | for (unsigned i = 0; i != NumSubElts; ++i) { |
| 286 | unsigned SubEltIdx = (NumSubElts - 1) - i; |
| 287 | auto SubElt = cast<ConstantInt>(CDV->getElementAsConstant(SubEltIdx)); |
| 288 | Count = Count.shl(BitWidth); |
| 289 | Count |= SubElt->getValue().zextOrTrunc(64); |
| 290 | } |
| 291 | } |
| 292 | else if (CInt) |
| 293 | Count = CInt->getValue(); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 294 | |
| 295 | auto Vec = II.getArgOperand(0); |
| 296 | auto VT = cast<VectorType>(Vec->getType()); |
| 297 | auto SVT = VT->getElementType(); |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 298 | unsigned VWidth = VT->getNumElements(); |
| 299 | unsigned BitWidth = SVT->getPrimitiveSizeInBits(); |
| 300 | |
| 301 | // If shift-by-zero then just return the original value. |
| 302 | if (Count == 0) |
| 303 | return Vec; |
| 304 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 305 | // Handle cases when Shift >= BitWidth. |
| 306 | if (Count.uge(BitWidth)) { |
| 307 | // If LogicalShift - just return zero. |
| 308 | if (LogicalShift) |
| 309 | return ConstantAggregateZero::get(VT); |
| 310 | |
| 311 | // If ArithmeticShift - clamp Shift to (BitWidth - 1). |
| 312 | Count = APInt(64, BitWidth - 1); |
| 313 | } |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 314 | |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 315 | // Get a constant vector of the same type as the first operand. |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 316 | auto ShiftAmt = ConstantInt::get(SVT, Count.zextOrTrunc(BitWidth)); |
| 317 | auto ShiftVec = Builder.CreateVectorSplat(VWidth, ShiftAmt); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 318 | |
| 319 | if (ShiftLeft) |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 320 | return Builder.CreateShl(Vec, ShiftVec); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 321 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 322 | if (LogicalShift) |
| 323 | return Builder.CreateLShr(Vec, ShiftVec); |
| 324 | |
| 325 | return Builder.CreateAShr(Vec, ShiftVec); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 328 | // Attempt to simplify AVX2 per-element shift intrinsics to a generic IR shift. |
| 329 | // Unlike the generic IR shifts, the intrinsics have defined behaviour for out |
| 330 | // of range shift amounts (logical - set to zero, arithmetic - splat sign bit). |
| 331 | static Value *simplifyX86varShift(const IntrinsicInst &II, |
| 332 | InstCombiner::BuilderTy &Builder) { |
| 333 | bool LogicalShift = false; |
| 334 | bool ShiftLeft = false; |
| 335 | |
| 336 | switch (II.getIntrinsicID()) { |
| 337 | default: |
| 338 | return nullptr; |
| 339 | case Intrinsic::x86_avx2_psrav_d: |
| 340 | case Intrinsic::x86_avx2_psrav_d_256: |
| 341 | LogicalShift = false; |
| 342 | ShiftLeft = false; |
| 343 | break; |
| 344 | case Intrinsic::x86_avx2_psrlv_d: |
| 345 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 346 | case Intrinsic::x86_avx2_psrlv_q: |
| 347 | case Intrinsic::x86_avx2_psrlv_q_256: |
| 348 | LogicalShift = true; |
| 349 | ShiftLeft = false; |
| 350 | break; |
| 351 | case Intrinsic::x86_avx2_psllv_d: |
| 352 | case Intrinsic::x86_avx2_psllv_d_256: |
| 353 | case Intrinsic::x86_avx2_psllv_q: |
| 354 | case Intrinsic::x86_avx2_psllv_q_256: |
| 355 | LogicalShift = true; |
| 356 | ShiftLeft = true; |
| 357 | break; |
| 358 | } |
| 359 | assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); |
| 360 | |
| 361 | // Simplify if all shift amounts are constant/undef. |
| 362 | auto *CShift = dyn_cast<Constant>(II.getArgOperand(1)); |
| 363 | if (!CShift) |
| 364 | return nullptr; |
| 365 | |
| 366 | auto Vec = II.getArgOperand(0); |
| 367 | auto VT = cast<VectorType>(II.getType()); |
| 368 | auto SVT = VT->getVectorElementType(); |
| 369 | int NumElts = VT->getNumElements(); |
| 370 | int BitWidth = SVT->getIntegerBitWidth(); |
| 371 | |
| 372 | // Collect each element's shift amount. |
| 373 | // We also collect special cases: UNDEF = -1, OUT-OF-RANGE = BitWidth. |
| 374 | bool AnyOutOfRange = false; |
| 375 | SmallVector<int, 8> ShiftAmts; |
| 376 | for (int I = 0; I < NumElts; ++I) { |
| 377 | auto *CElt = CShift->getAggregateElement(I); |
| 378 | if (CElt && isa<UndefValue>(CElt)) { |
| 379 | ShiftAmts.push_back(-1); |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | auto *COp = dyn_cast_or_null<ConstantInt>(CElt); |
| 384 | if (!COp) |
| 385 | return nullptr; |
| 386 | |
| 387 | // Handle out of range shifts. |
| 388 | // If LogicalShift - set to BitWidth (special case). |
| 389 | // If ArithmeticShift - set to (BitWidth - 1) (sign splat). |
| 390 | APInt ShiftVal = COp->getValue(); |
| 391 | if (ShiftVal.uge(BitWidth)) { |
| 392 | AnyOutOfRange = LogicalShift; |
| 393 | ShiftAmts.push_back(LogicalShift ? BitWidth : BitWidth - 1); |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | ShiftAmts.push_back((int)ShiftVal.getZExtValue()); |
| 398 | } |
| 399 | |
| 400 | // If all elements out of range or UNDEF, return vector of zeros/undefs. |
| 401 | // ArithmeticShift should only hit this if they are all UNDEF. |
| 402 | auto OutOfRange = [&](int Idx) { return (Idx < 0) || (BitWidth <= Idx); }; |
| 403 | if (llvm::all_of(ShiftAmts, OutOfRange)) { |
| 404 | SmallVector<Constant *, 8> ConstantVec; |
| 405 | for (int Idx : ShiftAmts) { |
| 406 | if (Idx < 0) { |
| 407 | ConstantVec.push_back(UndefValue::get(SVT)); |
| 408 | } else { |
| 409 | assert(LogicalShift && "Logical shift expected"); |
| 410 | ConstantVec.push_back(ConstantInt::getNullValue(SVT)); |
| 411 | } |
| 412 | } |
| 413 | return ConstantVector::get(ConstantVec); |
| 414 | } |
| 415 | |
| 416 | // We can't handle only some out of range values with generic logical shifts. |
| 417 | if (AnyOutOfRange) |
| 418 | return nullptr; |
| 419 | |
| 420 | // Build the shift amount constant vector. |
| 421 | SmallVector<Constant *, 8> ShiftVecAmts; |
| 422 | for (int Idx : ShiftAmts) { |
| 423 | if (Idx < 0) |
| 424 | ShiftVecAmts.push_back(UndefValue::get(SVT)); |
| 425 | else |
| 426 | ShiftVecAmts.push_back(ConstantInt::get(SVT, Idx)); |
| 427 | } |
| 428 | auto ShiftVec = ConstantVector::get(ShiftVecAmts); |
| 429 | |
| 430 | if (ShiftLeft) |
| 431 | return Builder.CreateShl(Vec, ShiftVec); |
| 432 | |
| 433 | if (LogicalShift) |
| 434 | return Builder.CreateLShr(Vec, ShiftVec); |
| 435 | |
| 436 | return Builder.CreateAShr(Vec, ShiftVec); |
| 437 | } |
| 438 | |
Simon Pilgrim | 91e3ac8 | 2016-06-07 08:18:35 +0000 | [diff] [blame] | 439 | static Value *simplifyX86movmsk(const IntrinsicInst &II, |
| 440 | InstCombiner::BuilderTy &Builder) { |
| 441 | Value *Arg = II.getArgOperand(0); |
| 442 | Type *ResTy = II.getType(); |
| 443 | Type *ArgTy = Arg->getType(); |
| 444 | |
| 445 | // movmsk(undef) -> zero as we must ensure the upper bits are zero. |
| 446 | if (isa<UndefValue>(Arg)) |
| 447 | return Constant::getNullValue(ResTy); |
| 448 | |
| 449 | // We can't easily peek through x86_mmx types. |
| 450 | if (!ArgTy->isVectorTy()) |
| 451 | return nullptr; |
| 452 | |
| 453 | auto *C = dyn_cast<Constant>(Arg); |
| 454 | if (!C) |
| 455 | return nullptr; |
| 456 | |
| 457 | // Extract signbits of the vector input and pack into integer result. |
| 458 | APInt Result(ResTy->getPrimitiveSizeInBits(), 0); |
| 459 | for (unsigned I = 0, E = ArgTy->getVectorNumElements(); I != E; ++I) { |
| 460 | auto *COp = C->getAggregateElement(I); |
| 461 | if (!COp) |
| 462 | return nullptr; |
| 463 | if (isa<UndefValue>(COp)) |
| 464 | continue; |
| 465 | |
| 466 | auto *CInt = dyn_cast<ConstantInt>(COp); |
| 467 | auto *CFp = dyn_cast<ConstantFP>(COp); |
| 468 | if (!CInt && !CFp) |
| 469 | return nullptr; |
| 470 | |
| 471 | if ((CInt && CInt->isNegative()) || (CFp && CFp->isNegative())) |
| 472 | Result.setBit(I); |
| 473 | } |
| 474 | |
| 475 | return Constant::getIntegerValue(ResTy, Result); |
| 476 | } |
| 477 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 478 | static Value *simplifyX86insertps(const IntrinsicInst &II, |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 479 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 480 | auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2)); |
| 481 | if (!CInt) |
| 482 | return nullptr; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 483 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 484 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 485 | assert(VecTy->getNumElements() == 4 && "insertps with wrong vector type"); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 486 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 487 | // The immediate permute control byte looks like this: |
| 488 | // [3:0] - zero mask for each 32-bit lane |
| 489 | // [5:4] - select one 32-bit destination lane |
| 490 | // [7:6] - select one 32-bit source lane |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 491 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 492 | uint8_t Imm = CInt->getZExtValue(); |
| 493 | uint8_t ZMask = Imm & 0xf; |
| 494 | uint8_t DestLane = (Imm >> 4) & 0x3; |
| 495 | uint8_t SourceLane = (Imm >> 6) & 0x3; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 496 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 497 | ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 498 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 499 | // If all zero mask bits are set, this was just a weird way to |
| 500 | // generate a zero vector. |
| 501 | if (ZMask == 0xf) |
| 502 | return ZeroVector; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 503 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 504 | // Initialize by passing all of the first source bits through. |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 505 | uint32_t ShuffleMask[4] = { 0, 1, 2, 3 }; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 506 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 507 | // We may replace the second operand with the zero vector. |
| 508 | Value *V1 = II.getArgOperand(1); |
| 509 | |
| 510 | if (ZMask) { |
| 511 | // If the zero mask is being used with a single input or the zero mask |
| 512 | // overrides the destination lane, this is a shuffle with the zero vector. |
| 513 | if ((II.getArgOperand(0) == II.getArgOperand(1)) || |
| 514 | (ZMask & (1 << DestLane))) { |
| 515 | V1 = ZeroVector; |
| 516 | // We may still move 32-bits of the first source vector from one lane |
| 517 | // to another. |
| 518 | ShuffleMask[DestLane] = SourceLane; |
| 519 | // The zero mask may override the previous insert operation. |
| 520 | for (unsigned i = 0; i < 4; ++i) |
| 521 | if ((ZMask >> i) & 0x1) |
| 522 | ShuffleMask[i] = i + 4; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 523 | } else { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 524 | // TODO: Model this case as 2 shuffles or a 'logical and' plus shuffle? |
| 525 | return nullptr; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 526 | } |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 527 | } else { |
| 528 | // Replace the selected destination lane with the selected source lane. |
| 529 | ShuffleMask[DestLane] = SourceLane + 4; |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 530 | } |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 531 | |
| 532 | return Builder.CreateShuffleVector(II.getArgOperand(0), V1, ShuffleMask); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 535 | /// Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding |
| 536 | /// or conversion to a shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 537 | static Value *simplifyX86extrq(IntrinsicInst &II, Value *Op0, |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 538 | ConstantInt *CILength, ConstantInt *CIIndex, |
| 539 | InstCombiner::BuilderTy &Builder) { |
| 540 | auto LowConstantHighUndef = [&](uint64_t Val) { |
| 541 | Type *IntTy64 = Type::getInt64Ty(II.getContext()); |
| 542 | Constant *Args[] = {ConstantInt::get(IntTy64, Val), |
| 543 | UndefValue::get(IntTy64)}; |
| 544 | return ConstantVector::get(Args); |
| 545 | }; |
| 546 | |
| 547 | // See if we're dealing with constant values. |
| 548 | Constant *C0 = dyn_cast<Constant>(Op0); |
| 549 | ConstantInt *CI0 = |
| 550 | C0 ? dyn_cast<ConstantInt>(C0->getAggregateElement((unsigned)0)) |
| 551 | : nullptr; |
| 552 | |
| 553 | // Attempt to constant fold. |
| 554 | if (CILength && CIIndex) { |
| 555 | // From AMD documentation: "The bit index and field length are each six |
| 556 | // bits in length other bits of the field are ignored." |
| 557 | APInt APIndex = CIIndex->getValue().zextOrTrunc(6); |
| 558 | APInt APLength = CILength->getValue().zextOrTrunc(6); |
| 559 | |
| 560 | unsigned Index = APIndex.getZExtValue(); |
| 561 | |
| 562 | // From AMD documentation: "a value of zero in the field length is |
| 563 | // defined as length of 64". |
| 564 | unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); |
| 565 | |
| 566 | // From AMD documentation: "If the sum of the bit index + length field |
| 567 | // is greater than 64, the results are undefined". |
| 568 | unsigned End = Index + Length; |
| 569 | |
| 570 | // Note that both field index and field length are 8-bit quantities. |
| 571 | // Since variables 'Index' and 'Length' are unsigned values |
| 572 | // obtained from zero-extending field index and field length |
| 573 | // respectively, their sum should never wrap around. |
| 574 | if (End > 64) |
| 575 | return UndefValue::get(II.getType()); |
| 576 | |
| 577 | // If we are inserting whole bytes, we can convert this to a shuffle. |
| 578 | // Lowering can recognize EXTRQI shuffle masks. |
| 579 | if ((Length % 8) == 0 && (Index % 8) == 0) { |
| 580 | // Convert bit indices to byte indices. |
| 581 | Length /= 8; |
| 582 | Index /= 8; |
| 583 | |
| 584 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 585 | Type *IntTy32 = Type::getInt32Ty(II.getContext()); |
| 586 | VectorType *ShufTy = VectorType::get(IntTy8, 16); |
| 587 | |
| 588 | SmallVector<Constant *, 16> ShuffleMask; |
| 589 | for (int i = 0; i != (int)Length; ++i) |
| 590 | ShuffleMask.push_back( |
| 591 | Constant::getIntegerValue(IntTy32, APInt(32, i + Index))); |
| 592 | for (int i = Length; i != 8; ++i) |
| 593 | ShuffleMask.push_back( |
| 594 | Constant::getIntegerValue(IntTy32, APInt(32, i + 16))); |
| 595 | for (int i = 8; i != 16; ++i) |
| 596 | ShuffleMask.push_back(UndefValue::get(IntTy32)); |
| 597 | |
| 598 | Value *SV = Builder.CreateShuffleVector( |
| 599 | Builder.CreateBitCast(Op0, ShufTy), |
| 600 | ConstantAggregateZero::get(ShufTy), ConstantVector::get(ShuffleMask)); |
| 601 | return Builder.CreateBitCast(SV, II.getType()); |
| 602 | } |
| 603 | |
| 604 | // Constant Fold - shift Index'th bit to lowest position and mask off |
| 605 | // Length bits. |
| 606 | if (CI0) { |
| 607 | APInt Elt = CI0->getValue(); |
| 608 | Elt = Elt.lshr(Index).zextOrTrunc(Length); |
| 609 | return LowConstantHighUndef(Elt.getZExtValue()); |
| 610 | } |
| 611 | |
| 612 | // If we were an EXTRQ call, we'll save registers if we convert to EXTRQI. |
| 613 | if (II.getIntrinsicID() == Intrinsic::x86_sse4a_extrq) { |
| 614 | Value *Args[] = {Op0, CILength, CIIndex}; |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 615 | Module *M = II.getModule(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 616 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_extrqi); |
| 617 | return Builder.CreateCall(F, Args); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Constant Fold - extraction from zero is always {zero, undef}. |
| 622 | if (CI0 && CI0->equalsInt(0)) |
| 623 | return LowConstantHighUndef(0); |
| 624 | |
| 625 | return nullptr; |
| 626 | } |
| 627 | |
| 628 | /// Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant |
| 629 | /// folding or conversion to a shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 630 | static Value *simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1, |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 631 | APInt APLength, APInt APIndex, |
| 632 | InstCombiner::BuilderTy &Builder) { |
| 633 | |
| 634 | // From AMD documentation: "The bit index and field length are each six bits |
| 635 | // in length other bits of the field are ignored." |
| 636 | APIndex = APIndex.zextOrTrunc(6); |
| 637 | APLength = APLength.zextOrTrunc(6); |
| 638 | |
| 639 | // Attempt to constant fold. |
| 640 | unsigned Index = APIndex.getZExtValue(); |
| 641 | |
| 642 | // From AMD documentation: "a value of zero in the field length is |
| 643 | // defined as length of 64". |
| 644 | unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); |
| 645 | |
| 646 | // From AMD documentation: "If the sum of the bit index + length field |
| 647 | // is greater than 64, the results are undefined". |
| 648 | unsigned End = Index + Length; |
| 649 | |
| 650 | // Note that both field index and field length are 8-bit quantities. |
| 651 | // Since variables 'Index' and 'Length' are unsigned values |
| 652 | // obtained from zero-extending field index and field length |
| 653 | // respectively, their sum should never wrap around. |
| 654 | if (End > 64) |
| 655 | return UndefValue::get(II.getType()); |
| 656 | |
| 657 | // If we are inserting whole bytes, we can convert this to a shuffle. |
| 658 | // Lowering can recognize INSERTQI shuffle masks. |
| 659 | if ((Length % 8) == 0 && (Index % 8) == 0) { |
| 660 | // Convert bit indices to byte indices. |
| 661 | Length /= 8; |
| 662 | Index /= 8; |
| 663 | |
| 664 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 665 | Type *IntTy32 = Type::getInt32Ty(II.getContext()); |
| 666 | VectorType *ShufTy = VectorType::get(IntTy8, 16); |
| 667 | |
| 668 | SmallVector<Constant *, 16> ShuffleMask; |
| 669 | for (int i = 0; i != (int)Index; ++i) |
| 670 | ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i))); |
| 671 | for (int i = 0; i != (int)Length; ++i) |
| 672 | ShuffleMask.push_back( |
| 673 | Constant::getIntegerValue(IntTy32, APInt(32, i + 16))); |
| 674 | for (int i = Index + Length; i != 8; ++i) |
| 675 | ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i))); |
| 676 | for (int i = 8; i != 16; ++i) |
| 677 | ShuffleMask.push_back(UndefValue::get(IntTy32)); |
| 678 | |
| 679 | Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy), |
| 680 | Builder.CreateBitCast(Op1, ShufTy), |
| 681 | ConstantVector::get(ShuffleMask)); |
| 682 | return Builder.CreateBitCast(SV, II.getType()); |
| 683 | } |
| 684 | |
| 685 | // See if we're dealing with constant values. |
| 686 | Constant *C0 = dyn_cast<Constant>(Op0); |
| 687 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 688 | ConstantInt *CI00 = |
| 689 | C0 ? dyn_cast<ConstantInt>(C0->getAggregateElement((unsigned)0)) |
| 690 | : nullptr; |
| 691 | ConstantInt *CI10 = |
| 692 | C1 ? dyn_cast<ConstantInt>(C1->getAggregateElement((unsigned)0)) |
| 693 | : nullptr; |
| 694 | |
| 695 | // Constant Fold - insert bottom Length bits starting at the Index'th bit. |
| 696 | if (CI00 && CI10) { |
| 697 | APInt V00 = CI00->getValue(); |
| 698 | APInt V10 = CI10->getValue(); |
| 699 | APInt Mask = APInt::getLowBitsSet(64, Length).shl(Index); |
| 700 | V00 = V00 & ~Mask; |
| 701 | V10 = V10.zextOrTrunc(Length).zextOrTrunc(64).shl(Index); |
| 702 | APInt Val = V00 | V10; |
| 703 | Type *IntTy64 = Type::getInt64Ty(II.getContext()); |
| 704 | Constant *Args[] = {ConstantInt::get(IntTy64, Val.getZExtValue()), |
| 705 | UndefValue::get(IntTy64)}; |
| 706 | return ConstantVector::get(Args); |
| 707 | } |
| 708 | |
| 709 | // If we were an INSERTQ call, we'll save demanded elements if we convert to |
| 710 | // INSERTQI. |
| 711 | if (II.getIntrinsicID() == Intrinsic::x86_sse4a_insertq) { |
| 712 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 713 | Constant *CILength = ConstantInt::get(IntTy8, Length, false); |
| 714 | Constant *CIIndex = ConstantInt::get(IntTy8, Index, false); |
| 715 | |
| 716 | Value *Args[] = {Op0, Op1, CILength, CIIndex}; |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 717 | Module *M = II.getModule(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 718 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_insertqi); |
| 719 | return Builder.CreateCall(F, Args); |
| 720 | } |
| 721 | |
| 722 | return nullptr; |
| 723 | } |
| 724 | |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 725 | /// Attempt to convert pshufb* to shufflevector if the mask is constant. |
| 726 | static Value *simplifyX86pshufb(const IntrinsicInst &II, |
| 727 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 728 | Constant *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 729 | if (!V) |
| 730 | return nullptr; |
| 731 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 732 | auto *VecTy = cast<VectorType>(II.getType()); |
| 733 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
| 734 | unsigned NumElts = VecTy->getNumElements(); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 735 | assert((NumElts == 16 || NumElts == 32) && |
| 736 | "Unexpected number of elements in shuffle mask!"); |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 737 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 738 | // Construct a shuffle mask from constant integers or UNDEFs. |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 739 | Constant *Indexes[32] = {NULL}; |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 740 | |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 741 | // Each byte in the shuffle control mask forms an index to permute the |
| 742 | // corresponding byte in the destination operand. |
| 743 | for (unsigned I = 0; I < NumElts; ++I) { |
| 744 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 745 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 746 | return nullptr; |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 747 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 748 | if (isa<UndefValue>(COp)) { |
| 749 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 750 | continue; |
| 751 | } |
| 752 | |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 753 | int8_t Index = cast<ConstantInt>(COp)->getValue().getZExtValue(); |
| 754 | |
| 755 | // If the most significant bit (bit[7]) of each byte of the shuffle |
| 756 | // control mask is set, then zero is written in the result byte. |
| 757 | // The zero vector is in the right-hand side of the resulting |
| 758 | // shufflevector. |
| 759 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 760 | // The value of each index for the high 128-bit lane is the least |
| 761 | // significant 4 bits of the respective shuffle control byte. |
| 762 | Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0); |
| 763 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 764 | } |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 765 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 766 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts)); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 767 | auto V1 = II.getArgOperand(0); |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 768 | auto V2 = Constant::getNullValue(VecTy); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 769 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 770 | } |
| 771 | |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 772 | /// Attempt to convert vpermilvar* to shufflevector if the mask is constant. |
| 773 | static Value *simplifyX86vpermilvar(const IntrinsicInst &II, |
| 774 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 775 | Constant *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 776 | if (!V) |
| 777 | return nullptr; |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 778 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 779 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
| 780 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
| 781 | assert(NumElts == 8 || NumElts == 4 || NumElts == 2); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 782 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 783 | // Construct a shuffle mask from constant integers or UNDEFs. |
| 784 | Constant *Indexes[8] = {NULL}; |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 785 | |
| 786 | // The intrinsics only read one or two bits, clear the rest. |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 787 | for (unsigned I = 0; I < NumElts; ++I) { |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 788 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 789 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 790 | return nullptr; |
| 791 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 792 | if (isa<UndefValue>(COp)) { |
| 793 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 794 | continue; |
| 795 | } |
| 796 | |
| 797 | APInt Index = cast<ConstantInt>(COp)->getValue(); |
| 798 | Index = Index.zextOrTrunc(32).getLoBits(2); |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 799 | |
| 800 | // The PD variants uses bit 1 to select per-lane element index, so |
| 801 | // shift down to convert to generic shuffle mask index. |
| 802 | if (II.getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd || |
| 803 | II.getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd_256) |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 804 | Index = Index.lshr(1); |
| 805 | |
| 806 | // The _256 variants are a bit trickier since the mask bits always index |
| 807 | // into the corresponding 128 half. In order to convert to a generic |
| 808 | // shuffle, we have to make that explicit. |
| 809 | if ((II.getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_ps_256 || |
| 810 | II.getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd_256) && |
| 811 | ((NumElts / 2) <= I)) { |
| 812 | Index += APInt(32, NumElts / 2); |
| 813 | } |
| 814 | |
| 815 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 818 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts)); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 819 | auto V1 = II.getArgOperand(0); |
| 820 | auto V2 = UndefValue::get(V1->getType()); |
| 821 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 822 | } |
| 823 | |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 824 | /// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant. |
| 825 | static Value *simplifyX86vpermv(const IntrinsicInst &II, |
| 826 | InstCombiner::BuilderTy &Builder) { |
| 827 | auto *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 828 | if (!V) |
| 829 | return nullptr; |
| 830 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 831 | auto *VecTy = cast<VectorType>(II.getType()); |
| 832 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 833 | unsigned Size = VecTy->getNumElements(); |
| 834 | assert(Size == 8 && "Unexpected shuffle mask size"); |
| 835 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 836 | // Construct a shuffle mask from constant integers or UNDEFs. |
| 837 | Constant *Indexes[8] = {NULL}; |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 838 | |
| 839 | for (unsigned I = 0; I < Size; ++I) { |
| 840 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 841 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 842 | return nullptr; |
| 843 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 844 | if (isa<UndefValue>(COp)) { |
| 845 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 846 | continue; |
| 847 | } |
| 848 | |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 849 | APInt Index = cast<ConstantInt>(COp)->getValue(); |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 850 | Index = Index.zextOrTrunc(32).getLoBits(3); |
| 851 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 854 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, Size)); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 855 | auto V1 = II.getArgOperand(0); |
| 856 | auto V2 = UndefValue::get(VecTy); |
| 857 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 858 | } |
| 859 | |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 860 | /// The shuffle mask for a perm2*128 selects any two halves of two 256-bit |
| 861 | /// source vectors, unless a zero bit is set. If a zero bit is set, |
| 862 | /// then ignore that half of the mask and clear that half of the vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 863 | static Value *simplifyX86vperm2(const IntrinsicInst &II, |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 864 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 865 | auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2)); |
| 866 | if (!CInt) |
| 867 | return nullptr; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 868 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 869 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 870 | ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy); |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 871 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 872 | // The immediate permute control byte looks like this: |
| 873 | // [1:0] - select 128 bits from sources for low half of destination |
| 874 | // [2] - ignore |
| 875 | // [3] - zero low half of destination |
| 876 | // [5:4] - select 128 bits from sources for high half of destination |
| 877 | // [6] - ignore |
| 878 | // [7] - zero high half of destination |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 879 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 880 | uint8_t Imm = CInt->getZExtValue(); |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 881 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 882 | bool LowHalfZero = Imm & 0x08; |
| 883 | bool HighHalfZero = Imm & 0x80; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 884 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 885 | // If both zero mask bits are set, this was just a weird way to |
| 886 | // generate a zero vector. |
| 887 | if (LowHalfZero && HighHalfZero) |
| 888 | return ZeroVector; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 889 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 890 | // If 0 or 1 zero mask bits are set, this is a simple shuffle. |
| 891 | unsigned NumElts = VecTy->getNumElements(); |
| 892 | unsigned HalfSize = NumElts / 2; |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 893 | SmallVector<uint32_t, 8> ShuffleMask(NumElts); |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 894 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 895 | // The high bit of the selection field chooses the 1st or 2nd operand. |
| 896 | bool LowInputSelect = Imm & 0x02; |
| 897 | bool HighInputSelect = Imm & 0x20; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 898 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 899 | // The low bit of the selection field chooses the low or high half |
| 900 | // of the selected operand. |
| 901 | bool LowHalfSelect = Imm & 0x01; |
| 902 | bool HighHalfSelect = Imm & 0x10; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 903 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 904 | // Determine which operand(s) are actually in use for this instruction. |
| 905 | Value *V0 = LowInputSelect ? II.getArgOperand(1) : II.getArgOperand(0); |
| 906 | Value *V1 = HighInputSelect ? II.getArgOperand(1) : II.getArgOperand(0); |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 907 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 908 | // If needed, replace operands based on zero mask. |
| 909 | V0 = LowHalfZero ? ZeroVector : V0; |
| 910 | V1 = HighHalfZero ? ZeroVector : V1; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 911 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 912 | // Permute low half of result. |
| 913 | unsigned StartIndex = LowHalfSelect ? HalfSize : 0; |
| 914 | for (unsigned i = 0; i < HalfSize; ++i) |
| 915 | ShuffleMask[i] = StartIndex + i; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 916 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 917 | // Permute high half of result. |
| 918 | StartIndex = HighHalfSelect ? HalfSize : 0; |
| 919 | StartIndex += NumElts; |
| 920 | for (unsigned i = 0; i < HalfSize; ++i) |
| 921 | ShuffleMask[i + HalfSize] = StartIndex + i; |
| 922 | |
| 923 | return Builder.CreateShuffleVector(V0, V1, ShuffleMask); |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 926 | /// Decode XOP integer vector comparison intrinsics. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 927 | static Value *simplifyX86vpcom(const IntrinsicInst &II, |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 928 | InstCombiner::BuilderTy &Builder, |
| 929 | bool IsSigned) { |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 930 | if (auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2))) { |
| 931 | uint64_t Imm = CInt->getZExtValue() & 0x7; |
| 932 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 933 | CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
| 934 | |
| 935 | switch (Imm) { |
| 936 | case 0x0: |
| 937 | Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
| 938 | break; |
| 939 | case 0x1: |
| 940 | Pred = IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
| 941 | break; |
| 942 | case 0x2: |
| 943 | Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 944 | break; |
| 945 | case 0x3: |
| 946 | Pred = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
| 947 | break; |
| 948 | case 0x4: |
| 949 | Pred = ICmpInst::ICMP_EQ; break; |
| 950 | case 0x5: |
| 951 | Pred = ICmpInst::ICMP_NE; break; |
| 952 | case 0x6: |
| 953 | return ConstantInt::getSigned(VecTy, 0); // FALSE |
| 954 | case 0x7: |
| 955 | return ConstantInt::getSigned(VecTy, -1); // TRUE |
| 956 | } |
| 957 | |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 958 | if (Value *Cmp = Builder.CreateICmp(Pred, II.getArgOperand(0), |
| 959 | II.getArgOperand(1))) |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 960 | return Builder.CreateSExtOrTrunc(Cmp, VecTy); |
| 961 | } |
| 962 | return nullptr; |
| 963 | } |
| 964 | |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 965 | static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) { |
| 966 | Value *Arg0 = II.getArgOperand(0); |
| 967 | Value *Arg1 = II.getArgOperand(1); |
| 968 | |
| 969 | // fmin(x, x) -> x |
| 970 | if (Arg0 == Arg1) |
| 971 | return Arg0; |
| 972 | |
| 973 | const auto *C1 = dyn_cast<ConstantFP>(Arg1); |
| 974 | |
| 975 | // fmin(x, nan) -> x |
| 976 | if (C1 && C1->isNaN()) |
| 977 | return Arg0; |
| 978 | |
| 979 | // This is the value because if undef were NaN, we would return the other |
| 980 | // value and cannot return a NaN unless both operands are. |
| 981 | // |
| 982 | // fmin(undef, x) -> x |
| 983 | if (isa<UndefValue>(Arg0)) |
| 984 | return Arg1; |
| 985 | |
| 986 | // fmin(x, undef) -> x |
| 987 | if (isa<UndefValue>(Arg1)) |
| 988 | return Arg0; |
| 989 | |
| 990 | Value *X = nullptr; |
| 991 | Value *Y = nullptr; |
| 992 | if (II.getIntrinsicID() == Intrinsic::minnum) { |
| 993 | // fmin(x, fmin(x, y)) -> fmin(x, y) |
| 994 | // fmin(y, fmin(x, y)) -> fmin(x, y) |
| 995 | if (match(Arg1, m_FMin(m_Value(X), m_Value(Y)))) { |
| 996 | if (Arg0 == X || Arg0 == Y) |
| 997 | return Arg1; |
| 998 | } |
| 999 | |
| 1000 | // fmin(fmin(x, y), x) -> fmin(x, y) |
| 1001 | // fmin(fmin(x, y), y) -> fmin(x, y) |
| 1002 | if (match(Arg0, m_FMin(m_Value(X), m_Value(Y)))) { |
| 1003 | if (Arg1 == X || Arg1 == Y) |
| 1004 | return Arg0; |
| 1005 | } |
| 1006 | |
| 1007 | // TODO: fmin(nnan x, inf) -> x |
| 1008 | // TODO: fmin(nnan ninf x, flt_max) -> x |
| 1009 | if (C1 && C1->isInfinity()) { |
| 1010 | // fmin(x, -inf) -> -inf |
| 1011 | if (C1->isNegative()) |
| 1012 | return Arg1; |
| 1013 | } |
| 1014 | } else { |
| 1015 | assert(II.getIntrinsicID() == Intrinsic::maxnum); |
| 1016 | // fmax(x, fmax(x, y)) -> fmax(x, y) |
| 1017 | // fmax(y, fmax(x, y)) -> fmax(x, y) |
| 1018 | if (match(Arg1, m_FMax(m_Value(X), m_Value(Y)))) { |
| 1019 | if (Arg0 == X || Arg0 == Y) |
| 1020 | return Arg1; |
| 1021 | } |
| 1022 | |
| 1023 | // fmax(fmax(x, y), x) -> fmax(x, y) |
| 1024 | // fmax(fmax(x, y), y) -> fmax(x, y) |
| 1025 | if (match(Arg0, m_FMax(m_Value(X), m_Value(Y)))) { |
| 1026 | if (Arg1 == X || Arg1 == Y) |
| 1027 | return Arg0; |
| 1028 | } |
| 1029 | |
| 1030 | // TODO: fmax(nnan x, -inf) -> x |
| 1031 | // TODO: fmax(nnan ninf x, -flt_max) -> x |
| 1032 | if (C1 && C1->isInfinity()) { |
| 1033 | // fmax(x, inf) -> inf |
| 1034 | if (!C1->isNegative()) |
| 1035 | return Arg1; |
| 1036 | } |
| 1037 | } |
| 1038 | return nullptr; |
| 1039 | } |
| 1040 | |
David Majnemer | 666aa94 | 2016-07-14 06:58:42 +0000 | [diff] [blame] | 1041 | static bool maskIsAllOneOrUndef(Value *Mask) { |
| 1042 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 1043 | if (!ConstMask) |
| 1044 | return false; |
| 1045 | if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask)) |
| 1046 | return true; |
| 1047 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 1048 | ++I) { |
| 1049 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 1050 | if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt)) |
| 1051 | continue; |
| 1052 | return false; |
| 1053 | } |
| 1054 | return true; |
| 1055 | } |
| 1056 | |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1057 | static Value *simplifyMaskedLoad(const IntrinsicInst &II, |
| 1058 | InstCombiner::BuilderTy &Builder) { |
David Majnemer | 666aa94 | 2016-07-14 06:58:42 +0000 | [diff] [blame] | 1059 | // If the mask is all ones or undefs, this is a plain vector load of the 1st |
| 1060 | // argument. |
| 1061 | if (maskIsAllOneOrUndef(II.getArgOperand(2))) { |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1062 | Value *LoadPtr = II.getArgOperand(0); |
| 1063 | unsigned Alignment = cast<ConstantInt>(II.getArgOperand(1))->getZExtValue(); |
| 1064 | return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload"); |
| 1065 | } |
| 1066 | |
| 1067 | return nullptr; |
| 1068 | } |
| 1069 | |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1070 | static Instruction *simplifyMaskedStore(IntrinsicInst &II, InstCombiner &IC) { |
| 1071 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3)); |
| 1072 | if (!ConstMask) |
| 1073 | return nullptr; |
| 1074 | |
| 1075 | // If the mask is all zeros, this instruction does nothing. |
| 1076 | if (ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1077 | return IC.eraseInstFromFunction(II); |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1078 | |
| 1079 | // If the mask is all ones, this is a plain vector store of the 1st argument. |
| 1080 | if (ConstMask->isAllOnesValue()) { |
| 1081 | Value *StorePtr = II.getArgOperand(1); |
| 1082 | unsigned Alignment = cast<ConstantInt>(II.getArgOperand(2))->getZExtValue(); |
| 1083 | return new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment); |
| 1084 | } |
| 1085 | |
| 1086 | return nullptr; |
| 1087 | } |
| 1088 | |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1089 | static Instruction *simplifyMaskedGather(IntrinsicInst &II, InstCombiner &IC) { |
| 1090 | // If the mask is all zeros, return the "passthru" argument of the gather. |
| 1091 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2)); |
| 1092 | if (ConstMask && ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1093 | return IC.replaceInstUsesWith(II, II.getArgOperand(3)); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1094 | |
| 1095 | return nullptr; |
| 1096 | } |
| 1097 | |
| 1098 | static Instruction *simplifyMaskedScatter(IntrinsicInst &II, InstCombiner &IC) { |
| 1099 | // If the mask is all zeros, a scatter does nothing. |
| 1100 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3)); |
| 1101 | if (ConstMask && ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1102 | return IC.eraseInstFromFunction(II); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1103 | |
| 1104 | return nullptr; |
| 1105 | } |
| 1106 | |
Sanjay Patel | 8e3ab17 | 2016-08-05 22:42:46 +0000 | [diff] [blame] | 1107 | static Value *foldCttzCtlz(IntrinsicInst &II, InstCombiner &IC) { |
| 1108 | Value *Op0 = II.getArgOperand(0); |
| 1109 | // FIXME: Try to simplify vectors of integers. |
| 1110 | auto *IT = dyn_cast<IntegerType>(Op0->getType()); |
| 1111 | if (!IT) |
| 1112 | return nullptr; |
| 1113 | |
| 1114 | unsigned BitWidth = IT->getBitWidth(); |
| 1115 | APInt KnownZero(BitWidth, 0); |
| 1116 | APInt KnownOne(BitWidth, 0); |
| 1117 | IC.computeKnownBits(Op0, KnownZero, KnownOne, 0, &II); |
| 1118 | |
| 1119 | // Create a mask for bits above (ctlz) or below (cttz) the first known one. |
| 1120 | bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz; |
| 1121 | unsigned NumMaskBits = IsTZ ? KnownOne.countTrailingZeros() |
| 1122 | : KnownOne.countLeadingZeros(); |
| 1123 | APInt Mask = IsTZ ? APInt::getLowBitsSet(BitWidth, NumMaskBits) |
| 1124 | : APInt::getHighBitsSet(BitWidth, NumMaskBits); |
| 1125 | |
| 1126 | // If all bits above (ctlz) or below (cttz) the first known one are known |
| 1127 | // zero, this value is constant. |
| 1128 | // FIXME: This should be in InstSimplify because we're replacing an |
| 1129 | // instruction with a constant. |
| 1130 | if ((Mask & KnownZero) == Mask) |
| 1131 | return ConstantInt::get(IT, APInt(BitWidth, NumMaskBits)); |
| 1132 | |
| 1133 | return nullptr; |
| 1134 | } |
| 1135 | |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1136 | // TODO: If the x86 backend knew how to convert a bool vector mask back to an |
| 1137 | // XMM register mask efficiently, we could transform all x86 masked intrinsics |
| 1138 | // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1139 | static Instruction *simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC) { |
| 1140 | Value *Ptr = II.getOperand(0); |
| 1141 | Value *Mask = II.getOperand(1); |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1142 | Constant *ZeroVec = Constant::getNullValue(II.getType()); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Special case a zero mask since that's not a ConstantDataVector. |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1145 | // This masked load instruction creates a zero vector. |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1146 | if (isa<ConstantAggregateZero>(Mask)) |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1147 | return IC.replaceInstUsesWith(II, ZeroVec); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1148 | |
| 1149 | auto *ConstMask = dyn_cast<ConstantDataVector>(Mask); |
| 1150 | if (!ConstMask) |
| 1151 | return nullptr; |
| 1152 | |
| 1153 | // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic |
| 1154 | // to allow target-independent optimizations. |
| 1155 | |
| 1156 | // First, cast the x86 intrinsic scalar pointer to a vector pointer to match |
| 1157 | // the LLVM intrinsic definition for the pointer argument. |
| 1158 | unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 1159 | PointerType *VecPtrTy = PointerType::get(II.getType(), AddrSpace); |
| 1160 | Value *PtrCast = IC.Builder->CreateBitCast(Ptr, VecPtrTy, "castvec"); |
| 1161 | |
| 1162 | // Second, convert the x86 XMM integer vector mask to a vector of bools based |
| 1163 | // on each element's most significant bit (the sign bit). |
| 1164 | Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask); |
| 1165 | |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1166 | // The pass-through vector for an x86 masked load is a zero vector. |
| 1167 | CallInst *NewMaskedLoad = |
| 1168 | IC.Builder->CreateMaskedLoad(PtrCast, 1, BoolMask, ZeroVec); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1169 | return IC.replaceInstUsesWith(II, NewMaskedLoad); |
| 1170 | } |
| 1171 | |
| 1172 | // TODO: If the x86 backend knew how to convert a bool vector mask back to an |
| 1173 | // XMM register mask efficiently, we could transform all x86 masked intrinsics |
| 1174 | // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1175 | static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC) { |
| 1176 | Value *Ptr = II.getOperand(0); |
| 1177 | Value *Mask = II.getOperand(1); |
| 1178 | Value *Vec = II.getOperand(2); |
| 1179 | |
| 1180 | // Special case a zero mask since that's not a ConstantDataVector: |
| 1181 | // this masked store instruction does nothing. |
| 1182 | if (isa<ConstantAggregateZero>(Mask)) { |
| 1183 | IC.eraseInstFromFunction(II); |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
Sanjay Patel | c4acbae | 2016-03-12 15:16:59 +0000 | [diff] [blame] | 1187 | // The SSE2 version is too weird (eg, unaligned but non-temporal) to do |
| 1188 | // anything else at this level. |
| 1189 | if (II.getIntrinsicID() == Intrinsic::x86_sse2_maskmov_dqu) |
| 1190 | return false; |
| 1191 | |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1192 | auto *ConstMask = dyn_cast<ConstantDataVector>(Mask); |
| 1193 | if (!ConstMask) |
| 1194 | return false; |
| 1195 | |
| 1196 | // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic |
| 1197 | // to allow target-independent optimizations. |
| 1198 | |
| 1199 | // First, cast the x86 intrinsic scalar pointer to a vector pointer to match |
| 1200 | // the LLVM intrinsic definition for the pointer argument. |
| 1201 | unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 1202 | PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace); |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1203 | Value *PtrCast = IC.Builder->CreateBitCast(Ptr, VecPtrTy, "castvec"); |
| 1204 | |
| 1205 | // Second, convert the x86 XMM integer vector mask to a vector of bools based |
| 1206 | // on each element's most significant bit (the sign bit). |
| 1207 | Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask); |
| 1208 | |
| 1209 | IC.Builder->CreateMaskedStore(Vec, PtrCast, 1, BoolMask); |
| 1210 | |
| 1211 | // 'Replace uses' doesn't work for stores. Erase the original masked store. |
| 1212 | IC.eraseInstFromFunction(II); |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 1216 | // Returns true iff the 2 intrinsics have the same operands, limiting the |
| 1217 | // comparison to the first NumOperands. |
| 1218 | static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E, |
| 1219 | unsigned NumOperands) { |
| 1220 | assert(I.getNumArgOperands() >= NumOperands && "Not enough operands"); |
| 1221 | assert(E.getNumArgOperands() >= NumOperands && "Not enough operands"); |
| 1222 | for (unsigned i = 0; i < NumOperands; i++) |
| 1223 | if (I.getArgOperand(i) != E.getArgOperand(i)) |
| 1224 | return false; |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | // Remove trivially empty start/end intrinsic ranges, i.e. a start |
| 1229 | // immediately followed by an end (ignoring debuginfo or other |
| 1230 | // start/end intrinsics in between). As this handles only the most trivial |
| 1231 | // cases, tracking the nesting level is not needed: |
| 1232 | // |
| 1233 | // call @llvm.foo.start(i1 0) ; &I |
| 1234 | // call @llvm.foo.start(i1 0) |
| 1235 | // call @llvm.foo.end(i1 0) ; This one will not be skipped: it will be removed |
| 1236 | // call @llvm.foo.end(i1 0) |
| 1237 | static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID, |
| 1238 | unsigned EndID, InstCombiner &IC) { |
| 1239 | assert(I.getIntrinsicID() == StartID && |
| 1240 | "Start intrinsic does not have expected ID"); |
| 1241 | BasicBlock::iterator BI(I), BE(I.getParent()->end()); |
| 1242 | for (++BI; BI != BE; ++BI) { |
| 1243 | if (auto *E = dyn_cast<IntrinsicInst>(BI)) { |
| 1244 | if (isa<DbgInfoIntrinsic>(E) || E->getIntrinsicID() == StartID) |
| 1245 | continue; |
| 1246 | if (E->getIntrinsicID() == EndID && |
| 1247 | haveSameOperands(I, *E, E->getNumArgOperands())) { |
| 1248 | IC.eraseInstFromFunction(*E); |
| 1249 | IC.eraseInstFromFunction(I); |
| 1250 | return true; |
| 1251 | } |
| 1252 | } |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | Instruction *InstCombiner::visitVAStartInst(VAStartInst &I) { |
| 1260 | removeTriviallyEmptyRange(I, Intrinsic::vastart, Intrinsic::vaend, *this); |
| 1261 | return nullptr; |
| 1262 | } |
| 1263 | |
| 1264 | Instruction *InstCombiner::visitVACopyInst(VACopyInst &I) { |
| 1265 | removeTriviallyEmptyRange(I, Intrinsic::vacopy, Intrinsic::vaend, *this); |
| 1266 | return nullptr; |
| 1267 | } |
| 1268 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 1269 | /// CallInst simplification. This mostly only handles folding of intrinsic |
| 1270 | /// instructions. For normal calls, it allows visitCallSite to do the heavy |
| 1271 | /// lifting. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1272 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 1273 | auto Args = CI.arg_operands(); |
| 1274 | if (Value *V = SimplifyCall(CI.getCalledValue(), Args.begin(), Args.end(), DL, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1275 | &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1276 | return replaceInstUsesWith(CI, V); |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 1277 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1278 | if (isFreeCall(&CI, &TLI)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1279 | return visitFree(CI); |
| 1280 | |
| 1281 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 1282 | // callee isn't. |
Sanjay Patel | 5a47095 | 2016-08-11 15:16:06 +0000 | [diff] [blame^] | 1283 | if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1284 | CI.setDoesNotThrow(); |
| 1285 | return &CI; |
| 1286 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1288 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 1289 | if (!II) return visitCallSite(&CI); |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1291 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 1292 | // visitCallSite. |
| 1293 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
| 1294 | bool Changed = false; |
| 1295 | |
| 1296 | // memmove/cpy/set of zero bytes is a noop. |
| 1297 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
Chris Lattner | c663a67 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 1298 | if (NumBytes->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1299 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 1302 | if (CI->getZExtValue() == 1) { |
| 1303 | // Replace the instruction with just byte operations. We would |
| 1304 | // transform other cases to loads/stores, but we don't know if |
| 1305 | // alignment is sufficient. |
| 1306 | } |
| 1307 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | c663a67 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 1309 | // No other transformations apply to volatile transfers. |
| 1310 | if (MI->isVolatile()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1311 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1312 | |
| 1313 | // If we have a memmove and the source operation is a constant global, |
| 1314 | // then the source and dest pointers can't alias, so we can change this |
| 1315 | // into a call to memcpy. |
| 1316 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 1317 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 1318 | if (GVSrc->isConstant()) { |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 1319 | Module *M = CI.getModule(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1320 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1321 | Type *Tys[3] = { CI.getArgOperand(0)->getType(), |
| 1322 | CI.getArgOperand(1)->getType(), |
| 1323 | CI.getArgOperand(2)->getType() }; |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 1324 | CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1325 | Changed = true; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { |
| 1330 | // memmove(x,x,size) -> noop. |
| 1331 | if (MTI->getSource() == MTI->getDest()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1332 | return eraseInstFromFunction(CI); |
Eric Christopher | 7258dcd | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 1333 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1334 | |
Eric Christopher | 7258dcd | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 1335 | // If we can determine a pointer alignment that is bigger than currently |
| 1336 | // set, update the alignment. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1337 | if (isa<MemTransferInst>(MI)) { |
| 1338 | if (Instruction *I = SimplifyMemTransfer(MI)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1339 | return I; |
| 1340 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 1341 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 1342 | return I; |
| 1343 | } |
Gabor Greif | 590d95e | 2010-06-24 13:42:49 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1345 | if (Changed) return II; |
| 1346 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1347 | |
Sanjay Patel | 1c600c6 | 2016-01-20 16:41:43 +0000 | [diff] [blame] | 1348 | auto SimplifyDemandedVectorEltsLow = [this](Value *Op, unsigned Width, |
| 1349 | unsigned DemandedWidth) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1350 | APInt UndefElts(Width, 0); |
| 1351 | APInt DemandedElts = APInt::getLowBitsSet(Width, DemandedWidth); |
| 1352 | return SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts); |
| 1353 | }; |
Simon Pilgrim | 424da16 | 2016-04-24 18:12:42 +0000 | [diff] [blame] | 1354 | auto SimplifyDemandedVectorEltsHigh = [this](Value *Op, unsigned Width, |
| 1355 | unsigned DemandedWidth) { |
| 1356 | APInt UndefElts(Width, 0); |
| 1357 | APInt DemandedElts = APInt::getHighBitsSet(Width, DemandedWidth); |
| 1358 | return SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts); |
| 1359 | }; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1361 | switch (II->getIntrinsicID()) { |
| 1362 | default: break; |
Eric Christopher | 7b7028f | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 1363 | case Intrinsic::objectsize: { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1364 | uint64_t Size; |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1365 | if (getObjectSize(II->getArgOperand(0), Size, DL, &TLI)) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1366 | APInt APSize(II->getType()->getIntegerBitWidth(), Size); |
| 1367 | // Equality check to be sure that `Size` can fit in a value of type |
| 1368 | // `II->getType()` |
| 1369 | if (APSize == Size) |
| 1370 | return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), APSize)); |
| 1371 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1372 | return nullptr; |
Eric Christopher | 7b7028f | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 1373 | } |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1374 | case Intrinsic::bswap: { |
| 1375 | Value *IIOperand = II->getArgOperand(0); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1376 | Value *X = nullptr; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1378 | // bswap(bswap(x)) -> x |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1379 | if (match(IIOperand, m_BSwap(m_Value(X)))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1380 | return replaceInstUsesWith(CI, X); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1382 | // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1383 | if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) { |
| 1384 | unsigned C = X->getType()->getPrimitiveSizeInBits() - |
| 1385 | IIOperand->getType()->getPrimitiveSizeInBits(); |
| 1386 | Value *CV = ConstantInt::get(X->getType(), C); |
| 1387 | Value *V = Builder->CreateLShr(X, CV); |
| 1388 | return new TruncInst(V, IIOperand->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1389 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1390 | break; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
James Molloy | 2d09c00 | 2015-11-12 12:39:41 +0000 | [diff] [blame] | 1393 | case Intrinsic::bitreverse: { |
| 1394 | Value *IIOperand = II->getArgOperand(0); |
| 1395 | Value *X = nullptr; |
| 1396 | |
| 1397 | // bitreverse(bitreverse(x)) -> x |
| 1398 | if (match(IIOperand, m_Intrinsic<Intrinsic::bitreverse>(m_Value(X)))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1399 | return replaceInstUsesWith(CI, X); |
James Molloy | 2d09c00 | 2015-11-12 12:39:41 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | } |
| 1402 | |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1403 | case Intrinsic::masked_load: |
| 1404 | if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1405 | return replaceInstUsesWith(CI, SimplifiedMaskedOp); |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1406 | break; |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1407 | case Intrinsic::masked_store: |
| 1408 | return simplifyMaskedStore(*II, *this); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1409 | case Intrinsic::masked_gather: |
| 1410 | return simplifyMaskedGather(*II, *this); |
| 1411 | case Intrinsic::masked_scatter: |
| 1412 | return simplifyMaskedScatter(*II, *this); |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1414 | case Intrinsic::powi: |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1415 | if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1416 | // powi(x, 0) -> 1.0 |
| 1417 | if (Power->isZero()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1418 | return replaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1419 | // powi(x, 1) -> x |
| 1420 | if (Power->isOne()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1421 | return replaceInstUsesWith(CI, II->getArgOperand(0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1422 | // powi(x, -1) -> 1/x |
| 1423 | if (Power->isAllOnesValue()) |
| 1424 | return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1425 | II->getArgOperand(0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1426 | } |
| 1427 | break; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1428 | |
Sanjay Patel | 8e3ab17 | 2016-08-05 22:42:46 +0000 | [diff] [blame] | 1429 | case Intrinsic::cttz: |
| 1430 | case Intrinsic::ctlz: |
| 1431 | if (Value *V = foldCttzCtlz(*II, *this)) |
| 1432 | return replaceInstUsesWith(*II, V); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1433 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1434 | |
Nick Lewycky | abe2cc1 | 2015-04-13 19:17:37 +0000 | [diff] [blame] | 1435 | case Intrinsic::uadd_with_overflow: |
| 1436 | case Intrinsic::sadd_with_overflow: |
| 1437 | case Intrinsic::umul_with_overflow: |
| 1438 | case Intrinsic::smul_with_overflow: |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 1439 | if (isa<Constant>(II->getArgOperand(0)) && |
| 1440 | !isa<Constant>(II->getArgOperand(1))) { |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1441 | // Canonicalize constants into the RHS. |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 1442 | Value *LHS = II->getArgOperand(0); |
| 1443 | II->setArgOperand(0, II->getArgOperand(1)); |
| 1444 | II->setArgOperand(1, LHS); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1445 | return II; |
| 1446 | } |
Nick Lewycky | d6f241d | 2015-04-13 20:03:08 +0000 | [diff] [blame] | 1447 | // fall through |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1448 | |
Nick Lewycky | abe2cc1 | 2015-04-13 19:17:37 +0000 | [diff] [blame] | 1449 | case Intrinsic::usub_with_overflow: |
| 1450 | case Intrinsic::ssub_with_overflow: { |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1451 | OverflowCheckFlavor OCF = |
| 1452 | IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID()); |
| 1453 | assert(OCF != OCF_INVALID && "unexpected!"); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1454 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1455 | Value *OperationResult = nullptr; |
| 1456 | Constant *OverflowResult = nullptr; |
| 1457 | if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1), |
| 1458 | *II, OperationResult, OverflowResult)) |
| 1459 | return CreateOverflowTuple(II, OperationResult, OverflowResult); |
Benjamin Kramer | a420df2 | 2014-07-04 10:22:21 +0000 | [diff] [blame] | 1460 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1461 | break; |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 1462 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1463 | |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1464 | case Intrinsic::minnum: |
| 1465 | case Intrinsic::maxnum: { |
| 1466 | Value *Arg0 = II->getArgOperand(0); |
| 1467 | Value *Arg1 = II->getArgOperand(1); |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 1468 | // Canonicalize constants to the RHS. |
| 1469 | if (isa<ConstantFP>(Arg0) && !isa<ConstantFP>(Arg1)) { |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1470 | II->setArgOperand(0, Arg1); |
| 1471 | II->setArgOperand(1, Arg0); |
| 1472 | return II; |
| 1473 | } |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 1474 | if (Value *V = simplifyMinnumMaxnum(*II)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1475 | return replaceInstUsesWith(*II, V); |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1478 | case Intrinsic::ppc_altivec_lvx: |
| 1479 | case Intrinsic::ppc_altivec_lvxl: |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 1480 | // Turn PPC lvx -> load if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1481 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC, |
| 1482 | &DT) >= 16) { |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1483 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1484 | PointerType::getUnqual(II->getType())); |
| 1485 | return new LoadInst(Ptr); |
| 1486 | } |
| 1487 | break; |
Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 1488 | case Intrinsic::ppc_vsx_lxvw4x: |
| 1489 | case Intrinsic::ppc_vsx_lxvd2x: { |
| 1490 | // Turn PPC VSX loads into normal loads. |
| 1491 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
| 1492 | PointerType::getUnqual(II->getType())); |
| 1493 | return new LoadInst(Ptr, Twine(""), false, 1); |
| 1494 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1495 | case Intrinsic::ppc_altivec_stvx: |
| 1496 | case Intrinsic::ppc_altivec_stvxl: |
| 1497 | // Turn stvx -> store if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1498 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC, |
| 1499 | &DT) >= 16) { |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1500 | Type *OpPtrTy = |
Gabor Greif | a6d75e2 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 1501 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 1502 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 1503 | return new StoreInst(II->getArgOperand(0), Ptr); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1504 | } |
| 1505 | break; |
Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 1506 | case Intrinsic::ppc_vsx_stxvw4x: |
| 1507 | case Intrinsic::ppc_vsx_stxvd2x: { |
| 1508 | // Turn PPC VSX stores into normal stores. |
| 1509 | Type *OpPtrTy = PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 1510 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 1511 | return new StoreInst(II->getArgOperand(0), Ptr, false, 1); |
| 1512 | } |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1513 | case Intrinsic::ppc_qpx_qvlfs: |
| 1514 | // Turn PPC QPX qvlfs -> load if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1515 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC, |
| 1516 | &DT) >= 16) { |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 1517 | Type *VTy = VectorType::get(Builder->getFloatTy(), |
| 1518 | II->getType()->getVectorNumElements()); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1519 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 1520 | PointerType::getUnqual(VTy)); |
| 1521 | Value *Load = Builder->CreateLoad(Ptr); |
| 1522 | return new FPExtInst(Load, II->getType()); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1523 | } |
| 1524 | break; |
| 1525 | case Intrinsic::ppc_qpx_qvlfd: |
| 1526 | // Turn PPC QPX qvlfd -> load if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1527 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 32, DL, II, &AC, |
| 1528 | &DT) >= 32) { |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1529 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
| 1530 | PointerType::getUnqual(II->getType())); |
| 1531 | return new LoadInst(Ptr); |
| 1532 | } |
| 1533 | break; |
| 1534 | case Intrinsic::ppc_qpx_qvstfs: |
| 1535 | // Turn PPC QPX qvstfs -> store if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1536 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC, |
| 1537 | &DT) >= 16) { |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 1538 | Type *VTy = VectorType::get(Builder->getFloatTy(), |
| 1539 | II->getArgOperand(0)->getType()->getVectorNumElements()); |
| 1540 | Value *TOp = Builder->CreateFPTrunc(II->getArgOperand(0), VTy); |
| 1541 | Type *OpPtrTy = PointerType::getUnqual(VTy); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1542 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 1543 | return new StoreInst(TOp, Ptr); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1544 | } |
| 1545 | break; |
| 1546 | case Intrinsic::ppc_qpx_qvstfd: |
| 1547 | // Turn PPC QPX qvstfd -> store if the pointer is known aligned. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1548 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 32, DL, II, &AC, |
| 1549 | &DT) >= 32) { |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 1550 | Type *OpPtrTy = |
| 1551 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 1552 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 1553 | return new StoreInst(II->getArgOperand(0), Ptr); |
| 1554 | } |
| 1555 | break; |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1556 | |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1557 | case Intrinsic::x86_vcvtph2ps_128: |
| 1558 | case Intrinsic::x86_vcvtph2ps_256: { |
| 1559 | auto Arg = II->getArgOperand(0); |
| 1560 | auto ArgType = cast<VectorType>(Arg->getType()); |
| 1561 | auto RetType = cast<VectorType>(II->getType()); |
| 1562 | unsigned ArgWidth = ArgType->getNumElements(); |
| 1563 | unsigned RetWidth = RetType->getNumElements(); |
| 1564 | assert(RetWidth <= ArgWidth && "Unexpected input/return vector widths"); |
| 1565 | assert(ArgType->isIntOrIntVectorTy() && |
| 1566 | ArgType->getScalarSizeInBits() == 16 && |
| 1567 | "CVTPH2PS input type should be 16-bit integer vector"); |
| 1568 | assert(RetType->getScalarType()->isFloatTy() && |
| 1569 | "CVTPH2PS output type should be 32-bit float vector"); |
| 1570 | |
| 1571 | // Constant folding: Convert to generic half to single conversion. |
Simon Pilgrim | 48ffca0 | 2015-09-12 14:00:17 +0000 | [diff] [blame] | 1572 | if (isa<ConstantAggregateZero>(Arg)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1573 | return replaceInstUsesWith(*II, ConstantAggregateZero::get(RetType)); |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1574 | |
Simon Pilgrim | 48ffca0 | 2015-09-12 14:00:17 +0000 | [diff] [blame] | 1575 | if (isa<ConstantDataVector>(Arg)) { |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1576 | auto VectorHalfAsShorts = Arg; |
| 1577 | if (RetWidth < ArgWidth) { |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 1578 | SmallVector<uint32_t, 8> SubVecMask; |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1579 | for (unsigned i = 0; i != RetWidth; ++i) |
| 1580 | SubVecMask.push_back((int)i); |
| 1581 | VectorHalfAsShorts = Builder->CreateShuffleVector( |
| 1582 | Arg, UndefValue::get(ArgType), SubVecMask); |
| 1583 | } |
| 1584 | |
| 1585 | auto VectorHalfType = |
| 1586 | VectorType::get(Type::getHalfTy(II->getContext()), RetWidth); |
| 1587 | auto VectorHalfs = |
| 1588 | Builder->CreateBitCast(VectorHalfAsShorts, VectorHalfType); |
| 1589 | auto VectorFloats = Builder->CreateFPExt(VectorHalfs, RetType); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1590 | return replaceInstUsesWith(*II, VectorFloats); |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // We only use the lowest lanes of the argument. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 1594 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg, ArgWidth, RetWidth)) { |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 1595 | II->setArgOperand(0, V); |
| 1596 | return II; |
| 1597 | } |
| 1598 | break; |
| 1599 | } |
| 1600 | |
Chandler Carruth | cf414cf | 2011-01-10 07:19:37 +0000 | [diff] [blame] | 1601 | case Intrinsic::x86_sse_cvtss2si: |
| 1602 | case Intrinsic::x86_sse_cvtss2si64: |
| 1603 | case Intrinsic::x86_sse_cvttss2si: |
| 1604 | case Intrinsic::x86_sse_cvttss2si64: |
| 1605 | case Intrinsic::x86_sse2_cvtsd2si: |
| 1606 | case Intrinsic::x86_sse2_cvtsd2si64: |
| 1607 | case Intrinsic::x86_sse2_cvttsd2si: |
| 1608 | case Intrinsic::x86_sse2_cvttsd2si64: { |
| 1609 | // These intrinsics only demand the 0th element of their input vectors. If |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1610 | // we can simplify the input based on that, do so now. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 1611 | Value *Arg = II->getArgOperand(0); |
| 1612 | unsigned VWidth = Arg->getType()->getVectorNumElements(); |
| 1613 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg, VWidth, 1)) { |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 1614 | II->setArgOperand(0, V); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1615 | return II; |
| 1616 | } |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 1617 | break; |
| 1618 | } |
| 1619 | |
Simon Pilgrim | 91e3ac8 | 2016-06-07 08:18:35 +0000 | [diff] [blame] | 1620 | case Intrinsic::x86_mmx_pmovmskb: |
| 1621 | case Intrinsic::x86_sse_movmsk_ps: |
| 1622 | case Intrinsic::x86_sse2_movmsk_pd: |
| 1623 | case Intrinsic::x86_sse2_pmovmskb_128: |
| 1624 | case Intrinsic::x86_avx_movmsk_pd_256: |
| 1625 | case Intrinsic::x86_avx_movmsk_ps_256: |
| 1626 | case Intrinsic::x86_avx2_pmovmskb: { |
| 1627 | if (Value *V = simplifyX86movmsk(*II, *Builder)) |
| 1628 | return replaceInstUsesWith(*II, V); |
| 1629 | break; |
| 1630 | } |
| 1631 | |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 1632 | case Intrinsic::x86_sse_comieq_ss: |
| 1633 | case Intrinsic::x86_sse_comige_ss: |
| 1634 | case Intrinsic::x86_sse_comigt_ss: |
| 1635 | case Intrinsic::x86_sse_comile_ss: |
| 1636 | case Intrinsic::x86_sse_comilt_ss: |
| 1637 | case Intrinsic::x86_sse_comineq_ss: |
| 1638 | case Intrinsic::x86_sse_ucomieq_ss: |
| 1639 | case Intrinsic::x86_sse_ucomige_ss: |
| 1640 | case Intrinsic::x86_sse_ucomigt_ss: |
| 1641 | case Intrinsic::x86_sse_ucomile_ss: |
| 1642 | case Intrinsic::x86_sse_ucomilt_ss: |
| 1643 | case Intrinsic::x86_sse_ucomineq_ss: |
| 1644 | case Intrinsic::x86_sse2_comieq_sd: |
| 1645 | case Intrinsic::x86_sse2_comige_sd: |
| 1646 | case Intrinsic::x86_sse2_comigt_sd: |
| 1647 | case Intrinsic::x86_sse2_comile_sd: |
| 1648 | case Intrinsic::x86_sse2_comilt_sd: |
| 1649 | case Intrinsic::x86_sse2_comineq_sd: |
| 1650 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 1651 | case Intrinsic::x86_sse2_ucomige_sd: |
| 1652 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 1653 | case Intrinsic::x86_sse2_ucomile_sd: |
| 1654 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 1655 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 1656 | // These intrinsics only demand the 0th element of their input vectors. If |
| 1657 | // we can simplify the input based on that, do so now. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1658 | bool MadeChange = false; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 1659 | Value *Arg0 = II->getArgOperand(0); |
| 1660 | Value *Arg1 = II->getArgOperand(1); |
| 1661 | unsigned VWidth = Arg0->getType()->getVectorNumElements(); |
| 1662 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg0, VWidth, 1)) { |
| 1663 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1664 | MadeChange = true; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 1665 | } |
| 1666 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) { |
| 1667 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1668 | MadeChange = true; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 1669 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1670 | if (MadeChange) |
| 1671 | return II; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | } |
| 1674 | |
Simon Pilgrim | 424da16 | 2016-04-24 18:12:42 +0000 | [diff] [blame] | 1675 | case Intrinsic::x86_sse_add_ss: |
| 1676 | case Intrinsic::x86_sse_sub_ss: |
| 1677 | case Intrinsic::x86_sse_mul_ss: |
| 1678 | case Intrinsic::x86_sse_div_ss: |
| 1679 | case Intrinsic::x86_sse_min_ss: |
| 1680 | case Intrinsic::x86_sse_max_ss: |
| 1681 | case Intrinsic::x86_sse_cmp_ss: |
| 1682 | case Intrinsic::x86_sse2_add_sd: |
| 1683 | case Intrinsic::x86_sse2_sub_sd: |
| 1684 | case Intrinsic::x86_sse2_mul_sd: |
| 1685 | case Intrinsic::x86_sse2_div_sd: |
| 1686 | case Intrinsic::x86_sse2_min_sd: |
| 1687 | case Intrinsic::x86_sse2_max_sd: |
| 1688 | case Intrinsic::x86_sse2_cmp_sd: { |
| 1689 | // These intrinsics only demand the lowest element of the second input |
| 1690 | // vector. |
| 1691 | Value *Arg1 = II->getArgOperand(1); |
| 1692 | unsigned VWidth = Arg1->getType()->getVectorNumElements(); |
| 1693 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) { |
| 1694 | II->setArgOperand(1, V); |
| 1695 | return II; |
| 1696 | } |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | case Intrinsic::x86_sse41_round_ss: |
| 1701 | case Intrinsic::x86_sse41_round_sd: { |
| 1702 | // These intrinsics demand the upper elements of the first input vector and |
| 1703 | // the lowest element of the second input vector. |
| 1704 | bool MadeChange = false; |
| 1705 | Value *Arg0 = II->getArgOperand(0); |
| 1706 | Value *Arg1 = II->getArgOperand(1); |
| 1707 | unsigned VWidth = Arg0->getType()->getVectorNumElements(); |
| 1708 | if (Value *V = SimplifyDemandedVectorEltsHigh(Arg0, VWidth, VWidth - 1)) { |
| 1709 | II->setArgOperand(0, V); |
| 1710 | MadeChange = true; |
| 1711 | } |
| 1712 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) { |
| 1713 | II->setArgOperand(1, V); |
| 1714 | MadeChange = true; |
| 1715 | } |
| 1716 | if (MadeChange) |
| 1717 | return II; |
| 1718 | break; |
| 1719 | } |
| 1720 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 1721 | // Constant fold ashr( <A x Bi>, Ci ). |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1722 | // Constant fold lshr( <A x Bi>, Ci ). |
| 1723 | // Constant fold shl( <A x Bi>, Ci ). |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 1724 | case Intrinsic::x86_sse2_psrai_d: |
| 1725 | case Intrinsic::x86_sse2_psrai_w: |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 1726 | case Intrinsic::x86_avx2_psrai_d: |
| 1727 | case Intrinsic::x86_avx2_psrai_w: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 1728 | case Intrinsic::x86_sse2_psrli_d: |
| 1729 | case Intrinsic::x86_sse2_psrli_q: |
| 1730 | case Intrinsic::x86_sse2_psrli_w: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 1731 | case Intrinsic::x86_avx2_psrli_d: |
| 1732 | case Intrinsic::x86_avx2_psrli_q: |
| 1733 | case Intrinsic::x86_avx2_psrli_w: |
Michael J. Spencer | dee4b2c | 2014-04-24 00:58:18 +0000 | [diff] [blame] | 1734 | case Intrinsic::x86_sse2_pslli_d: |
| 1735 | case Intrinsic::x86_sse2_pslli_q: |
| 1736 | case Intrinsic::x86_sse2_pslli_w: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 1737 | case Intrinsic::x86_avx2_pslli_d: |
| 1738 | case Intrinsic::x86_avx2_pslli_q: |
| 1739 | case Intrinsic::x86_avx2_pslli_w: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1740 | if (Value *V = simplifyX86immShift(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1741 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 1742 | break; |
| 1743 | |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1744 | case Intrinsic::x86_sse2_psra_d: |
| 1745 | case Intrinsic::x86_sse2_psra_w: |
| 1746 | case Intrinsic::x86_avx2_psra_d: |
| 1747 | case Intrinsic::x86_avx2_psra_w: |
| 1748 | case Intrinsic::x86_sse2_psrl_d: |
| 1749 | case Intrinsic::x86_sse2_psrl_q: |
| 1750 | case Intrinsic::x86_sse2_psrl_w: |
| 1751 | case Intrinsic::x86_avx2_psrl_d: |
| 1752 | case Intrinsic::x86_avx2_psrl_q: |
| 1753 | case Intrinsic::x86_avx2_psrl_w: |
| 1754 | case Intrinsic::x86_sse2_psll_d: |
| 1755 | case Intrinsic::x86_sse2_psll_q: |
| 1756 | case Intrinsic::x86_sse2_psll_w: |
| 1757 | case Intrinsic::x86_avx2_psll_d: |
| 1758 | case Intrinsic::x86_avx2_psll_q: |
| 1759 | case Intrinsic::x86_avx2_psll_w: { |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1760 | if (Value *V = simplifyX86immShift(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1761 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1762 | |
| 1763 | // SSE2/AVX2 uses only the first 64-bits of the 128-bit vector |
| 1764 | // operand to compute the shift amount. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 1765 | Value *Arg1 = II->getArgOperand(1); |
| 1766 | assert(Arg1->getType()->getPrimitiveSizeInBits() == 128 && |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1767 | "Unexpected packed shift size"); |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 1768 | unsigned VWidth = Arg1->getType()->getVectorNumElements(); |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1769 | |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 1770 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, VWidth / 2)) { |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 1771 | II->setArgOperand(1, V); |
| 1772 | return II; |
| 1773 | } |
| 1774 | break; |
| 1775 | } |
| 1776 | |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 1777 | case Intrinsic::x86_avx2_psllv_d: |
| 1778 | case Intrinsic::x86_avx2_psllv_d_256: |
| 1779 | case Intrinsic::x86_avx2_psllv_q: |
| 1780 | case Intrinsic::x86_avx2_psllv_q_256: |
| 1781 | case Intrinsic::x86_avx2_psrav_d: |
| 1782 | case Intrinsic::x86_avx2_psrav_d_256: |
| 1783 | case Intrinsic::x86_avx2_psrlv_d: |
| 1784 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 1785 | case Intrinsic::x86_avx2_psrlv_q: |
| 1786 | case Intrinsic::x86_avx2_psrlv_q_256: |
| 1787 | if (Value *V = simplifyX86varShift(*II, *Builder)) |
| 1788 | return replaceInstUsesWith(*II, V); |
| 1789 | break; |
| 1790 | |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 1791 | case Intrinsic::x86_sse41_insertps: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1792 | if (Value *V = simplifyX86insertps(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1793 | return replaceInstUsesWith(*II, V); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 1794 | break; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 1795 | |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1796 | case Intrinsic::x86_sse4a_extrq: { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1797 | Value *Op0 = II->getArgOperand(0); |
| 1798 | Value *Op1 = II->getArgOperand(1); |
| 1799 | unsigned VWidth0 = Op0->getType()->getVectorNumElements(); |
| 1800 | unsigned VWidth1 = Op1->getType()->getVectorNumElements(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1801 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 1802 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && |
| 1803 | VWidth1 == 16 && "Unexpected operand sizes"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1804 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1805 | // See if we're dealing with constant values. |
| 1806 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 1807 | ConstantInt *CILength = |
| 1808 | C1 ? dyn_cast<ConstantInt>(C1->getAggregateElement((unsigned)0)) |
| 1809 | : nullptr; |
| 1810 | ConstantInt *CIIndex = |
| 1811 | C1 ? dyn_cast<ConstantInt>(C1->getAggregateElement((unsigned)1)) |
| 1812 | : nullptr; |
| 1813 | |
| 1814 | // Attempt to simplify to a constant, shuffle vector or EXTRQI call. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1815 | if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1816 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1817 | |
| 1818 | // EXTRQ only uses the lowest 64-bits of the first 128-bit vector |
| 1819 | // operands and the lowest 16-bits of the second. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1820 | bool MadeChange = false; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1821 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { |
| 1822 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1823 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1824 | } |
| 1825 | if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 2)) { |
| 1826 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1827 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1828 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1829 | if (MadeChange) |
| 1830 | return II; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | case Intrinsic::x86_sse4a_extrqi: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1835 | // EXTRQI: Extract Length bits starting from Index. Zero pad the remaining |
| 1836 | // bits of the lower 64-bits. The upper 64-bits are undefined. |
| 1837 | Value *Op0 = II->getArgOperand(0); |
| 1838 | unsigned VWidth = Op0->getType()->getVectorNumElements(); |
| 1839 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && |
| 1840 | "Unexpected operand size"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1841 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1842 | // See if we're dealing with constant values. |
| 1843 | ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(1)); |
| 1844 | ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(2)); |
| 1845 | |
| 1846 | // Attempt to simplify to a constant or shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1847 | if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1848 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1849 | |
| 1850 | // EXTRQI only uses the lowest 64-bits of the first 128-bit vector |
| 1851 | // operand. |
| 1852 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1853 | II->setArgOperand(0, V); |
| 1854 | return II; |
| 1855 | } |
| 1856 | break; |
| 1857 | } |
| 1858 | |
| 1859 | case Intrinsic::x86_sse4a_insertq: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1860 | Value *Op0 = II->getArgOperand(0); |
| 1861 | Value *Op1 = II->getArgOperand(1); |
| 1862 | unsigned VWidth = Op0->getType()->getVectorNumElements(); |
| 1863 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 1864 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && |
| 1865 | Op1->getType()->getVectorNumElements() == 2 && |
| 1866 | "Unexpected operand size"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1867 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1868 | // See if we're dealing with constant values. |
| 1869 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 1870 | ConstantInt *CI11 = |
| 1871 | C1 ? dyn_cast<ConstantInt>(C1->getAggregateElement((unsigned)1)) |
| 1872 | : nullptr; |
| 1873 | |
| 1874 | // Attempt to simplify to a constant, shuffle vector or INSERTQI call. |
| 1875 | if (CI11) { |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 1876 | const APInt &V11 = CI11->getValue(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1877 | APInt Len = V11.zextOrTrunc(6); |
| 1878 | APInt Idx = V11.lshr(8).zextOrTrunc(6); |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1879 | if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1880 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | // INSERTQ only uses the lowest 64-bits of the first 128-bit vector |
| 1884 | // operand. |
| 1885 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1886 | II->setArgOperand(0, V); |
| 1887 | return II; |
| 1888 | } |
| 1889 | break; |
| 1890 | } |
| 1891 | |
Filipe Cabecinhas | 1a80595 | 2014-04-24 00:38:14 +0000 | [diff] [blame] | 1892 | case Intrinsic::x86_sse4a_insertqi: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1893 | // INSERTQI: Extract lowest Length bits from lower half of second source and |
| 1894 | // insert over first source starting at Index bit. The upper 64-bits are |
| 1895 | // undefined. |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1896 | Value *Op0 = II->getArgOperand(0); |
| 1897 | Value *Op1 = II->getArgOperand(1); |
| 1898 | unsigned VWidth0 = Op0->getType()->getVectorNumElements(); |
| 1899 | unsigned VWidth1 = Op1->getType()->getVectorNumElements(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1900 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 1901 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && |
| 1902 | VWidth1 == 2 && "Unexpected operand sizes"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1903 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1904 | // See if we're dealing with constant values. |
| 1905 | ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(2)); |
| 1906 | ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(3)); |
| 1907 | |
| 1908 | // Attempt to simplify to a constant or shuffle vector. |
| 1909 | if (CILength && CIIndex) { |
| 1910 | APInt Len = CILength->getValue().zextOrTrunc(6); |
| 1911 | APInt Idx = CIIndex->getValue().zextOrTrunc(6); |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1912 | if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1913 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | // INSERTQI only uses the lowest 64-bits of the first two 128-bit vector |
| 1917 | // operands. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1918 | bool MadeChange = false; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1919 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { |
| 1920 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1921 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1922 | } |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1923 | if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 1)) { |
| 1924 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1925 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1926 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 1927 | if (MadeChange) |
| 1928 | return II; |
Filipe Cabecinhas | 1a80595 | 2014-04-24 00:38:14 +0000 | [diff] [blame] | 1929 | break; |
| 1930 | } |
| 1931 | |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 1932 | case Intrinsic::x86_sse41_pblendvb: |
| 1933 | case Intrinsic::x86_sse41_blendvps: |
| 1934 | case Intrinsic::x86_sse41_blendvpd: |
| 1935 | case Intrinsic::x86_avx_blendv_ps_256: |
| 1936 | case Intrinsic::x86_avx_blendv_pd_256: |
| 1937 | case Intrinsic::x86_avx2_pblendvb: { |
| 1938 | // Convert blendv* to vector selects if the mask is constant. |
| 1939 | // This optimization is convoluted because the intrinsic is defined as |
| 1940 | // getting a vector of floats or doubles for the ps and pd versions. |
| 1941 | // FIXME: That should be changed. |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1942 | |
| 1943 | Value *Op0 = II->getArgOperand(0); |
| 1944 | Value *Op1 = II->getArgOperand(1); |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 1945 | Value *Mask = II->getArgOperand(2); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1946 | |
| 1947 | // fold (blend A, A, Mask) -> A |
| 1948 | if (Op0 == Op1) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1949 | return replaceInstUsesWith(CI, Op0); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1950 | |
| 1951 | // Zero Mask - select 1st argument. |
Simon Pilgrim | 93f59f5 | 2015-08-12 08:23:36 +0000 | [diff] [blame] | 1952 | if (isa<ConstantAggregateZero>(Mask)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1953 | return replaceInstUsesWith(CI, Op0); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Constant Mask - select 1st/2nd argument lane based on top bit of mask. |
Sanjay Patel | 368ac5d | 2016-02-21 17:29:33 +0000 | [diff] [blame] | 1956 | if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) { |
| 1957 | Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1958 | return SelectInst::Create(NewSelector, Op1, Op0, "blendv"); |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 1959 | } |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 1960 | break; |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Andrea Di Biagio | 0594e2a | 2015-09-30 16:44:39 +0000 | [diff] [blame] | 1963 | case Intrinsic::x86_ssse3_pshuf_b_128: |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 1964 | case Intrinsic::x86_avx2_pshuf_b: |
| 1965 | if (Value *V = simplifyX86pshufb(*II, *Builder)) |
| 1966 | return replaceInstUsesWith(*II, V); |
| 1967 | break; |
Andrea Di Biagio | 0594e2a | 2015-09-30 16:44:39 +0000 | [diff] [blame] | 1968 | |
Rafael Espindola | bad3f77 | 2014-04-21 22:06:04 +0000 | [diff] [blame] | 1969 | case Intrinsic::x86_avx_vpermilvar_ps: |
| 1970 | case Intrinsic::x86_avx_vpermilvar_ps_256: |
| 1971 | case Intrinsic::x86_avx_vpermilvar_pd: |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 1972 | case Intrinsic::x86_avx_vpermilvar_pd_256: |
| 1973 | if (Value *V = simplifyX86vpermilvar(*II, *Builder)) |
| 1974 | return replaceInstUsesWith(*II, V); |
| 1975 | break; |
Rafael Espindola | bad3f77 | 2014-04-21 22:06:04 +0000 | [diff] [blame] | 1976 | |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1977 | case Intrinsic::x86_avx2_permd: |
| 1978 | case Intrinsic::x86_avx2_permps: |
| 1979 | if (Value *V = simplifyX86vpermv(*II, *Builder)) |
| 1980 | return replaceInstUsesWith(*II, V); |
| 1981 | break; |
| 1982 | |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1983 | case Intrinsic::x86_avx_vperm2f128_pd_256: |
| 1984 | case Intrinsic::x86_avx_vperm2f128_ps_256: |
| 1985 | case Intrinsic::x86_avx_vperm2f128_si_256: |
Sanjay Patel | e304bea | 2015-03-24 22:39:29 +0000 | [diff] [blame] | 1986 | case Intrinsic::x86_avx2_vperm2i128: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1987 | if (Value *V = simplifyX86vperm2(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1988 | return replaceInstUsesWith(*II, V); |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1989 | break; |
| 1990 | |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1991 | case Intrinsic::x86_avx_maskload_ps: |
Sanjay Patel | 6f2c01f | 2016-02-29 23:59:00 +0000 | [diff] [blame] | 1992 | case Intrinsic::x86_avx_maskload_pd: |
| 1993 | case Intrinsic::x86_avx_maskload_ps_256: |
| 1994 | case Intrinsic::x86_avx_maskload_pd_256: |
| 1995 | case Intrinsic::x86_avx2_maskload_d: |
| 1996 | case Intrinsic::x86_avx2_maskload_q: |
| 1997 | case Intrinsic::x86_avx2_maskload_d_256: |
| 1998 | case Intrinsic::x86_avx2_maskload_q_256: |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1999 | if (Instruction *I = simplifyX86MaskedLoad(*II, *this)) |
| 2000 | return I; |
| 2001 | break; |
| 2002 | |
Sanjay Patel | c4acbae | 2016-03-12 15:16:59 +0000 | [diff] [blame] | 2003 | case Intrinsic::x86_sse2_maskmov_dqu: |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 2004 | case Intrinsic::x86_avx_maskstore_ps: |
| 2005 | case Intrinsic::x86_avx_maskstore_pd: |
| 2006 | case Intrinsic::x86_avx_maskstore_ps_256: |
| 2007 | case Intrinsic::x86_avx_maskstore_pd_256: |
Sanjay Patel | fc7e7eb | 2016-02-26 21:51:44 +0000 | [diff] [blame] | 2008 | case Intrinsic::x86_avx2_maskstore_d: |
| 2009 | case Intrinsic::x86_avx2_maskstore_q: |
| 2010 | case Intrinsic::x86_avx2_maskstore_d_256: |
| 2011 | case Intrinsic::x86_avx2_maskstore_q_256: |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 2012 | if (simplifyX86MaskedStore(*II, *this)) |
| 2013 | return nullptr; |
| 2014 | break; |
| 2015 | |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2016 | case Intrinsic::x86_xop_vpcomb: |
| 2017 | case Intrinsic::x86_xop_vpcomd: |
| 2018 | case Intrinsic::x86_xop_vpcomq: |
| 2019 | case Intrinsic::x86_xop_vpcomw: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2020 | if (Value *V = simplifyX86vpcom(*II, *Builder, true)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2021 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2022 | break; |
| 2023 | |
| 2024 | case Intrinsic::x86_xop_vpcomub: |
| 2025 | case Intrinsic::x86_xop_vpcomud: |
| 2026 | case Intrinsic::x86_xop_vpcomuq: |
| 2027 | case Intrinsic::x86_xop_vpcomuw: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2028 | if (Value *V = simplifyX86vpcom(*II, *Builder, false)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2029 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2032 | case Intrinsic::ppc_altivec_vperm: |
| 2033 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Bill Schmidt | a118463 | 2014-06-05 19:46:04 +0000 | [diff] [blame] | 2034 | // Note that ppc_altivec_vperm has a big-endian bias, so when creating |
| 2035 | // a vectorshuffle for little endian, we must undo the transformation |
| 2036 | // performed on vec_perm in altivec.h. That is, we must complement |
| 2037 | // the permutation mask with respect to 31 and reverse the order of |
| 2038 | // V1 and V2. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2039 | if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) { |
| 2040 | assert(Mask->getType()->getVectorNumElements() == 16 && |
| 2041 | "Bad type for intrinsic!"); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2043 | // Check that all of the elements are integer constants or undefs. |
| 2044 | bool AllEltsOk = true; |
| 2045 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2046 | Constant *Elt = Mask->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2047 | if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2048 | AllEltsOk = false; |
| 2049 | break; |
| 2050 | } |
| 2051 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2052 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2053 | if (AllEltsOk) { |
| 2054 | // Cast the input vectors to byte vectors. |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 2055 | Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0), |
| 2056 | Mask->getType()); |
| 2057 | Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1), |
| 2058 | Mask->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2059 | Value *Result = UndefValue::get(Op0->getType()); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2061 | // Only extract each element once. |
| 2062 | Value *ExtractedElts[32]; |
| 2063 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2065 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2066 | if (isa<UndefValue>(Mask->getAggregateElement(i))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2067 | continue; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2068 | unsigned Idx = |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2069 | cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2070 | Idx &= 31; // Match the hardware behavior. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2071 | if (DL.isLittleEndian()) |
Bill Schmidt | a118463 | 2014-06-05 19:46:04 +0000 | [diff] [blame] | 2072 | Idx = 31 - Idx; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2073 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2074 | if (!ExtractedElts[Idx]) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2075 | Value *Op0ToUse = (DL.isLittleEndian()) ? Op1 : Op0; |
| 2076 | Value *Op1ToUse = (DL.isLittleEndian()) ? Op0 : Op1; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2077 | ExtractedElts[Idx] = |
Bill Schmidt | a118463 | 2014-06-05 19:46:04 +0000 | [diff] [blame] | 2078 | Builder->CreateExtractElement(Idx < 16 ? Op0ToUse : Op1ToUse, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2079 | Builder->getInt32(Idx&15)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2080 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2082 | // Insert this value into the result vector. |
| 2083 | Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2084 | Builder->getInt32(i)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2085 | } |
| 2086 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
| 2087 | } |
| 2088 | } |
| 2089 | break; |
| 2090 | |
Bob Wilson | a4e231c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 2091 | case Intrinsic::arm_neon_vld1: |
| 2092 | case Intrinsic::arm_neon_vld2: |
| 2093 | case Intrinsic::arm_neon_vld3: |
| 2094 | case Intrinsic::arm_neon_vld4: |
| 2095 | case Intrinsic::arm_neon_vld2lane: |
| 2096 | case Intrinsic::arm_neon_vld3lane: |
| 2097 | case Intrinsic::arm_neon_vld4lane: |
| 2098 | case Intrinsic::arm_neon_vst1: |
| 2099 | case Intrinsic::arm_neon_vst2: |
| 2100 | case Intrinsic::arm_neon_vst3: |
| 2101 | case Intrinsic::arm_neon_vst4: |
| 2102 | case Intrinsic::arm_neon_vst2lane: |
| 2103 | case Intrinsic::arm_neon_vst3lane: |
| 2104 | case Intrinsic::arm_neon_vst4lane: { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2105 | unsigned MemAlign = |
| 2106 | getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT); |
Bob Wilson | a4e231c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 2107 | unsigned AlignArg = II->getNumArgOperands() - 1; |
| 2108 | ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg)); |
| 2109 | if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) { |
| 2110 | II->setArgOperand(AlignArg, |
| 2111 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 2112 | MemAlign, false)); |
| 2113 | return II; |
| 2114 | } |
| 2115 | break; |
| 2116 | } |
| 2117 | |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2118 | case Intrinsic::arm_neon_vmulls: |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2119 | case Intrinsic::arm_neon_vmullu: |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 2120 | case Intrinsic::aarch64_neon_smull: |
| 2121 | case Intrinsic::aarch64_neon_umull: { |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2122 | Value *Arg0 = II->getArgOperand(0); |
| 2123 | Value *Arg1 = II->getArgOperand(1); |
| 2124 | |
| 2125 | // Handle mul by zero first: |
| 2126 | if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2127 | return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType())); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | // Check for constant LHS & RHS - in this case we just simplify. |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2131 | bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu || |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 2132 | II->getIntrinsicID() == Intrinsic::aarch64_neon_umull); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2133 | VectorType *NewVT = cast<VectorType>(II->getType()); |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2134 | if (Constant *CV0 = dyn_cast<Constant>(Arg0)) { |
| 2135 | if (Constant *CV1 = dyn_cast<Constant>(Arg1)) { |
| 2136 | CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext); |
| 2137 | CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext); |
| 2138 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2139 | return replaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1)); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2142 | // Couldn't simplify - canonicalize constant to the RHS. |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2143 | std::swap(Arg0, Arg1); |
| 2144 | } |
| 2145 | |
| 2146 | // Handle mul by one: |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2147 | if (Constant *CV1 = dyn_cast<Constant>(Arg1)) |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2148 | if (ConstantInt *Splat = |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2149 | dyn_cast_or_null<ConstantInt>(CV1->getSplatValue())) |
| 2150 | if (Splat->isOne()) |
| 2151 | return CastInst::CreateIntegerCast(Arg0, II->getType(), |
| 2152 | /*isSigned=*/!Zext); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2153 | |
| 2154 | break; |
| 2155 | } |
| 2156 | |
Matt Arsenault | bef34e2 | 2016-01-22 21:30:34 +0000 | [diff] [blame] | 2157 | case Intrinsic::amdgcn_rcp: { |
Matt Arsenault | a0050b0 | 2014-06-19 01:19:19 +0000 | [diff] [blame] | 2158 | if (const ConstantFP *C = dyn_cast<ConstantFP>(II->getArgOperand(0))) { |
| 2159 | const APFloat &ArgVal = C->getValueAPF(); |
| 2160 | APFloat Val(ArgVal.getSemantics(), 1.0); |
| 2161 | APFloat::opStatus Status = Val.divide(ArgVal, |
| 2162 | APFloat::rmNearestTiesToEven); |
| 2163 | // Only do this if it was exact and therefore not dependent on the |
| 2164 | // rounding mode. |
| 2165 | if (Status == APFloat::opOK) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2166 | return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), Val)); |
Matt Arsenault | a0050b0 | 2014-06-19 01:19:19 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | break; |
| 2170 | } |
Matt Arsenault | 2fe4fbc | 2016-03-30 22:28:52 +0000 | [diff] [blame] | 2171 | case Intrinsic::amdgcn_frexp_mant: |
| 2172 | case Intrinsic::amdgcn_frexp_exp: { |
Matt Arsenault | 5cd4f8f | 2016-03-30 22:28:26 +0000 | [diff] [blame] | 2173 | Value *Src = II->getArgOperand(0); |
| 2174 | if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) { |
| 2175 | int Exp; |
| 2176 | APFloat Significand = frexp(C->getValueAPF(), Exp, |
| 2177 | APFloat::rmNearestTiesToEven); |
| 2178 | |
Matt Arsenault | 2fe4fbc | 2016-03-30 22:28:52 +0000 | [diff] [blame] | 2179 | if (II->getIntrinsicID() == Intrinsic::amdgcn_frexp_mant) { |
| 2180 | return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), |
| 2181 | Significand)); |
| 2182 | } |
| 2183 | |
| 2184 | // Match instruction special case behavior. |
| 2185 | if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf) |
| 2186 | Exp = 0; |
| 2187 | |
| 2188 | return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Exp)); |
| 2189 | } |
| 2190 | |
| 2191 | if (isa<UndefValue>(Src)) |
| 2192 | return replaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Matt Arsenault | 5cd4f8f | 2016-03-30 22:28:26 +0000 | [diff] [blame] | 2193 | |
| 2194 | break; |
| 2195 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2196 | case Intrinsic::stackrestore: { |
| 2197 | // If the save is right next to the restore, remove the restore. This can |
| 2198 | // happen when variable allocas are DCE'd. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 2199 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2200 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 2201 | if (&*++SS->getIterator() == II) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2202 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2203 | } |
| 2204 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2206 | // Scan down this block to see if there is another stack restore in the |
| 2207 | // same block without an intervening call/alloca. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 2208 | BasicBlock::iterator BI(II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2209 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 2210 | bool CannotRemove = false; |
| 2211 | for (++BI; &*BI != TI; ++BI) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 2212 | if (isa<AllocaInst>(BI)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2213 | CannotRemove = true; |
| 2214 | break; |
| 2215 | } |
| 2216 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 2217 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 2218 | // If there is a stackrestore below this one, remove this one. |
| 2219 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2220 | return eraseInstFromFunction(CI); |
Reid Kleckner | 892ae2e | 2016-02-27 00:53:54 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Bail if we cross over an intrinsic with side effects, such as |
| 2223 | // llvm.stacksave, llvm.read_register, or llvm.setjmp. |
| 2224 | if (II->mayHaveSideEffects()) { |
| 2225 | CannotRemove = true; |
| 2226 | break; |
| 2227 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2228 | } else { |
| 2229 | // If we found a non-intrinsic call, we can't remove the stack |
| 2230 | // restore. |
| 2231 | CannotRemove = true; |
| 2232 | break; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2236 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2237 | // If the stack restore is in a return, resume, or unwind block and if there |
| 2238 | // are no allocas or calls between the restore and the return, nuke the |
| 2239 | // restore. |
Bill Wendling | d5d95b0 | 2012-02-06 21:16:41 +0000 | [diff] [blame] | 2240 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2241 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2242 | break; |
| 2243 | } |
Vitaly Buka | f0500b6 | 2016-07-28 22:50:48 +0000 | [diff] [blame] | 2244 | case Intrinsic::lifetime_start: |
Vitaly Buka | 0ab23cf | 2016-07-28 22:59:03 +0000 | [diff] [blame] | 2245 | // Asan needs to poison memory to detect invalid access which is possible |
| 2246 | // even for empty lifetime range. |
| 2247 | if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress)) |
| 2248 | break; |
| 2249 | |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 2250 | if (removeTriviallyEmptyRange(*II, Intrinsic::lifetime_start, |
| 2251 | Intrinsic::lifetime_end, *this)) |
| 2252 | return nullptr; |
Arnaud A. de Grandmaison | 849f3bf | 2015-10-01 14:54:31 +0000 | [diff] [blame] | 2253 | break; |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2254 | case Intrinsic::assume: { |
David Majnemer | fcc5811 | 2016-04-08 16:37:12 +0000 | [diff] [blame] | 2255 | Value *IIOperand = II->getArgOperand(0); |
| 2256 | // Remove an assume if it is immediately followed by an identical assume. |
| 2257 | if (match(II->getNextNode(), |
| 2258 | m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand)))) |
| 2259 | return eraseInstFromFunction(CI); |
| 2260 | |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2261 | // Canonicalize assume(a && b) -> assume(a); assume(b); |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 2262 | // Note: New assumption intrinsics created here are registered by |
| 2263 | // the InstCombineIRInserter object. |
David Majnemer | fcc5811 | 2016-04-08 16:37:12 +0000 | [diff] [blame] | 2264 | Value *AssumeIntrinsic = II->getCalledValue(), *A, *B; |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2265 | if (match(IIOperand, m_And(m_Value(A), m_Value(B)))) { |
| 2266 | Builder->CreateCall(AssumeIntrinsic, A, II->getName()); |
| 2267 | Builder->CreateCall(AssumeIntrinsic, B, II->getName()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2268 | return eraseInstFromFunction(*II); |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2269 | } |
| 2270 | // assume(!(a || b)) -> assume(!a); assume(!b); |
| 2271 | if (match(IIOperand, m_Not(m_Or(m_Value(A), m_Value(B))))) { |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 2272 | Builder->CreateCall(AssumeIntrinsic, Builder->CreateNot(A), |
| 2273 | II->getName()); |
| 2274 | Builder->CreateCall(AssumeIntrinsic, Builder->CreateNot(B), |
| 2275 | II->getName()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2276 | return eraseInstFromFunction(*II); |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2277 | } |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 2278 | |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 2279 | // assume( (load addr) != null ) -> add 'nonnull' metadata to load |
| 2280 | // (if assume is valid at the load) |
| 2281 | if (ICmpInst* ICmp = dyn_cast<ICmpInst>(IIOperand)) { |
| 2282 | Value *LHS = ICmp->getOperand(0); |
| 2283 | Value *RHS = ICmp->getOperand(1); |
| 2284 | if (ICmpInst::ICMP_NE == ICmp->getPredicate() && |
| 2285 | isa<LoadInst>(LHS) && |
| 2286 | isa<Constant>(RHS) && |
| 2287 | RHS->getType()->isPointerTy() && |
| 2288 | cast<Constant>(RHS)->isNullValue()) { |
| 2289 | LoadInst* LI = cast<LoadInst>(LHS); |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2290 | if (isValidAssumeForContext(II, LI, &DT)) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2291 | MDNode *MD = MDNode::get(II->getContext(), None); |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 2292 | LI->setMetadata(LLVMContext::MD_nonnull, MD); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2293 | return eraseInstFromFunction(*II); |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
Chandler Carruth | 2496910 | 2015-02-10 08:07:32 +0000 | [diff] [blame] | 2296 | // TODO: apply nonnull return attributes to calls and invokes |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 2297 | // TODO: apply range metadata for range check patterns? |
| 2298 | } |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 2299 | // If there is a dominating assume with the same condition as this one, |
| 2300 | // then this one is redundant, and should be removed. |
Hal Finkel | 4564688 | 2014-10-05 00:53:02 +0000 | [diff] [blame] | 2301 | APInt KnownZero(1, 0), KnownOne(1, 0); |
| 2302 | computeKnownBits(IIOperand, KnownZero, KnownOne, 0, II); |
| 2303 | if (KnownOne.isAllOnesValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2304 | return eraseInstFromFunction(*II); |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 2305 | |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | } |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2308 | case Intrinsic::experimental_gc_relocate: { |
| 2309 | // Translate facts known about a pointer before relocating into |
| 2310 | // facts about the relocate value, while being careful to |
| 2311 | // preserve relocation semantics. |
Manuel Jacob | 83eefa6 | 2016-01-05 04:03:00 +0000 | [diff] [blame] | 2312 | Value *DerivedPtr = cast<GCRelocateInst>(II)->getDerivedPtr(); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2313 | |
| 2314 | // Remove the relocation if unused, note that this check is required |
| 2315 | // to prevent the cases below from looping forever. |
| 2316 | if (II->use_empty()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2317 | return eraseInstFromFunction(*II); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2318 | |
| 2319 | // Undef is undef, even after relocation. |
| 2320 | // TODO: provide a hook for this in GCStrategy. This is clearly legal for |
| 2321 | // most practical collectors, but there was discussion in the review thread |
| 2322 | // about whether it was legal for all possible collectors. |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 2323 | if (isa<UndefValue>(DerivedPtr)) |
| 2324 | // Use undef of gc_relocate's type to replace it. |
| 2325 | return replaceInstUsesWith(*II, UndefValue::get(II->getType())); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2326 | |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 2327 | if (auto *PT = dyn_cast<PointerType>(II->getType())) { |
| 2328 | // The relocation of null will be null for most any collector. |
| 2329 | // TODO: provide a hook for this in GCStrategy. There might be some |
| 2330 | // weird collector this property does not hold for. |
| 2331 | if (isa<ConstantPointerNull>(DerivedPtr)) |
| 2332 | // Use null-pointer of gc_relocate's type to replace it. |
| 2333 | return replaceInstUsesWith(*II, ConstantPointerNull::get(PT)); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 2334 | |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 2335 | // isKnownNonNull -> nonnull attribute |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2336 | if (isKnownNonNullAt(DerivedPtr, II, &DT)) |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 2337 | II->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 2338 | } |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2339 | |
| 2340 | // TODO: bitcast(relocate(p)) -> relocate(bitcast(p)) |
| 2341 | // Canonicalize on the type from the uses to the defs |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 2342 | |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2343 | // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...) |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 2344 | break; |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 2345 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | return visitCallSite(II); |
| 2349 | } |
| 2350 | |
| 2351 | // InvokeInst simplification |
| 2352 | // |
| 2353 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
| 2354 | return visitCallSite(&II); |
| 2355 | } |
| 2356 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 2357 | /// If this cast does not affect the value passed through the varargs area, we |
| 2358 | /// can eliminate the use of the cast. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2359 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2360 | const DataLayout &DL, |
| 2361 | const CastInst *const CI, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2362 | const int ix) { |
| 2363 | if (!CI->isLosslessCast()) |
| 2364 | return false; |
| 2365 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 2366 | // If this is a GC intrinsic, avoid munging types. We need types for |
| 2367 | // statepoint reconstruction in SelectionDAG. |
| 2368 | // TODO: This is probably something which should be expanded to all |
| 2369 | // intrinsics since the entire point of intrinsics is that |
| 2370 | // they are understandable by the optimizer. |
| 2371 | if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS)) |
| 2372 | return false; |
| 2373 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 2374 | // The size of ByVal or InAlloca arguments is derived from the type, so we |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2375 | // can't change to a type with a different size. If the size were |
| 2376 | // passed explicitly we could avoid this check. |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 2377 | if (!CS.isByValOrInAllocaArgument(ix)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2378 | return true; |
| 2379 | |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2380 | Type* SrcTy = |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2381 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2382 | Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2383 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 2384 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2385 | if (DL.getTypeAllocSize(SrcTy) != DL.getTypeAllocSize(DstTy)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2386 | return false; |
| 2387 | return true; |
| 2388 | } |
| 2389 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2390 | Instruction *InstCombiner::tryOptimizeCall(CallInst *CI) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2391 | if (!CI->getCalledFunction()) return nullptr; |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 2392 | |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 2393 | auto InstCombineRAUW = [this](Instruction *From, Value *With) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2394 | replaceInstUsesWith(*From, With); |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 2395 | }; |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2396 | LibCallSimplifier Simplifier(DL, &TLI, InstCombineRAUW); |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 2397 | if (Value *With = Simplifier.optimizeCall(CI)) { |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 2398 | ++NumSimplified; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2399 | return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With); |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 2400 | } |
Meador Inge | df796f8 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 2401 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2402 | return nullptr; |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2405 | static IntrinsicInst *findInitTrampolineFromAlloca(Value *TrampMem) { |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2406 | // Strip off at most one level of pointer casts, looking for an alloca. This |
| 2407 | // is good enough in practice and simpler than handling any number of casts. |
| 2408 | Value *Underlying = TrampMem->stripPointerCasts(); |
| 2409 | if (Underlying != TrampMem && |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2410 | (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2411 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2412 | if (!isa<AllocaInst>(Underlying)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2413 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2414 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2415 | IntrinsicInst *InitTrampoline = nullptr; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2416 | for (User *U : TrampMem->users()) { |
| 2417 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2418 | if (!II) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2419 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2420 | if (II->getIntrinsicID() == Intrinsic::init_trampoline) { |
| 2421 | if (InitTrampoline) |
| 2422 | // More than one init_trampoline writes to this value. Give up. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2423 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2424 | InitTrampoline = II; |
| 2425 | continue; |
| 2426 | } |
| 2427 | if (II->getIntrinsicID() == Intrinsic::adjust_trampoline) |
| 2428 | // Allow any number of calls to adjust.trampoline. |
| 2429 | continue; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2430 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | // No call to init.trampoline found. |
| 2434 | if (!InitTrampoline) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2435 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2436 | |
| 2437 | // Check that the alloca is being used in the expected way. |
| 2438 | if (InitTrampoline->getOperand(0) != TrampMem) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2439 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2440 | |
| 2441 | return InitTrampoline; |
| 2442 | } |
| 2443 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2444 | static IntrinsicInst *findInitTrampolineFromBB(IntrinsicInst *AdjustTramp, |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2445 | Value *TrampMem) { |
| 2446 | // Visit all the previous instructions in the basic block, and try to find a |
| 2447 | // init.trampoline which has a direct path to the adjust.trampoline. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 2448 | for (BasicBlock::iterator I = AdjustTramp->getIterator(), |
| 2449 | E = AdjustTramp->getParent()->begin(); |
| 2450 | I != E;) { |
| 2451 | Instruction *Inst = &*--I; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2452 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2453 | if (II->getIntrinsicID() == Intrinsic::init_trampoline && |
| 2454 | II->getOperand(0) == TrampMem) |
| 2455 | return II; |
| 2456 | if (Inst->mayWriteToMemory()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2457 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2458 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2459 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | // Given a call to llvm.adjust.trampoline, find and return the corresponding |
| 2463 | // call to llvm.init.trampoline if the call to the trampoline can be optimized |
| 2464 | // to a direct call to a function. Otherwise return NULL. |
| 2465 | // |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2466 | static IntrinsicInst *findInitTrampoline(Value *Callee) { |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2467 | Callee = Callee->stripPointerCasts(); |
| 2468 | IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee); |
| 2469 | if (!AdjustTramp || |
| 2470 | AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2471 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2472 | |
| 2473 | Value *TrampMem = AdjustTramp->getOperand(0); |
| 2474 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2475 | if (IntrinsicInst *IT = findInitTrampolineFromAlloca(TrampMem)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2476 | return IT; |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2477 | if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2478 | return IT; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2479 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 2482 | /// Improvements for call and invoke instructions. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2483 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Vitaly Buka | f0500b6 | 2016-07-28 22:50:48 +0000 | [diff] [blame] | 2484 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2485 | if (isAllocLikeFn(CS.getInstruction(), &TLI)) |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 2486 | return visitAllocSite(*CS.getInstruction()); |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2488 | bool Changed = false; |
| 2489 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 2490 | // Mark any parameters that are known to be non-null with the nonnull |
| 2491 | // attribute. This is helpful for inlining calls to functions with null |
| 2492 | // checks on their arguments. |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 2493 | SmallVector<unsigned, 4> Indices; |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 2494 | unsigned ArgNo = 0; |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 2495 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 2496 | for (Value *V : CS.args()) { |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 2497 | if (V->getType()->isPointerTy() && |
| 2498 | !CS.paramHasAttr(ArgNo + 1, Attribute::NonNull) && |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2499 | isKnownNonNullAt(V, CS.getInstruction(), &DT)) |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 2500 | Indices.push_back(ArgNo + 1); |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 2501 | ArgNo++; |
| 2502 | } |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 2503 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 2504 | assert(ArgNo == CS.arg_size() && "sanity check"); |
| 2505 | |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 2506 | if (!Indices.empty()) { |
| 2507 | AttributeSet AS = CS.getAttributes(); |
| 2508 | LLVMContext &Ctx = CS.getInstruction()->getContext(); |
| 2509 | AS = AS.addAttribute(Ctx, Indices, |
| 2510 | Attribute::get(Ctx, Attribute::NonNull)); |
| 2511 | CS.setAttributes(AS); |
| 2512 | Changed = true; |
| 2513 | } |
| 2514 | |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 2515 | // If the callee is a pointer to a function, attempt to move any casts to the |
| 2516 | // arguments of the call/invoke. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2517 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 2518 | if (!isa<Function>(Callee) && transformConstExprCastCall(CS)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2519 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2520 | |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 2521 | if (Function *CalleeF = dyn_cast<Function>(Callee)) { |
| 2522 | // Remove the convergent attr on calls when the callee is not convergent. |
Matt Arsenault | 802ebcb | 2016-06-20 19:04:44 +0000 | [diff] [blame] | 2523 | if (CS.isConvergent() && !CalleeF->isConvergent() && |
| 2524 | !CalleeF->isIntrinsic()) { |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 2525 | DEBUG(dbgs() << "Removing convergent attr from instr " |
| 2526 | << CS.getInstruction() << "\n"); |
| 2527 | CS.setNotConvergent(); |
| 2528 | return CS.getInstruction(); |
| 2529 | } |
| 2530 | |
Chris Lattner | 846a52e | 2010-02-01 18:11:34 +0000 | [diff] [blame] | 2531 | // If the call and callee calling conventions don't match, this call must |
| 2532 | // be unreachable, as the call is undefined. |
| 2533 | if (CalleeF->getCallingConv() != CS.getCallingConv() && |
| 2534 | // Only do this for calls to a function with a body. A prototype may |
| 2535 | // not actually end up matching the implementation's calling conv for a |
| 2536 | // variety of reasons (e.g. it may be written in assembly). |
| 2537 | !CalleeF->isDeclaration()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2538 | Instruction *OldCall = CS.getInstruction(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2539 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2540 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2541 | OldCall); |
Chad Rosier | e28ae30 | 2012-12-13 00:18:46 +0000 | [diff] [blame] | 2542 | // If OldCall does not return void then replaceAllUsesWith undef. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2543 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 2544 | if (!OldCall->getType()->isVoidTy()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2545 | replaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType())); |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 2546 | if (isa<CallInst>(OldCall)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2547 | return eraseInstFromFunction(*OldCall); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 2549 | // We cannot remove an invoke, because it would change the CFG, just |
| 2550 | // change the callee to a null pointer. |
Gabor Greif | febf6ab | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 2551 | cast<InvokeInst>(OldCall)->setCalledFunction( |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 2552 | Constant::getNullValue(CalleeF->getType())); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2553 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2554 | } |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 2555 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2556 | |
| 2557 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 2558 | // If CS does not return void then replaceAllUsesWith undef. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2559 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 2560 | if (!CS.getInstruction()->getType()->isVoidTy()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2561 | replaceInstUsesWith(*CS.getInstruction(), |
Eli Friedman | b9ed18f | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 2562 | UndefValue::get(CS.getInstruction()->getType())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2563 | |
Nuno Lopes | 771e7bd | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 2564 | if (isa<InvokeInst>(CS.getInstruction())) { |
| 2565 | // Can't remove an invoke because we cannot change the CFG. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2566 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2567 | } |
Nuno Lopes | 771e7bd | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 2568 | |
| 2569 | // This instruction is not reachable, just remove it. We insert a store to |
| 2570 | // undef so that we know that this code is not reachable, despite the fact |
| 2571 | // that we can't modify the CFG here. |
| 2572 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 2573 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
| 2574 | CS.getInstruction()); |
| 2575 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2576 | return eraseInstFromFunction(*CS.getInstruction()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2579 | if (IntrinsicInst *II = findInitTrampoline(Callee)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2580 | return transformCallThroughTrampoline(CS, II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2582 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 2583 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2584 | if (FTy->isVarArg()) { |
Eli Friedman | 7534b468 | 2011-11-29 01:18:23 +0000 | [diff] [blame] | 2585 | int ix = FTy->getNumParams(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2586 | // See if we can optimize any arguments passed through the varargs area of |
| 2587 | // the call. |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 2588 | for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2589 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 2590 | CastInst *CI = dyn_cast<CastInst>(*I); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2591 | if (CI && isSafeToEliminateVarargsCast(CS, DL, CI, ix)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2592 | *I = CI->getOperand(0); |
| 2593 | Changed = true; |
| 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
| 2599 | // Inline asm calls cannot throw - mark them 'nounwind'. |
| 2600 | CS.setDoesNotThrow(); |
| 2601 | Changed = true; |
| 2602 | } |
| 2603 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2604 | // Try to optimize the call if possible, we require DataLayout for most of |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 2605 | // this. None of these calls are seen as possibly dead so go ahead and |
| 2606 | // delete the instruction now. |
| 2607 | if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2608 | Instruction *I = tryOptimizeCall(CI); |
Eric Christopher | 1810d77 | 2010-03-06 10:59:25 +0000 | [diff] [blame] | 2609 | // If we changed something return the result, etc. Otherwise let |
| 2610 | // the fallthrough check. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2611 | if (I) return eraseInstFromFunction(*I); |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2614 | return Changed ? CS.getInstruction() : nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 2617 | /// If the callee is a constexpr cast of a function, attempt to move the cast to |
| 2618 | /// the arguments of the call/invoke. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2619 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 2620 | Function *Callee = |
| 2621 | dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2622 | if (!Callee) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2623 | return false; |
David Majnemer | 4c0a6e9 | 2015-01-21 22:32:04 +0000 | [diff] [blame] | 2624 | // The prototype of thunks are a lie, don't try to directly call such |
| 2625 | // functions. |
| 2626 | if (Callee->hasFnAttribute("thunk")) |
| 2627 | return false; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2628 | Instruction *Caller = CS.getInstruction(); |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 2629 | const AttributeSet &CallerPAL = CS.getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2630 | |
| 2631 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 2632 | // would cause a type conversion of one of our arguments, change this call to |
| 2633 | // be a direct call with arguments casted to the appropriate types. |
| 2634 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2635 | FunctionType *FT = Callee->getFunctionType(); |
| 2636 | Type *OldRetTy = Caller->getType(); |
| 2637 | Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2639 | // Check to see if we are changing the return type... |
| 2640 | if (OldRetTy != NewRetTy) { |
Nick Lewycky | a6a17d7 | 2014-01-18 22:47:12 +0000 | [diff] [blame] | 2641 | |
| 2642 | if (NewRetTy->isStructTy()) |
| 2643 | return false; // TODO: Handle multiple return values. |
| 2644 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2645 | if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) { |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 2646 | if (Callee->isDeclaration()) |
| 2647 | return false; // Cannot transform this return value. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2648 | |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 2649 | if (!Caller->use_empty() && |
| 2650 | // void -> non-void is handled specially |
| 2651 | !NewRetTy->isVoidTy()) |
Frederic Riss | c1892e2 | 2014-10-23 04:08:42 +0000 | [diff] [blame] | 2652 | return false; // Cannot transform this return value. |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 2653 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 2656 | AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex); |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 2657 | if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(NewRetTy))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2658 | return false; // Attribute not compatible with transformed value. |
| 2659 | } |
| 2660 | |
| 2661 | // If the callsite is an invoke instruction, and the return value is used by |
| 2662 | // a PHI node in a successor, we cannot change the return type of the call |
| 2663 | // because there is no place to put the cast instruction (without breaking |
| 2664 | // the critical edge). Bail out in this case. |
| 2665 | if (!Caller->use_empty()) |
| 2666 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2667 | for (User *U : II->users()) |
| 2668 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2669 | if (PN->getParent() == II->getNormalDest() || |
| 2670 | PN->getParent() == II->getUnwindDest()) |
| 2671 | return false; |
| 2672 | } |
| 2673 | |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 2674 | unsigned NumActualArgs = CS.arg_size(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2675 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 2676 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2677 | // Prevent us turning: |
| 2678 | // declare void @takes_i32_inalloca(i32* inalloca) |
| 2679 | // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0) |
| 2680 | // |
| 2681 | // into: |
| 2682 | // call void @takes_i32_inalloca(i32* null) |
David Majnemer | d61a6fd | 2015-03-11 18:03:05 +0000 | [diff] [blame] | 2683 | // |
| 2684 | // Similarly, avoid folding away bitcasts of byval calls. |
| 2685 | if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) || |
| 2686 | Callee->getAttributes().hasAttrSomewhere(Attribute::ByVal)) |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2687 | return false; |
| 2688 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2689 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 2690 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2691 | Type *ParamTy = FT->getParamType(i); |
| 2692 | Type *ActTy = (*AI)->getType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2693 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2694 | if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2695 | return false; // Cannot transform this parameter value. |
| 2696 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2697 | if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1). |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 2698 | overlaps(AttributeFuncs::typeIncompatible(ParamTy))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2699 | return false; // Attribute not compatible with transformed value. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2700 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 2701 | if (CS.isInAllocaArgument(i)) |
| 2702 | return false; // Cannot transform to and from inalloca. |
| 2703 | |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 2704 | // If the parameter is passed as a byval argument, then we have to have a |
| 2705 | // sized type and the sized type has to have the same size as the old type. |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2706 | if (ParamTy != ActTy && |
| 2707 | CallerPAL.getParamAttributes(i + 1).hasAttribute(i + 1, |
| 2708 | Attribute::ByVal)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2709 | PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2710 | if (!ParamPTy || !ParamPTy->getElementType()->isSized()) |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 2711 | return false; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2712 | |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 2713 | Type *CurElTy = ActTy->getPointerElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2714 | if (DL.getTypeAllocSize(CurElTy) != |
| 2715 | DL.getTypeAllocSize(ParamPTy->getElementType())) |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 2716 | return false; |
| 2717 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 2720 | if (Callee->isDeclaration()) { |
| 2721 | // Do not delete arguments unless we have a function body. |
| 2722 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg()) |
| 2723 | return false; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2724 | |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 2725 | // If the callee is just a declaration, don't change the varargsness of the |
| 2726 | // call. We don't want to introduce a varargs call where one doesn't |
| 2727 | // already exist. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2728 | PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 2729 | if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg()) |
| 2730 | return false; |
Jim Grosbach | e84ae7b | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 2731 | |
| 2732 | // If both the callee and the cast type are varargs, we still have to make |
| 2733 | // sure the number of fixed parameters are the same or we have the same |
| 2734 | // ABI issues as if we introduce a varargs call. |
Jim Grosbach | 1df8cdc | 2012-02-03 00:26:07 +0000 | [diff] [blame] | 2735 | if (FT->isVarArg() && |
| 2736 | cast<FunctionType>(APTy->getElementType())->isVarArg() && |
| 2737 | FT->getNumParams() != |
Jim Grosbach | e84ae7b | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 2738 | cast<FunctionType>(APTy->getElementType())->getNumParams()) |
| 2739 | return false; |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 2740 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2741 | |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 2742 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 2743 | !CallerPAL.isEmpty()) |
| 2744 | // In this case we have more arguments than the new function type, but we |
| 2745 | // won't be dropping them. Check that these extra arguments have attributes |
| 2746 | // that are compatible with being a vararg call argument. |
| 2747 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 2748 | unsigned Index = CallerPAL.getSlotIndex(i - 1); |
| 2749 | if (Index <= FT->getNumParams()) |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 2750 | break; |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 2751 | |
Bill Wendling | d97b75d | 2012-12-19 08:57:40 +0000 | [diff] [blame] | 2752 | // Check if it has an attribute that's incompatible with varargs. |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 2753 | AttributeSet PAttrs = CallerPAL.getSlotAttributes(i - 1); |
| 2754 | if (PAttrs.hasAttribute(Index, Attribute::StructRet)) |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 2755 | return false; |
| 2756 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2757 | |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2758 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2759 | // Okay, we decided that this is a safe thing to do: go ahead and start |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 2760 | // inserting cast instructions as necessary. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2761 | std::vector<Value*> Args; |
| 2762 | Args.reserve(NumActualArgs); |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2763 | SmallVector<AttributeSet, 8> attrVec; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2764 | attrVec.reserve(NumCommonArgs); |
| 2765 | |
| 2766 | // Get any return attributes. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 2767 | AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2768 | |
| 2769 | // If the return value is not being used, the type may not be compatible |
| 2770 | // with the existing attributes. Wipe out any problematic attributes. |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 2771 | RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2772 | |
| 2773 | // Add the new return attributes. |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 2774 | if (RAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2775 | attrVec.push_back(AttributeSet::get(Caller->getContext(), |
| 2776 | AttributeSet::ReturnIndex, RAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2777 | |
| 2778 | AI = CS.arg_begin(); |
| 2779 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2780 | Type *ParamTy = FT->getParamType(i); |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2782 | if ((*AI)->getType() == ParamTy) { |
| 2783 | Args.push_back(*AI); |
| 2784 | } else { |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2785 | Args.push_back(Builder->CreateBitOrPointerCast(*AI, ParamTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | // Add any parameter attributes. |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2789 | AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1); |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 2790 | if (PAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2791 | attrVec.push_back(AttributeSet::get(Caller->getContext(), i + 1, |
| 2792 | PAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | // If the function takes more arguments than the call was taking, add them |
| 2796 | // now. |
| 2797 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 2798 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 2799 | |
| 2800 | // If we are removing arguments to the function, emit an obnoxious warning. |
| 2801 | if (FT->getNumParams() < NumActualArgs) { |
Nick Lewycky | 90053a1 | 2012-12-26 22:00:35 +0000 | [diff] [blame] | 2802 | // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722 |
| 2803 | if (FT->isVarArg()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2804 | // Add all of the arguments in their promoted form to the arg list. |
| 2805 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2806 | Type *PTy = getPromotedType((*AI)->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2807 | if (PTy != (*AI)->getType()) { |
| 2808 | // Must promote to pass through va_arg area! |
| 2809 | Instruction::CastOps opcode = |
| 2810 | CastInst::getCastOpcode(*AI, false, PTy, false); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2811 | Args.push_back(Builder->CreateCast(opcode, *AI, PTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2812 | } else { |
| 2813 | Args.push_back(*AI); |
| 2814 | } |
| 2815 | |
| 2816 | // Add any parameter attributes. |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2817 | AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1); |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 2818 | if (PAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2819 | attrVec.push_back(AttributeSet::get(FT->getContext(), i + 1, |
| 2820 | PAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | } |
| 2824 | |
Bill Wendling | bd4ea16 | 2013-01-21 21:57:28 +0000 | [diff] [blame] | 2825 | AttributeSet FnAttrs = CallerPAL.getFnAttributes(); |
Bill Wendling | 7754389 | 2013-01-18 21:11:39 +0000 | [diff] [blame] | 2826 | if (CallerPAL.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2827 | attrVec.push_back(AttributeSet::get(Callee->getContext(), FnAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2828 | |
| 2829 | if (NewRetTy->isVoidTy()) |
| 2830 | Caller->setName(""); // Void type should not have a name. |
| 2831 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 2832 | const AttributeSet &NewCallerPAL = AttributeSet::get(Callee->getContext(), |
Bill Wendling | bd4ea16 | 2013-01-21 21:57:28 +0000 | [diff] [blame] | 2833 | attrVec); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2834 | |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 2835 | SmallVector<OperandBundleDef, 1> OpBundles; |
Sanjoy Das | c521c7b | 2015-11-25 00:42:24 +0000 | [diff] [blame] | 2836 | CS.getOperandBundlesAsDefs(OpBundles); |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2838 | Instruction *NC; |
| 2839 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 2840 | NC = Builder->CreateInvoke(Callee, II->getNormalDest(), II->getUnwindDest(), |
| 2841 | Args, OpBundles); |
Eli Friedman | 96254a0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 2842 | NC->takeName(II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2843 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
| 2844 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
| 2845 | } else { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2846 | CallInst *CI = cast<CallInst>(Caller); |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 2847 | NC = Builder->CreateCall(Callee, Args, OpBundles); |
Eli Friedman | 96254a0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 2848 | NC->takeName(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2849 | if (CI->isTailCall()) |
| 2850 | cast<CallInst>(NC)->setTailCall(); |
| 2851 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
| 2852 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
| 2853 | } |
| 2854 | |
| 2855 | // Insert a cast of the return type as necessary. |
| 2856 | Value *NV = NC; |
| 2857 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
| 2858 | if (!NV->getType()->isVoidTy()) { |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 2859 | NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 2860 | NC->setDebugLoc(Caller->getDebugLoc()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2861 | |
| 2862 | // If this is an invoke instruction, we should insert it after the first |
| 2863 | // non-phi, instruction in the normal successor block. |
| 2864 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Bill Wendling | 07efd6f | 2011-08-25 01:08:34 +0000 | [diff] [blame] | 2865 | BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2866 | InsertNewInstBefore(NC, *I); |
| 2867 | } else { |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 2868 | // Otherwise, it's a call, just insert cast right after the call. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2869 | InsertNewInstBefore(NC, *Caller); |
| 2870 | } |
| 2871 | Worklist.AddUsersToWorkList(*Caller); |
| 2872 | } else { |
| 2873 | NV = UndefValue::get(Caller->getType()); |
| 2874 | } |
| 2875 | } |
| 2876 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2877 | if (!Caller->use_empty()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2878 | replaceInstUsesWith(*Caller, NV); |
Frederic Riss | c1892e2 | 2014-10-23 04:08:42 +0000 | [diff] [blame] | 2879 | else if (Caller->hasValueHandle()) { |
| 2880 | if (OldRetTy == NV->getType()) |
| 2881 | ValueHandleBase::ValueIsRAUWd(Caller, NV); |
| 2882 | else |
| 2883 | // We cannot call ValueIsRAUWd with a different type, and the |
| 2884 | // actual tracked value will disappear. |
| 2885 | ValueHandleBase::ValueIsDeleted(Caller); |
| 2886 | } |
Eli Friedman | b9ed18f | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 2887 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2888 | eraseInstFromFunction(*Caller); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2889 | return true; |
| 2890 | } |
| 2891 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 2892 | /// Turn a call to a function created by init_trampoline / adjust_trampoline |
| 2893 | /// intrinsic pair into a direct call to the underlying function. |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2894 | Instruction * |
| 2895 | InstCombiner::transformCallThroughTrampoline(CallSite CS, |
| 2896 | IntrinsicInst *Tramp) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2897 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2898 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 2899 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 2900 | const AttributeSet &Attrs = CS.getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2901 | |
| 2902 | // If the call already has the 'nest' attribute somewhere then give up - |
| 2903 | // otherwise 'nest' would occur twice after splicing in the chain. |
Bill Wendling | 6e95ae8 | 2012-12-31 00:49:59 +0000 | [diff] [blame] | 2904 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2905 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2906 | |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2907 | assert(Tramp && |
| 2908 | "transformCallThroughTrampoline called with incorrect CallSite."); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2909 | |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 2910 | Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts()); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 2911 | FunctionType *NestFTy = cast<FunctionType>(NestF->getValueType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2912 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 2913 | const AttributeSet &NestAttrs = NestF->getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2914 | if (!NestAttrs.isEmpty()) { |
| 2915 | unsigned NestIdx = 1; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2916 | Type *NestTy = nullptr; |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2917 | AttributeSet NestAttr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2918 | |
| 2919 | // Look for a parameter marked with the 'nest' attribute. |
| 2920 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 2921 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2922 | if (NestAttrs.hasAttribute(NestIdx, Attribute::Nest)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2923 | // Record the parameter type and any other attributes. |
| 2924 | NestTy = *I; |
| 2925 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
| 2926 | break; |
| 2927 | } |
| 2928 | |
| 2929 | if (NestTy) { |
| 2930 | Instruction *Caller = CS.getInstruction(); |
| 2931 | std::vector<Value*> NewArgs; |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 2932 | NewArgs.reserve(CS.arg_size() + 1); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2933 | |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2934 | SmallVector<AttributeSet, 8> NewAttrs; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2935 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
| 2936 | |
| 2937 | // Insert the nest argument into the call argument list, which may |
| 2938 | // mean appending it. Likewise for attributes. |
| 2939 | |
| 2940 | // Add any result attributes. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 2941 | if (Attrs.hasAttributes(AttributeSet::ReturnIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2942 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 2943 | Attrs.getRetAttributes())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2944 | |
| 2945 | { |
| 2946 | unsigned Idx = 1; |
| 2947 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 2948 | do { |
| 2949 | if (Idx == NestIdx) { |
| 2950 | // Add the chain argument and attributes. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 2951 | Value *NestVal = Tramp->getArgOperand(2); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2952 | if (NestVal->getType() != NestTy) |
Eli Friedman | 41e509a | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 2953 | NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest"); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2954 | NewArgs.push_back(NestVal); |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2955 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 2956 | NestAttr)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | if (I == E) |
| 2960 | break; |
| 2961 | |
| 2962 | // Add the original argument and attributes. |
| 2963 | NewArgs.push_back(*I); |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2964 | AttributeSet Attr = Attrs.getParamAttributes(Idx); |
| 2965 | if (Attr.hasAttributes(Idx)) { |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2966 | AttrBuilder B(Attr, Idx); |
| 2967 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 2968 | Idx + (Idx >= NestIdx), B)); |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 2969 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2970 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 2971 | ++Idx; |
| 2972 | ++I; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2973 | } while (1); |
| 2974 | } |
| 2975 | |
| 2976 | // Add any function attributes. |
Bill Wendling | 7754389 | 2013-01-18 21:11:39 +0000 | [diff] [blame] | 2977 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 2978 | NewAttrs.push_back(AttributeSet::get(FTy->getContext(), |
| 2979 | Attrs.getFnAttributes())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2980 | |
| 2981 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 2982 | // Handle this by synthesizing a new function type, equal to FTy |
| 2983 | // with the chain parameter inserted. |
| 2984 | |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 2985 | std::vector<Type*> NewTypes; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2986 | NewTypes.reserve(FTy->getNumParams()+1); |
| 2987 | |
| 2988 | // Insert the chain's type into the list of parameter types, which may |
| 2989 | // mean appending it. |
| 2990 | { |
| 2991 | unsigned Idx = 1; |
| 2992 | FunctionType::param_iterator I = FTy->param_begin(), |
| 2993 | E = FTy->param_end(); |
| 2994 | |
| 2995 | do { |
| 2996 | if (Idx == NestIdx) |
| 2997 | // Add the chain's type. |
| 2998 | NewTypes.push_back(NestTy); |
| 2999 | |
| 3000 | if (I == E) |
| 3001 | break; |
| 3002 | |
| 3003 | // Add the original type. |
| 3004 | NewTypes.push_back(*I); |
| 3005 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 3006 | ++Idx; |
| 3007 | ++I; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3008 | } while (1); |
| 3009 | } |
| 3010 | |
| 3011 | // Replace the trampoline call with a direct call. Let the generic |
| 3012 | // code sort out any function type mismatches. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3013 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3014 | FTy->isVarArg()); |
| 3015 | Constant *NewCallee = |
| 3016 | NestF->getType() == PointerType::getUnqual(NewFTy) ? |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3017 | NestF : ConstantExpr::getBitCast(NestF, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3018 | PointerType::getUnqual(NewFTy)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 3019 | const AttributeSet &NewPAL = |
| 3020 | AttributeSet::get(FTy->getContext(), NewAttrs); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3021 | |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3022 | SmallVector<OperandBundleDef, 1> OpBundles; |
| 3023 | CS.getOperandBundlesAsDefs(OpBundles); |
| 3024 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3025 | Instruction *NewCaller; |
| 3026 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 3027 | NewCaller = InvokeInst::Create(NewCallee, |
| 3028 | II->getNormalDest(), II->getUnwindDest(), |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3029 | NewArgs, OpBundles); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3030 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
| 3031 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
| 3032 | } else { |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3033 | NewCaller = CallInst::Create(NewCallee, NewArgs, OpBundles); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3034 | if (cast<CallInst>(Caller)->isTailCall()) |
| 3035 | cast<CallInst>(NewCaller)->setTailCall(); |
| 3036 | cast<CallInst>(NewCaller)-> |
| 3037 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
| 3038 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
| 3039 | } |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 3040 | |
| 3041 | return NewCaller; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 3046 | // parameter, there is no need to adjust the argument list. Let the generic |
| 3047 | // code sort out any function type mismatches. |
| 3048 | Constant *NewCallee = |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3049 | NestF->getType() == PTy ? NestF : |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3050 | ConstantExpr::getBitCast(NestF, PTy); |
| 3051 | CS.setCalledFunction(NewCallee); |
| 3052 | return CS.getInstruction(); |
| 3053 | } |