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