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