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" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/ADT/APInt.h" |
| 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/None.h" |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/Twine.h" |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/MemoryBuiltins.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
| 26 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 27 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Constant.h" |
| 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/DerivedTypes.h" |
| 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/GlobalVariable.h" |
| 33 | #include "llvm/IR/InstrTypes.h" |
| 34 | #include "llvm/IR/Instruction.h" |
| 35 | #include "llvm/IR/Instructions.h" |
| 36 | #include "llvm/IR/IntrinsicInst.h" |
| 37 | #include "llvm/IR/Intrinsics.h" |
| 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 40 | #include "llvm/IR/PatternMatch.h" |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Statepoint.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Type.h" |
| 43 | #include "llvm/IR/Value.h" |
| 44 | #include "llvm/IR/ValueHandle.h" |
| 45 | #include "llvm/Support/Casting.h" |
| 46 | #include "llvm/Support/Debug.h" |
| 47 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 6fcd32e | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/SimplifyLibCalls.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 50 | #include <algorithm> |
| 51 | #include <cassert> |
| 52 | #include <cstdint> |
| 53 | #include <cstring> |
| 54 | #include <vector> |
| 55 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 56 | using namespace llvm; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 57 | using namespace PatternMatch; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 58 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 59 | #define DEBUG_TYPE "instcombine" |
| 60 | |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 61 | STATISTIC(NumSimplified, "Number of library calls simplified"); |
| 62 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 63 | /// Return the specified type promoted as it would be to pass though a va_arg |
| 64 | /// area. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 65 | static Type *getPromotedType(Type *Ty) { |
| 66 | if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 67 | if (ITy->getBitWidth() < 32) |
| 68 | return Type::getInt32Ty(Ty->getContext()); |
| 69 | } |
| 70 | return Ty; |
| 71 | } |
| 72 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 73 | /// Given an aggregate type which ultimately holds a single scalar element, |
| 74 | /// like {{{type}}} or [1 x type], return type. |
Dan Gohman | d0080c4 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 75 | static Type *reduceToSingleValueType(Type *T) { |
| 76 | while (!T->isSingleValueType()) { |
| 77 | if (StructType *STy = dyn_cast<StructType>(T)) { |
| 78 | if (STy->getNumElements() == 1) |
| 79 | T = STy->getElementType(0); |
| 80 | else |
| 81 | break; |
| 82 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(T)) { |
| 83 | if (ATy->getNumElements() == 1) |
| 84 | T = ATy->getElementType(); |
| 85 | else |
| 86 | break; |
| 87 | } else |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | return T; |
| 92 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 93 | |
Sanjay Patel | 368ac5d | 2016-02-21 17:29:33 +0000 | [diff] [blame] | 94 | /// Return a constant boolean vector that has true elements in all positions |
Sanjay Patel | 2440130 | 2016-02-21 17:33:31 +0000 | [diff] [blame] | 95 | /// 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] | 96 | static Constant *getNegativeIsTrueBoolVec(ConstantDataVector *V) { |
| 97 | SmallVector<Constant *, 32> BoolVec; |
| 98 | IntegerType *BoolTy = Type::getInt1Ty(V->getContext()); |
| 99 | for (unsigned I = 0, E = V->getNumElements(); I != E; ++I) { |
| 100 | Constant *Elt = V->getElementAsConstant(I); |
| 101 | assert((isa<ConstantInt>(Elt) || isa<ConstantFP>(Elt)) && |
| 102 | "Unexpected constant data vector element type"); |
| 103 | bool Sign = V->getElementType()->isIntegerTy() |
| 104 | ? cast<ConstantInt>(Elt)->isNegative() |
| 105 | : cast<ConstantFP>(Elt)->isNegative(); |
| 106 | BoolVec.push_back(ConstantInt::get(BoolTy, Sign)); |
| 107 | } |
| 108 | return ConstantVector::get(BoolVec); |
| 109 | } |
| 110 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 111 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 112 | unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), DL, MI, &AC, &DT); |
| 113 | unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), DL, MI, &AC, &DT); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 114 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 115 | unsigned CopyAlign = MI->getAlignment(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 116 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 117 | if (CopyAlign < MinAlign) { |
| 118 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), MinAlign, false)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 119 | return MI; |
| 120 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 122 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 123 | // load/store. |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 124 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 125 | if (!MemOpLength) return nullptr; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 127 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 128 | // if the size is something we can handle with a single primitive load/store. |
| 129 | // A single load+store correctly handles overlapping memory in the memmove |
| 130 | // case. |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 131 | uint64_t Size = MemOpLength->getLimitedValue(); |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 132 | assert(Size && "0-sized memory transferring should be removed already."); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 134 | if (Size > 8 || (Size&(Size-1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 135 | return nullptr; // If not 1/2/4/8 bytes, exit. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 137 | // Use an integer load+store unless we can find something better. |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 138 | unsigned SrcAddrSp = |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 139 | cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace(); |
Gabor Greif | f375520 | 2010-04-16 15:33:14 +0000 | [diff] [blame] | 140 | unsigned DstAddrSp = |
Gabor Greif | 0a136c9 | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 141 | cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace(); |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 143 | IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3); |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 144 | Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp); |
| 145 | Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 147 | // Memcpy forces the use of i8* for the source and destination. That means |
| 148 | // that if you're using memcpy to move one double around, you'll get a cast |
| 149 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 150 | // an i64 load+store, here because this improves the odds that the source or |
| 151 | // dest address will be promotable. See if we can find a better type than the |
| 152 | // integer datatype. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 153 | Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 154 | MDNode *CopyMD = nullptr; |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 155 | if (StrippedDest != MI->getArgOperand(0)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 156 | Type *SrcETy = cast<PointerType>(StrippedDest->getType()) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 157 | ->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 158 | if (SrcETy->isSized() && DL.getTypeStoreSize(SrcETy) == Size) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 159 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 160 | // down through these levels if so. |
Dan Gohman | d0080c4 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 161 | SrcETy = reduceToSingleValueType(SrcETy); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 162 | |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 163 | if (SrcETy->isSingleValueType()) { |
| 164 | NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp); |
| 165 | NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 166 | |
| 167 | // If the memcpy has metadata describing the members, see if we can |
| 168 | // get the TBAA tag describing our copy. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 169 | if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 170 | if (M->getNumOperands() == 3 && M->getOperand(0) && |
| 171 | mdconst::hasa<ConstantInt>(M->getOperand(0)) && |
| 172 | mdconst::extract<ConstantInt>(M->getOperand(0))->isNullValue() && |
Nick Lewycky | 49ac81a | 2012-10-11 02:05:23 +0000 | [diff] [blame] | 173 | M->getOperand(1) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 174 | mdconst::hasa<ConstantInt>(M->getOperand(1)) && |
| 175 | mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() == |
| 176 | Size && |
| 177 | M->getOperand(2) && isa<MDNode>(M->getOperand(2))) |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 178 | CopyMD = cast<MDNode>(M->getOperand(2)); |
| 179 | } |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 184 | // If the memcpy/memmove provides better alignment info than we can |
| 185 | // infer, use it. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 186 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 187 | DstAlign = std::max(DstAlign, CopyAlign); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 188 | |
Gabor Greif | 5f3e656 | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 189 | Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy); |
| 190 | Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy); |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 191 | LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile()); |
| 192 | L->setAlignment(SrcAlign); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 193 | if (CopyMD) |
| 194 | L->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Dorit Nuzman | abd15f6 | 2016-09-04 07:49:39 +0000 | [diff] [blame] | 195 | MDNode *LoopMemParallelMD = |
| 196 | MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access); |
| 197 | if (LoopMemParallelMD) |
| 198 | L->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD); |
Dorit Nuzman | 7673ba7 | 2016-09-04 07:06:00 +0000 | [diff] [blame] | 199 | |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 200 | StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile()); |
| 201 | S->setAlignment(DstAlign); |
Dan Gohman | 3f553c2 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 202 | if (CopyMD) |
| 203 | S->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Dorit Nuzman | abd15f6 | 2016-09-04 07:49:39 +0000 | [diff] [blame] | 204 | if (LoopMemParallelMD) |
| 205 | S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 206 | |
| 207 | // 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] | 208 | MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 209 | return MI; |
| 210 | } |
| 211 | |
| 212 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 213 | unsigned Alignment = getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 214 | if (MI->getAlignment() < Alignment) { |
| 215 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
| 216 | Alignment, false)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 217 | return MI; |
| 218 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 220 | // Extract the length and alignment and fill if they are constant. |
| 221 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 222 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 223 | if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 224 | return nullptr; |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 225 | uint64_t Len = LenC->getLimitedValue(); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 226 | Alignment = MI->getAlignment(); |
Michael Liao | 69e172a | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 227 | assert(Len && "0-sized memory setting should be removed already."); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 229 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 230 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 231 | Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 233 | Value *Dest = MI->getDest(); |
Mon P Wang | 1991c47 | 2010-12-20 01:05:30 +0000 | [diff] [blame] | 234 | unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace(); |
| 235 | Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp); |
| 236 | Dest = Builder->CreateBitCast(Dest, NewDstPtrTy); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 237 | |
| 238 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 239 | if (Alignment == 0) Alignment = 1; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 241 | // Extract the fill value and store. |
| 242 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 243 | StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest, |
| 244 | MI->isVolatile()); |
| 245 | S->setAlignment(Alignment); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 247 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 248 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 249 | return MI; |
| 250 | } |
| 251 | |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 252 | return nullptr; |
| 253 | } |
| 254 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 255 | static Value *simplifyX86immShift(const IntrinsicInst &II, |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 256 | InstCombiner::BuilderTy &Builder) { |
| 257 | bool LogicalShift = false; |
| 258 | bool ShiftLeft = false; |
| 259 | |
| 260 | switch (II.getIntrinsicID()) { |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 261 | default: llvm_unreachable("Unexpected intrinsic!"); |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 262 | case Intrinsic::x86_sse2_psra_d: |
| 263 | case Intrinsic::x86_sse2_psra_w: |
| 264 | case Intrinsic::x86_sse2_psrai_d: |
| 265 | case Intrinsic::x86_sse2_psrai_w: |
| 266 | case Intrinsic::x86_avx2_psra_d: |
| 267 | case Intrinsic::x86_avx2_psra_w: |
| 268 | case Intrinsic::x86_avx2_psrai_d: |
| 269 | case Intrinsic::x86_avx2_psrai_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 270 | case Intrinsic::x86_avx512_psra_q_128: |
| 271 | case Intrinsic::x86_avx512_psrai_q_128: |
| 272 | case Intrinsic::x86_avx512_psra_q_256: |
| 273 | case Intrinsic::x86_avx512_psrai_q_256: |
| 274 | case Intrinsic::x86_avx512_psra_d_512: |
| 275 | case Intrinsic::x86_avx512_psra_q_512: |
| 276 | case Intrinsic::x86_avx512_psra_w_512: |
| 277 | case Intrinsic::x86_avx512_psrai_d_512: |
| 278 | case Intrinsic::x86_avx512_psrai_q_512: |
| 279 | case Intrinsic::x86_avx512_psrai_w_512: |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 280 | LogicalShift = false; ShiftLeft = false; |
| 281 | break; |
| 282 | case Intrinsic::x86_sse2_psrl_d: |
| 283 | case Intrinsic::x86_sse2_psrl_q: |
| 284 | case Intrinsic::x86_sse2_psrl_w: |
| 285 | case Intrinsic::x86_sse2_psrli_d: |
| 286 | case Intrinsic::x86_sse2_psrli_q: |
| 287 | case Intrinsic::x86_sse2_psrli_w: |
| 288 | case Intrinsic::x86_avx2_psrl_d: |
| 289 | case Intrinsic::x86_avx2_psrl_q: |
| 290 | case Intrinsic::x86_avx2_psrl_w: |
| 291 | case Intrinsic::x86_avx2_psrli_d: |
| 292 | case Intrinsic::x86_avx2_psrli_q: |
| 293 | case Intrinsic::x86_avx2_psrli_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 294 | case Intrinsic::x86_avx512_psrl_d_512: |
| 295 | case Intrinsic::x86_avx512_psrl_q_512: |
| 296 | case Intrinsic::x86_avx512_psrl_w_512: |
| 297 | case Intrinsic::x86_avx512_psrli_d_512: |
| 298 | case Intrinsic::x86_avx512_psrli_q_512: |
| 299 | case Intrinsic::x86_avx512_psrli_w_512: |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 300 | LogicalShift = true; ShiftLeft = false; |
| 301 | break; |
| 302 | case Intrinsic::x86_sse2_psll_d: |
| 303 | case Intrinsic::x86_sse2_psll_q: |
| 304 | case Intrinsic::x86_sse2_psll_w: |
| 305 | case Intrinsic::x86_sse2_pslli_d: |
| 306 | case Intrinsic::x86_sse2_pslli_q: |
| 307 | case Intrinsic::x86_sse2_pslli_w: |
| 308 | case Intrinsic::x86_avx2_psll_d: |
| 309 | case Intrinsic::x86_avx2_psll_q: |
| 310 | case Intrinsic::x86_avx2_psll_w: |
| 311 | case Intrinsic::x86_avx2_pslli_d: |
| 312 | case Intrinsic::x86_avx2_pslli_q: |
| 313 | case Intrinsic::x86_avx2_pslli_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 314 | case Intrinsic::x86_avx512_psll_d_512: |
| 315 | case Intrinsic::x86_avx512_psll_q_512: |
| 316 | case Intrinsic::x86_avx512_psll_w_512: |
| 317 | case Intrinsic::x86_avx512_pslli_d_512: |
| 318 | case Intrinsic::x86_avx512_pslli_q_512: |
| 319 | case Intrinsic::x86_avx512_pslli_w_512: |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 320 | LogicalShift = true; ShiftLeft = true; |
| 321 | break; |
| 322 | } |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 323 | assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); |
| 324 | |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 325 | // Simplify if count is constant. |
| 326 | auto Arg1 = II.getArgOperand(1); |
| 327 | auto CAZ = dyn_cast<ConstantAggregateZero>(Arg1); |
| 328 | auto CDV = dyn_cast<ConstantDataVector>(Arg1); |
| 329 | auto CInt = dyn_cast<ConstantInt>(Arg1); |
| 330 | if (!CAZ && !CDV && !CInt) |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 331 | return nullptr; |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 332 | |
| 333 | APInt Count(64, 0); |
| 334 | if (CDV) { |
| 335 | // SSE2/AVX2 uses all the first 64-bits of the 128-bit vector |
| 336 | // operand to compute the shift amount. |
| 337 | auto VT = cast<VectorType>(CDV->getType()); |
| 338 | unsigned BitWidth = VT->getElementType()->getPrimitiveSizeInBits(); |
| 339 | assert((64 % BitWidth) == 0 && "Unexpected packed shift size"); |
| 340 | unsigned NumSubElts = 64 / BitWidth; |
| 341 | |
| 342 | // Concatenate the sub-elements to create the 64-bit value. |
| 343 | for (unsigned i = 0; i != NumSubElts; ++i) { |
| 344 | unsigned SubEltIdx = (NumSubElts - 1) - i; |
| 345 | auto SubElt = cast<ConstantInt>(CDV->getElementAsConstant(SubEltIdx)); |
| 346 | Count = Count.shl(BitWidth); |
| 347 | Count |= SubElt->getValue().zextOrTrunc(64); |
| 348 | } |
| 349 | } |
| 350 | else if (CInt) |
| 351 | Count = CInt->getValue(); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 352 | |
| 353 | auto Vec = II.getArgOperand(0); |
| 354 | auto VT = cast<VectorType>(Vec->getType()); |
| 355 | auto SVT = VT->getElementType(); |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 356 | unsigned VWidth = VT->getNumElements(); |
| 357 | unsigned BitWidth = SVT->getPrimitiveSizeInBits(); |
| 358 | |
| 359 | // If shift-by-zero then just return the original value. |
| 360 | if (Count == 0) |
| 361 | return Vec; |
| 362 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 363 | // Handle cases when Shift >= BitWidth. |
| 364 | if (Count.uge(BitWidth)) { |
| 365 | // If LogicalShift - just return zero. |
| 366 | if (LogicalShift) |
| 367 | return ConstantAggregateZero::get(VT); |
| 368 | |
| 369 | // If ArithmeticShift - clamp Shift to (BitWidth - 1). |
| 370 | Count = APInt(64, BitWidth - 1); |
| 371 | } |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 372 | |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 373 | // Get a constant vector of the same type as the first operand. |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 374 | auto ShiftAmt = ConstantInt::get(SVT, Count.zextOrTrunc(BitWidth)); |
| 375 | auto ShiftVec = Builder.CreateVectorSplat(VWidth, ShiftAmt); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 376 | |
| 377 | if (ShiftLeft) |
Simon Pilgrim | 3815c16 | 2015-08-07 18:22:50 +0000 | [diff] [blame] | 378 | return Builder.CreateShl(Vec, ShiftVec); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 379 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 380 | if (LogicalShift) |
| 381 | return Builder.CreateLShr(Vec, ShiftVec); |
| 382 | |
| 383 | return Builder.CreateAShr(Vec, ShiftVec); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 386 | // Attempt to simplify AVX2 per-element shift intrinsics to a generic IR shift. |
| 387 | // Unlike the generic IR shifts, the intrinsics have defined behaviour for out |
| 388 | // of range shift amounts (logical - set to zero, arithmetic - splat sign bit). |
| 389 | static Value *simplifyX86varShift(const IntrinsicInst &II, |
| 390 | InstCombiner::BuilderTy &Builder) { |
| 391 | bool LogicalShift = false; |
| 392 | bool ShiftLeft = false; |
| 393 | |
| 394 | switch (II.getIntrinsicID()) { |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 395 | default: llvm_unreachable("Unexpected intrinsic!"); |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 396 | case Intrinsic::x86_avx2_psrav_d: |
| 397 | case Intrinsic::x86_avx2_psrav_d_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 398 | case Intrinsic::x86_avx512_psrav_q_128: |
| 399 | case Intrinsic::x86_avx512_psrav_q_256: |
| 400 | case Intrinsic::x86_avx512_psrav_d_512: |
| 401 | case Intrinsic::x86_avx512_psrav_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 402 | case Intrinsic::x86_avx512_psrav_w_128: |
| 403 | case Intrinsic::x86_avx512_psrav_w_256: |
| 404 | case Intrinsic::x86_avx512_psrav_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 405 | LogicalShift = false; |
| 406 | ShiftLeft = false; |
| 407 | break; |
| 408 | case Intrinsic::x86_avx2_psrlv_d: |
| 409 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 410 | case Intrinsic::x86_avx2_psrlv_q: |
| 411 | case Intrinsic::x86_avx2_psrlv_q_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 412 | case Intrinsic::x86_avx512_psrlv_d_512: |
| 413 | case Intrinsic::x86_avx512_psrlv_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 414 | case Intrinsic::x86_avx512_psrlv_w_128: |
| 415 | case Intrinsic::x86_avx512_psrlv_w_256: |
| 416 | case Intrinsic::x86_avx512_psrlv_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 417 | LogicalShift = true; |
| 418 | ShiftLeft = false; |
| 419 | break; |
| 420 | case Intrinsic::x86_avx2_psllv_d: |
| 421 | case Intrinsic::x86_avx2_psllv_d_256: |
| 422 | case Intrinsic::x86_avx2_psllv_q: |
| 423 | case Intrinsic::x86_avx2_psllv_q_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 424 | case Intrinsic::x86_avx512_psllv_d_512: |
| 425 | case Intrinsic::x86_avx512_psllv_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 426 | case Intrinsic::x86_avx512_psllv_w_128: |
| 427 | case Intrinsic::x86_avx512_psllv_w_256: |
| 428 | case Intrinsic::x86_avx512_psllv_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 429 | LogicalShift = true; |
| 430 | ShiftLeft = true; |
| 431 | break; |
| 432 | } |
| 433 | assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); |
| 434 | |
| 435 | // Simplify if all shift amounts are constant/undef. |
| 436 | auto *CShift = dyn_cast<Constant>(II.getArgOperand(1)); |
| 437 | if (!CShift) |
| 438 | return nullptr; |
| 439 | |
| 440 | auto Vec = II.getArgOperand(0); |
| 441 | auto VT = cast<VectorType>(II.getType()); |
| 442 | auto SVT = VT->getVectorElementType(); |
| 443 | int NumElts = VT->getNumElements(); |
| 444 | int BitWidth = SVT->getIntegerBitWidth(); |
| 445 | |
| 446 | // Collect each element's shift amount. |
| 447 | // We also collect special cases: UNDEF = -1, OUT-OF-RANGE = BitWidth. |
| 448 | bool AnyOutOfRange = false; |
| 449 | SmallVector<int, 8> ShiftAmts; |
| 450 | for (int I = 0; I < NumElts; ++I) { |
| 451 | auto *CElt = CShift->getAggregateElement(I); |
| 452 | if (CElt && isa<UndefValue>(CElt)) { |
| 453 | ShiftAmts.push_back(-1); |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | auto *COp = dyn_cast_or_null<ConstantInt>(CElt); |
| 458 | if (!COp) |
| 459 | return nullptr; |
| 460 | |
| 461 | // Handle out of range shifts. |
| 462 | // If LogicalShift - set to BitWidth (special case). |
| 463 | // If ArithmeticShift - set to (BitWidth - 1) (sign splat). |
| 464 | APInt ShiftVal = COp->getValue(); |
| 465 | if (ShiftVal.uge(BitWidth)) { |
| 466 | AnyOutOfRange = LogicalShift; |
| 467 | ShiftAmts.push_back(LogicalShift ? BitWidth : BitWidth - 1); |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | ShiftAmts.push_back((int)ShiftVal.getZExtValue()); |
| 472 | } |
| 473 | |
| 474 | // If all elements out of range or UNDEF, return vector of zeros/undefs. |
| 475 | // ArithmeticShift should only hit this if they are all UNDEF. |
| 476 | auto OutOfRange = [&](int Idx) { return (Idx < 0) || (BitWidth <= Idx); }; |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 477 | if (all_of(ShiftAmts, OutOfRange)) { |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 478 | SmallVector<Constant *, 8> ConstantVec; |
| 479 | for (int Idx : ShiftAmts) { |
| 480 | if (Idx < 0) { |
| 481 | ConstantVec.push_back(UndefValue::get(SVT)); |
| 482 | } else { |
| 483 | assert(LogicalShift && "Logical shift expected"); |
| 484 | ConstantVec.push_back(ConstantInt::getNullValue(SVT)); |
| 485 | } |
| 486 | } |
| 487 | return ConstantVector::get(ConstantVec); |
| 488 | } |
| 489 | |
| 490 | // We can't handle only some out of range values with generic logical shifts. |
| 491 | if (AnyOutOfRange) |
| 492 | return nullptr; |
| 493 | |
| 494 | // Build the shift amount constant vector. |
| 495 | SmallVector<Constant *, 8> ShiftVecAmts; |
| 496 | for (int Idx : ShiftAmts) { |
| 497 | if (Idx < 0) |
| 498 | ShiftVecAmts.push_back(UndefValue::get(SVT)); |
| 499 | else |
| 500 | ShiftVecAmts.push_back(ConstantInt::get(SVT, Idx)); |
| 501 | } |
| 502 | auto ShiftVec = ConstantVector::get(ShiftVecAmts); |
| 503 | |
| 504 | if (ShiftLeft) |
| 505 | return Builder.CreateShl(Vec, ShiftVec); |
| 506 | |
| 507 | if (LogicalShift) |
| 508 | return Builder.CreateLShr(Vec, ShiftVec); |
| 509 | |
| 510 | return Builder.CreateAShr(Vec, ShiftVec); |
| 511 | } |
| 512 | |
Simon Pilgrim | f6f3a361 | 2017-01-23 15:22:59 +0000 | [diff] [blame] | 513 | static Value *simplifyX86muldq(const IntrinsicInst &II, |
| 514 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | a50a93f | 2017-01-20 18:20:30 +0000 | [diff] [blame] | 515 | Value *Arg0 = II.getArgOperand(0); |
| 516 | Value *Arg1 = II.getArgOperand(1); |
| 517 | Type *ResTy = II.getType(); |
Simon Pilgrim | f6f3a361 | 2017-01-23 15:22:59 +0000 | [diff] [blame] | 518 | assert(Arg0->getType()->getScalarSizeInBits() == 32 && |
| 519 | Arg1->getType()->getScalarSizeInBits() == 32 && |
| 520 | ResTy->getScalarSizeInBits() == 64 && "Unexpected muldq/muludq types"); |
Simon Pilgrim | a50a93f | 2017-01-20 18:20:30 +0000 | [diff] [blame] | 521 | |
Simon Pilgrim | bb13fda | 2017-01-23 12:07:32 +0000 | [diff] [blame] | 522 | // muldq/muludq(undef, undef) -> zero (matches generic mul behavior) |
Simon Pilgrim | 78f8630 | 2017-01-24 11:07:41 +0000 | [diff] [blame] | 523 | if (isa<UndefValue>(Arg0) || isa<UndefValue>(Arg1)) |
Simon Pilgrim | bb13fda | 2017-01-23 12:07:32 +0000 | [diff] [blame] | 524 | return ConstantAggregateZero::get(ResTy); |
Simon Pilgrim | a50a93f | 2017-01-20 18:20:30 +0000 | [diff] [blame] | 525 | |
Simon Pilgrim | f6f3a361 | 2017-01-23 15:22:59 +0000 | [diff] [blame] | 526 | // Constant folding. |
| 527 | // PMULDQ = (mul(vXi64 sext(shuffle<0,2,..>(Arg0)), |
| 528 | // vXi64 sext(shuffle<0,2,..>(Arg1)))) |
| 529 | // PMULUDQ = (mul(vXi64 zext(shuffle<0,2,..>(Arg0)), |
| 530 | // vXi64 zext(shuffle<0,2,..>(Arg1)))) |
| 531 | if (!isa<Constant>(Arg0) || !isa<Constant>(Arg1)) |
| 532 | return nullptr; |
| 533 | |
| 534 | unsigned NumElts = ResTy->getVectorNumElements(); |
| 535 | assert(Arg0->getType()->getVectorNumElements() == (2 * NumElts) && |
| 536 | Arg1->getType()->getVectorNumElements() == (2 * NumElts) && |
| 537 | "Unexpected muldq/muludq types"); |
| 538 | |
| 539 | unsigned IntrinsicID = II.getIntrinsicID(); |
| 540 | bool IsSigned = (Intrinsic::x86_sse41_pmuldq == IntrinsicID || |
| 541 | Intrinsic::x86_avx2_pmul_dq == IntrinsicID || |
| 542 | Intrinsic::x86_avx512_pmul_dq_512 == IntrinsicID); |
| 543 | |
| 544 | SmallVector<unsigned, 16> ShuffleMask; |
| 545 | for (unsigned i = 0; i != NumElts; ++i) |
| 546 | ShuffleMask.push_back(i * 2); |
| 547 | |
| 548 | auto *LHS = Builder.CreateShuffleVector(Arg0, Arg0, ShuffleMask); |
| 549 | auto *RHS = Builder.CreateShuffleVector(Arg1, Arg1, ShuffleMask); |
| 550 | |
| 551 | if (IsSigned) { |
| 552 | LHS = Builder.CreateSExt(LHS, ResTy); |
| 553 | RHS = Builder.CreateSExt(RHS, ResTy); |
| 554 | } else { |
| 555 | LHS = Builder.CreateZExt(LHS, ResTy); |
| 556 | RHS = Builder.CreateZExt(RHS, ResTy); |
| 557 | } |
| 558 | |
| 559 | return Builder.CreateMul(LHS, RHS); |
Simon Pilgrim | a50a93f | 2017-01-20 18:20:30 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Simon Pilgrim | 6f6b279 | 2017-01-25 14:37:24 +0000 | [diff] [blame] | 562 | static Value *simplifyX86pack(IntrinsicInst &II, InstCombiner &IC, |
| 563 | InstCombiner::BuilderTy &Builder, bool IsSigned) { |
| 564 | Value *Arg0 = II.getArgOperand(0); |
| 565 | Value *Arg1 = II.getArgOperand(1); |
| 566 | Type *ResTy = II.getType(); |
| 567 | |
| 568 | // Fast all undef handling. |
| 569 | if (isa<UndefValue>(Arg0) && isa<UndefValue>(Arg1)) |
| 570 | return UndefValue::get(ResTy); |
| 571 | |
| 572 | Type *ArgTy = Arg0->getType(); |
| 573 | unsigned NumLanes = ResTy->getPrimitiveSizeInBits() / 128; |
| 574 | unsigned NumDstElts = ResTy->getVectorNumElements(); |
| 575 | unsigned NumSrcElts = ArgTy->getVectorNumElements(); |
| 576 | assert(NumDstElts == (2 * NumSrcElts) && "Unexpected packing types"); |
| 577 | |
| 578 | unsigned NumDstEltsPerLane = NumDstElts / NumLanes; |
| 579 | unsigned NumSrcEltsPerLane = NumSrcElts / NumLanes; |
| 580 | unsigned DstScalarSizeInBits = ResTy->getScalarSizeInBits(); |
| 581 | assert(ArgTy->getScalarSizeInBits() == (2 * DstScalarSizeInBits) && |
| 582 | "Unexpected packing types"); |
| 583 | |
| 584 | // Constant folding. |
| 585 | auto *Cst0 = dyn_cast<Constant>(Arg0); |
| 586 | auto *Cst1 = dyn_cast<Constant>(Arg1); |
| 587 | if (!Cst0 || !Cst1) |
| 588 | return nullptr; |
| 589 | |
| 590 | SmallVector<Constant *, 32> Vals; |
| 591 | for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { |
| 592 | for (unsigned Elt = 0; Elt != NumDstEltsPerLane; ++Elt) { |
| 593 | unsigned SrcIdx = Lane * NumSrcEltsPerLane + Elt % NumSrcEltsPerLane; |
| 594 | auto *Cst = (Elt >= NumSrcEltsPerLane) ? Cst1 : Cst0; |
| 595 | auto *COp = Cst->getAggregateElement(SrcIdx); |
| 596 | if (COp && isa<UndefValue>(COp)) { |
| 597 | Vals.push_back(UndefValue::get(ResTy->getScalarType())); |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | auto *CInt = dyn_cast_or_null<ConstantInt>(COp); |
| 602 | if (!CInt) |
| 603 | return nullptr; |
| 604 | |
| 605 | APInt Val = CInt->getValue(); |
| 606 | assert(Val.getBitWidth() == ArgTy->getScalarSizeInBits() && |
| 607 | "Unexpected constant bitwidth"); |
| 608 | |
| 609 | if (IsSigned) { |
| 610 | // PACKSS: Truncate signed value with signed saturation. |
| 611 | // Source values less than dst minint are saturated to minint. |
| 612 | // Source values greater than dst maxint are saturated to maxint. |
| 613 | if (Val.isSignedIntN(DstScalarSizeInBits)) |
| 614 | Val = Val.trunc(DstScalarSizeInBits); |
| 615 | else if (Val.isNegative()) |
| 616 | Val = APInt::getSignedMinValue(DstScalarSizeInBits); |
| 617 | else |
| 618 | Val = APInt::getSignedMaxValue(DstScalarSizeInBits); |
| 619 | } else { |
| 620 | // PACKUS: Truncate signed value with unsigned saturation. |
| 621 | // Source values less than zero are saturated to zero. |
| 622 | // Source values greater than dst maxuint are saturated to maxuint. |
| 623 | if (Val.isIntN(DstScalarSizeInBits)) |
| 624 | Val = Val.trunc(DstScalarSizeInBits); |
| 625 | else if (Val.isNegative()) |
| 626 | Val = APInt::getNullValue(DstScalarSizeInBits); |
| 627 | else |
| 628 | Val = APInt::getAllOnesValue(DstScalarSizeInBits); |
| 629 | } |
| 630 | |
| 631 | Vals.push_back(ConstantInt::get(ResTy->getScalarType(), Val)); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | return ConstantVector::get(Vals); |
| 636 | } |
| 637 | |
Simon Pilgrim | 91e3ac8 | 2016-06-07 08:18:35 +0000 | [diff] [blame] | 638 | static Value *simplifyX86movmsk(const IntrinsicInst &II, |
| 639 | InstCombiner::BuilderTy &Builder) { |
| 640 | Value *Arg = II.getArgOperand(0); |
| 641 | Type *ResTy = II.getType(); |
| 642 | Type *ArgTy = Arg->getType(); |
| 643 | |
| 644 | // movmsk(undef) -> zero as we must ensure the upper bits are zero. |
| 645 | if (isa<UndefValue>(Arg)) |
| 646 | return Constant::getNullValue(ResTy); |
| 647 | |
| 648 | // We can't easily peek through x86_mmx types. |
| 649 | if (!ArgTy->isVectorTy()) |
| 650 | return nullptr; |
| 651 | |
| 652 | auto *C = dyn_cast<Constant>(Arg); |
| 653 | if (!C) |
| 654 | return nullptr; |
| 655 | |
| 656 | // Extract signbits of the vector input and pack into integer result. |
| 657 | APInt Result(ResTy->getPrimitiveSizeInBits(), 0); |
| 658 | for (unsigned I = 0, E = ArgTy->getVectorNumElements(); I != E; ++I) { |
| 659 | auto *COp = C->getAggregateElement(I); |
| 660 | if (!COp) |
| 661 | return nullptr; |
| 662 | if (isa<UndefValue>(COp)) |
| 663 | continue; |
| 664 | |
| 665 | auto *CInt = dyn_cast<ConstantInt>(COp); |
| 666 | auto *CFp = dyn_cast<ConstantFP>(COp); |
| 667 | if (!CInt && !CFp) |
| 668 | return nullptr; |
| 669 | |
| 670 | if ((CInt && CInt->isNegative()) || (CFp && CFp->isNegative())) |
| 671 | Result.setBit(I); |
| 672 | } |
| 673 | |
| 674 | return Constant::getIntegerValue(ResTy, Result); |
| 675 | } |
| 676 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 677 | static Value *simplifyX86insertps(const IntrinsicInst &II, |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 678 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 679 | auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2)); |
| 680 | if (!CInt) |
| 681 | return nullptr; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 682 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 683 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 684 | assert(VecTy->getNumElements() == 4 && "insertps with wrong vector type"); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 685 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 686 | // The immediate permute control byte looks like this: |
| 687 | // [3:0] - zero mask for each 32-bit lane |
| 688 | // [5:4] - select one 32-bit destination lane |
| 689 | // [7:6] - select one 32-bit source lane |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 690 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 691 | uint8_t Imm = CInt->getZExtValue(); |
| 692 | uint8_t ZMask = Imm & 0xf; |
| 693 | uint8_t DestLane = (Imm >> 4) & 0x3; |
| 694 | uint8_t SourceLane = (Imm >> 6) & 0x3; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 695 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 696 | ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 697 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 698 | // If all zero mask bits are set, this was just a weird way to |
| 699 | // generate a zero vector. |
| 700 | if (ZMask == 0xf) |
| 701 | return ZeroVector; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 702 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 703 | // Initialize by passing all of the first source bits through. |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 704 | uint32_t ShuffleMask[4] = { 0, 1, 2, 3 }; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 705 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 706 | // We may replace the second operand with the zero vector. |
| 707 | Value *V1 = II.getArgOperand(1); |
| 708 | |
| 709 | if (ZMask) { |
| 710 | // If the zero mask is being used with a single input or the zero mask |
| 711 | // overrides the destination lane, this is a shuffle with the zero vector. |
| 712 | if ((II.getArgOperand(0) == II.getArgOperand(1)) || |
| 713 | (ZMask & (1 << DestLane))) { |
| 714 | V1 = ZeroVector; |
| 715 | // We may still move 32-bits of the first source vector from one lane |
| 716 | // to another. |
| 717 | ShuffleMask[DestLane] = SourceLane; |
| 718 | // The zero mask may override the previous insert operation. |
| 719 | for (unsigned i = 0; i < 4; ++i) |
| 720 | if ((ZMask >> i) & 0x1) |
| 721 | ShuffleMask[i] = i + 4; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 722 | } else { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 723 | // TODO: Model this case as 2 shuffles or a 'logical and' plus shuffle? |
| 724 | return nullptr; |
Sanjay Patel | c1d20a3 | 2015-04-25 20:55:25 +0000 | [diff] [blame] | 725 | } |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 726 | } else { |
| 727 | // Replace the selected destination lane with the selected source lane. |
| 728 | ShuffleMask[DestLane] = SourceLane + 4; |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 729 | } |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 730 | |
| 731 | return Builder.CreateShuffleVector(II.getArgOperand(0), V1, ShuffleMask); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 734 | /// Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding |
| 735 | /// or conversion to a shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 736 | static Value *simplifyX86extrq(IntrinsicInst &II, Value *Op0, |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 737 | ConstantInt *CILength, ConstantInt *CIIndex, |
| 738 | InstCombiner::BuilderTy &Builder) { |
| 739 | auto LowConstantHighUndef = [&](uint64_t Val) { |
| 740 | Type *IntTy64 = Type::getInt64Ty(II.getContext()); |
| 741 | Constant *Args[] = {ConstantInt::get(IntTy64, Val), |
| 742 | UndefValue::get(IntTy64)}; |
| 743 | return ConstantVector::get(Args); |
| 744 | }; |
| 745 | |
| 746 | // See if we're dealing with constant values. |
| 747 | Constant *C0 = dyn_cast<Constant>(Op0); |
| 748 | ConstantInt *CI0 = |
Andrea Di Biagio | 8df5b9c | 2016-09-07 12:03:03 +0000 | [diff] [blame] | 749 | C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 750 | : nullptr; |
| 751 | |
| 752 | // Attempt to constant fold. |
| 753 | if (CILength && CIIndex) { |
| 754 | // From AMD documentation: "The bit index and field length are each six |
| 755 | // bits in length other bits of the field are ignored." |
| 756 | APInt APIndex = CIIndex->getValue().zextOrTrunc(6); |
| 757 | APInt APLength = CILength->getValue().zextOrTrunc(6); |
| 758 | |
| 759 | unsigned Index = APIndex.getZExtValue(); |
| 760 | |
| 761 | // From AMD documentation: "a value of zero in the field length is |
| 762 | // defined as length of 64". |
| 763 | unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); |
| 764 | |
| 765 | // From AMD documentation: "If the sum of the bit index + length field |
| 766 | // is greater than 64, the results are undefined". |
| 767 | unsigned End = Index + Length; |
| 768 | |
| 769 | // Note that both field index and field length are 8-bit quantities. |
| 770 | // Since variables 'Index' and 'Length' are unsigned values |
| 771 | // obtained from zero-extending field index and field length |
| 772 | // respectively, their sum should never wrap around. |
| 773 | if (End > 64) |
| 774 | return UndefValue::get(II.getType()); |
| 775 | |
| 776 | // If we are inserting whole bytes, we can convert this to a shuffle. |
| 777 | // Lowering can recognize EXTRQI shuffle masks. |
| 778 | if ((Length % 8) == 0 && (Index % 8) == 0) { |
| 779 | // Convert bit indices to byte indices. |
| 780 | Length /= 8; |
| 781 | Index /= 8; |
| 782 | |
| 783 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 784 | Type *IntTy32 = Type::getInt32Ty(II.getContext()); |
| 785 | VectorType *ShufTy = VectorType::get(IntTy8, 16); |
| 786 | |
| 787 | SmallVector<Constant *, 16> ShuffleMask; |
| 788 | for (int i = 0; i != (int)Length; ++i) |
| 789 | ShuffleMask.push_back( |
| 790 | Constant::getIntegerValue(IntTy32, APInt(32, i + Index))); |
| 791 | for (int i = Length; i != 8; ++i) |
| 792 | ShuffleMask.push_back( |
| 793 | Constant::getIntegerValue(IntTy32, APInt(32, i + 16))); |
| 794 | for (int i = 8; i != 16; ++i) |
| 795 | ShuffleMask.push_back(UndefValue::get(IntTy32)); |
| 796 | |
| 797 | Value *SV = Builder.CreateShuffleVector( |
| 798 | Builder.CreateBitCast(Op0, ShufTy), |
| 799 | ConstantAggregateZero::get(ShufTy), ConstantVector::get(ShuffleMask)); |
| 800 | return Builder.CreateBitCast(SV, II.getType()); |
| 801 | } |
| 802 | |
| 803 | // Constant Fold - shift Index'th bit to lowest position and mask off |
| 804 | // Length bits. |
| 805 | if (CI0) { |
| 806 | APInt Elt = CI0->getValue(); |
| 807 | Elt = Elt.lshr(Index).zextOrTrunc(Length); |
| 808 | return LowConstantHighUndef(Elt.getZExtValue()); |
| 809 | } |
| 810 | |
| 811 | // If we were an EXTRQ call, we'll save registers if we convert to EXTRQI. |
| 812 | if (II.getIntrinsicID() == Intrinsic::x86_sse4a_extrq) { |
| 813 | Value *Args[] = {Op0, CILength, CIIndex}; |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 814 | Module *M = II.getModule(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 815 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_extrqi); |
| 816 | return Builder.CreateCall(F, Args); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // Constant Fold - extraction from zero is always {zero, undef}. |
| 821 | if (CI0 && CI0->equalsInt(0)) |
| 822 | return LowConstantHighUndef(0); |
| 823 | |
| 824 | return nullptr; |
| 825 | } |
| 826 | |
| 827 | /// Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant |
| 828 | /// folding or conversion to a shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 829 | static Value *simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1, |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 830 | APInt APLength, APInt APIndex, |
| 831 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 832 | // From AMD documentation: "The bit index and field length are each six bits |
| 833 | // in length other bits of the field are ignored." |
| 834 | APIndex = APIndex.zextOrTrunc(6); |
| 835 | APLength = APLength.zextOrTrunc(6); |
| 836 | |
| 837 | // Attempt to constant fold. |
| 838 | unsigned Index = APIndex.getZExtValue(); |
| 839 | |
| 840 | // From AMD documentation: "a value of zero in the field length is |
| 841 | // defined as length of 64". |
| 842 | unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); |
| 843 | |
| 844 | // From AMD documentation: "If the sum of the bit index + length field |
| 845 | // is greater than 64, the results are undefined". |
| 846 | unsigned End = Index + Length; |
| 847 | |
| 848 | // Note that both field index and field length are 8-bit quantities. |
| 849 | // Since variables 'Index' and 'Length' are unsigned values |
| 850 | // obtained from zero-extending field index and field length |
| 851 | // respectively, their sum should never wrap around. |
| 852 | if (End > 64) |
| 853 | return UndefValue::get(II.getType()); |
| 854 | |
| 855 | // If we are inserting whole bytes, we can convert this to a shuffle. |
| 856 | // Lowering can recognize INSERTQI shuffle masks. |
| 857 | if ((Length % 8) == 0 && (Index % 8) == 0) { |
| 858 | // Convert bit indices to byte indices. |
| 859 | Length /= 8; |
| 860 | Index /= 8; |
| 861 | |
| 862 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 863 | Type *IntTy32 = Type::getInt32Ty(II.getContext()); |
| 864 | VectorType *ShufTy = VectorType::get(IntTy8, 16); |
| 865 | |
| 866 | SmallVector<Constant *, 16> ShuffleMask; |
| 867 | for (int i = 0; i != (int)Index; ++i) |
| 868 | ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i))); |
| 869 | for (int i = 0; i != (int)Length; ++i) |
| 870 | ShuffleMask.push_back( |
| 871 | Constant::getIntegerValue(IntTy32, APInt(32, i + 16))); |
| 872 | for (int i = Index + Length; i != 8; ++i) |
| 873 | ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i))); |
| 874 | for (int i = 8; i != 16; ++i) |
| 875 | ShuffleMask.push_back(UndefValue::get(IntTy32)); |
| 876 | |
| 877 | Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy), |
| 878 | Builder.CreateBitCast(Op1, ShufTy), |
| 879 | ConstantVector::get(ShuffleMask)); |
| 880 | return Builder.CreateBitCast(SV, II.getType()); |
| 881 | } |
| 882 | |
| 883 | // See if we're dealing with constant values. |
| 884 | Constant *C0 = dyn_cast<Constant>(Op0); |
| 885 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 886 | ConstantInt *CI00 = |
Andrea Di Biagio | f3fd316 | 2016-09-07 12:47:53 +0000 | [diff] [blame] | 887 | C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 888 | : nullptr; |
| 889 | ConstantInt *CI10 = |
Andrea Di Biagio | f3fd316 | 2016-09-07 12:47:53 +0000 | [diff] [blame] | 890 | C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 891 | : nullptr; |
| 892 | |
| 893 | // Constant Fold - insert bottom Length bits starting at the Index'th bit. |
| 894 | if (CI00 && CI10) { |
| 895 | APInt V00 = CI00->getValue(); |
| 896 | APInt V10 = CI10->getValue(); |
| 897 | APInt Mask = APInt::getLowBitsSet(64, Length).shl(Index); |
| 898 | V00 = V00 & ~Mask; |
| 899 | V10 = V10.zextOrTrunc(Length).zextOrTrunc(64).shl(Index); |
| 900 | APInt Val = V00 | V10; |
| 901 | Type *IntTy64 = Type::getInt64Ty(II.getContext()); |
| 902 | Constant *Args[] = {ConstantInt::get(IntTy64, Val.getZExtValue()), |
| 903 | UndefValue::get(IntTy64)}; |
| 904 | return ConstantVector::get(Args); |
| 905 | } |
| 906 | |
| 907 | // If we were an INSERTQ call, we'll save demanded elements if we convert to |
| 908 | // INSERTQI. |
| 909 | if (II.getIntrinsicID() == Intrinsic::x86_sse4a_insertq) { |
| 910 | Type *IntTy8 = Type::getInt8Ty(II.getContext()); |
| 911 | Constant *CILength = ConstantInt::get(IntTy8, Length, false); |
| 912 | Constant *CIIndex = ConstantInt::get(IntTy8, Index, false); |
| 913 | |
| 914 | Value *Args[] = {Op0, Op1, CILength, CIIndex}; |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 915 | Module *M = II.getModule(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 916 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_insertqi); |
| 917 | return Builder.CreateCall(F, Args); |
| 918 | } |
| 919 | |
| 920 | return nullptr; |
| 921 | } |
| 922 | |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 923 | /// Attempt to convert pshufb* to shufflevector if the mask is constant. |
| 924 | static Value *simplifyX86pshufb(const IntrinsicInst &II, |
| 925 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 926 | Constant *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 927 | if (!V) |
| 928 | return nullptr; |
| 929 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 930 | auto *VecTy = cast<VectorType>(II.getType()); |
| 931 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
| 932 | unsigned NumElts = VecTy->getNumElements(); |
Craig Topper | 9a63d7a | 2016-12-11 00:23:50 +0000 | [diff] [blame] | 933 | assert((NumElts == 16 || NumElts == 32 || NumElts == 64) && |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 934 | "Unexpected number of elements in shuffle mask!"); |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 935 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 936 | // Construct a shuffle mask from constant integers or UNDEFs. |
Craig Topper | 9a63d7a | 2016-12-11 00:23:50 +0000 | [diff] [blame] | 937 | Constant *Indexes[64] = {nullptr}; |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 938 | |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 939 | // Each byte in the shuffle control mask forms an index to permute the |
| 940 | // corresponding byte in the destination operand. |
| 941 | for (unsigned I = 0; I < NumElts; ++I) { |
| 942 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 943 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 944 | return nullptr; |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 945 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 946 | if (isa<UndefValue>(COp)) { |
| 947 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 948 | continue; |
| 949 | } |
| 950 | |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 951 | int8_t Index = cast<ConstantInt>(COp)->getValue().getZExtValue(); |
| 952 | |
| 953 | // If the most significant bit (bit[7]) of each byte of the shuffle |
| 954 | // control mask is set, then zero is written in the result byte. |
| 955 | // The zero vector is in the right-hand side of the resulting |
| 956 | // shufflevector. |
| 957 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 958 | // The value of each index for the high 128-bit lane is the least |
| 959 | // significant 4 bits of the respective shuffle control byte. |
| 960 | Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0); |
| 961 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | bf60cc4 | 2016-04-29 21:34:54 +0000 | [diff] [blame] | 962 | } |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 963 | |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 964 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts)); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 965 | auto V1 = II.getArgOperand(0); |
Simon Pilgrim | e5e8c2f | 2016-05-01 19:26:21 +0000 | [diff] [blame] | 966 | auto V2 = Constant::getNullValue(VecTy); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 967 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 968 | } |
| 969 | |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 970 | /// Attempt to convert vpermilvar* to shufflevector if the mask is constant. |
| 971 | static Value *simplifyX86vpermilvar(const IntrinsicInst &II, |
| 972 | InstCombiner::BuilderTy &Builder) { |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 973 | Constant *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 974 | if (!V) |
| 975 | return nullptr; |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 976 | |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 977 | auto *VecTy = cast<VectorType>(II.getType()); |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 978 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 979 | unsigned NumElts = VecTy->getVectorNumElements(); |
| 980 | bool IsPD = VecTy->getScalarType()->isDoubleTy(); |
| 981 | unsigned NumLaneElts = IsPD ? 2 : 4; |
| 982 | assert(NumElts == 16 || NumElts == 8 || NumElts == 4 || NumElts == 2); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 983 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 984 | // Construct a shuffle mask from constant integers or UNDEFs. |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 985 | Constant *Indexes[16] = {nullptr}; |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 986 | |
| 987 | // The intrinsics only read one or two bits, clear the rest. |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 988 | for (unsigned I = 0; I < NumElts; ++I) { |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 989 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 990 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 991 | return nullptr; |
| 992 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 993 | if (isa<UndefValue>(COp)) { |
| 994 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | APInt Index = cast<ConstantInt>(COp)->getValue(); |
| 999 | Index = Index.zextOrTrunc(32).getLoBits(2); |
Simon Pilgrim | 640f996 | 2016-04-30 07:23:30 +0000 | [diff] [blame] | 1000 | |
| 1001 | // The PD variants uses bit 1 to select per-lane element index, so |
| 1002 | // shift down to convert to generic shuffle mask index. |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 1003 | if (IsPD) |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 1004 | Index = Index.lshr(1); |
| 1005 | |
| 1006 | // The _256 variants are a bit trickier since the mask bits always index |
| 1007 | // into the corresponding 128 half. In order to convert to a generic |
| 1008 | // shuffle, we have to make that explicit. |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 1009 | Index += APInt(32, (I / NumLaneElts) * NumLaneElts); |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 1010 | |
| 1011 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Simon Pilgrim | eeacc40 | 2016-05-01 20:22:42 +0000 | [diff] [blame] | 1014 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts)); |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 1015 | auto V1 = II.getArgOperand(0); |
| 1016 | auto V2 = UndefValue::get(V1->getType()); |
| 1017 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 1018 | } |
| 1019 | |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1020 | /// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant. |
| 1021 | static Value *simplifyX86vpermv(const IntrinsicInst &II, |
| 1022 | InstCombiner::BuilderTy &Builder) { |
| 1023 | auto *V = dyn_cast<Constant>(II.getArgOperand(1)); |
| 1024 | if (!V) |
| 1025 | return nullptr; |
| 1026 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1027 | auto *VecTy = cast<VectorType>(II.getType()); |
| 1028 | auto *MaskEltTy = Type::getInt32Ty(II.getContext()); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1029 | unsigned Size = VecTy->getNumElements(); |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1030 | assert((Size == 4 || Size == 8 || Size == 16 || Size == 32 || Size == 64) && |
| 1031 | "Unexpected shuffle mask size"); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1032 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1033 | // Construct a shuffle mask from constant integers or UNDEFs. |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1034 | Constant *Indexes[64] = {nullptr}; |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1035 | |
| 1036 | for (unsigned I = 0; I < Size; ++I) { |
| 1037 | Constant *COp = V->getAggregateElement(I); |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1038 | if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1039 | return nullptr; |
| 1040 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1041 | if (isa<UndefValue>(COp)) { |
| 1042 | Indexes[I] = UndefValue::get(MaskEltTy); |
| 1043 | continue; |
| 1044 | } |
| 1045 | |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1046 | uint32_t Index = cast<ConstantInt>(COp)->getZExtValue(); |
| 1047 | Index &= Size - 1; |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1048 | Indexes[I] = ConstantInt::get(MaskEltTy, Index); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Simon Pilgrim | ca140b1 | 2016-05-01 20:43:02 +0000 | [diff] [blame] | 1051 | auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, Size)); |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 1052 | auto V1 = II.getArgOperand(0); |
| 1053 | auto V2 = UndefValue::get(VecTy); |
| 1054 | return Builder.CreateShuffleVector(V1, V2, ShuffleMask); |
| 1055 | } |
| 1056 | |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1057 | /// The shuffle mask for a perm2*128 selects any two halves of two 256-bit |
| 1058 | /// source vectors, unless a zero bit is set. If a zero bit is set, |
| 1059 | /// 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] | 1060 | static Value *simplifyX86vperm2(const IntrinsicInst &II, |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1061 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1062 | auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2)); |
| 1063 | if (!CInt) |
| 1064 | return nullptr; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1065 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1066 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 1067 | ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy); |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1068 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1069 | // The immediate permute control byte looks like this: |
| 1070 | // [1:0] - select 128 bits from sources for low half of destination |
| 1071 | // [2] - ignore |
| 1072 | // [3] - zero low half of destination |
| 1073 | // [5:4] - select 128 bits from sources for high half of destination |
| 1074 | // [6] - ignore |
| 1075 | // [7] - zero high half of destination |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1076 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1077 | uint8_t Imm = CInt->getZExtValue(); |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1078 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1079 | bool LowHalfZero = Imm & 0x08; |
| 1080 | bool HighHalfZero = Imm & 0x80; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1081 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1082 | // If both zero mask bits are set, this was just a weird way to |
| 1083 | // generate a zero vector. |
| 1084 | if (LowHalfZero && HighHalfZero) |
| 1085 | return ZeroVector; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1086 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1087 | // If 0 or 1 zero mask bits are set, this is a simple shuffle. |
| 1088 | unsigned NumElts = VecTy->getNumElements(); |
| 1089 | unsigned HalfSize = NumElts / 2; |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 1090 | SmallVector<uint32_t, 8> ShuffleMask(NumElts); |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 1091 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1092 | // The high bit of the selection field chooses the 1st or 2nd operand. |
| 1093 | bool LowInputSelect = Imm & 0x02; |
| 1094 | bool HighInputSelect = Imm & 0x20; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1095 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1096 | // The low bit of the selection field chooses the low or high half |
| 1097 | // of the selected operand. |
| 1098 | bool LowHalfSelect = Imm & 0x01; |
| 1099 | bool HighHalfSelect = Imm & 0x10; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 1100 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1101 | // Determine which operand(s) are actually in use for this instruction. |
| 1102 | Value *V0 = LowInputSelect ? II.getArgOperand(1) : II.getArgOperand(0); |
| 1103 | Value *V1 = HighInputSelect ? II.getArgOperand(1) : II.getArgOperand(0); |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 1104 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1105 | // If needed, replace operands based on zero mask. |
| 1106 | V0 = LowHalfZero ? ZeroVector : V0; |
| 1107 | V1 = HighHalfZero ? ZeroVector : V1; |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1108 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1109 | // Permute low half of result. |
| 1110 | unsigned StartIndex = LowHalfSelect ? HalfSize : 0; |
| 1111 | for (unsigned i = 0; i < HalfSize; ++i) |
| 1112 | ShuffleMask[i] = StartIndex + i; |
Sanjay Patel | 43a87fd | 2015-03-24 20:36:42 +0000 | [diff] [blame] | 1113 | |
Sanjay Patel | 03c03f5 | 2016-01-28 00:03:16 +0000 | [diff] [blame] | 1114 | // Permute high half of result. |
| 1115 | StartIndex = HighHalfSelect ? HalfSize : 0; |
| 1116 | StartIndex += NumElts; |
| 1117 | for (unsigned i = 0; i < HalfSize; ++i) |
| 1118 | ShuffleMask[i + HalfSize] = StartIndex + i; |
| 1119 | |
| 1120 | return Builder.CreateShuffleVector(V0, V1, ShuffleMask); |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 1123 | /// Decode XOP integer vector comparison intrinsics. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 1124 | static Value *simplifyX86vpcom(const IntrinsicInst &II, |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 1125 | InstCombiner::BuilderTy &Builder, |
| 1126 | bool IsSigned) { |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 1127 | if (auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2))) { |
| 1128 | uint64_t Imm = CInt->getZExtValue() & 0x7; |
| 1129 | VectorType *VecTy = cast<VectorType>(II.getType()); |
| 1130 | CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
| 1131 | |
| 1132 | switch (Imm) { |
| 1133 | case 0x0: |
| 1134 | Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
| 1135 | break; |
| 1136 | case 0x1: |
| 1137 | Pred = IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
| 1138 | break; |
| 1139 | case 0x2: |
| 1140 | Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 1141 | break; |
| 1142 | case 0x3: |
| 1143 | Pred = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
| 1144 | break; |
| 1145 | case 0x4: |
| 1146 | Pred = ICmpInst::ICMP_EQ; break; |
| 1147 | case 0x5: |
| 1148 | Pred = ICmpInst::ICMP_NE; break; |
| 1149 | case 0x6: |
| 1150 | return ConstantInt::getSigned(VecTy, 0); // FALSE |
| 1151 | case 0x7: |
| 1152 | return ConstantInt::getSigned(VecTy, -1); // TRUE |
| 1153 | } |
| 1154 | |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 1155 | if (Value *Cmp = Builder.CreateICmp(Pred, II.getArgOperand(0), |
| 1156 | II.getArgOperand(1))) |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 1157 | return Builder.CreateSExtOrTrunc(Cmp, VecTy); |
| 1158 | } |
| 1159 | return nullptr; |
| 1160 | } |
| 1161 | |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1162 | // Emit a select instruction and appropriate bitcasts to help simplify |
| 1163 | // masked intrinsics. |
| 1164 | static Value *emitX86MaskSelect(Value *Mask, Value *Op0, Value *Op1, |
| 1165 | InstCombiner::BuilderTy &Builder) { |
Craig Topper | 9916363 | 2016-12-30 23:06:28 +0000 | [diff] [blame] | 1166 | unsigned VWidth = Op0->getType()->getVectorNumElements(); |
| 1167 | |
| 1168 | // If the mask is all ones we don't need the select. But we need to check |
| 1169 | // only the bit thats will be used in case VWidth is less than 8. |
| 1170 | if (auto *C = dyn_cast<ConstantInt>(Mask)) |
| 1171 | if (C->getValue().zextOrTrunc(VWidth).isAllOnesValue()) |
| 1172 | return Op0; |
| 1173 | |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1174 | auto *MaskTy = VectorType::get(Builder.getInt1Ty(), |
| 1175 | cast<IntegerType>(Mask->getType())->getBitWidth()); |
| 1176 | Mask = Builder.CreateBitCast(Mask, MaskTy); |
| 1177 | |
| 1178 | // If we have less than 8 elements, then the starting mask was an i8 and |
| 1179 | // we need to extract down to the right number of elements. |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 1180 | if (VWidth < 8) { |
| 1181 | uint32_t Indices[4]; |
| 1182 | for (unsigned i = 0; i != VWidth; ++i) |
| 1183 | Indices[i] = i; |
| 1184 | Mask = Builder.CreateShuffleVector(Mask, Mask, |
| 1185 | makeArrayRef(Indices, VWidth), |
| 1186 | "extract"); |
| 1187 | } |
| 1188 | |
| 1189 | return Builder.CreateSelect(Mask, Op0, Op1); |
| 1190 | } |
| 1191 | |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 1192 | static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) { |
| 1193 | Value *Arg0 = II.getArgOperand(0); |
| 1194 | Value *Arg1 = II.getArgOperand(1); |
| 1195 | |
| 1196 | // fmin(x, x) -> x |
| 1197 | if (Arg0 == Arg1) |
| 1198 | return Arg0; |
| 1199 | |
| 1200 | const auto *C1 = dyn_cast<ConstantFP>(Arg1); |
| 1201 | |
| 1202 | // fmin(x, nan) -> x |
| 1203 | if (C1 && C1->isNaN()) |
| 1204 | return Arg0; |
| 1205 | |
| 1206 | // This is the value because if undef were NaN, we would return the other |
| 1207 | // value and cannot return a NaN unless both operands are. |
| 1208 | // |
| 1209 | // fmin(undef, x) -> x |
| 1210 | if (isa<UndefValue>(Arg0)) |
| 1211 | return Arg1; |
| 1212 | |
| 1213 | // fmin(x, undef) -> x |
| 1214 | if (isa<UndefValue>(Arg1)) |
| 1215 | return Arg0; |
| 1216 | |
| 1217 | Value *X = nullptr; |
| 1218 | Value *Y = nullptr; |
| 1219 | if (II.getIntrinsicID() == Intrinsic::minnum) { |
| 1220 | // fmin(x, fmin(x, y)) -> fmin(x, y) |
| 1221 | // fmin(y, fmin(x, y)) -> fmin(x, y) |
| 1222 | if (match(Arg1, m_FMin(m_Value(X), m_Value(Y)))) { |
| 1223 | if (Arg0 == X || Arg0 == Y) |
| 1224 | return Arg1; |
| 1225 | } |
| 1226 | |
| 1227 | // fmin(fmin(x, y), x) -> fmin(x, y) |
| 1228 | // fmin(fmin(x, y), y) -> fmin(x, y) |
| 1229 | if (match(Arg0, m_FMin(m_Value(X), m_Value(Y)))) { |
| 1230 | if (Arg1 == X || Arg1 == Y) |
| 1231 | return Arg0; |
| 1232 | } |
| 1233 | |
| 1234 | // TODO: fmin(nnan x, inf) -> x |
| 1235 | // TODO: fmin(nnan ninf x, flt_max) -> x |
| 1236 | if (C1 && C1->isInfinity()) { |
| 1237 | // fmin(x, -inf) -> -inf |
| 1238 | if (C1->isNegative()) |
| 1239 | return Arg1; |
| 1240 | } |
| 1241 | } else { |
| 1242 | assert(II.getIntrinsicID() == Intrinsic::maxnum); |
| 1243 | // fmax(x, fmax(x, y)) -> fmax(x, y) |
| 1244 | // fmax(y, fmax(x, y)) -> fmax(x, y) |
| 1245 | if (match(Arg1, m_FMax(m_Value(X), m_Value(Y)))) { |
| 1246 | if (Arg0 == X || Arg0 == Y) |
| 1247 | return Arg1; |
| 1248 | } |
| 1249 | |
| 1250 | // fmax(fmax(x, y), x) -> fmax(x, y) |
| 1251 | // fmax(fmax(x, y), y) -> fmax(x, y) |
| 1252 | if (match(Arg0, m_FMax(m_Value(X), m_Value(Y)))) { |
| 1253 | if (Arg1 == X || Arg1 == Y) |
| 1254 | return Arg0; |
| 1255 | } |
| 1256 | |
| 1257 | // TODO: fmax(nnan x, -inf) -> x |
| 1258 | // TODO: fmax(nnan ninf x, -flt_max) -> x |
| 1259 | if (C1 && C1->isInfinity()) { |
| 1260 | // fmax(x, inf) -> inf |
| 1261 | if (!C1->isNegative()) |
| 1262 | return Arg1; |
| 1263 | } |
| 1264 | } |
| 1265 | return nullptr; |
| 1266 | } |
| 1267 | |
David Majnemer | 666aa94 | 2016-07-14 06:58:42 +0000 | [diff] [blame] | 1268 | static bool maskIsAllOneOrUndef(Value *Mask) { |
| 1269 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 1270 | if (!ConstMask) |
| 1271 | return false; |
| 1272 | if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask)) |
| 1273 | return true; |
| 1274 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 1275 | ++I) { |
| 1276 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 1277 | if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt)) |
| 1278 | continue; |
| 1279 | return false; |
| 1280 | } |
| 1281 | return true; |
| 1282 | } |
| 1283 | |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1284 | static Value *simplifyMaskedLoad(const IntrinsicInst &II, |
| 1285 | InstCombiner::BuilderTy &Builder) { |
David Majnemer | 666aa94 | 2016-07-14 06:58:42 +0000 | [diff] [blame] | 1286 | // If the mask is all ones or undefs, this is a plain vector load of the 1st |
| 1287 | // argument. |
| 1288 | if (maskIsAllOneOrUndef(II.getArgOperand(2))) { |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1289 | Value *LoadPtr = II.getArgOperand(0); |
| 1290 | unsigned Alignment = cast<ConstantInt>(II.getArgOperand(1))->getZExtValue(); |
| 1291 | return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload"); |
| 1292 | } |
| 1293 | |
| 1294 | return nullptr; |
| 1295 | } |
| 1296 | |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1297 | static Instruction *simplifyMaskedStore(IntrinsicInst &II, InstCombiner &IC) { |
| 1298 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3)); |
| 1299 | if (!ConstMask) |
| 1300 | return nullptr; |
| 1301 | |
| 1302 | // If the mask is all zeros, this instruction does nothing. |
| 1303 | if (ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1304 | return IC.eraseInstFromFunction(II); |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1305 | |
| 1306 | // If the mask is all ones, this is a plain vector store of the 1st argument. |
| 1307 | if (ConstMask->isAllOnesValue()) { |
| 1308 | Value *StorePtr = II.getArgOperand(1); |
| 1309 | unsigned Alignment = cast<ConstantInt>(II.getArgOperand(2))->getZExtValue(); |
| 1310 | return new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment); |
| 1311 | } |
| 1312 | |
| 1313 | return nullptr; |
| 1314 | } |
| 1315 | |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1316 | static Instruction *simplifyMaskedGather(IntrinsicInst &II, InstCombiner &IC) { |
| 1317 | // If the mask is all zeros, return the "passthru" argument of the gather. |
| 1318 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2)); |
| 1319 | if (ConstMask && ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1320 | return IC.replaceInstUsesWith(II, II.getArgOperand(3)); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1321 | |
| 1322 | return nullptr; |
| 1323 | } |
| 1324 | |
| 1325 | static Instruction *simplifyMaskedScatter(IntrinsicInst &II, InstCombiner &IC) { |
| 1326 | // If the mask is all zeros, a scatter does nothing. |
| 1327 | auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3)); |
| 1328 | if (ConstMask && ConstMask->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1329 | return IC.eraseInstFromFunction(II); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1330 | |
| 1331 | return nullptr; |
| 1332 | } |
| 1333 | |
Amaury Sechet | 763c59d | 2016-08-18 20:43:50 +0000 | [diff] [blame] | 1334 | static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombiner &IC) { |
| 1335 | assert((II.getIntrinsicID() == Intrinsic::cttz || |
| 1336 | II.getIntrinsicID() == Intrinsic::ctlz) && |
| 1337 | "Expected cttz or ctlz intrinsic"); |
Sanjay Patel | 8e3ab17 | 2016-08-05 22:42:46 +0000 | [diff] [blame] | 1338 | Value *Op0 = II.getArgOperand(0); |
| 1339 | // FIXME: Try to simplify vectors of integers. |
| 1340 | auto *IT = dyn_cast<IntegerType>(Op0->getType()); |
| 1341 | if (!IT) |
| 1342 | return nullptr; |
| 1343 | |
| 1344 | unsigned BitWidth = IT->getBitWidth(); |
| 1345 | APInt KnownZero(BitWidth, 0); |
| 1346 | APInt KnownOne(BitWidth, 0); |
| 1347 | IC.computeKnownBits(Op0, KnownZero, KnownOne, 0, &II); |
| 1348 | |
| 1349 | // Create a mask for bits above (ctlz) or below (cttz) the first known one. |
| 1350 | bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz; |
| 1351 | unsigned NumMaskBits = IsTZ ? KnownOne.countTrailingZeros() |
| 1352 | : KnownOne.countLeadingZeros(); |
| 1353 | APInt Mask = IsTZ ? APInt::getLowBitsSet(BitWidth, NumMaskBits) |
| 1354 | : APInt::getHighBitsSet(BitWidth, NumMaskBits); |
| 1355 | |
| 1356 | // If all bits above (ctlz) or below (cttz) the first known one are known |
| 1357 | // zero, this value is constant. |
| 1358 | // FIXME: This should be in InstSimplify because we're replacing an |
| 1359 | // instruction with a constant. |
Amaury Sechet | 763c59d | 2016-08-18 20:43:50 +0000 | [diff] [blame] | 1360 | if ((Mask & KnownZero) == Mask) { |
| 1361 | auto *C = ConstantInt::get(IT, APInt(BitWidth, NumMaskBits)); |
| 1362 | return IC.replaceInstUsesWith(II, C); |
| 1363 | } |
| 1364 | |
| 1365 | // If the input to cttz/ctlz is known to be non-zero, |
| 1366 | // then change the 'ZeroIsUndef' parameter to 'true' |
| 1367 | // because we know the zero behavior can't affect the result. |
| 1368 | if (KnownOne != 0 || isKnownNonZero(Op0, IC.getDataLayout())) { |
| 1369 | if (!match(II.getArgOperand(1), m_One())) { |
| 1370 | II.setOperand(1, IC.Builder->getTrue()); |
| 1371 | return &II; |
| 1372 | } |
| 1373 | } |
Sanjay Patel | 8e3ab17 | 2016-08-05 22:42:46 +0000 | [diff] [blame] | 1374 | |
| 1375 | return nullptr; |
| 1376 | } |
| 1377 | |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1378 | // TODO: If the x86 backend knew how to convert a bool vector mask back to an |
| 1379 | // XMM register mask efficiently, we could transform all x86 masked intrinsics |
| 1380 | // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1381 | static Instruction *simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC) { |
| 1382 | Value *Ptr = II.getOperand(0); |
| 1383 | Value *Mask = II.getOperand(1); |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1384 | Constant *ZeroVec = Constant::getNullValue(II.getType()); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1385 | |
| 1386 | // Special case a zero mask since that's not a ConstantDataVector. |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1387 | // This masked load instruction creates a zero vector. |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1388 | if (isa<ConstantAggregateZero>(Mask)) |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1389 | return IC.replaceInstUsesWith(II, ZeroVec); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1390 | |
| 1391 | auto *ConstMask = dyn_cast<ConstantDataVector>(Mask); |
| 1392 | if (!ConstMask) |
| 1393 | return nullptr; |
| 1394 | |
| 1395 | // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic |
| 1396 | // to allow target-independent optimizations. |
| 1397 | |
| 1398 | // First, cast the x86 intrinsic scalar pointer to a vector pointer to match |
| 1399 | // the LLVM intrinsic definition for the pointer argument. |
| 1400 | unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 1401 | PointerType *VecPtrTy = PointerType::get(II.getType(), AddrSpace); |
| 1402 | Value *PtrCast = IC.Builder->CreateBitCast(Ptr, VecPtrTy, "castvec"); |
| 1403 | |
| 1404 | // Second, convert the x86 XMM integer vector mask to a vector of bools based |
| 1405 | // on each element's most significant bit (the sign bit). |
| 1406 | Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask); |
| 1407 | |
Sanjay Patel | 5e5056d | 2016-04-12 23:16:23 +0000 | [diff] [blame] | 1408 | // The pass-through vector for an x86 masked load is a zero vector. |
| 1409 | CallInst *NewMaskedLoad = |
| 1410 | IC.Builder->CreateMaskedLoad(PtrCast, 1, BoolMask, ZeroVec); |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 1411 | return IC.replaceInstUsesWith(II, NewMaskedLoad); |
| 1412 | } |
| 1413 | |
| 1414 | // TODO: If the x86 backend knew how to convert a bool vector mask back to an |
| 1415 | // XMM register mask efficiently, we could transform all x86 masked intrinsics |
| 1416 | // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1417 | static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC) { |
| 1418 | Value *Ptr = II.getOperand(0); |
| 1419 | Value *Mask = II.getOperand(1); |
| 1420 | Value *Vec = II.getOperand(2); |
| 1421 | |
| 1422 | // Special case a zero mask since that's not a ConstantDataVector: |
| 1423 | // this masked store instruction does nothing. |
| 1424 | if (isa<ConstantAggregateZero>(Mask)) { |
| 1425 | IC.eraseInstFromFunction(II); |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
Sanjay Patel | c4acbae | 2016-03-12 15:16:59 +0000 | [diff] [blame] | 1429 | // The SSE2 version is too weird (eg, unaligned but non-temporal) to do |
| 1430 | // anything else at this level. |
| 1431 | if (II.getIntrinsicID() == Intrinsic::x86_sse2_maskmov_dqu) |
| 1432 | return false; |
| 1433 | |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1434 | auto *ConstMask = dyn_cast<ConstantDataVector>(Mask); |
| 1435 | if (!ConstMask) |
| 1436 | return false; |
| 1437 | |
| 1438 | // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic |
| 1439 | // to allow target-independent optimizations. |
| 1440 | |
| 1441 | // First, cast the x86 intrinsic scalar pointer to a vector pointer to match |
| 1442 | // the LLVM intrinsic definition for the pointer argument. |
| 1443 | unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 1444 | PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace); |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 1445 | Value *PtrCast = IC.Builder->CreateBitCast(Ptr, VecPtrTy, "castvec"); |
| 1446 | |
| 1447 | // Second, convert the x86 XMM integer vector mask to a vector of bools based |
| 1448 | // on each element's most significant bit (the sign bit). |
| 1449 | Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask); |
| 1450 | |
| 1451 | IC.Builder->CreateMaskedStore(Vec, PtrCast, 1, BoolMask); |
| 1452 | |
| 1453 | // 'Replace uses' doesn't work for stores. Erase the original masked store. |
| 1454 | IC.eraseInstFromFunction(II); |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 1458 | // Returns true iff the 2 intrinsics have the same operands, limiting the |
| 1459 | // comparison to the first NumOperands. |
| 1460 | static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E, |
| 1461 | unsigned NumOperands) { |
| 1462 | assert(I.getNumArgOperands() >= NumOperands && "Not enough operands"); |
| 1463 | assert(E.getNumArgOperands() >= NumOperands && "Not enough operands"); |
| 1464 | for (unsigned i = 0; i < NumOperands; i++) |
| 1465 | if (I.getArgOperand(i) != E.getArgOperand(i)) |
| 1466 | return false; |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
| 1470 | // Remove trivially empty start/end intrinsic ranges, i.e. a start |
| 1471 | // immediately followed by an end (ignoring debuginfo or other |
| 1472 | // start/end intrinsics in between). As this handles only the most trivial |
| 1473 | // cases, tracking the nesting level is not needed: |
| 1474 | // |
| 1475 | // call @llvm.foo.start(i1 0) ; &I |
| 1476 | // call @llvm.foo.start(i1 0) |
| 1477 | // call @llvm.foo.end(i1 0) ; This one will not be skipped: it will be removed |
| 1478 | // call @llvm.foo.end(i1 0) |
| 1479 | static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID, |
| 1480 | unsigned EndID, InstCombiner &IC) { |
| 1481 | assert(I.getIntrinsicID() == StartID && |
| 1482 | "Start intrinsic does not have expected ID"); |
| 1483 | BasicBlock::iterator BI(I), BE(I.getParent()->end()); |
| 1484 | for (++BI; BI != BE; ++BI) { |
| 1485 | if (auto *E = dyn_cast<IntrinsicInst>(BI)) { |
| 1486 | if (isa<DbgInfoIntrinsic>(E) || E->getIntrinsicID() == StartID) |
| 1487 | continue; |
| 1488 | if (E->getIntrinsicID() == EndID && |
| 1489 | haveSameOperands(I, *E, E->getNumArgOperands())) { |
| 1490 | IC.eraseInstFromFunction(*E); |
| 1491 | IC.eraseInstFromFunction(I); |
| 1492 | return true; |
| 1493 | } |
| 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | return false; |
| 1499 | } |
| 1500 | |
Justin Lebar | 698c31b | 2017-01-27 00:58:58 +0000 | [diff] [blame] | 1501 | // Convert NVVM intrinsics to target-generic LLVM code where possible. |
| 1502 | static Instruction *SimplifyNVVMIntrinsic(IntrinsicInst *II, InstCombiner &IC) { |
| 1503 | // Each NVVM intrinsic we can simplify can be replaced with one of: |
| 1504 | // |
| 1505 | // * an LLVM intrinsic, |
| 1506 | // * an LLVM cast operation, |
| 1507 | // * an LLVM binary operation, or |
| 1508 | // * ad-hoc LLVM IR for the particular operation. |
| 1509 | |
| 1510 | // Some transformations are only valid when the module's |
| 1511 | // flush-denormals-to-zero (ftz) setting is true/false, whereas other |
| 1512 | // transformations are valid regardless of the module's ftz setting. |
| 1513 | enum FtzRequirementTy { |
| 1514 | FTZ_Any, // Any ftz setting is ok. |
| 1515 | FTZ_MustBeOn, // Transformation is valid only if ftz is on. |
| 1516 | FTZ_MustBeOff, // Transformation is valid only if ftz is off. |
| 1517 | }; |
| 1518 | // Classes of NVVM intrinsics that can't be replaced one-to-one with a |
| 1519 | // target-generic intrinsic, cast op, or binary op but that we can nonetheless |
| 1520 | // simplify. |
| 1521 | enum SpecialCase { |
| 1522 | SPC_Reciprocal, |
| 1523 | }; |
| 1524 | |
| 1525 | // SimplifyAction is a poor-man's variant (plus an additional flag) that |
| 1526 | // represents how to replace an NVVM intrinsic with target-generic LLVM IR. |
| 1527 | struct SimplifyAction { |
| 1528 | // Invariant: At most one of these Optionals has a value. |
| 1529 | Optional<Intrinsic::ID> IID; |
| 1530 | Optional<Instruction::CastOps> CastOp; |
| 1531 | Optional<Instruction::BinaryOps> BinaryOp; |
| 1532 | Optional<SpecialCase> Special; |
| 1533 | |
| 1534 | FtzRequirementTy FtzRequirement = FTZ_Any; |
| 1535 | |
| 1536 | SimplifyAction() = default; |
| 1537 | |
| 1538 | SimplifyAction(Intrinsic::ID IID, FtzRequirementTy FtzReq) |
| 1539 | : IID(IID), FtzRequirement(FtzReq) {} |
| 1540 | |
| 1541 | // Cast operations don't have anything to do with FTZ, so we skip that |
| 1542 | // argument. |
| 1543 | SimplifyAction(Instruction::CastOps CastOp) : CastOp(CastOp) {} |
| 1544 | |
| 1545 | SimplifyAction(Instruction::BinaryOps BinaryOp, FtzRequirementTy FtzReq) |
| 1546 | : BinaryOp(BinaryOp), FtzRequirement(FtzReq) {} |
| 1547 | |
| 1548 | SimplifyAction(SpecialCase Special, FtzRequirementTy FtzReq) |
| 1549 | : Special(Special), FtzRequirement(FtzReq) {} |
| 1550 | }; |
| 1551 | |
| 1552 | // Try to generate a SimplifyAction describing how to replace our |
| 1553 | // IntrinsicInstr with target-generic LLVM IR. |
| 1554 | const SimplifyAction Action = [II]() -> SimplifyAction { |
| 1555 | switch (II->getIntrinsicID()) { |
| 1556 | |
| 1557 | // NVVM intrinsics that map directly to LLVM intrinsics. |
| 1558 | case Intrinsic::nvvm_ceil_d: |
| 1559 | return {Intrinsic::ceil, FTZ_Any}; |
| 1560 | case Intrinsic::nvvm_ceil_f: |
| 1561 | return {Intrinsic::ceil, FTZ_MustBeOff}; |
| 1562 | case Intrinsic::nvvm_ceil_ftz_f: |
| 1563 | return {Intrinsic::ceil, FTZ_MustBeOn}; |
| 1564 | case Intrinsic::nvvm_fabs_d: |
| 1565 | return {Intrinsic::fabs, FTZ_Any}; |
| 1566 | case Intrinsic::nvvm_fabs_f: |
| 1567 | return {Intrinsic::fabs, FTZ_MustBeOff}; |
| 1568 | case Intrinsic::nvvm_fabs_ftz_f: |
| 1569 | return {Intrinsic::fabs, FTZ_MustBeOn}; |
| 1570 | case Intrinsic::nvvm_floor_d: |
| 1571 | return {Intrinsic::floor, FTZ_Any}; |
| 1572 | case Intrinsic::nvvm_floor_f: |
| 1573 | return {Intrinsic::floor, FTZ_MustBeOff}; |
| 1574 | case Intrinsic::nvvm_floor_ftz_f: |
| 1575 | return {Intrinsic::floor, FTZ_MustBeOn}; |
| 1576 | case Intrinsic::nvvm_fma_rn_d: |
| 1577 | return {Intrinsic::fma, FTZ_Any}; |
| 1578 | case Intrinsic::nvvm_fma_rn_f: |
| 1579 | return {Intrinsic::fma, FTZ_MustBeOff}; |
| 1580 | case Intrinsic::nvvm_fma_rn_ftz_f: |
| 1581 | return {Intrinsic::fma, FTZ_MustBeOn}; |
| 1582 | case Intrinsic::nvvm_fmax_d: |
| 1583 | return {Intrinsic::maxnum, FTZ_Any}; |
| 1584 | case Intrinsic::nvvm_fmax_f: |
| 1585 | return {Intrinsic::maxnum, FTZ_MustBeOff}; |
| 1586 | case Intrinsic::nvvm_fmax_ftz_f: |
| 1587 | return {Intrinsic::maxnum, FTZ_MustBeOn}; |
| 1588 | case Intrinsic::nvvm_fmin_d: |
| 1589 | return {Intrinsic::minnum, FTZ_Any}; |
| 1590 | case Intrinsic::nvvm_fmin_f: |
| 1591 | return {Intrinsic::minnum, FTZ_MustBeOff}; |
| 1592 | case Intrinsic::nvvm_fmin_ftz_f: |
| 1593 | return {Intrinsic::minnum, FTZ_MustBeOn}; |
| 1594 | case Intrinsic::nvvm_round_d: |
| 1595 | return {Intrinsic::round, FTZ_Any}; |
| 1596 | case Intrinsic::nvvm_round_f: |
| 1597 | return {Intrinsic::round, FTZ_MustBeOff}; |
| 1598 | case Intrinsic::nvvm_round_ftz_f: |
| 1599 | return {Intrinsic::round, FTZ_MustBeOn}; |
| 1600 | case Intrinsic::nvvm_sqrt_rn_d: |
| 1601 | return {Intrinsic::sqrt, FTZ_Any}; |
| 1602 | case Intrinsic::nvvm_sqrt_f: |
| 1603 | // nvvm_sqrt_f is a special case. For most intrinsics, foo_ftz_f is the |
| 1604 | // ftz version, and foo_f is the non-ftz version. But nvvm_sqrt_f adopts |
| 1605 | // the ftz-ness of the surrounding code. sqrt_rn_f and sqrt_rn_ftz_f are |
| 1606 | // the versions with explicit ftz-ness. |
| 1607 | return {Intrinsic::sqrt, FTZ_Any}; |
| 1608 | case Intrinsic::nvvm_sqrt_rn_f: |
| 1609 | return {Intrinsic::sqrt, FTZ_MustBeOff}; |
| 1610 | case Intrinsic::nvvm_sqrt_rn_ftz_f: |
| 1611 | return {Intrinsic::sqrt, FTZ_MustBeOn}; |
| 1612 | case Intrinsic::nvvm_trunc_d: |
| 1613 | return {Intrinsic::trunc, FTZ_Any}; |
| 1614 | case Intrinsic::nvvm_trunc_f: |
| 1615 | return {Intrinsic::trunc, FTZ_MustBeOff}; |
| 1616 | case Intrinsic::nvvm_trunc_ftz_f: |
| 1617 | return {Intrinsic::trunc, FTZ_MustBeOn}; |
| 1618 | |
| 1619 | // NVVM intrinsics that map to LLVM cast operations. |
| 1620 | // |
| 1621 | // Note that llvm's target-generic conversion operators correspond to the rz |
| 1622 | // (round to zero) versions of the nvvm conversion intrinsics, even though |
| 1623 | // most everything else here uses the rn (round to nearest even) nvvm ops. |
| 1624 | case Intrinsic::nvvm_d2i_rz: |
| 1625 | case Intrinsic::nvvm_f2i_rz: |
| 1626 | case Intrinsic::nvvm_d2ll_rz: |
| 1627 | case Intrinsic::nvvm_f2ll_rz: |
| 1628 | return {Instruction::FPToSI}; |
| 1629 | case Intrinsic::nvvm_d2ui_rz: |
| 1630 | case Intrinsic::nvvm_f2ui_rz: |
| 1631 | case Intrinsic::nvvm_d2ull_rz: |
| 1632 | case Intrinsic::nvvm_f2ull_rz: |
| 1633 | return {Instruction::FPToUI}; |
| 1634 | case Intrinsic::nvvm_i2d_rz: |
| 1635 | case Intrinsic::nvvm_i2f_rz: |
| 1636 | case Intrinsic::nvvm_ll2d_rz: |
| 1637 | case Intrinsic::nvvm_ll2f_rz: |
| 1638 | return {Instruction::SIToFP}; |
| 1639 | case Intrinsic::nvvm_ui2d_rz: |
| 1640 | case Intrinsic::nvvm_ui2f_rz: |
| 1641 | case Intrinsic::nvvm_ull2d_rz: |
| 1642 | case Intrinsic::nvvm_ull2f_rz: |
| 1643 | return {Instruction::UIToFP}; |
| 1644 | |
| 1645 | // NVVM intrinsics that map to LLVM binary ops. |
| 1646 | case Intrinsic::nvvm_add_rn_d: |
| 1647 | return {Instruction::FAdd, FTZ_Any}; |
| 1648 | case Intrinsic::nvvm_add_rn_f: |
| 1649 | return {Instruction::FAdd, FTZ_MustBeOff}; |
| 1650 | case Intrinsic::nvvm_add_rn_ftz_f: |
| 1651 | return {Instruction::FAdd, FTZ_MustBeOn}; |
| 1652 | case Intrinsic::nvvm_mul_rn_d: |
| 1653 | return {Instruction::FMul, FTZ_Any}; |
| 1654 | case Intrinsic::nvvm_mul_rn_f: |
| 1655 | return {Instruction::FMul, FTZ_MustBeOff}; |
| 1656 | case Intrinsic::nvvm_mul_rn_ftz_f: |
| 1657 | return {Instruction::FMul, FTZ_MustBeOn}; |
| 1658 | case Intrinsic::nvvm_div_rn_d: |
| 1659 | return {Instruction::FDiv, FTZ_Any}; |
| 1660 | case Intrinsic::nvvm_div_rn_f: |
| 1661 | return {Instruction::FDiv, FTZ_MustBeOff}; |
| 1662 | case Intrinsic::nvvm_div_rn_ftz_f: |
| 1663 | return {Instruction::FDiv, FTZ_MustBeOn}; |
| 1664 | |
| 1665 | // The remainder of cases are NVVM intrinsics that map to LLVM idioms, but |
| 1666 | // need special handling. |
| 1667 | // |
| 1668 | // We seem to be mising intrinsics for rcp.approx.{ftz.}f32, which is just |
| 1669 | // as well. |
| 1670 | case Intrinsic::nvvm_rcp_rn_d: |
| 1671 | return {SPC_Reciprocal, FTZ_Any}; |
| 1672 | case Intrinsic::nvvm_rcp_rn_f: |
| 1673 | return {SPC_Reciprocal, FTZ_MustBeOff}; |
| 1674 | case Intrinsic::nvvm_rcp_rn_ftz_f: |
| 1675 | return {SPC_Reciprocal, FTZ_MustBeOn}; |
| 1676 | |
| 1677 | // We do not currently simplify intrinsics that give an approximate answer. |
| 1678 | // These include: |
| 1679 | // |
| 1680 | // - nvvm_cos_approx_{f,ftz_f} |
| 1681 | // - nvvm_ex2_approx_{d,f,ftz_f} |
| 1682 | // - nvvm_lg2_approx_{d,f,ftz_f} |
| 1683 | // - nvvm_sin_approx_{f,ftz_f} |
| 1684 | // - nvvm_sqrt_approx_{f,ftz_f} |
| 1685 | // - nvvm_rsqrt_approx_{d,f,ftz_f} |
| 1686 | // - nvvm_div_approx_{ftz_d,ftz_f,f} |
| 1687 | // - nvvm_rcp_approx_ftz_d |
| 1688 | // |
| 1689 | // Ideally we'd encode them as e.g. "fast call @llvm.cos", where "fast" |
| 1690 | // means that fastmath is enabled in the intrinsic. Unfortunately only |
| 1691 | // binary operators (currently) have a fastmath bit in SelectionDAG, so this |
| 1692 | // information gets lost and we can't select on it. |
| 1693 | // |
| 1694 | // TODO: div and rcp are lowered to a binary op, so these we could in theory |
| 1695 | // lower them to "fast fdiv". |
| 1696 | |
| 1697 | default: |
| 1698 | return {}; |
| 1699 | } |
| 1700 | }(); |
| 1701 | |
| 1702 | // If Action.FtzRequirementTy is not satisfied by the module's ftz state, we |
| 1703 | // can bail out now. (Notice that in the case that IID is not an NVVM |
| 1704 | // intrinsic, we don't have to look up any module metadata, as |
| 1705 | // FtzRequirementTy will be FTZ_Any.) |
| 1706 | if (Action.FtzRequirement != FTZ_Any) { |
| 1707 | bool FtzEnabled = |
| 1708 | II->getFunction()->getFnAttribute("nvptx-f32ftz").getValueAsString() == |
| 1709 | "true"; |
| 1710 | |
| 1711 | if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn)) |
| 1712 | return nullptr; |
| 1713 | } |
| 1714 | |
| 1715 | // Simplify to target-generic intrinsic. |
| 1716 | if (Action.IID) { |
| 1717 | SmallVector<Value *, 4> Args(II->arg_operands()); |
| 1718 | // All the target-generic intrinsics currently of interest to us have one |
| 1719 | // type argument, equal to that of the nvvm intrinsic's argument. |
Justin Lebar | e3ac0fb | 2017-01-27 01:49:39 +0000 | [diff] [blame^] | 1720 | Type *Tys[] = {II->getArgOperand(0)->getType()}; |
Justin Lebar | 698c31b | 2017-01-27 00:58:58 +0000 | [diff] [blame] | 1721 | return CallInst::Create( |
| 1722 | Intrinsic::getDeclaration(II->getModule(), *Action.IID, Tys), Args); |
| 1723 | } |
| 1724 | |
| 1725 | // Simplify to target-generic binary op. |
| 1726 | if (Action.BinaryOp) |
| 1727 | return BinaryOperator::Create(*Action.BinaryOp, II->getArgOperand(0), |
| 1728 | II->getArgOperand(1), II->getName()); |
| 1729 | |
| 1730 | // Simplify to target-generic cast op. |
| 1731 | if (Action.CastOp) |
| 1732 | return CastInst::Create(*Action.CastOp, II->getArgOperand(0), II->getType(), |
| 1733 | II->getName()); |
| 1734 | |
| 1735 | // All that's left are the special cases. |
| 1736 | if (!Action.Special) |
| 1737 | return nullptr; |
| 1738 | |
| 1739 | switch (*Action.Special) { |
| 1740 | case SPC_Reciprocal: |
| 1741 | // Simplify reciprocal. |
| 1742 | return BinaryOperator::Create( |
| 1743 | Instruction::FDiv, ConstantFP::get(II->getArgOperand(0)->getType(), 1), |
| 1744 | II->getArgOperand(0), II->getName()); |
| 1745 | } |
| 1746 | } |
| 1747 | |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 1748 | Instruction *InstCombiner::visitVAStartInst(VAStartInst &I) { |
| 1749 | removeTriviallyEmptyRange(I, Intrinsic::vastart, Intrinsic::vaend, *this); |
| 1750 | return nullptr; |
| 1751 | } |
| 1752 | |
| 1753 | Instruction *InstCombiner::visitVACopyInst(VACopyInst &I) { |
| 1754 | removeTriviallyEmptyRange(I, Intrinsic::vacopy, Intrinsic::vaend, *this); |
| 1755 | return nullptr; |
| 1756 | } |
| 1757 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 1758 | /// CallInst simplification. This mostly only handles folding of intrinsic |
| 1759 | /// instructions. For normal calls, it allows visitCallSite to do the heavy |
| 1760 | /// lifting. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1761 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 1762 | auto Args = CI.arg_operands(); |
| 1763 | if (Value *V = SimplifyCall(CI.getCalledValue(), Args.begin(), Args.end(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1764 | &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1765 | return replaceInstUsesWith(CI, V); |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 1766 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 1767 | if (isFreeCall(&CI, &TLI)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1768 | return visitFree(CI); |
| 1769 | |
| 1770 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 1771 | // callee isn't. |
Sanjay Patel | 5a47095 | 2016-08-11 15:16:06 +0000 | [diff] [blame] | 1772 | if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1773 | CI.setDoesNotThrow(); |
| 1774 | return &CI; |
| 1775 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1777 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 1778 | if (!II) return visitCallSite(&CI); |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1780 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 1781 | // visitCallSite. |
| 1782 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
| 1783 | bool Changed = false; |
| 1784 | |
| 1785 | // memmove/cpy/set of zero bytes is a noop. |
| 1786 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
Chris Lattner | c663a67 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 1787 | if (NumBytes->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1788 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 1791 | if (CI->getZExtValue() == 1) { |
| 1792 | // Replace the instruction with just byte operations. We would |
| 1793 | // transform other cases to loads/stores, but we don't know if |
| 1794 | // alignment is sufficient. |
| 1795 | } |
| 1796 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | c663a67 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 1798 | // No other transformations apply to volatile transfers. |
| 1799 | if (MI->isVolatile()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1800 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1801 | |
| 1802 | // If we have a memmove and the source operation is a constant global, |
| 1803 | // then the source and dest pointers can't alias, so we can change this |
| 1804 | // into a call to memcpy. |
| 1805 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 1806 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 1807 | if (GVSrc->isConstant()) { |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 1808 | Module *M = CI.getModule(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1809 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1810 | Type *Tys[3] = { CI.getArgOperand(0)->getType(), |
| 1811 | CI.getArgOperand(1)->getType(), |
| 1812 | CI.getArgOperand(2)->getType() }; |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 1813 | CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1814 | Changed = true; |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { |
| 1819 | // memmove(x,x,size) -> noop. |
| 1820 | if (MTI->getSource() == MTI->getDest()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1821 | return eraseInstFromFunction(CI); |
Eric Christopher | 7258dcd | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 1822 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1823 | |
Eric Christopher | 7258dcd | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 1824 | // If we can determine a pointer alignment that is bigger than currently |
| 1825 | // set, update the alignment. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1826 | if (isa<MemTransferInst>(MI)) { |
| 1827 | if (Instruction *I = SimplifyMemTransfer(MI)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1828 | return I; |
| 1829 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 1830 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 1831 | return I; |
| 1832 | } |
Gabor Greif | 590d95e | 2010-06-24 13:42:49 +0000 | [diff] [blame] | 1833 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1834 | if (Changed) return II; |
| 1835 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1836 | |
Justin Lebar | 698c31b | 2017-01-27 00:58:58 +0000 | [diff] [blame] | 1837 | if (Instruction *I = SimplifyNVVMIntrinsic(II, *this)) |
| 1838 | return I; |
| 1839 | |
Sanjay Patel | 1c600c6 | 2016-01-20 16:41:43 +0000 | [diff] [blame] | 1840 | auto SimplifyDemandedVectorEltsLow = [this](Value *Op, unsigned Width, |
| 1841 | unsigned DemandedWidth) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 1842 | APInt UndefElts(Width, 0); |
| 1843 | APInt DemandedElts = APInt::getLowBitsSet(Width, DemandedWidth); |
| 1844 | return SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts); |
| 1845 | }; |
| 1846 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1847 | switch (II->getIntrinsicID()) { |
| 1848 | default: break; |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 1849 | case Intrinsic::objectsize: |
| 1850 | if (ConstantInt *N = |
| 1851 | lowerObjectSizeCall(II, DL, &TLI, /*MustSucceed=*/false)) |
| 1852 | return replaceInstUsesWith(CI, N); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1853 | return nullptr; |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 1854 | |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1855 | case Intrinsic::bswap: { |
| 1856 | Value *IIOperand = II->getArgOperand(0); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1857 | Value *X = nullptr; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1859 | // bswap(bswap(x)) -> x |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1860 | if (match(IIOperand, m_BSwap(m_Value(X)))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1861 | return replaceInstUsesWith(CI, X); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1863 | // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1864 | if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) { |
| 1865 | unsigned C = X->getType()->getPrimitiveSizeInBits() - |
| 1866 | IIOperand->getType()->getPrimitiveSizeInBits(); |
| 1867 | Value *CV = ConstantInt::get(X->getType(), C); |
| 1868 | Value *V = Builder->CreateLShr(X, CV); |
| 1869 | return new TruncInst(V, IIOperand->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1870 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1871 | break; |
Michael Ilseman | 536cc32 | 2012-12-13 03:13:36 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
James Molloy | 2d09c00 | 2015-11-12 12:39:41 +0000 | [diff] [blame] | 1874 | case Intrinsic::bitreverse: { |
| 1875 | Value *IIOperand = II->getArgOperand(0); |
| 1876 | Value *X = nullptr; |
| 1877 | |
| 1878 | // bitreverse(bitreverse(x)) -> x |
| 1879 | if (match(IIOperand, m_Intrinsic<Intrinsic::bitreverse>(m_Value(X)))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1880 | return replaceInstUsesWith(CI, X); |
James Molloy | 2d09c00 | 2015-11-12 12:39:41 +0000 | [diff] [blame] | 1881 | break; |
| 1882 | } |
| 1883 | |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1884 | case Intrinsic::masked_load: |
| 1885 | if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1886 | return replaceInstUsesWith(CI, SimplifiedMaskedOp); |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1887 | break; |
Sanjay Patel | 04f792b | 2016-02-01 19:39:52 +0000 | [diff] [blame] | 1888 | case Intrinsic::masked_store: |
| 1889 | return simplifyMaskedStore(*II, *this); |
Sanjay Patel | 103ab7d | 2016-02-01 22:10:26 +0000 | [diff] [blame] | 1890 | case Intrinsic::masked_gather: |
| 1891 | return simplifyMaskedGather(*II, *this); |
| 1892 | case Intrinsic::masked_scatter: |
| 1893 | return simplifyMaskedScatter(*II, *this); |
Sanjay Patel | b695c55 | 2016-02-01 17:00:10 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1895 | case Intrinsic::powi: |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1896 | if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1897 | // powi(x, 0) -> 1.0 |
| 1898 | if (Power->isZero()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1899 | return replaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1900 | // powi(x, 1) -> x |
| 1901 | if (Power->isOne()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1902 | return replaceInstUsesWith(CI, II->getArgOperand(0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1903 | // powi(x, -1) -> 1/x |
| 1904 | if (Power->isAllOnesValue()) |
| 1905 | return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1906 | II->getArgOperand(0)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1907 | } |
| 1908 | break; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1909 | |
Sanjay Patel | 8e3ab17 | 2016-08-05 22:42:46 +0000 | [diff] [blame] | 1910 | case Intrinsic::cttz: |
| 1911 | case Intrinsic::ctlz: |
Amaury Sechet | 763c59d | 2016-08-18 20:43:50 +0000 | [diff] [blame] | 1912 | if (auto *I = foldCttzCtlz(*II, *this)) |
| 1913 | return I; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1914 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1915 | |
Nick Lewycky | abe2cc1 | 2015-04-13 19:17:37 +0000 | [diff] [blame] | 1916 | case Intrinsic::uadd_with_overflow: |
| 1917 | case Intrinsic::sadd_with_overflow: |
| 1918 | case Intrinsic::umul_with_overflow: |
| 1919 | case Intrinsic::smul_with_overflow: |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 1920 | if (isa<Constant>(II->getArgOperand(0)) && |
| 1921 | !isa<Constant>(II->getArgOperand(1))) { |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1922 | // Canonicalize constants into the RHS. |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 1923 | Value *LHS = II->getArgOperand(0); |
| 1924 | II->setArgOperand(0, II->getArgOperand(1)); |
| 1925 | II->setArgOperand(1, LHS); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1926 | return II; |
| 1927 | } |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1928 | LLVM_FALLTHROUGH; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1929 | |
Nick Lewycky | abe2cc1 | 2015-04-13 19:17:37 +0000 | [diff] [blame] | 1930 | case Intrinsic::usub_with_overflow: |
| 1931 | case Intrinsic::ssub_with_overflow: { |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1932 | OverflowCheckFlavor OCF = |
| 1933 | IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID()); |
| 1934 | assert(OCF != OCF_INVALID && "unexpected!"); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1935 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 1936 | Value *OperationResult = nullptr; |
| 1937 | Constant *OverflowResult = nullptr; |
| 1938 | if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1), |
| 1939 | *II, OperationResult, OverflowResult)) |
| 1940 | return CreateOverflowTuple(II, OperationResult, OverflowResult); |
Benjamin Kramer | a420df2 | 2014-07-04 10:22:21 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1942 | break; |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 1943 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1944 | |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1945 | case Intrinsic::minnum: |
| 1946 | case Intrinsic::maxnum: { |
| 1947 | Value *Arg0 = II->getArgOperand(0); |
| 1948 | Value *Arg1 = II->getArgOperand(1); |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 1949 | // Canonicalize constants to the RHS. |
| 1950 | if (isa<ConstantFP>(Arg0) && !isa<ConstantFP>(Arg1)) { |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1951 | II->setArgOperand(0, Arg1); |
| 1952 | II->setArgOperand(1, Arg0); |
| 1953 | return II; |
| 1954 | } |
Sanjay Patel | 0069f56 | 2016-01-31 16:35:23 +0000 | [diff] [blame] | 1955 | if (Value *V = simplifyMinnumMaxnum(*II)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1956 | return replaceInstUsesWith(*II, V); |
Matt Arsenault | d6511b4 | 2014-10-21 23:00:20 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1959 | case Intrinsic::fma: |
| 1960 | case Intrinsic::fmuladd: { |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1961 | Value *Src0 = II->getArgOperand(0); |
| 1962 | Value *Src1 = II->getArgOperand(1); |
| 1963 | |
Matt Arsenault | b264c94 | 2017-01-03 04:32:35 +0000 | [diff] [blame] | 1964 | // Canonicalize constants into the RHS. |
| 1965 | if (isa<Constant>(Src0) && !isa<Constant>(Src1)) { |
| 1966 | II->setArgOperand(0, Src1); |
| 1967 | II->setArgOperand(1, Src0); |
| 1968 | std::swap(Src0, Src1); |
| 1969 | } |
| 1970 | |
| 1971 | Value *LHS = nullptr; |
| 1972 | Value *RHS = nullptr; |
| 1973 | |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1974 | // fma fneg(x), fneg(y), z -> fma x, y, z |
| 1975 | if (match(Src0, m_FNeg(m_Value(LHS))) && |
| 1976 | match(Src1, m_FNeg(m_Value(RHS)))) { |
Matt Arsenault | 3f50904 | 2017-01-10 23:17:52 +0000 | [diff] [blame] | 1977 | II->setArgOperand(0, LHS); |
| 1978 | II->setArgOperand(1, RHS); |
| 1979 | return II; |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | // fma fabs(x), fabs(x), z -> fma x, x, z |
| 1983 | if (match(Src0, m_Intrinsic<Intrinsic::fabs>(m_Value(LHS))) && |
| 1984 | match(Src1, m_Intrinsic<Intrinsic::fabs>(m_Value(RHS))) && LHS == RHS) { |
Matt Arsenault | 3f50904 | 2017-01-10 23:17:52 +0000 | [diff] [blame] | 1985 | II->setArgOperand(0, LHS); |
| 1986 | II->setArgOperand(1, RHS); |
| 1987 | return II; |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Matt Arsenault | b264c94 | 2017-01-03 04:32:35 +0000 | [diff] [blame] | 1990 | // fma x, 1, z -> fadd x, z |
| 1991 | if (match(Src1, m_FPOne())) { |
| 1992 | Instruction *RI = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2)); |
| 1993 | RI->copyFastMathFlags(II); |
| 1994 | return RI; |
| 1995 | } |
| 1996 | |
Matt Arsenault | 1cc294c | 2017-01-03 04:32:31 +0000 | [diff] [blame] | 1997 | break; |
| 1998 | } |
Matt Arsenault | 56ff483 | 2017-01-03 22:40:34 +0000 | [diff] [blame] | 1999 | case Intrinsic::fabs: { |
| 2000 | Value *Cond; |
| 2001 | Constant *LHS, *RHS; |
| 2002 | if (match(II->getArgOperand(0), |
| 2003 | m_Select(m_Value(Cond), m_Constant(LHS), m_Constant(RHS)))) { |
| 2004 | CallInst *Call0 = Builder->CreateCall(II->getCalledFunction(), {LHS}); |
| 2005 | CallInst *Call1 = Builder->CreateCall(II->getCalledFunction(), {RHS}); |
| 2006 | return SelectInst::Create(Cond, Call0, Call1); |
| 2007 | } |
| 2008 | |
Matt Arsenault | 954a624 | 2017-01-23 23:55:08 +0000 | [diff] [blame] | 2009 | LLVM_FALLTHROUGH; |
| 2010 | } |
| 2011 | case Intrinsic::ceil: |
| 2012 | case Intrinsic::floor: |
| 2013 | case Intrinsic::round: |
| 2014 | case Intrinsic::nearbyint: |
| 2015 | case Intrinsic::trunc: { |
Matt Arsenault | 7233344 | 2017-01-17 00:10:40 +0000 | [diff] [blame] | 2016 | Value *ExtSrc; |
| 2017 | if (match(II->getArgOperand(0), m_FPExt(m_Value(ExtSrc))) && |
| 2018 | II->getArgOperand(0)->hasOneUse()) { |
| 2019 | // fabs (fpext x) -> fpext (fabs x) |
Matt Arsenault | 954a624 | 2017-01-23 23:55:08 +0000 | [diff] [blame] | 2020 | Value *F = Intrinsic::getDeclaration(II->getModule(), II->getIntrinsicID(), |
Matt Arsenault | 7233344 | 2017-01-17 00:10:40 +0000 | [diff] [blame] | 2021 | { ExtSrc->getType() }); |
| 2022 | CallInst *NewFabs = Builder->CreateCall(F, ExtSrc); |
| 2023 | NewFabs->copyFastMathFlags(II); |
| 2024 | NewFabs->takeName(II); |
| 2025 | return new FPExtInst(NewFabs, II->getType()); |
| 2026 | } |
| 2027 | |
Matt Arsenault | 56ff483 | 2017-01-03 22:40:34 +0000 | [diff] [blame] | 2028 | break; |
| 2029 | } |
Matt Arsenault | 3bdd75d | 2017-01-04 22:49:03 +0000 | [diff] [blame] | 2030 | case Intrinsic::cos: |
| 2031 | case Intrinsic::amdgcn_cos: { |
| 2032 | Value *SrcSrc; |
| 2033 | Value *Src = II->getArgOperand(0); |
| 2034 | if (match(Src, m_FNeg(m_Value(SrcSrc))) || |
| 2035 | match(Src, m_Intrinsic<Intrinsic::fabs>(m_Value(SrcSrc)))) { |
| 2036 | // cos(-x) -> cos(x) |
| 2037 | // cos(fabs(x)) -> cos(x) |
| 2038 | II->setArgOperand(0, SrcSrc); |
| 2039 | return II; |
| 2040 | } |
| 2041 | |
| 2042 | break; |
| 2043 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2044 | case Intrinsic::ppc_altivec_lvx: |
| 2045 | case Intrinsic::ppc_altivec_lvxl: |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 2046 | // Turn PPC lvx -> load if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2047 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2048 | &DT) >= 16) { |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 2049 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2050 | PointerType::getUnqual(II->getType())); |
| 2051 | return new LoadInst(Ptr); |
| 2052 | } |
| 2053 | break; |
Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 2054 | case Intrinsic::ppc_vsx_lxvw4x: |
| 2055 | case Intrinsic::ppc_vsx_lxvd2x: { |
| 2056 | // Turn PPC VSX loads into normal loads. |
| 2057 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
| 2058 | PointerType::getUnqual(II->getType())); |
| 2059 | return new LoadInst(Ptr, Twine(""), false, 1); |
| 2060 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2061 | case Intrinsic::ppc_altivec_stvx: |
| 2062 | case Intrinsic::ppc_altivec_stvxl: |
| 2063 | // Turn stvx -> store if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2064 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2065 | &DT) >= 16) { |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2066 | Type *OpPtrTy = |
Gabor Greif | a6d75e2 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 2067 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 2068 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 2069 | return new StoreInst(II->getArgOperand(0), Ptr); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2070 | } |
| 2071 | break; |
Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 2072 | case Intrinsic::ppc_vsx_stxvw4x: |
| 2073 | case Intrinsic::ppc_vsx_stxvd2x: { |
| 2074 | // Turn PPC VSX stores into normal stores. |
| 2075 | Type *OpPtrTy = PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 2076 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 2077 | return new StoreInst(II->getArgOperand(0), Ptr, false, 1); |
| 2078 | } |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2079 | case Intrinsic::ppc_qpx_qvlfs: |
| 2080 | // Turn PPC QPX qvlfs -> load if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2081 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2082 | &DT) >= 16) { |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 2083 | Type *VTy = VectorType::get(Builder->getFloatTy(), |
| 2084 | II->getType()->getVectorNumElements()); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2085 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 2086 | PointerType::getUnqual(VTy)); |
| 2087 | Value *Load = Builder->CreateLoad(Ptr); |
| 2088 | return new FPExtInst(Load, II->getType()); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2089 | } |
| 2090 | break; |
| 2091 | case Intrinsic::ppc_qpx_qvlfd: |
| 2092 | // Turn PPC QPX qvlfd -> load if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2093 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 32, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2094 | &DT) >= 32) { |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2095 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
| 2096 | PointerType::getUnqual(II->getType())); |
| 2097 | return new LoadInst(Ptr); |
| 2098 | } |
| 2099 | break; |
| 2100 | case Intrinsic::ppc_qpx_qvstfs: |
| 2101 | // Turn PPC QPX qvstfs -> store if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2102 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2103 | &DT) >= 16) { |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 2104 | Type *VTy = VectorType::get(Builder->getFloatTy(), |
| 2105 | II->getArgOperand(0)->getType()->getVectorNumElements()); |
| 2106 | Value *TOp = Builder->CreateFPTrunc(II->getArgOperand(0), VTy); |
| 2107 | Type *OpPtrTy = PointerType::getUnqual(VTy); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2108 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
Hal Finkel | f0d68d7 | 2015-05-11 06:37:03 +0000 | [diff] [blame] | 2109 | return new StoreInst(TOp, Ptr); |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2110 | } |
| 2111 | break; |
| 2112 | case Intrinsic::ppc_qpx_qvstfd: |
| 2113 | // Turn PPC QPX qvstfd -> store if the pointer is known aligned. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2114 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 32, DL, II, &AC, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2115 | &DT) >= 32) { |
Hal Finkel | 221f467 | 2015-02-26 18:56:03 +0000 | [diff] [blame] | 2116 | Type *OpPtrTy = |
| 2117 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 2118 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 2119 | return new StoreInst(II->getArgOperand(0), Ptr); |
| 2120 | } |
| 2121 | break; |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2122 | |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2123 | case Intrinsic::x86_vcvtph2ps_128: |
| 2124 | case Intrinsic::x86_vcvtph2ps_256: { |
| 2125 | auto Arg = II->getArgOperand(0); |
| 2126 | auto ArgType = cast<VectorType>(Arg->getType()); |
| 2127 | auto RetType = cast<VectorType>(II->getType()); |
| 2128 | unsigned ArgWidth = ArgType->getNumElements(); |
| 2129 | unsigned RetWidth = RetType->getNumElements(); |
| 2130 | assert(RetWidth <= ArgWidth && "Unexpected input/return vector widths"); |
| 2131 | assert(ArgType->isIntOrIntVectorTy() && |
| 2132 | ArgType->getScalarSizeInBits() == 16 && |
| 2133 | "CVTPH2PS input type should be 16-bit integer vector"); |
| 2134 | assert(RetType->getScalarType()->isFloatTy() && |
| 2135 | "CVTPH2PS output type should be 32-bit float vector"); |
| 2136 | |
| 2137 | // Constant folding: Convert to generic half to single conversion. |
Simon Pilgrim | 48ffca0 | 2015-09-12 14:00:17 +0000 | [diff] [blame] | 2138 | if (isa<ConstantAggregateZero>(Arg)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2139 | return replaceInstUsesWith(*II, ConstantAggregateZero::get(RetType)); |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2140 | |
Simon Pilgrim | 48ffca0 | 2015-09-12 14:00:17 +0000 | [diff] [blame] | 2141 | if (isa<ConstantDataVector>(Arg)) { |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2142 | auto VectorHalfAsShorts = Arg; |
| 2143 | if (RetWidth < ArgWidth) { |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 2144 | SmallVector<uint32_t, 8> SubVecMask; |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2145 | for (unsigned i = 0; i != RetWidth; ++i) |
| 2146 | SubVecMask.push_back((int)i); |
| 2147 | VectorHalfAsShorts = Builder->CreateShuffleVector( |
| 2148 | Arg, UndefValue::get(ArgType), SubVecMask); |
| 2149 | } |
| 2150 | |
| 2151 | auto VectorHalfType = |
| 2152 | VectorType::get(Type::getHalfTy(II->getContext()), RetWidth); |
| 2153 | auto VectorHalfs = |
| 2154 | Builder->CreateBitCast(VectorHalfAsShorts, VectorHalfType); |
| 2155 | auto VectorFloats = Builder->CreateFPExt(VectorHalfs, RetType); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2156 | return replaceInstUsesWith(*II, VectorFloats); |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | // We only use the lowest lanes of the argument. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 2160 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg, ArgWidth, RetWidth)) { |
Simon Pilgrim | 20c607b | 2015-09-12 13:39:53 +0000 | [diff] [blame] | 2161 | II->setArgOperand(0, V); |
| 2162 | return II; |
| 2163 | } |
| 2164 | break; |
| 2165 | } |
| 2166 | |
Chandler Carruth | cf414cf | 2011-01-10 07:19:37 +0000 | [diff] [blame] | 2167 | case Intrinsic::x86_sse_cvtss2si: |
| 2168 | case Intrinsic::x86_sse_cvtss2si64: |
| 2169 | case Intrinsic::x86_sse_cvttss2si: |
| 2170 | case Intrinsic::x86_sse_cvttss2si64: |
| 2171 | case Intrinsic::x86_sse2_cvtsd2si: |
| 2172 | case Intrinsic::x86_sse2_cvtsd2si64: |
| 2173 | case Intrinsic::x86_sse2_cvttsd2si: |
Craig Topper | aeaa52c | 2016-12-14 07:46:12 +0000 | [diff] [blame] | 2174 | case Intrinsic::x86_sse2_cvttsd2si64: |
| 2175 | case Intrinsic::x86_avx512_vcvtss2si32: |
| 2176 | case Intrinsic::x86_avx512_vcvtss2si64: |
| 2177 | case Intrinsic::x86_avx512_vcvtss2usi32: |
| 2178 | case Intrinsic::x86_avx512_vcvtss2usi64: |
| 2179 | case Intrinsic::x86_avx512_vcvtsd2si32: |
| 2180 | case Intrinsic::x86_avx512_vcvtsd2si64: |
| 2181 | case Intrinsic::x86_avx512_vcvtsd2usi32: |
| 2182 | case Intrinsic::x86_avx512_vcvtsd2usi64: |
| 2183 | case Intrinsic::x86_avx512_cvttss2si: |
| 2184 | case Intrinsic::x86_avx512_cvttss2si64: |
| 2185 | case Intrinsic::x86_avx512_cvttss2usi: |
| 2186 | case Intrinsic::x86_avx512_cvttss2usi64: |
| 2187 | case Intrinsic::x86_avx512_cvttsd2si: |
| 2188 | case Intrinsic::x86_avx512_cvttsd2si64: |
| 2189 | case Intrinsic::x86_avx512_cvttsd2usi: |
| 2190 | case Intrinsic::x86_avx512_cvttsd2usi64: { |
Chandler Carruth | cf414cf | 2011-01-10 07:19:37 +0000 | [diff] [blame] | 2191 | // These intrinsics only demand the 0th element of their input vectors. If |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2192 | // we can simplify the input based on that, do so now. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 2193 | Value *Arg = II->getArgOperand(0); |
| 2194 | unsigned VWidth = Arg->getType()->getVectorNumElements(); |
| 2195 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg, VWidth, 1)) { |
Gabor Greif | 5b1370e | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 2196 | II->setArgOperand(0, V); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2197 | return II; |
| 2198 | } |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 2199 | break; |
| 2200 | } |
| 2201 | |
Simon Pilgrim | 91e3ac8 | 2016-06-07 08:18:35 +0000 | [diff] [blame] | 2202 | case Intrinsic::x86_mmx_pmovmskb: |
| 2203 | case Intrinsic::x86_sse_movmsk_ps: |
| 2204 | case Intrinsic::x86_sse2_movmsk_pd: |
| 2205 | case Intrinsic::x86_sse2_pmovmskb_128: |
| 2206 | case Intrinsic::x86_avx_movmsk_pd_256: |
| 2207 | case Intrinsic::x86_avx_movmsk_ps_256: |
| 2208 | case Intrinsic::x86_avx2_pmovmskb: { |
| 2209 | if (Value *V = simplifyX86movmsk(*II, *Builder)) |
| 2210 | return replaceInstUsesWith(*II, V); |
| 2211 | break; |
| 2212 | } |
| 2213 | |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2214 | case Intrinsic::x86_sse_comieq_ss: |
| 2215 | case Intrinsic::x86_sse_comige_ss: |
| 2216 | case Intrinsic::x86_sse_comigt_ss: |
| 2217 | case Intrinsic::x86_sse_comile_ss: |
| 2218 | case Intrinsic::x86_sse_comilt_ss: |
| 2219 | case Intrinsic::x86_sse_comineq_ss: |
| 2220 | case Intrinsic::x86_sse_ucomieq_ss: |
| 2221 | case Intrinsic::x86_sse_ucomige_ss: |
| 2222 | case Intrinsic::x86_sse_ucomigt_ss: |
| 2223 | case Intrinsic::x86_sse_ucomile_ss: |
| 2224 | case Intrinsic::x86_sse_ucomilt_ss: |
| 2225 | case Intrinsic::x86_sse_ucomineq_ss: |
| 2226 | case Intrinsic::x86_sse2_comieq_sd: |
| 2227 | case Intrinsic::x86_sse2_comige_sd: |
| 2228 | case Intrinsic::x86_sse2_comigt_sd: |
| 2229 | case Intrinsic::x86_sse2_comile_sd: |
| 2230 | case Intrinsic::x86_sse2_comilt_sd: |
| 2231 | case Intrinsic::x86_sse2_comineq_sd: |
| 2232 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 2233 | case Intrinsic::x86_sse2_ucomige_sd: |
| 2234 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 2235 | case Intrinsic::x86_sse2_ucomile_sd: |
| 2236 | case Intrinsic::x86_sse2_ucomilt_sd: |
Craig Topper | d963953 | 2016-12-11 07:42:04 +0000 | [diff] [blame] | 2237 | case Intrinsic::x86_sse2_ucomineq_sd: |
Craig Topper | d00db69 | 2016-12-31 00:45:06 +0000 | [diff] [blame] | 2238 | case Intrinsic::x86_avx512_vcomi_ss: |
| 2239 | case Intrinsic::x86_avx512_vcomi_sd: |
Craig Topper | d963953 | 2016-12-11 07:42:04 +0000 | [diff] [blame] | 2240 | case Intrinsic::x86_avx512_mask_cmp_ss: |
| 2241 | case Intrinsic::x86_avx512_mask_cmp_sd: { |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2242 | // These intrinsics only demand the 0th element of their input vectors. If |
| 2243 | // we can simplify the input based on that, do so now. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2244 | bool MadeChange = false; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2245 | Value *Arg0 = II->getArgOperand(0); |
| 2246 | Value *Arg1 = II->getArgOperand(1); |
| 2247 | unsigned VWidth = Arg0->getType()->getVectorNumElements(); |
| 2248 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg0, VWidth, 1)) { |
| 2249 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2250 | MadeChange = true; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2251 | } |
| 2252 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) { |
| 2253 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2254 | MadeChange = true; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2255 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2256 | if (MadeChange) |
| 2257 | return II; |
Simon Pilgrim | 471efd2 | 2016-02-20 23:17:35 +0000 | [diff] [blame] | 2258 | break; |
| 2259 | } |
| 2260 | |
Craig Topper | 020b228 | 2016-12-27 00:23:16 +0000 | [diff] [blame] | 2261 | case Intrinsic::x86_avx512_mask_add_ps_512: |
| 2262 | case Intrinsic::x86_avx512_mask_div_ps_512: |
| 2263 | case Intrinsic::x86_avx512_mask_mul_ps_512: |
| 2264 | case Intrinsic::x86_avx512_mask_sub_ps_512: |
| 2265 | case Intrinsic::x86_avx512_mask_add_pd_512: |
| 2266 | case Intrinsic::x86_avx512_mask_div_pd_512: |
| 2267 | case Intrinsic::x86_avx512_mask_mul_pd_512: |
| 2268 | case Intrinsic::x86_avx512_mask_sub_pd_512: |
| 2269 | // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular |
| 2270 | // IR operations. |
| 2271 | if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(4))) { |
| 2272 | if (R->getValue() == 4) { |
| 2273 | Value *Arg0 = II->getArgOperand(0); |
| 2274 | Value *Arg1 = II->getArgOperand(1); |
| 2275 | |
| 2276 | Value *V; |
| 2277 | switch (II->getIntrinsicID()) { |
| 2278 | default: llvm_unreachable("Case stmts out of sync!"); |
| 2279 | case Intrinsic::x86_avx512_mask_add_ps_512: |
| 2280 | case Intrinsic::x86_avx512_mask_add_pd_512: |
| 2281 | V = Builder->CreateFAdd(Arg0, Arg1); |
| 2282 | break; |
| 2283 | case Intrinsic::x86_avx512_mask_sub_ps_512: |
| 2284 | case Intrinsic::x86_avx512_mask_sub_pd_512: |
| 2285 | V = Builder->CreateFSub(Arg0, Arg1); |
| 2286 | break; |
| 2287 | case Intrinsic::x86_avx512_mask_mul_ps_512: |
| 2288 | case Intrinsic::x86_avx512_mask_mul_pd_512: |
| 2289 | V = Builder->CreateFMul(Arg0, Arg1); |
| 2290 | break; |
| 2291 | case Intrinsic::x86_avx512_mask_div_ps_512: |
| 2292 | case Intrinsic::x86_avx512_mask_div_pd_512: |
| 2293 | V = Builder->CreateFDiv(Arg0, Arg1); |
| 2294 | break; |
| 2295 | } |
| 2296 | |
| 2297 | // Create a select for the masking. |
| 2298 | V = emitX86MaskSelect(II->getArgOperand(3), V, II->getArgOperand(2), |
| 2299 | *Builder); |
| 2300 | return replaceInstUsesWith(*II, V); |
| 2301 | } |
| 2302 | } |
| 2303 | break; |
| 2304 | |
Craig Topper | 790d0fa | 2016-12-11 07:42:01 +0000 | [diff] [blame] | 2305 | case Intrinsic::x86_avx512_mask_add_ss_round: |
| 2306 | case Intrinsic::x86_avx512_mask_div_ss_round: |
| 2307 | case Intrinsic::x86_avx512_mask_mul_ss_round: |
| 2308 | case Intrinsic::x86_avx512_mask_sub_ss_round: |
Craig Topper | 790d0fa | 2016-12-11 07:42:01 +0000 | [diff] [blame] | 2309 | case Intrinsic::x86_avx512_mask_add_sd_round: |
| 2310 | case Intrinsic::x86_avx512_mask_div_sd_round: |
| 2311 | case Intrinsic::x86_avx512_mask_mul_sd_round: |
| 2312 | case Intrinsic::x86_avx512_mask_sub_sd_round: |
Craig Topper | 7b788ada | 2016-12-26 06:33:19 +0000 | [diff] [blame] | 2313 | // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular |
| 2314 | // IR operations. |
| 2315 | if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(4))) { |
| 2316 | if (R->getValue() == 4) { |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2317 | // Extract the element as scalars. |
| 2318 | Value *Arg0 = II->getArgOperand(0); |
| 2319 | Value *Arg1 = II->getArgOperand(1); |
| 2320 | Value *LHS = Builder->CreateExtractElement(Arg0, (uint64_t)0); |
| 2321 | Value *RHS = Builder->CreateExtractElement(Arg1, (uint64_t)0); |
Craig Topper | 7b788ada | 2016-12-26 06:33:19 +0000 | [diff] [blame] | 2322 | |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2323 | Value *V; |
| 2324 | switch (II->getIntrinsicID()) { |
| 2325 | default: llvm_unreachable("Case stmts out of sync!"); |
| 2326 | case Intrinsic::x86_avx512_mask_add_ss_round: |
| 2327 | case Intrinsic::x86_avx512_mask_add_sd_round: |
| 2328 | V = Builder->CreateFAdd(LHS, RHS); |
| 2329 | break; |
| 2330 | case Intrinsic::x86_avx512_mask_sub_ss_round: |
| 2331 | case Intrinsic::x86_avx512_mask_sub_sd_round: |
| 2332 | V = Builder->CreateFSub(LHS, RHS); |
| 2333 | break; |
| 2334 | case Intrinsic::x86_avx512_mask_mul_ss_round: |
| 2335 | case Intrinsic::x86_avx512_mask_mul_sd_round: |
| 2336 | V = Builder->CreateFMul(LHS, RHS); |
| 2337 | break; |
| 2338 | case Intrinsic::x86_avx512_mask_div_ss_round: |
| 2339 | case Intrinsic::x86_avx512_mask_div_sd_round: |
| 2340 | V = Builder->CreateFDiv(LHS, RHS); |
| 2341 | break; |
Craig Topper | 7b788ada | 2016-12-26 06:33:19 +0000 | [diff] [blame] | 2342 | } |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2343 | |
| 2344 | // Handle the masking aspect of the intrinsic. |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2345 | Value *Mask = II->getArgOperand(3); |
Craig Topper | 9916363 | 2016-12-30 23:06:28 +0000 | [diff] [blame] | 2346 | auto *C = dyn_cast<ConstantInt>(Mask); |
| 2347 | // We don't need a select if we know the mask bit is a 1. |
| 2348 | if (!C || !C->getValue()[0]) { |
| 2349 | // Cast the mask to an i1 vector and then extract the lowest element. |
| 2350 | auto *MaskTy = VectorType::get(Builder->getInt1Ty(), |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2351 | cast<IntegerType>(Mask->getType())->getBitWidth()); |
Craig Topper | 9916363 | 2016-12-30 23:06:28 +0000 | [diff] [blame] | 2352 | Mask = Builder->CreateBitCast(Mask, MaskTy); |
| 2353 | Mask = Builder->CreateExtractElement(Mask, (uint64_t)0); |
| 2354 | // Extract the lowest element from the passthru operand. |
| 2355 | Value *Passthru = Builder->CreateExtractElement(II->getArgOperand(2), |
| 2356 | (uint64_t)0); |
| 2357 | V = Builder->CreateSelect(Mask, V, Passthru); |
| 2358 | } |
Craig Topper | 7f8540b | 2016-12-27 01:56:30 +0000 | [diff] [blame] | 2359 | |
| 2360 | // Insert the result back into the original argument 0. |
| 2361 | V = Builder->CreateInsertElement(Arg0, V, (uint64_t)0); |
| 2362 | |
| 2363 | return replaceInstUsesWith(*II, V); |
Craig Topper | 7b788ada | 2016-12-26 06:33:19 +0000 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | LLVM_FALLTHROUGH; |
| 2367 | |
| 2368 | // X86 scalar intrinsics simplified with SimplifyDemandedVectorElts. |
| 2369 | case Intrinsic::x86_avx512_mask_max_ss_round: |
| 2370 | case Intrinsic::x86_avx512_mask_min_ss_round: |
Craig Topper | 790d0fa | 2016-12-11 07:42:01 +0000 | [diff] [blame] | 2371 | case Intrinsic::x86_avx512_mask_max_sd_round: |
Craig Topper | 268b3ab | 2016-12-14 06:06:58 +0000 | [diff] [blame] | 2372 | case Intrinsic::x86_avx512_mask_min_sd_round: |
Craig Topper | ab5f355 | 2016-12-15 03:49:45 +0000 | [diff] [blame] | 2373 | case Intrinsic::x86_avx512_mask_vfmadd_ss: |
| 2374 | case Intrinsic::x86_avx512_mask_vfmadd_sd: |
| 2375 | case Intrinsic::x86_avx512_maskz_vfmadd_ss: |
| 2376 | case Intrinsic::x86_avx512_maskz_vfmadd_sd: |
| 2377 | case Intrinsic::x86_avx512_mask3_vfmadd_ss: |
| 2378 | case Intrinsic::x86_avx512_mask3_vfmadd_sd: |
| 2379 | case Intrinsic::x86_avx512_mask3_vfmsub_ss: |
| 2380 | case Intrinsic::x86_avx512_mask3_vfmsub_sd: |
| 2381 | case Intrinsic::x86_avx512_mask3_vfnmsub_ss: |
| 2382 | case Intrinsic::x86_avx512_mask3_vfnmsub_sd: |
Craig Topper | dfd268d | 2016-12-14 05:43:05 +0000 | [diff] [blame] | 2383 | case Intrinsic::x86_fma_vfmadd_ss: |
| 2384 | case Intrinsic::x86_fma_vfmsub_ss: |
| 2385 | case Intrinsic::x86_fma_vfnmadd_ss: |
| 2386 | case Intrinsic::x86_fma_vfnmsub_ss: |
| 2387 | case Intrinsic::x86_fma_vfmadd_sd: |
| 2388 | case Intrinsic::x86_fma_vfmsub_sd: |
| 2389 | case Intrinsic::x86_fma_vfnmadd_sd: |
| 2390 | case Intrinsic::x86_fma_vfnmsub_sd: |
Craig Topper | a0372de | 2016-12-14 03:17:27 +0000 | [diff] [blame] | 2391 | case Intrinsic::x86_sse_cmp_ss: |
| 2392 | case Intrinsic::x86_sse_min_ss: |
| 2393 | case Intrinsic::x86_sse_max_ss: |
| 2394 | case Intrinsic::x86_sse2_cmp_sd: |
| 2395 | case Intrinsic::x86_sse2_min_sd: |
| 2396 | case Intrinsic::x86_sse2_max_sd: |
Craig Topper | eb6a20e | 2016-12-14 03:17:30 +0000 | [diff] [blame] | 2397 | case Intrinsic::x86_sse41_round_ss: |
| 2398 | case Intrinsic::x86_sse41_round_sd: |
Craig Topper | ac75bca | 2016-12-13 07:45:45 +0000 | [diff] [blame] | 2399 | case Intrinsic::x86_xop_vfrcz_ss: |
| 2400 | case Intrinsic::x86_xop_vfrcz_sd: { |
| 2401 | unsigned VWidth = II->getType()->getVectorNumElements(); |
| 2402 | APInt UndefElts(VWidth, 0); |
| 2403 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 2404 | if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, UndefElts)) { |
| 2405 | if (V != II) |
| 2406 | return replaceInstUsesWith(*II, V); |
| 2407 | return II; |
| 2408 | } |
| 2409 | break; |
| 2410 | } |
| 2411 | |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 2412 | // Constant fold ashr( <A x Bi>, Ci ). |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2413 | // Constant fold lshr( <A x Bi>, Ci ). |
| 2414 | // Constant fold shl( <A x Bi>, Ci ). |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 2415 | case Intrinsic::x86_sse2_psrai_d: |
| 2416 | case Intrinsic::x86_sse2_psrai_w: |
Simon Pilgrim | a3a72b4 | 2015-08-10 20:21:15 +0000 | [diff] [blame] | 2417 | case Intrinsic::x86_avx2_psrai_d: |
| 2418 | case Intrinsic::x86_avx2_psrai_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2419 | case Intrinsic::x86_avx512_psrai_q_128: |
| 2420 | case Intrinsic::x86_avx512_psrai_q_256: |
| 2421 | case Intrinsic::x86_avx512_psrai_d_512: |
| 2422 | case Intrinsic::x86_avx512_psrai_q_512: |
| 2423 | case Intrinsic::x86_avx512_psrai_w_512: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 2424 | case Intrinsic::x86_sse2_psrli_d: |
| 2425 | case Intrinsic::x86_sse2_psrli_q: |
| 2426 | case Intrinsic::x86_sse2_psrli_w: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 2427 | case Intrinsic::x86_avx2_psrli_d: |
| 2428 | case Intrinsic::x86_avx2_psrli_q: |
| 2429 | case Intrinsic::x86_avx2_psrli_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2430 | case Intrinsic::x86_avx512_psrli_d_512: |
| 2431 | case Intrinsic::x86_avx512_psrli_q_512: |
| 2432 | case Intrinsic::x86_avx512_psrli_w_512: |
Michael J. Spencer | dee4b2c | 2014-04-24 00:58:18 +0000 | [diff] [blame] | 2433 | case Intrinsic::x86_sse2_pslli_d: |
| 2434 | case Intrinsic::x86_sse2_pslli_q: |
| 2435 | case Intrinsic::x86_sse2_pslli_w: |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 2436 | case Intrinsic::x86_avx2_pslli_d: |
| 2437 | case Intrinsic::x86_avx2_pslli_q: |
| 2438 | case Intrinsic::x86_avx2_pslli_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2439 | case Intrinsic::x86_avx512_pslli_d_512: |
| 2440 | case Intrinsic::x86_avx512_pslli_q_512: |
| 2441 | case Intrinsic::x86_avx512_pslli_w_512: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2442 | if (Value *V = simplifyX86immShift(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2443 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 18617d1 | 2015-08-05 08:18:00 +0000 | [diff] [blame] | 2444 | break; |
| 2445 | |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2446 | case Intrinsic::x86_sse2_psra_d: |
| 2447 | case Intrinsic::x86_sse2_psra_w: |
| 2448 | case Intrinsic::x86_avx2_psra_d: |
| 2449 | case Intrinsic::x86_avx2_psra_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2450 | case Intrinsic::x86_avx512_psra_q_128: |
| 2451 | case Intrinsic::x86_avx512_psra_q_256: |
| 2452 | case Intrinsic::x86_avx512_psra_d_512: |
| 2453 | case Intrinsic::x86_avx512_psra_q_512: |
| 2454 | case Intrinsic::x86_avx512_psra_w_512: |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2455 | case Intrinsic::x86_sse2_psrl_d: |
| 2456 | case Intrinsic::x86_sse2_psrl_q: |
| 2457 | case Intrinsic::x86_sse2_psrl_w: |
| 2458 | case Intrinsic::x86_avx2_psrl_d: |
| 2459 | case Intrinsic::x86_avx2_psrl_q: |
| 2460 | case Intrinsic::x86_avx2_psrl_w: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2461 | case Intrinsic::x86_avx512_psrl_d_512: |
| 2462 | case Intrinsic::x86_avx512_psrl_q_512: |
| 2463 | case Intrinsic::x86_avx512_psrl_w_512: |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2464 | case Intrinsic::x86_sse2_psll_d: |
| 2465 | case Intrinsic::x86_sse2_psll_q: |
| 2466 | case Intrinsic::x86_sse2_psll_w: |
| 2467 | case Intrinsic::x86_avx2_psll_d: |
| 2468 | case Intrinsic::x86_avx2_psll_q: |
Craig Topper | 8b831cb | 2016-11-13 01:51:55 +0000 | [diff] [blame] | 2469 | case Intrinsic::x86_avx2_psll_w: |
| 2470 | case Intrinsic::x86_avx512_psll_d_512: |
| 2471 | case Intrinsic::x86_avx512_psll_q_512: |
| 2472 | case Intrinsic::x86_avx512_psll_w_512: { |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2473 | if (Value *V = simplifyX86immShift(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2474 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2475 | |
| 2476 | // SSE2/AVX2 uses only the first 64-bits of the 128-bit vector |
| 2477 | // operand to compute the shift amount. |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 2478 | Value *Arg1 = II->getArgOperand(1); |
| 2479 | assert(Arg1->getType()->getPrimitiveSizeInBits() == 128 && |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2480 | "Unexpected packed shift size"); |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 2481 | unsigned VWidth = Arg1->getType()->getVectorNumElements(); |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2482 | |
Simon Pilgrim | 996725e | 2015-09-19 11:41:53 +0000 | [diff] [blame] | 2483 | if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, VWidth / 2)) { |
Simon Pilgrim | becd5e8 | 2015-08-13 07:39:03 +0000 | [diff] [blame] | 2484 | II->setArgOperand(1, V); |
| 2485 | return II; |
| 2486 | } |
| 2487 | break; |
| 2488 | } |
| 2489 | |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 2490 | case Intrinsic::x86_avx2_psllv_d: |
| 2491 | case Intrinsic::x86_avx2_psllv_d_256: |
| 2492 | case Intrinsic::x86_avx2_psllv_q: |
| 2493 | case Intrinsic::x86_avx2_psllv_q_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 2494 | case Intrinsic::x86_avx512_psllv_d_512: |
| 2495 | case Intrinsic::x86_avx512_psllv_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 2496 | case Intrinsic::x86_avx512_psllv_w_128: |
| 2497 | case Intrinsic::x86_avx512_psllv_w_256: |
| 2498 | case Intrinsic::x86_avx512_psllv_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 2499 | case Intrinsic::x86_avx2_psrav_d: |
| 2500 | case Intrinsic::x86_avx2_psrav_d_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 2501 | case Intrinsic::x86_avx512_psrav_q_128: |
| 2502 | case Intrinsic::x86_avx512_psrav_q_256: |
| 2503 | case Intrinsic::x86_avx512_psrav_d_512: |
| 2504 | case Intrinsic::x86_avx512_psrav_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 2505 | case Intrinsic::x86_avx512_psrav_w_128: |
| 2506 | case Intrinsic::x86_avx512_psrav_w_256: |
| 2507 | case Intrinsic::x86_avx512_psrav_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 2508 | case Intrinsic::x86_avx2_psrlv_d: |
| 2509 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 2510 | case Intrinsic::x86_avx2_psrlv_q: |
| 2511 | case Intrinsic::x86_avx2_psrlv_q_256: |
Craig Topper | b4173a5 | 2016-11-13 07:26:19 +0000 | [diff] [blame] | 2512 | case Intrinsic::x86_avx512_psrlv_d_512: |
| 2513 | case Intrinsic::x86_avx512_psrlv_q_512: |
Craig Topper | 1de753f | 2016-11-18 06:04:33 +0000 | [diff] [blame] | 2514 | case Intrinsic::x86_avx512_psrlv_w_128: |
| 2515 | case Intrinsic::x86_avx512_psrlv_w_256: |
| 2516 | case Intrinsic::x86_avx512_psrlv_w_512: |
Simon Pilgrim | db9893f | 2016-06-07 10:27:15 +0000 | [diff] [blame] | 2517 | if (Value *V = simplifyX86varShift(*II, *Builder)) |
| 2518 | return replaceInstUsesWith(*II, V); |
| 2519 | break; |
| 2520 | |
Simon Pilgrim | c9cf7fc | 2016-12-26 23:28:17 +0000 | [diff] [blame] | 2521 | case Intrinsic::x86_sse2_pmulu_dq: |
| 2522 | case Intrinsic::x86_sse41_pmuldq: |
| 2523 | case Intrinsic::x86_avx2_pmul_dq: |
Craig Topper | 72f2d4e | 2016-12-27 05:30:09 +0000 | [diff] [blame] | 2524 | case Intrinsic::x86_avx2_pmulu_dq: |
| 2525 | case Intrinsic::x86_avx512_pmul_dq_512: |
| 2526 | case Intrinsic::x86_avx512_pmulu_dq_512: { |
Simon Pilgrim | f6f3a361 | 2017-01-23 15:22:59 +0000 | [diff] [blame] | 2527 | if (Value *V = simplifyX86muldq(*II, *Builder)) |
Simon Pilgrim | a50a93f | 2017-01-20 18:20:30 +0000 | [diff] [blame] | 2528 | return replaceInstUsesWith(*II, V); |
| 2529 | |
Simon Pilgrim | c9cf7fc | 2016-12-26 23:28:17 +0000 | [diff] [blame] | 2530 | unsigned VWidth = II->getType()->getVectorNumElements(); |
| 2531 | APInt UndefElts(VWidth, 0); |
| 2532 | APInt DemandedElts = APInt::getAllOnesValue(VWidth); |
| 2533 | if (Value *V = SimplifyDemandedVectorElts(II, DemandedElts, UndefElts)) { |
| 2534 | if (V != II) |
| 2535 | return replaceInstUsesWith(*II, V); |
| 2536 | return II; |
| 2537 | } |
| 2538 | break; |
| 2539 | } |
| 2540 | |
Simon Pilgrim | 6f6b279 | 2017-01-25 14:37:24 +0000 | [diff] [blame] | 2541 | case Intrinsic::x86_sse2_packssdw_128: |
| 2542 | case Intrinsic::x86_sse2_packsswb_128: |
| 2543 | case Intrinsic::x86_avx2_packssdw: |
| 2544 | case Intrinsic::x86_avx2_packsswb: |
| 2545 | // TODO Add support for Intrinsic::x86_avx512_mask_packss* |
| 2546 | if (Value *V = simplifyX86pack(*II, *this, *Builder, true)) |
| 2547 | return replaceInstUsesWith(*II, V); |
| 2548 | break; |
| 2549 | |
| 2550 | case Intrinsic::x86_sse2_packuswb_128: |
| 2551 | case Intrinsic::x86_sse41_packusdw: |
| 2552 | case Intrinsic::x86_avx2_packusdw: |
| 2553 | case Intrinsic::x86_avx2_packuswb: |
| 2554 | // TODO Add support for Intrinsic::x86_avx512_mask_packus* |
| 2555 | if (Value *V = simplifyX86pack(*II, *this, *Builder, false)) |
| 2556 | return replaceInstUsesWith(*II, V); |
| 2557 | break; |
| 2558 | |
Craig Topper | b612212 | 2017-01-26 05:17:13 +0000 | [diff] [blame] | 2559 | case Intrinsic::x86_pclmulqdq: { |
| 2560 | if (auto *C = dyn_cast<ConstantInt>(II->getArgOperand(2))) { |
| 2561 | unsigned Imm = C->getZExtValue(); |
| 2562 | |
| 2563 | bool MadeChange = false; |
| 2564 | Value *Arg0 = II->getArgOperand(0); |
| 2565 | Value *Arg1 = II->getArgOperand(1); |
| 2566 | unsigned VWidth = Arg0->getType()->getVectorNumElements(); |
| 2567 | APInt DemandedElts(VWidth, 0); |
| 2568 | |
| 2569 | APInt UndefElts1(VWidth, 0); |
| 2570 | DemandedElts = (Imm & 0x01) ? 2 : 1; |
| 2571 | if (Value *V = SimplifyDemandedVectorElts(Arg0, DemandedElts, |
| 2572 | UndefElts1)) { |
| 2573 | II->setArgOperand(0, V); |
| 2574 | MadeChange = true; |
| 2575 | } |
| 2576 | |
| 2577 | APInt UndefElts2(VWidth, 0); |
| 2578 | DemandedElts = (Imm & 0x10) ? 2 : 1; |
| 2579 | if (Value *V = SimplifyDemandedVectorElts(Arg1, DemandedElts, |
| 2580 | UndefElts2)) { |
| 2581 | II->setArgOperand(1, V); |
| 2582 | MadeChange = true; |
| 2583 | } |
| 2584 | |
| 2585 | // If both input elements are undef, the result is undef. |
| 2586 | if (UndefElts1[(Imm & 0x01) ? 1 : 0] || |
| 2587 | UndefElts2[(Imm & 0x10) ? 1 : 0]) |
| 2588 | return replaceInstUsesWith(*II, |
| 2589 | ConstantAggregateZero::get(II->getType())); |
| 2590 | |
| 2591 | if (MadeChange) |
| 2592 | return II; |
| 2593 | } |
| 2594 | break; |
| 2595 | } |
| 2596 | |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 2597 | case Intrinsic::x86_sse41_insertps: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2598 | if (Value *V = simplifyX86insertps(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2599 | return replaceInstUsesWith(*II, V); |
Sanjay Patel | c86867c | 2015-04-16 17:52:13 +0000 | [diff] [blame] | 2600 | break; |
Simon Pilgrim | 54fcd62 | 2015-07-25 20:41:00 +0000 | [diff] [blame] | 2601 | |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2602 | case Intrinsic::x86_sse4a_extrq: { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2603 | Value *Op0 = II->getArgOperand(0); |
| 2604 | Value *Op1 = II->getArgOperand(1); |
| 2605 | unsigned VWidth0 = Op0->getType()->getVectorNumElements(); |
| 2606 | unsigned VWidth1 = Op1->getType()->getVectorNumElements(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2607 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 2608 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && |
| 2609 | VWidth1 == 16 && "Unexpected operand sizes"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2610 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2611 | // See if we're dealing with constant values. |
| 2612 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 2613 | ConstantInt *CILength = |
Andrea Di Biagio | 8df5b9c | 2016-09-07 12:03:03 +0000 | [diff] [blame] | 2614 | C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2615 | : nullptr; |
| 2616 | ConstantInt *CIIndex = |
Andrea Di Biagio | 8df5b9c | 2016-09-07 12:03:03 +0000 | [diff] [blame] | 2617 | C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2618 | : nullptr; |
| 2619 | |
| 2620 | // Attempt to simplify to a constant, shuffle vector or EXTRQI call. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2621 | if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2622 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2623 | |
| 2624 | // EXTRQ only uses the lowest 64-bits of the first 128-bit vector |
| 2625 | // operands and the lowest 16-bits of the second. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2626 | bool MadeChange = false; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2627 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { |
| 2628 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2629 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2630 | } |
| 2631 | if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 2)) { |
| 2632 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2633 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2634 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2635 | if (MadeChange) |
| 2636 | return II; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | } |
| 2639 | |
| 2640 | case Intrinsic::x86_sse4a_extrqi: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2641 | // EXTRQI: Extract Length bits starting from Index. Zero pad the remaining |
| 2642 | // bits of the lower 64-bits. The upper 64-bits are undefined. |
| 2643 | Value *Op0 = II->getArgOperand(0); |
| 2644 | unsigned VWidth = Op0->getType()->getVectorNumElements(); |
| 2645 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && |
| 2646 | "Unexpected operand size"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2647 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2648 | // See if we're dealing with constant values. |
| 2649 | ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(1)); |
| 2650 | ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(2)); |
| 2651 | |
| 2652 | // Attempt to simplify to a constant or shuffle vector. |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2653 | if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2654 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2655 | |
| 2656 | // EXTRQI only uses the lowest 64-bits of the first 128-bit vector |
| 2657 | // operand. |
| 2658 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2659 | II->setArgOperand(0, V); |
| 2660 | return II; |
| 2661 | } |
| 2662 | break; |
| 2663 | } |
| 2664 | |
| 2665 | case Intrinsic::x86_sse4a_insertq: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2666 | Value *Op0 = II->getArgOperand(0); |
| 2667 | Value *Op1 = II->getArgOperand(1); |
| 2668 | unsigned VWidth = Op0->getType()->getVectorNumElements(); |
| 2669 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 2670 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && |
| 2671 | Op1->getType()->getVectorNumElements() == 2 && |
| 2672 | "Unexpected operand size"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2673 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2674 | // See if we're dealing with constant values. |
| 2675 | Constant *C1 = dyn_cast<Constant>(Op1); |
| 2676 | ConstantInt *CI11 = |
Andrea Di Biagio | f3fd316 | 2016-09-07 12:47:53 +0000 | [diff] [blame] | 2677 | C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1)) |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2678 | : nullptr; |
| 2679 | |
| 2680 | // Attempt to simplify to a constant, shuffle vector or INSERTQI call. |
| 2681 | if (CI11) { |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 2682 | const APInt &V11 = CI11->getValue(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2683 | APInt Len = V11.zextOrTrunc(6); |
| 2684 | APInt Idx = V11.lshr(8).zextOrTrunc(6); |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2685 | if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2686 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | // INSERTQ only uses the lowest 64-bits of the first 128-bit vector |
| 2690 | // operand. |
| 2691 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2692 | II->setArgOperand(0, V); |
| 2693 | return II; |
| 2694 | } |
| 2695 | break; |
| 2696 | } |
| 2697 | |
Filipe Cabecinhas | 1a80595 | 2014-04-24 00:38:14 +0000 | [diff] [blame] | 2698 | case Intrinsic::x86_sse4a_insertqi: { |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2699 | // INSERTQI: Extract lowest Length bits from lower half of second source and |
| 2700 | // insert over first source starting at Index bit. The upper 64-bits are |
| 2701 | // undefined. |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2702 | Value *Op0 = II->getArgOperand(0); |
| 2703 | Value *Op1 = II->getArgOperand(1); |
| 2704 | unsigned VWidth0 = Op0->getType()->getVectorNumElements(); |
| 2705 | unsigned VWidth1 = Op1->getType()->getVectorNumElements(); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2706 | assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && |
| 2707 | Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && |
| 2708 | VWidth1 == 2 && "Unexpected operand sizes"); |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2709 | |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2710 | // See if we're dealing with constant values. |
| 2711 | ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(2)); |
| 2712 | ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(3)); |
| 2713 | |
| 2714 | // Attempt to simplify to a constant or shuffle vector. |
| 2715 | if (CILength && CIIndex) { |
| 2716 | APInt Len = CILength->getValue().zextOrTrunc(6); |
| 2717 | APInt Idx = CIIndex->getValue().zextOrTrunc(6); |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2718 | if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2719 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 216b1bf | 2015-10-17 11:40:05 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | // INSERTQI only uses the lowest 64-bits of the first two 128-bit vector |
| 2723 | // operands. |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2724 | bool MadeChange = false; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2725 | if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { |
| 2726 | II->setArgOperand(0, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2727 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2728 | } |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2729 | if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 1)) { |
| 2730 | II->setArgOperand(1, V); |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2731 | MadeChange = true; |
Simon Pilgrim | 61116dd | 2015-09-17 20:32:45 +0000 | [diff] [blame] | 2732 | } |
Simon Pilgrim | 1c9a9f2 | 2016-04-24 17:57:27 +0000 | [diff] [blame] | 2733 | if (MadeChange) |
| 2734 | return II; |
Filipe Cabecinhas | 1a80595 | 2014-04-24 00:38:14 +0000 | [diff] [blame] | 2735 | break; |
| 2736 | } |
| 2737 | |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 2738 | case Intrinsic::x86_sse41_pblendvb: |
| 2739 | case Intrinsic::x86_sse41_blendvps: |
| 2740 | case Intrinsic::x86_sse41_blendvpd: |
| 2741 | case Intrinsic::x86_avx_blendv_ps_256: |
| 2742 | case Intrinsic::x86_avx_blendv_pd_256: |
| 2743 | case Intrinsic::x86_avx2_pblendvb: { |
| 2744 | // Convert blendv* to vector selects if the mask is constant. |
| 2745 | // This optimization is convoluted because the intrinsic is defined as |
| 2746 | // getting a vector of floats or doubles for the ps and pd versions. |
| 2747 | // FIXME: That should be changed. |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2748 | |
| 2749 | Value *Op0 = II->getArgOperand(0); |
| 2750 | Value *Op1 = II->getArgOperand(1); |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 2751 | Value *Mask = II->getArgOperand(2); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2752 | |
| 2753 | // fold (blend A, A, Mask) -> A |
| 2754 | if (Op0 == Op1) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2755 | return replaceInstUsesWith(CI, Op0); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2756 | |
| 2757 | // Zero Mask - select 1st argument. |
Simon Pilgrim | 93f59f5 | 2015-08-12 08:23:36 +0000 | [diff] [blame] | 2758 | if (isa<ConstantAggregateZero>(Mask)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2759 | return replaceInstUsesWith(CI, Op0); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2760 | |
| 2761 | // 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] | 2762 | if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) { |
| 2763 | Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask); |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2764 | return SelectInst::Create(NewSelector, Op1, Op0, "blendv"); |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 2765 | } |
Simon Pilgrim | 8c049d5 | 2015-08-12 08:08:56 +0000 | [diff] [blame] | 2766 | break; |
Filipe Cabecinhas | 82ac07c | 2014-05-27 03:42:20 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Andrea Di Biagio | 0594e2a | 2015-09-30 16:44:39 +0000 | [diff] [blame] | 2769 | case Intrinsic::x86_ssse3_pshuf_b_128: |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 2770 | case Intrinsic::x86_avx2_pshuf_b: |
Simon Pilgrim | a22c3a1 | 2017-01-18 13:44:04 +0000 | [diff] [blame] | 2771 | case Intrinsic::x86_avx512_pshuf_b_512: |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 2772 | if (Value *V = simplifyX86pshufb(*II, *Builder)) |
| 2773 | return replaceInstUsesWith(*II, V); |
| 2774 | break; |
Andrea Di Biagio | 0594e2a | 2015-09-30 16:44:39 +0000 | [diff] [blame] | 2775 | |
Rafael Espindola | bad3f77 | 2014-04-21 22:06:04 +0000 | [diff] [blame] | 2776 | case Intrinsic::x86_avx_vpermilvar_ps: |
| 2777 | case Intrinsic::x86_avx_vpermilvar_ps_256: |
Craig Topper | 58917f3 | 2016-12-11 01:59:36 +0000 | [diff] [blame] | 2778 | case Intrinsic::x86_avx512_vpermilvar_ps_512: |
Rafael Espindola | bad3f77 | 2014-04-21 22:06:04 +0000 | [diff] [blame] | 2779 | case Intrinsic::x86_avx_vpermilvar_pd: |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 2780 | case Intrinsic::x86_avx_vpermilvar_pd_256: |
Simon Pilgrim | a22c3a1 | 2017-01-18 13:44:04 +0000 | [diff] [blame] | 2781 | case Intrinsic::x86_avx512_vpermilvar_pd_512: |
Simon Pilgrim | 2f6097d | 2016-04-24 17:23:46 +0000 | [diff] [blame] | 2782 | if (Value *V = simplifyX86vpermilvar(*II, *Builder)) |
| 2783 | return replaceInstUsesWith(*II, V); |
| 2784 | break; |
Rafael Espindola | bad3f77 | 2014-04-21 22:06:04 +0000 | [diff] [blame] | 2785 | |
Simon Pilgrim | 8cddf8b | 2016-05-01 16:41:22 +0000 | [diff] [blame] | 2786 | case Intrinsic::x86_avx2_permd: |
| 2787 | case Intrinsic::x86_avx2_permps: |
| 2788 | if (Value *V = simplifyX86vpermv(*II, *Builder)) |
| 2789 | return replaceInstUsesWith(*II, V); |
| 2790 | break; |
| 2791 | |
Craig Topper | e328045 | 2016-12-25 23:58:57 +0000 | [diff] [blame] | 2792 | case Intrinsic::x86_avx512_mask_permvar_df_256: |
| 2793 | case Intrinsic::x86_avx512_mask_permvar_df_512: |
| 2794 | case Intrinsic::x86_avx512_mask_permvar_di_256: |
| 2795 | case Intrinsic::x86_avx512_mask_permvar_di_512: |
| 2796 | case Intrinsic::x86_avx512_mask_permvar_hi_128: |
| 2797 | case Intrinsic::x86_avx512_mask_permvar_hi_256: |
| 2798 | case Intrinsic::x86_avx512_mask_permvar_hi_512: |
| 2799 | case Intrinsic::x86_avx512_mask_permvar_qi_128: |
| 2800 | case Intrinsic::x86_avx512_mask_permvar_qi_256: |
| 2801 | case Intrinsic::x86_avx512_mask_permvar_qi_512: |
| 2802 | case Intrinsic::x86_avx512_mask_permvar_sf_256: |
| 2803 | case Intrinsic::x86_avx512_mask_permvar_sf_512: |
| 2804 | case Intrinsic::x86_avx512_mask_permvar_si_256: |
| 2805 | case Intrinsic::x86_avx512_mask_permvar_si_512: |
| 2806 | if (Value *V = simplifyX86vpermv(*II, *Builder)) { |
| 2807 | // We simplified the permuting, now create a select for the masking. |
| 2808 | V = emitX86MaskSelect(II->getArgOperand(3), V, II->getArgOperand(2), |
| 2809 | *Builder); |
| 2810 | return replaceInstUsesWith(*II, V); |
| 2811 | } |
| 2812 | break; |
| 2813 | |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 2814 | case Intrinsic::x86_avx_vperm2f128_pd_256: |
| 2815 | case Intrinsic::x86_avx_vperm2f128_ps_256: |
| 2816 | case Intrinsic::x86_avx_vperm2f128_si_256: |
Sanjay Patel | e304bea | 2015-03-24 22:39:29 +0000 | [diff] [blame] | 2817 | case Intrinsic::x86_avx2_vperm2i128: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2818 | if (Value *V = simplifyX86vperm2(*II, *Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2819 | return replaceInstUsesWith(*II, V); |
Sanjay Patel | ccf5f24 | 2015-03-20 21:47:56 +0000 | [diff] [blame] | 2820 | break; |
| 2821 | |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 2822 | case Intrinsic::x86_avx_maskload_ps: |
Sanjay Patel | 6f2c01f | 2016-02-29 23:59:00 +0000 | [diff] [blame] | 2823 | case Intrinsic::x86_avx_maskload_pd: |
| 2824 | case Intrinsic::x86_avx_maskload_ps_256: |
| 2825 | case Intrinsic::x86_avx_maskload_pd_256: |
| 2826 | case Intrinsic::x86_avx2_maskload_d: |
| 2827 | case Intrinsic::x86_avx2_maskload_q: |
| 2828 | case Intrinsic::x86_avx2_maskload_d_256: |
| 2829 | case Intrinsic::x86_avx2_maskload_q_256: |
Sanjay Patel | 98a7150 | 2016-02-29 23:16:48 +0000 | [diff] [blame] | 2830 | if (Instruction *I = simplifyX86MaskedLoad(*II, *this)) |
| 2831 | return I; |
| 2832 | break; |
| 2833 | |
Sanjay Patel | c4acbae | 2016-03-12 15:16:59 +0000 | [diff] [blame] | 2834 | case Intrinsic::x86_sse2_maskmov_dqu: |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 2835 | case Intrinsic::x86_avx_maskstore_ps: |
| 2836 | case Intrinsic::x86_avx_maskstore_pd: |
| 2837 | case Intrinsic::x86_avx_maskstore_ps_256: |
| 2838 | case Intrinsic::x86_avx_maskstore_pd_256: |
Sanjay Patel | fc7e7eb | 2016-02-26 21:51:44 +0000 | [diff] [blame] | 2839 | case Intrinsic::x86_avx2_maskstore_d: |
| 2840 | case Intrinsic::x86_avx2_maskstore_q: |
| 2841 | case Intrinsic::x86_avx2_maskstore_d_256: |
| 2842 | case Intrinsic::x86_avx2_maskstore_q_256: |
Sanjay Patel | 1ace993 | 2016-02-26 21:04:14 +0000 | [diff] [blame] | 2843 | if (simplifyX86MaskedStore(*II, *this)) |
| 2844 | return nullptr; |
| 2845 | break; |
| 2846 | |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2847 | case Intrinsic::x86_xop_vpcomb: |
| 2848 | case Intrinsic::x86_xop_vpcomd: |
| 2849 | case Intrinsic::x86_xop_vpcomq: |
| 2850 | case Intrinsic::x86_xop_vpcomw: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2851 | if (Value *V = simplifyX86vpcom(*II, *Builder, true)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2852 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | |
| 2855 | case Intrinsic::x86_xop_vpcomub: |
| 2856 | case Intrinsic::x86_xop_vpcomud: |
| 2857 | case Intrinsic::x86_xop_vpcomuq: |
| 2858 | case Intrinsic::x86_xop_vpcomuw: |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 2859 | if (Value *V = simplifyX86vpcom(*II, *Builder, false)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2860 | return replaceInstUsesWith(*II, V); |
Simon Pilgrim | 1d1c56e2 | 2015-10-11 14:38:34 +0000 | [diff] [blame] | 2861 | break; |
| 2862 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2863 | case Intrinsic::ppc_altivec_vperm: |
| 2864 | // 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] | 2865 | // Note that ppc_altivec_vperm has a big-endian bias, so when creating |
| 2866 | // a vectorshuffle for little endian, we must undo the transformation |
| 2867 | // performed on vec_perm in altivec.h. That is, we must complement |
| 2868 | // the permutation mask with respect to 31 and reverse the order of |
| 2869 | // V1 and V2. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2870 | if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) { |
| 2871 | assert(Mask->getType()->getVectorNumElements() == 16 && |
| 2872 | "Bad type for intrinsic!"); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2874 | // Check that all of the elements are integer constants or undefs. |
| 2875 | bool AllEltsOk = true; |
| 2876 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2877 | Constant *Elt = Mask->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2878 | if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2879 | AllEltsOk = false; |
| 2880 | break; |
| 2881 | } |
| 2882 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2883 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2884 | if (AllEltsOk) { |
| 2885 | // Cast the input vectors to byte vectors. |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 2886 | Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0), |
| 2887 | Mask->getType()); |
| 2888 | Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1), |
| 2889 | Mask->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2890 | Value *Result = UndefValue::get(Op0->getType()); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2891 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2892 | // Only extract each element once. |
| 2893 | Value *ExtractedElts[32]; |
| 2894 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2896 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2897 | if (isa<UndefValue>(Mask->getAggregateElement(i))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2898 | continue; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2899 | unsigned Idx = |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2900 | cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2901 | Idx &= 31; // Match the hardware behavior. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2902 | if (DL.isLittleEndian()) |
Bill Schmidt | a118463 | 2014-06-05 19:46:04 +0000 | [diff] [blame] | 2903 | Idx = 31 - Idx; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2904 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2905 | if (!ExtractedElts[Idx]) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2906 | Value *Op0ToUse = (DL.isLittleEndian()) ? Op1 : Op0; |
| 2907 | Value *Op1ToUse = (DL.isLittleEndian()) ? Op0 : Op1; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2908 | ExtractedElts[Idx] = |
Bill Schmidt | a118463 | 2014-06-05 19:46:04 +0000 | [diff] [blame] | 2909 | Builder->CreateExtractElement(Idx < 16 ? Op0ToUse : Op1ToUse, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2910 | Builder->getInt32(Idx&15)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2911 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2913 | // Insert this value into the result vector. |
| 2914 | Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2915 | Builder->getInt32(i)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 2916 | } |
| 2917 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
| 2918 | } |
| 2919 | } |
| 2920 | break; |
| 2921 | |
Bob Wilson | a4e231c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 2922 | case Intrinsic::arm_neon_vld1: |
| 2923 | case Intrinsic::arm_neon_vld2: |
| 2924 | case Intrinsic::arm_neon_vld3: |
| 2925 | case Intrinsic::arm_neon_vld4: |
| 2926 | case Intrinsic::arm_neon_vld2lane: |
| 2927 | case Intrinsic::arm_neon_vld3lane: |
| 2928 | case Intrinsic::arm_neon_vld4lane: |
| 2929 | case Intrinsic::arm_neon_vst1: |
| 2930 | case Intrinsic::arm_neon_vst2: |
| 2931 | case Intrinsic::arm_neon_vst3: |
| 2932 | case Intrinsic::arm_neon_vst4: |
| 2933 | case Intrinsic::arm_neon_vst2lane: |
| 2934 | case Intrinsic::arm_neon_vst3lane: |
| 2935 | case Intrinsic::arm_neon_vst4lane: { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 2936 | unsigned MemAlign = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2937 | getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT); |
Bob Wilson | a4e231c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 2938 | unsigned AlignArg = II->getNumArgOperands() - 1; |
| 2939 | ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg)); |
| 2940 | if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) { |
| 2941 | II->setArgOperand(AlignArg, |
| 2942 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 2943 | MemAlign, false)); |
| 2944 | return II; |
| 2945 | } |
| 2946 | break; |
| 2947 | } |
| 2948 | |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2949 | case Intrinsic::arm_neon_vmulls: |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2950 | case Intrinsic::arm_neon_vmullu: |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 2951 | case Intrinsic::aarch64_neon_smull: |
| 2952 | case Intrinsic::aarch64_neon_umull: { |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2953 | Value *Arg0 = II->getArgOperand(0); |
| 2954 | Value *Arg1 = II->getArgOperand(1); |
| 2955 | |
| 2956 | // Handle mul by zero first: |
| 2957 | if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2958 | return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType())); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | // Check for constant LHS & RHS - in this case we just simplify. |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2962 | bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu || |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 2963 | II->getIntrinsicID() == Intrinsic::aarch64_neon_umull); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2964 | VectorType *NewVT = cast<VectorType>(II->getType()); |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2965 | if (Constant *CV0 = dyn_cast<Constant>(Arg0)) { |
| 2966 | if (Constant *CV1 = dyn_cast<Constant>(Arg1)) { |
| 2967 | CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext); |
| 2968 | CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext); |
| 2969 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2970 | return replaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1)); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2973 | // Couldn't simplify - canonicalize constant to the RHS. |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2974 | std::swap(Arg0, Arg1); |
| 2975 | } |
| 2976 | |
| 2977 | // Handle mul by one: |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2978 | if (Constant *CV1 = dyn_cast<Constant>(Arg1)) |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2979 | if (ConstantInt *Splat = |
Benjamin Kramer | 9204095 | 2014-02-13 18:23:24 +0000 | [diff] [blame] | 2980 | dyn_cast_or_null<ConstantInt>(CV1->getSplatValue())) |
| 2981 | if (Splat->isOne()) |
| 2982 | return CastInst::CreateIntegerCast(Arg0, II->getType(), |
| 2983 | /*isSigned=*/!Zext); |
Lang Hames | 3a90fab | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 2984 | |
| 2985 | break; |
| 2986 | } |
| 2987 | |
Matt Arsenault | bef34e2 | 2016-01-22 21:30:34 +0000 | [diff] [blame] | 2988 | case Intrinsic::amdgcn_rcp: { |
Matt Arsenault | a0050b0 | 2014-06-19 01:19:19 +0000 | [diff] [blame] | 2989 | if (const ConstantFP *C = dyn_cast<ConstantFP>(II->getArgOperand(0))) { |
| 2990 | const APFloat &ArgVal = C->getValueAPF(); |
| 2991 | APFloat Val(ArgVal.getSemantics(), 1.0); |
| 2992 | APFloat::opStatus Status = Val.divide(ArgVal, |
| 2993 | APFloat::rmNearestTiesToEven); |
| 2994 | // Only do this if it was exact and therefore not dependent on the |
| 2995 | // rounding mode. |
| 2996 | if (Status == APFloat::opOK) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2997 | return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), Val)); |
Matt Arsenault | a0050b0 | 2014-06-19 01:19:19 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | break; |
| 3001 | } |
Matt Arsenault | 2fe4fbc | 2016-03-30 22:28:52 +0000 | [diff] [blame] | 3002 | case Intrinsic::amdgcn_frexp_mant: |
| 3003 | case Intrinsic::amdgcn_frexp_exp: { |
Matt Arsenault | 5cd4f8f | 2016-03-30 22:28:26 +0000 | [diff] [blame] | 3004 | Value *Src = II->getArgOperand(0); |
| 3005 | if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) { |
| 3006 | int Exp; |
| 3007 | APFloat Significand = frexp(C->getValueAPF(), Exp, |
| 3008 | APFloat::rmNearestTiesToEven); |
| 3009 | |
Matt Arsenault | 2fe4fbc | 2016-03-30 22:28:52 +0000 | [diff] [blame] | 3010 | if (II->getIntrinsicID() == Intrinsic::amdgcn_frexp_mant) { |
| 3011 | return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), |
| 3012 | Significand)); |
| 3013 | } |
| 3014 | |
| 3015 | // Match instruction special case behavior. |
| 3016 | if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf) |
| 3017 | Exp = 0; |
| 3018 | |
| 3019 | return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Exp)); |
| 3020 | } |
| 3021 | |
| 3022 | if (isa<UndefValue>(Src)) |
| 3023 | return replaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Matt Arsenault | 5cd4f8f | 2016-03-30 22:28:26 +0000 | [diff] [blame] | 3024 | |
| 3025 | break; |
| 3026 | } |
Matt Arsenault | 46a0382 | 2016-09-03 07:06:58 +0000 | [diff] [blame] | 3027 | case Intrinsic::amdgcn_class: { |
| 3028 | enum { |
| 3029 | S_NAN = 1 << 0, // Signaling NaN |
| 3030 | Q_NAN = 1 << 1, // Quiet NaN |
| 3031 | N_INFINITY = 1 << 2, // Negative infinity |
| 3032 | N_NORMAL = 1 << 3, // Negative normal |
| 3033 | N_SUBNORMAL = 1 << 4, // Negative subnormal |
| 3034 | N_ZERO = 1 << 5, // Negative zero |
| 3035 | P_ZERO = 1 << 6, // Positive zero |
| 3036 | P_SUBNORMAL = 1 << 7, // Positive subnormal |
| 3037 | P_NORMAL = 1 << 8, // Positive normal |
| 3038 | P_INFINITY = 1 << 9 // Positive infinity |
| 3039 | }; |
| 3040 | |
| 3041 | const uint32_t FullMask = S_NAN | Q_NAN | N_INFINITY | N_NORMAL | |
| 3042 | N_SUBNORMAL | N_ZERO | P_ZERO | P_SUBNORMAL | P_NORMAL | P_INFINITY; |
| 3043 | |
| 3044 | Value *Src0 = II->getArgOperand(0); |
| 3045 | Value *Src1 = II->getArgOperand(1); |
| 3046 | const ConstantInt *CMask = dyn_cast<ConstantInt>(Src1); |
| 3047 | if (!CMask) { |
| 3048 | if (isa<UndefValue>(Src0)) |
| 3049 | return replaceInstUsesWith(*II, UndefValue::get(II->getType())); |
| 3050 | |
| 3051 | if (isa<UndefValue>(Src1)) |
| 3052 | return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false)); |
| 3053 | break; |
| 3054 | } |
| 3055 | |
| 3056 | uint32_t Mask = CMask->getZExtValue(); |
| 3057 | |
| 3058 | // If all tests are made, it doesn't matter what the value is. |
| 3059 | if ((Mask & FullMask) == FullMask) |
| 3060 | return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), true)); |
| 3061 | |
| 3062 | if ((Mask & FullMask) == 0) |
| 3063 | return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false)); |
| 3064 | |
| 3065 | if (Mask == (S_NAN | Q_NAN)) { |
| 3066 | // Equivalent of isnan. Replace with standard fcmp. |
| 3067 | Value *FCmp = Builder->CreateFCmpUNO(Src0, Src0); |
| 3068 | FCmp->takeName(II); |
| 3069 | return replaceInstUsesWith(*II, FCmp); |
| 3070 | } |
| 3071 | |
| 3072 | const ConstantFP *CVal = dyn_cast<ConstantFP>(Src0); |
| 3073 | if (!CVal) { |
| 3074 | if (isa<UndefValue>(Src0)) |
| 3075 | return replaceInstUsesWith(*II, UndefValue::get(II->getType())); |
| 3076 | |
| 3077 | // Clamp mask to used bits |
| 3078 | if ((Mask & FullMask) != Mask) { |
| 3079 | CallInst *NewCall = Builder->CreateCall(II->getCalledFunction(), |
| 3080 | { Src0, ConstantInt::get(Src1->getType(), Mask & FullMask) } |
| 3081 | ); |
| 3082 | |
| 3083 | NewCall->takeName(II); |
| 3084 | return replaceInstUsesWith(*II, NewCall); |
| 3085 | } |
| 3086 | |
| 3087 | break; |
| 3088 | } |
| 3089 | |
| 3090 | const APFloat &Val = CVal->getValueAPF(); |
| 3091 | |
| 3092 | bool Result = |
| 3093 | ((Mask & S_NAN) && Val.isNaN() && Val.isSignaling()) || |
| 3094 | ((Mask & Q_NAN) && Val.isNaN() && !Val.isSignaling()) || |
| 3095 | ((Mask & N_INFINITY) && Val.isInfinity() && Val.isNegative()) || |
| 3096 | ((Mask & N_NORMAL) && Val.isNormal() && Val.isNegative()) || |
| 3097 | ((Mask & N_SUBNORMAL) && Val.isDenormal() && Val.isNegative()) || |
| 3098 | ((Mask & N_ZERO) && Val.isZero() && Val.isNegative()) || |
| 3099 | ((Mask & P_ZERO) && Val.isZero() && !Val.isNegative()) || |
| 3100 | ((Mask & P_SUBNORMAL) && Val.isDenormal() && !Val.isNegative()) || |
| 3101 | ((Mask & P_NORMAL) && Val.isNormal() && !Val.isNegative()) || |
| 3102 | ((Mask & P_INFINITY) && Val.isInfinity() && !Val.isNegative()); |
| 3103 | |
| 3104 | return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), Result)); |
| 3105 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3106 | case Intrinsic::stackrestore: { |
| 3107 | // If the save is right next to the restore, remove the restore. This can |
| 3108 | // happen when variable allocas are DCE'd. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 3109 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3110 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 3111 | if (&*++SS->getIterator() == II) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3112 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3113 | } |
| 3114 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3116 | // Scan down this block to see if there is another stack restore in the |
| 3117 | // same block without an intervening call/alloca. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 3118 | BasicBlock::iterator BI(II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3119 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 3120 | bool CannotRemove = false; |
| 3121 | for (++BI; &*BI != TI; ++BI) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 3122 | if (isa<AllocaInst>(BI)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3123 | CannotRemove = true; |
| 3124 | break; |
| 3125 | } |
| 3126 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 3127 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 3128 | // If there is a stackrestore below this one, remove this one. |
| 3129 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3130 | return eraseInstFromFunction(CI); |
Reid Kleckner | 892ae2e | 2016-02-27 00:53:54 +0000 | [diff] [blame] | 3131 | |
| 3132 | // Bail if we cross over an intrinsic with side effects, such as |
| 3133 | // llvm.stacksave, llvm.read_register, or llvm.setjmp. |
| 3134 | if (II->mayHaveSideEffects()) { |
| 3135 | CannotRemove = true; |
| 3136 | break; |
| 3137 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3138 | } else { |
| 3139 | // If we found a non-intrinsic call, we can't remove the stack |
| 3140 | // restore. |
| 3141 | CannotRemove = true; |
| 3142 | break; |
| 3143 | } |
| 3144 | } |
| 3145 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3146 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3147 | // If the stack restore is in a return, resume, or unwind block and if there |
| 3148 | // are no allocas or calls between the restore and the return, nuke the |
| 3149 | // restore. |
Bill Wendling | d5d95b0 | 2012-02-06 21:16:41 +0000 | [diff] [blame] | 3150 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3151 | return eraseInstFromFunction(CI); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3152 | break; |
| 3153 | } |
Vitaly Buka | f0500b6 | 2016-07-28 22:50:48 +0000 | [diff] [blame] | 3154 | case Intrinsic::lifetime_start: |
Vitaly Buka | 0ab23cf | 2016-07-28 22:59:03 +0000 | [diff] [blame] | 3155 | // Asan needs to poison memory to detect invalid access which is possible |
| 3156 | // even for empty lifetime range. |
| 3157 | if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress)) |
| 3158 | break; |
| 3159 | |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 3160 | if (removeTriviallyEmptyRange(*II, Intrinsic::lifetime_start, |
| 3161 | Intrinsic::lifetime_end, *this)) |
| 3162 | return nullptr; |
Arnaud A. de Grandmaison | 849f3bf | 2015-10-01 14:54:31 +0000 | [diff] [blame] | 3163 | break; |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3164 | case Intrinsic::assume: { |
David Majnemer | fcc5811 | 2016-04-08 16:37:12 +0000 | [diff] [blame] | 3165 | Value *IIOperand = II->getArgOperand(0); |
| 3166 | // Remove an assume if it is immediately followed by an identical assume. |
| 3167 | if (match(II->getNextNode(), |
| 3168 | m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand)))) |
| 3169 | return eraseInstFromFunction(CI); |
| 3170 | |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3171 | // Canonicalize assume(a && b) -> assume(a); assume(b); |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 3172 | // Note: New assumption intrinsics created here are registered by |
| 3173 | // the InstCombineIRInserter object. |
David Majnemer | fcc5811 | 2016-04-08 16:37:12 +0000 | [diff] [blame] | 3174 | Value *AssumeIntrinsic = II->getCalledValue(), *A, *B; |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3175 | if (match(IIOperand, m_And(m_Value(A), m_Value(B)))) { |
| 3176 | Builder->CreateCall(AssumeIntrinsic, A, II->getName()); |
| 3177 | Builder->CreateCall(AssumeIntrinsic, B, II->getName()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3178 | return eraseInstFromFunction(*II); |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3179 | } |
| 3180 | // assume(!(a || b)) -> assume(!a); assume(!b); |
| 3181 | 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] | 3182 | Builder->CreateCall(AssumeIntrinsic, Builder->CreateNot(A), |
| 3183 | II->getName()); |
| 3184 | Builder->CreateCall(AssumeIntrinsic, Builder->CreateNot(B), |
| 3185 | II->getName()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3186 | return eraseInstFromFunction(*II); |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3187 | } |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 3188 | |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 3189 | // assume( (load addr) != null ) -> add 'nonnull' metadata to load |
| 3190 | // (if assume is valid at the load) |
Sanjay Patel | f0d1e773 | 2017-01-03 22:25:31 +0000 | [diff] [blame] | 3191 | CmpInst::Predicate Pred; |
| 3192 | Instruction *LHS; |
| 3193 | if (match(IIOperand, m_ICmp(Pred, m_Instruction(LHS), m_Zero())) && |
| 3194 | Pred == ICmpInst::ICMP_NE && LHS->getOpcode() == Instruction::Load && |
| 3195 | LHS->getType()->isPointerTy() && |
| 3196 | isValidAssumeForContext(II, LHS, &DT)) { |
| 3197 | MDNode *MD = MDNode::get(II->getContext(), None); |
| 3198 | LHS->setMetadata(LLVMContext::MD_nonnull, MD); |
| 3199 | return eraseInstFromFunction(*II); |
| 3200 | |
Chandler Carruth | 2496910 | 2015-02-10 08:07:32 +0000 | [diff] [blame] | 3201 | // TODO: apply nonnull return attributes to calls and invokes |
Philip Reames | 66c6de6 | 2014-11-11 23:33:19 +0000 | [diff] [blame] | 3202 | // TODO: apply range metadata for range check patterns? |
| 3203 | } |
Sanjay Patel | f0d1e773 | 2017-01-03 22:25:31 +0000 | [diff] [blame] | 3204 | |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 3205 | // If there is a dominating assume with the same condition as this one, |
| 3206 | // then this one is redundant, and should be removed. |
Hal Finkel | 4564688 | 2014-10-05 00:53:02 +0000 | [diff] [blame] | 3207 | APInt KnownZero(1, 0), KnownOne(1, 0); |
| 3208 | computeKnownBits(IIOperand, KnownZero, KnownOne, 0, II); |
| 3209 | if (KnownOne.isAllOnesValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3210 | return eraseInstFromFunction(*II); |
Hal Finkel | 04a1561 | 2014-10-04 21:27:06 +0000 | [diff] [blame] | 3211 | |
Hal Finkel | 8a9a783 | 2017-01-11 13:24:24 +0000 | [diff] [blame] | 3212 | // Update the cache of affected values for this assumption (we might be |
| 3213 | // here because we just simplified the condition). |
| 3214 | AC.updateAffectedValues(II); |
Hal Finkel | f5867a7 | 2014-07-25 21:45:17 +0000 | [diff] [blame] | 3215 | break; |
| 3216 | } |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3217 | case Intrinsic::experimental_gc_relocate: { |
| 3218 | // Translate facts known about a pointer before relocating into |
| 3219 | // facts about the relocate value, while being careful to |
| 3220 | // preserve relocation semantics. |
Manuel Jacob | 83eefa6 | 2016-01-05 04:03:00 +0000 | [diff] [blame] | 3221 | Value *DerivedPtr = cast<GCRelocateInst>(II)->getDerivedPtr(); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3222 | |
| 3223 | // Remove the relocation if unused, note that this check is required |
| 3224 | // to prevent the cases below from looping forever. |
| 3225 | if (II->use_empty()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3226 | return eraseInstFromFunction(*II); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3227 | |
| 3228 | // Undef is undef, even after relocation. |
| 3229 | // TODO: provide a hook for this in GCStrategy. This is clearly legal for |
| 3230 | // most practical collectors, but there was discussion in the review thread |
| 3231 | // about whether it was legal for all possible collectors. |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 3232 | if (isa<UndefValue>(DerivedPtr)) |
| 3233 | // Use undef of gc_relocate's type to replace it. |
| 3234 | return replaceInstUsesWith(*II, UndefValue::get(II->getType())); |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3235 | |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 3236 | if (auto *PT = dyn_cast<PointerType>(II->getType())) { |
| 3237 | // The relocation of null will be null for most any collector. |
| 3238 | // TODO: provide a hook for this in GCStrategy. There might be some |
| 3239 | // weird collector this property does not hold for. |
| 3240 | if (isa<ConstantPointerNull>(DerivedPtr)) |
| 3241 | // Use null-pointer of gc_relocate's type to replace it. |
| 3242 | return replaceInstUsesWith(*II, ConstantPointerNull::get(PT)); |
Simon Pilgrim | c0c56e7 | 2016-04-24 17:00:34 +0000 | [diff] [blame] | 3243 | |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 3244 | // isKnownNonNull -> nonnull attribute |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 3245 | if (isKnownNonNullAt(DerivedPtr, II, &DT)) |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 3246 | II->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 3247 | } |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3248 | |
| 3249 | // TODO: bitcast(relocate(p)) -> relocate(bitcast(p)) |
| 3250 | // Canonicalize on the type from the uses to the defs |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 3251 | |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3252 | // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...) |
Philip Reames | ea4d8e8 | 2016-02-09 21:09:22 +0000 | [diff] [blame] | 3253 | break; |
Philip Reames | 9db26ff | 2014-12-29 23:27:30 +0000 | [diff] [blame] | 3254 | } |
Artur Pilipenko | e812ca0 | 2017-01-25 14:12:12 +0000 | [diff] [blame] | 3255 | |
| 3256 | case Intrinsic::experimental_guard: { |
| 3257 | Value *IIOperand = II->getArgOperand(0); |
| 3258 | |
| 3259 | // Remove a guard if it is immediately followed by an identical guard. |
| 3260 | if (match(II->getNextNode(), |
| 3261 | m_Intrinsic<Intrinsic::experimental_guard>(m_Specific(IIOperand)))) |
| 3262 | return eraseInstFromFunction(*II); |
| 3263 | break; |
| 3264 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | return visitCallSite(II); |
| 3268 | } |
| 3269 | |
| 3270 | // InvokeInst simplification |
| 3271 | // |
| 3272 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
| 3273 | return visitCallSite(&II); |
| 3274 | } |
| 3275 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 3276 | /// If this cast does not affect the value passed through the varargs area, we |
| 3277 | /// can eliminate the use of the cast. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3278 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3279 | const DataLayout &DL, |
| 3280 | const CastInst *const CI, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3281 | const int ix) { |
| 3282 | if (!CI->isLosslessCast()) |
| 3283 | return false; |
| 3284 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 3285 | // If this is a GC intrinsic, avoid munging types. We need types for |
| 3286 | // statepoint reconstruction in SelectionDAG. |
| 3287 | // TODO: This is probably something which should be expanded to all |
| 3288 | // intrinsics since the entire point of intrinsics is that |
| 3289 | // they are understandable by the optimizer. |
| 3290 | if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS)) |
| 3291 | return false; |
| 3292 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 3293 | // 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] | 3294 | // can't change to a type with a different size. If the size were |
| 3295 | // passed explicitly we could avoid this check. |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 3296 | if (!CS.isByValOrInAllocaArgument(ix)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3297 | return true; |
| 3298 | |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3299 | Type* SrcTy = |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3300 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3301 | Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3302 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 3303 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3304 | if (DL.getTypeAllocSize(SrcTy) != DL.getTypeAllocSize(DstTy)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3305 | return false; |
| 3306 | return true; |
| 3307 | } |
| 3308 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3309 | Instruction *InstCombiner::tryOptimizeCall(CallInst *CI) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3310 | if (!CI->getCalledFunction()) return nullptr; |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 3311 | |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 3312 | auto InstCombineRAUW = [this](Instruction *From, Value *With) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3313 | replaceInstUsesWith(*From, With); |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 3314 | }; |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 3315 | LibCallSimplifier Simplifier(DL, &TLI, InstCombineRAUW); |
Chandler Carruth | ba4c517 | 2015-01-21 11:23:40 +0000 | [diff] [blame] | 3316 | if (Value *With = Simplifier.optimizeCall(CI)) { |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 3317 | ++NumSimplified; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3318 | return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With); |
Meador Inge | e3f2b26 | 2012-11-30 04:05:06 +0000 | [diff] [blame] | 3319 | } |
Meador Inge | df796f8 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 3320 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3321 | return nullptr; |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3324 | static IntrinsicInst *findInitTrampolineFromAlloca(Value *TrampMem) { |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3325 | // Strip off at most one level of pointer casts, looking for an alloca. This |
| 3326 | // is good enough in practice and simpler than handling any number of casts. |
| 3327 | Value *Underlying = TrampMem->stripPointerCasts(); |
| 3328 | if (Underlying != TrampMem && |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3329 | (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3330 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3331 | if (!isa<AllocaInst>(Underlying)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3332 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3333 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3334 | IntrinsicInst *InitTrampoline = nullptr; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3335 | for (User *U : TrampMem->users()) { |
| 3336 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3337 | if (!II) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3338 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3339 | if (II->getIntrinsicID() == Intrinsic::init_trampoline) { |
| 3340 | if (InitTrampoline) |
| 3341 | // More than one init_trampoline writes to this value. Give up. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3342 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3343 | InitTrampoline = II; |
| 3344 | continue; |
| 3345 | } |
| 3346 | if (II->getIntrinsicID() == Intrinsic::adjust_trampoline) |
| 3347 | // Allow any number of calls to adjust.trampoline. |
| 3348 | continue; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3349 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | // No call to init.trampoline found. |
| 3353 | if (!InitTrampoline) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3354 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3355 | |
| 3356 | // Check that the alloca is being used in the expected way. |
| 3357 | if (InitTrampoline->getOperand(0) != TrampMem) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3358 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3359 | |
| 3360 | return InitTrampoline; |
| 3361 | } |
| 3362 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3363 | static IntrinsicInst *findInitTrampolineFromBB(IntrinsicInst *AdjustTramp, |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3364 | Value *TrampMem) { |
| 3365 | // Visit all the previous instructions in the basic block, and try to find a |
| 3366 | // 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] | 3367 | for (BasicBlock::iterator I = AdjustTramp->getIterator(), |
| 3368 | E = AdjustTramp->getParent()->begin(); |
| 3369 | I != E;) { |
| 3370 | Instruction *Inst = &*--I; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3371 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 3372 | if (II->getIntrinsicID() == Intrinsic::init_trampoline && |
| 3373 | II->getOperand(0) == TrampMem) |
| 3374 | return II; |
| 3375 | if (Inst->mayWriteToMemory()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3376 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3377 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3378 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | // Given a call to llvm.adjust.trampoline, find and return the corresponding |
| 3382 | // call to llvm.init.trampoline if the call to the trampoline can be optimized |
| 3383 | // to a direct call to a function. Otherwise return NULL. |
| 3384 | // |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3385 | static IntrinsicInst *findInitTrampoline(Value *Callee) { |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3386 | Callee = Callee->stripPointerCasts(); |
| 3387 | IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee); |
| 3388 | if (!AdjustTramp || |
| 3389 | AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3390 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3391 | |
| 3392 | Value *TrampMem = AdjustTramp->getOperand(0); |
| 3393 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3394 | if (IntrinsicInst *IT = findInitTrampolineFromAlloca(TrampMem)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3395 | return IT; |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3396 | if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3397 | return IT; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3398 | return nullptr; |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 3401 | /// Improvements for call and invoke instructions. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3402 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 3403 | if (isAllocLikeFn(CS.getInstruction(), &TLI)) |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 3404 | return visitAllocSite(*CS.getInstruction()); |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 3405 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3406 | bool Changed = false; |
| 3407 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 3408 | // Mark any parameters that are known to be non-null with the nonnull |
| 3409 | // attribute. This is helpful for inlining calls to functions with null |
| 3410 | // checks on their arguments. |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 3411 | SmallVector<unsigned, 4> Indices; |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 3412 | unsigned ArgNo = 0; |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 3413 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 3414 | for (Value *V : CS.args()) { |
Sanjay Patel | f9f5d3c | 2016-01-29 23:14:58 +0000 | [diff] [blame] | 3415 | if (V->getType()->isPointerTy() && |
| 3416 | !CS.paramHasAttr(ArgNo + 1, Attribute::NonNull) && |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 3417 | isKnownNonNullAt(V, CS.getInstruction(), &DT)) |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 3418 | Indices.push_back(ArgNo + 1); |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 3419 | ArgNo++; |
| 3420 | } |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 3421 | |
Philip Reames | c25df11 | 2015-06-16 20:24:25 +0000 | [diff] [blame] | 3422 | assert(ArgNo == CS.arg_size() && "sanity check"); |
| 3423 | |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 3424 | if (!Indices.empty()) { |
| 3425 | AttributeSet AS = CS.getAttributes(); |
| 3426 | LLVMContext &Ctx = CS.getInstruction()->getContext(); |
| 3427 | AS = AS.addAttribute(Ctx, Indices, |
| 3428 | Attribute::get(Ctx, Attribute::NonNull)); |
| 3429 | CS.setAttributes(AS); |
| 3430 | Changed = true; |
| 3431 | } |
| 3432 | |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 3433 | // If the callee is a pointer to a function, attempt to move any casts to the |
| 3434 | // arguments of the call/invoke. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3435 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 3436 | if (!isa<Function>(Callee) && transformConstExprCastCall(CS)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3437 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3438 | |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 3439 | if (Function *CalleeF = dyn_cast<Function>(Callee)) { |
| 3440 | // Remove the convergent attr on calls when the callee is not convergent. |
Matt Arsenault | 802ebcb | 2016-06-20 19:04:44 +0000 | [diff] [blame] | 3441 | if (CS.isConvergent() && !CalleeF->isConvergent() && |
| 3442 | !CalleeF->isIntrinsic()) { |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 3443 | DEBUG(dbgs() << "Removing convergent attr from instr " |
| 3444 | << CS.getInstruction() << "\n"); |
| 3445 | CS.setNotConvergent(); |
| 3446 | return CS.getInstruction(); |
| 3447 | } |
| 3448 | |
Chris Lattner | 846a52e | 2010-02-01 18:11:34 +0000 | [diff] [blame] | 3449 | // If the call and callee calling conventions don't match, this call must |
| 3450 | // be unreachable, as the call is undefined. |
| 3451 | if (CalleeF->getCallingConv() != CS.getCallingConv() && |
| 3452 | // Only do this for calls to a function with a body. A prototype may |
| 3453 | // not actually end up matching the implementation's calling conv for a |
| 3454 | // variety of reasons (e.g. it may be written in assembly). |
| 3455 | !CalleeF->isDeclaration()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3456 | Instruction *OldCall = CS.getInstruction(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3457 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3458 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3459 | OldCall); |
Chad Rosier | e28ae30 | 2012-12-13 00:18:46 +0000 | [diff] [blame] | 3460 | // If OldCall does not return void then replaceAllUsesWith undef. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3461 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 3462 | if (!OldCall->getType()->isVoidTy()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3463 | replaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType())); |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 3464 | if (isa<CallInst>(OldCall)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3465 | return eraseInstFromFunction(*OldCall); |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3466 | |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 3467 | // We cannot remove an invoke, because it would change the CFG, just |
| 3468 | // change the callee to a null pointer. |
Gabor Greif | febf6ab | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 3469 | cast<InvokeInst>(OldCall)->setCalledFunction( |
Chris Lattner | 2cecedf | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 3470 | Constant::getNullValue(CalleeF->getType())); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3471 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3472 | } |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 3473 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3474 | |
| 3475 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 3476 | // If CS does not return void then replaceAllUsesWith undef. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3477 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 3478 | if (!CS.getInstruction()->getType()->isVoidTy()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3479 | replaceInstUsesWith(*CS.getInstruction(), |
Eli Friedman | b9ed18f | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 3480 | UndefValue::get(CS.getInstruction()->getType())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3481 | |
Nuno Lopes | 771e7bd | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 3482 | if (isa<InvokeInst>(CS.getInstruction())) { |
| 3483 | // Can't remove an invoke because we cannot change the CFG. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3484 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3485 | } |
Nuno Lopes | 771e7bd | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 3486 | |
| 3487 | // This instruction is not reachable, just remove it. We insert a store to |
| 3488 | // undef so that we know that this code is not reachable, despite the fact |
| 3489 | // that we can't modify the CFG here. |
| 3490 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 3491 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
| 3492 | CS.getInstruction()); |
| 3493 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3494 | return eraseInstFromFunction(*CS.getInstruction()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Sanjay Patel | 6038d3e | 2016-01-29 23:27:03 +0000 | [diff] [blame] | 3497 | if (IntrinsicInst *II = findInitTrampoline(Callee)) |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3498 | return transformCallThroughTrampoline(CS, II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3499 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3500 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 3501 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3502 | if (FTy->isVarArg()) { |
Eli Friedman | 7534b468 | 2011-11-29 01:18:23 +0000 | [diff] [blame] | 3503 | int ix = FTy->getNumParams(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3504 | // See if we can optimize any arguments passed through the varargs area of |
| 3505 | // the call. |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 3506 | for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(), |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3507 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 3508 | CastInst *CI = dyn_cast<CastInst>(*I); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3509 | if (CI && isSafeToEliminateVarargsCast(CS, DL, CI, ix)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3510 | *I = CI->getOperand(0); |
| 3511 | Changed = true; |
| 3512 | } |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
| 3517 | // Inline asm calls cannot throw - mark them 'nounwind'. |
| 3518 | CS.setDoesNotThrow(); |
| 3519 | Changed = true; |
| 3520 | } |
| 3521 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3522 | // 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] | 3523 | // this. None of these calls are seen as possibly dead so go ahead and |
| 3524 | // delete the instruction now. |
| 3525 | if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3526 | Instruction *I = tryOptimizeCall(CI); |
Eric Christopher | 1810d77 | 2010-03-06 10:59:25 +0000 | [diff] [blame] | 3527 | // If we changed something return the result, etc. Otherwise let |
| 3528 | // the fallthrough check. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3529 | if (I) return eraseInstFromFunction(*I); |
Eric Christopher | a7fb58f | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3532 | return Changed ? CS.getInstruction() : nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 3535 | /// If the callee is a constexpr cast of a function, attempt to move the cast to |
| 3536 | /// the arguments of the call/invoke. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3537 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
Sanjay Patel | e3c335c | 2016-08-11 15:21:21 +0000 | [diff] [blame] | 3538 | auto *Callee = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3539 | if (!Callee) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3540 | return false; |
Sanjay Patel | 38ae83d | 2016-08-11 15:23:56 +0000 | [diff] [blame] | 3541 | |
| 3542 | // The prototype of a thunk is a lie. Don't directly call such a function. |
David Majnemer | 4c0a6e9 | 2015-01-21 22:32:04 +0000 | [diff] [blame] | 3543 | if (Callee->hasFnAttribute("thunk")) |
| 3544 | return false; |
Sanjay Patel | 38ae83d | 2016-08-11 15:23:56 +0000 | [diff] [blame] | 3545 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3546 | Instruction *Caller = CS.getInstruction(); |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3547 | const AttributeSet &CallerPAL = CS.getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3548 | |
| 3549 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 3550 | // would cause a type conversion of one of our arguments, change this call to |
| 3551 | // be a direct call with arguments casted to the appropriate types. |
| 3552 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3553 | FunctionType *FT = Callee->getFunctionType(); |
| 3554 | Type *OldRetTy = Caller->getType(); |
| 3555 | Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3556 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3557 | // Check to see if we are changing the return type... |
| 3558 | if (OldRetTy != NewRetTy) { |
Nick Lewycky | a6a17d7 | 2014-01-18 22:47:12 +0000 | [diff] [blame] | 3559 | |
| 3560 | if (NewRetTy->isStructTy()) |
| 3561 | return false; // TODO: Handle multiple return values. |
| 3562 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3563 | if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) { |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 3564 | if (Callee->isDeclaration()) |
| 3565 | return false; // Cannot transform this return value. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3566 | |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 3567 | if (!Caller->use_empty() && |
| 3568 | // void -> non-void is handled specially |
| 3569 | !NewRetTy->isVoidTy()) |
Frederic Riss | c1892e2 | 2014-10-23 04:08:42 +0000 | [diff] [blame] | 3570 | return false; // Cannot transform this return value. |
Matt Arsenault | e6952f2 | 2013-09-17 21:10:14 +0000 | [diff] [blame] | 3571 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3572 | |
| 3573 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 3574 | AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex); |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 3575 | if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(NewRetTy))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3576 | return false; // Attribute not compatible with transformed value. |
| 3577 | } |
| 3578 | |
| 3579 | // If the callsite is an invoke instruction, and the return value is used by |
| 3580 | // a PHI node in a successor, we cannot change the return type of the call |
| 3581 | // because there is no place to put the cast instruction (without breaking |
| 3582 | // the critical edge). Bail out in this case. |
| 3583 | if (!Caller->use_empty()) |
| 3584 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3585 | for (User *U : II->users()) |
| 3586 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3587 | if (PN->getParent() == II->getNormalDest() || |
| 3588 | PN->getParent() == II->getUnwindDest()) |
| 3589 | return false; |
| 3590 | } |
| 3591 | |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 3592 | unsigned NumActualArgs = CS.arg_size(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3593 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 3594 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3595 | // Prevent us turning: |
| 3596 | // declare void @takes_i32_inalloca(i32* inalloca) |
| 3597 | // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0) |
| 3598 | // |
| 3599 | // into: |
| 3600 | // call void @takes_i32_inalloca(i32* null) |
David Majnemer | d61a6fd | 2015-03-11 18:03:05 +0000 | [diff] [blame] | 3601 | // |
| 3602 | // Similarly, avoid folding away bitcasts of byval calls. |
| 3603 | if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) || |
| 3604 | Callee->getAttributes().hasAttrSomewhere(Attribute::ByVal)) |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3605 | return false; |
| 3606 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3607 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 3608 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3609 | Type *ParamTy = FT->getParamType(i); |
| 3610 | Type *ActTy = (*AI)->getType(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3611 | |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3612 | if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL)) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3613 | return false; // Cannot transform this parameter value. |
| 3614 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3615 | if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1). |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 3616 | overlaps(AttributeFuncs::typeIncompatible(ParamTy))) |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3617 | return false; // Attribute not compatible with transformed value. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3618 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 3619 | if (CS.isInAllocaArgument(i)) |
| 3620 | return false; // Cannot transform to and from inalloca. |
| 3621 | |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 3622 | // If the parameter is passed as a byval argument, then we have to have a |
| 3623 | // 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] | 3624 | if (ParamTy != ActTy && |
| 3625 | CallerPAL.getParamAttributes(i + 1).hasAttribute(i + 1, |
| 3626 | Attribute::ByVal)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3627 | PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3628 | if (!ParamPTy || !ParamPTy->getElementType()->isSized()) |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 3629 | return false; |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3630 | |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 3631 | Type *CurElTy = ActTy->getPointerElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3632 | if (DL.getTypeAllocSize(CurElTy) != |
| 3633 | DL.getTypeAllocSize(ParamPTy->getElementType())) |
Chris Lattner | 27ca8eb | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 3634 | return false; |
| 3635 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 3638 | if (Callee->isDeclaration()) { |
| 3639 | // Do not delete arguments unless we have a function body. |
| 3640 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg()) |
| 3641 | return false; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3642 | |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 3643 | // If the callee is just a declaration, don't change the varargsness of the |
| 3644 | // call. We don't want to introduce a varargs call where one doesn't |
| 3645 | // already exist. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3646 | PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 3647 | if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg()) |
| 3648 | return false; |
Jim Grosbach | e84ae7b | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 3649 | |
| 3650 | // If both the callee and the cast type are varargs, we still have to make |
| 3651 | // sure the number of fixed parameters are the same or we have the same |
| 3652 | // ABI issues as if we introduce a varargs call. |
Jim Grosbach | 1df8cdc | 2012-02-03 00:26:07 +0000 | [diff] [blame] | 3653 | if (FT->isVarArg() && |
| 3654 | cast<FunctionType>(APTy->getElementType())->isVarArg() && |
| 3655 | FT->getNumParams() != |
Jim Grosbach | e84ae7b | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 3656 | cast<FunctionType>(APTy->getElementType())->getNumParams()) |
| 3657 | return false; |
Chris Lattner | adf38b3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 3658 | } |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3659 | |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 3660 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 3661 | !CallerPAL.isEmpty()) |
| 3662 | // In this case we have more arguments than the new function type, but we |
| 3663 | // won't be dropping them. Check that these extra arguments have attributes |
| 3664 | // that are compatible with being a vararg call argument. |
| 3665 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 3666 | unsigned Index = CallerPAL.getSlotIndex(i - 1); |
| 3667 | if (Index <= FT->getNumParams()) |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 3668 | break; |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 3669 | |
Bill Wendling | d97b75d | 2012-12-19 08:57:40 +0000 | [diff] [blame] | 3670 | // Check if it has an attribute that's incompatible with varargs. |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 3671 | AttributeSet PAttrs = CallerPAL.getSlotAttributes(i - 1); |
| 3672 | if (PAttrs.hasAttribute(Index, Attribute::StructRet)) |
Jim Grosbach | 0ab5418 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 3673 | return false; |
| 3674 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3675 | |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3676 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3677 | // 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] | 3678 | // inserting cast instructions as necessary. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3679 | std::vector<Value*> Args; |
| 3680 | Args.reserve(NumActualArgs); |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3681 | SmallVector<AttributeSet, 8> attrVec; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3682 | attrVec.reserve(NumCommonArgs); |
| 3683 | |
| 3684 | // Get any return attributes. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 3685 | AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3686 | |
| 3687 | // If the return value is not being used, the type may not be compatible |
| 3688 | // with the existing attributes. Wipe out any problematic attributes. |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 3689 | RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3690 | |
| 3691 | // Add the new return attributes. |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 3692 | if (RAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3693 | attrVec.push_back(AttributeSet::get(Caller->getContext(), |
| 3694 | AttributeSet::ReturnIndex, RAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3695 | |
| 3696 | AI = CS.arg_begin(); |
| 3697 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3698 | Type *ParamTy = FT->getParamType(i); |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 3699 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3700 | if ((*AI)->getType() == ParamTy) { |
| 3701 | Args.push_back(*AI); |
| 3702 | } else { |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3703 | Args.push_back(Builder->CreateBitOrPointerCast(*AI, ParamTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | // Add any parameter attributes. |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3707 | AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1); |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 3708 | if (PAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3709 | attrVec.push_back(AttributeSet::get(Caller->getContext(), i + 1, |
| 3710 | PAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | // If the function takes more arguments than the call was taking, add them |
| 3714 | // now. |
| 3715 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 3716 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 3717 | |
| 3718 | // If we are removing arguments to the function, emit an obnoxious warning. |
| 3719 | if (FT->getNumParams() < NumActualArgs) { |
Nick Lewycky | 90053a1 | 2012-12-26 22:00:35 +0000 | [diff] [blame] | 3720 | // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722 |
| 3721 | if (FT->isVarArg()) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3722 | // Add all of the arguments in their promoted form to the arg list. |
| 3723 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3724 | Type *PTy = getPromotedType((*AI)->getType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3725 | if (PTy != (*AI)->getType()) { |
| 3726 | // Must promote to pass through va_arg area! |
| 3727 | Instruction::CastOps opcode = |
| 3728 | CastInst::getCastOpcode(*AI, false, PTy, false); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 3729 | Args.push_back(Builder->CreateCast(opcode, *AI, PTy)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3730 | } else { |
| 3731 | Args.push_back(*AI); |
| 3732 | } |
| 3733 | |
| 3734 | // Add any parameter attributes. |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3735 | AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1); |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 3736 | if (PAttrs.hasAttributes()) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3737 | attrVec.push_back(AttributeSet::get(FT->getContext(), i + 1, |
| 3738 | PAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3739 | } |
| 3740 | } |
| 3741 | } |
| 3742 | |
Bill Wendling | bd4ea16 | 2013-01-21 21:57:28 +0000 | [diff] [blame] | 3743 | AttributeSet FnAttrs = CallerPAL.getFnAttributes(); |
Bill Wendling | 7754389 | 2013-01-18 21:11:39 +0000 | [diff] [blame] | 3744 | if (CallerPAL.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3745 | attrVec.push_back(AttributeSet::get(Callee->getContext(), FnAttrs)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3746 | |
| 3747 | if (NewRetTy->isVoidTy()) |
| 3748 | Caller->setName(""); // Void type should not have a name. |
| 3749 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3750 | const AttributeSet &NewCallerPAL = AttributeSet::get(Callee->getContext(), |
Bill Wendling | bd4ea16 | 2013-01-21 21:57:28 +0000 | [diff] [blame] | 3751 | attrVec); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3752 | |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 3753 | SmallVector<OperandBundleDef, 1> OpBundles; |
Sanjoy Das | c521c7b | 2015-11-25 00:42:24 +0000 | [diff] [blame] | 3754 | CS.getOperandBundlesAsDefs(OpBundles); |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 3755 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3756 | Instruction *NC; |
| 3757 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 3758 | NC = Builder->CreateInvoke(Callee, II->getNormalDest(), II->getUnwindDest(), |
| 3759 | Args, OpBundles); |
Eli Friedman | 96254a0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 3760 | NC->takeName(II); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3761 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
| 3762 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
| 3763 | } else { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3764 | CallInst *CI = cast<CallInst>(Caller); |
Sanjoy Das | 7629346 | 2015-11-25 00:42:19 +0000 | [diff] [blame] | 3765 | NC = Builder->CreateCall(Callee, Args, OpBundles); |
Eli Friedman | 96254a0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 3766 | NC->takeName(CI); |
David Majnemer | d5648c7 | 2016-11-25 22:35:09 +0000 | [diff] [blame] | 3767 | cast<CallInst>(NC)->setTailCallKind(CI->getTailCallKind()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3768 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
| 3769 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
| 3770 | } |
| 3771 | |
| 3772 | // Insert a cast of the return type as necessary. |
| 3773 | Value *NV = NC; |
| 3774 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
| 3775 | if (!NV->getType()->isVoidTy()) { |
David Majnemer | 9b6b822 | 2015-01-06 08:41:31 +0000 | [diff] [blame] | 3776 | NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 3777 | NC->setDebugLoc(Caller->getDebugLoc()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3778 | |
| 3779 | // If this is an invoke instruction, we should insert it after the first |
| 3780 | // non-phi, instruction in the normal successor block. |
| 3781 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Bill Wendling | 07efd6f | 2011-08-25 01:08:34 +0000 | [diff] [blame] | 3782 | BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3783 | InsertNewInstBefore(NC, *I); |
| 3784 | } else { |
Chris Lattner | 7398965 | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 3785 | // Otherwise, it's a call, just insert cast right after the call. |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3786 | InsertNewInstBefore(NC, *Caller); |
| 3787 | } |
| 3788 | Worklist.AddUsersToWorkList(*Caller); |
| 3789 | } else { |
| 3790 | NV = UndefValue::get(Caller->getType()); |
| 3791 | } |
| 3792 | } |
| 3793 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3794 | if (!Caller->use_empty()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3795 | replaceInstUsesWith(*Caller, NV); |
Frederic Riss | c1892e2 | 2014-10-23 04:08:42 +0000 | [diff] [blame] | 3796 | else if (Caller->hasValueHandle()) { |
| 3797 | if (OldRetTy == NV->getType()) |
| 3798 | ValueHandleBase::ValueIsRAUWd(Caller, NV); |
| 3799 | else |
| 3800 | // We cannot call ValueIsRAUWd with a different type, and the |
| 3801 | // actual tracked value will disappear. |
| 3802 | ValueHandleBase::ValueIsDeleted(Caller); |
| 3803 | } |
Eli Friedman | b9ed18f | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 3804 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3805 | eraseInstFromFunction(*Caller); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3806 | return true; |
| 3807 | } |
| 3808 | |
Sanjay Patel | cd4377c | 2016-01-20 22:24:38 +0000 | [diff] [blame] | 3809 | /// Turn a call to a function created by init_trampoline / adjust_trampoline |
| 3810 | /// intrinsic pair into a direct call to the underlying function. |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3811 | Instruction * |
| 3812 | InstCombiner::transformCallThroughTrampoline(CallSite CS, |
| 3813 | IntrinsicInst *Tramp) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3814 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3815 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 3816 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3817 | const AttributeSet &Attrs = CS.getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3818 | |
| 3819 | // If the call already has the 'nest' attribute somewhere then give up - |
| 3820 | // otherwise 'nest' would occur twice after splicing in the chain. |
Bill Wendling | 6e95ae8 | 2012-12-31 00:49:59 +0000 | [diff] [blame] | 3821 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3822 | return nullptr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3823 | |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 3824 | assert(Tramp && |
| 3825 | "transformCallThroughTrampoline called with incorrect CallSite."); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3826 | |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 3827 | Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts()); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 3828 | FunctionType *NestFTy = cast<FunctionType>(NestF->getValueType()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3829 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3830 | const AttributeSet &NestAttrs = NestF->getAttributes(); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3831 | if (!NestAttrs.isEmpty()) { |
| 3832 | unsigned NestIdx = 1; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3833 | Type *NestTy = nullptr; |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3834 | AttributeSet NestAttr; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3835 | |
| 3836 | // Look for a parameter marked with the 'nest' attribute. |
| 3837 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 3838 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3839 | if (NestAttrs.hasAttribute(NestIdx, Attribute::Nest)) { |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3840 | // Record the parameter type and any other attributes. |
| 3841 | NestTy = *I; |
| 3842 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
| 3843 | break; |
| 3844 | } |
| 3845 | |
| 3846 | if (NestTy) { |
| 3847 | Instruction *Caller = CS.getInstruction(); |
| 3848 | std::vector<Value*> NewArgs; |
Matt Arsenault | 5d2e85f | 2013-06-28 00:25:40 +0000 | [diff] [blame] | 3849 | NewArgs.reserve(CS.arg_size() + 1); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3850 | |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3851 | SmallVector<AttributeSet, 8> NewAttrs; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3852 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
| 3853 | |
| 3854 | // Insert the nest argument into the call argument list, which may |
| 3855 | // mean appending it. Likewise for attributes. |
| 3856 | |
| 3857 | // Add any result attributes. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 3858 | if (Attrs.hasAttributes(AttributeSet::ReturnIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3859 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 3860 | Attrs.getRetAttributes())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3861 | |
| 3862 | { |
| 3863 | unsigned Idx = 1; |
| 3864 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 3865 | do { |
| 3866 | if (Idx == NestIdx) { |
| 3867 | // Add the chain argument and attributes. |
Gabor Greif | 589a0b9 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 3868 | Value *NestVal = Tramp->getArgOperand(2); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3869 | if (NestVal->getType() != NestTy) |
Eli Friedman | 41e509a | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 3870 | NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest"); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3871 | NewArgs.push_back(NestVal); |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3872 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 3873 | NestAttr)); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | if (I == E) |
| 3877 | break; |
| 3878 | |
| 3879 | // Add the original argument and attributes. |
| 3880 | NewArgs.push_back(*I); |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3881 | AttributeSet Attr = Attrs.getParamAttributes(Idx); |
| 3882 | if (Attr.hasAttributes(Idx)) { |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3883 | AttrBuilder B(Attr, Idx); |
| 3884 | NewAttrs.push_back(AttributeSet::get(Caller->getContext(), |
| 3885 | Idx + (Idx >= NestIdx), B)); |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 3886 | } |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3887 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 3888 | ++Idx; |
| 3889 | ++I; |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 3890 | } while (true); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | // Add any function attributes. |
Bill Wendling | 7754389 | 2013-01-18 21:11:39 +0000 | [diff] [blame] | 3894 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 3575c8c | 2013-01-27 02:08:22 +0000 | [diff] [blame] | 3895 | NewAttrs.push_back(AttributeSet::get(FTy->getContext(), |
| 3896 | Attrs.getFnAttributes())); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3897 | |
| 3898 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 3899 | // Handle this by synthesizing a new function type, equal to FTy |
| 3900 | // with the chain parameter inserted. |
| 3901 | |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3902 | std::vector<Type*> NewTypes; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3903 | NewTypes.reserve(FTy->getNumParams()+1); |
| 3904 | |
| 3905 | // Insert the chain's type into the list of parameter types, which may |
| 3906 | // mean appending it. |
| 3907 | { |
| 3908 | unsigned Idx = 1; |
| 3909 | FunctionType::param_iterator I = FTy->param_begin(), |
| 3910 | E = FTy->param_end(); |
| 3911 | |
| 3912 | do { |
| 3913 | if (Idx == NestIdx) |
| 3914 | // Add the chain's type. |
| 3915 | NewTypes.push_back(NestTy); |
| 3916 | |
| 3917 | if (I == E) |
| 3918 | break; |
| 3919 | |
| 3920 | // Add the original type. |
| 3921 | NewTypes.push_back(*I); |
| 3922 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 3923 | ++Idx; |
| 3924 | ++I; |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 3925 | } while (true); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | // Replace the trampoline call with a direct call. Let the generic |
| 3929 | // code sort out any function type mismatches. |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3930 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3931 | FTy->isVarArg()); |
| 3932 | Constant *NewCallee = |
| 3933 | NestF->getType() == PointerType::getUnqual(NewFTy) ? |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3934 | NestF : ConstantExpr::getBitCast(NestF, |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3935 | PointerType::getUnqual(NewFTy)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 3936 | const AttributeSet &NewPAL = |
| 3937 | AttributeSet::get(FTy->getContext(), NewAttrs); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3938 | |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3939 | SmallVector<OperandBundleDef, 1> OpBundles; |
| 3940 | CS.getOperandBundlesAsDefs(OpBundles); |
| 3941 | |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3942 | Instruction *NewCaller; |
| 3943 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 3944 | NewCaller = InvokeInst::Create(NewCallee, |
| 3945 | II->getNormalDest(), II->getUnwindDest(), |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3946 | NewArgs, OpBundles); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3947 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
| 3948 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
| 3949 | } else { |
David Majnemer | 231a68c | 2016-04-29 08:07:20 +0000 | [diff] [blame] | 3950 | NewCaller = CallInst::Create(NewCallee, NewArgs, OpBundles); |
David Majnemer | d5648c7 | 2016-11-25 22:35:09 +0000 | [diff] [blame] | 3951 | cast<CallInst>(NewCaller)->setTailCallKind( |
| 3952 | cast<CallInst>(Caller)->getTailCallKind()); |
| 3953 | cast<CallInst>(NewCaller)->setCallingConv( |
| 3954 | cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3955 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
| 3956 | } |
Eli Friedman | 4934601 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 3957 | |
| 3958 | return NewCaller; |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 3963 | // parameter, there is no need to adjust the argument list. Let the generic |
| 3964 | // code sort out any function type mismatches. |
| 3965 | Constant *NewCallee = |
Jim Grosbach | 7815f56 | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 3966 | NestF->getType() == PTy ? NestF : |
Chris Lattner | 7a9e47a | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 3967 | ConstantExpr::getBitCast(NestF, PTy); |
| 3968 | CS.setCalledFunction(NewCallee); |
| 3969 | return CS.getInstruction(); |
| 3970 | } |