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