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