xref: /llvm-project/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (revision 26424c96c03ee4098d7f129de9234a6f177e49ac)
1 //===- InstCombineCasts.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visit functions for cast operations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InstCombineInternal.h"
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/Analysis/ConstantFolding.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/IR/PatternMatch.h"
19 #include "llvm/Support/KnownBits.h"
20 #include "llvm/Transforms/InstCombine/InstCombiner.h"
21 #include <optional>
22 
23 using namespace llvm;
24 using namespace PatternMatch;
25 
26 #define DEBUG_TYPE "instcombine"
27 
28 /// Analyze 'Val', seeing if it is a simple linear expression.
29 /// If so, decompose it, returning some value X, such that Val is
30 /// X*Scale+Offset.
31 ///
32 static Value *decomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
33                                         uint64_t &Offset) {
34   if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
35     Offset = CI->getZExtValue();
36     Scale  = 0;
37     return ConstantInt::get(Val->getType(), 0);
38   }
39 
40   if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
41     // Cannot look past anything that might overflow.
42     // We specifically require nuw because we store the Scale in an unsigned
43     // and perform an unsigned divide on it.
44     OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val);
45     if (OBI && !OBI->hasNoUnsignedWrap()) {
46       Scale = 1;
47       Offset = 0;
48       return Val;
49     }
50 
51     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
52       if (I->getOpcode() == Instruction::Shl) {
53         // This is a value scaled by '1 << the shift amt'.
54         Scale = UINT64_C(1) << RHS->getZExtValue();
55         Offset = 0;
56         return I->getOperand(0);
57       }
58 
59       if (I->getOpcode() == Instruction::Mul) {
60         // This value is scaled by 'RHS'.
61         Scale = RHS->getZExtValue();
62         Offset = 0;
63         return I->getOperand(0);
64       }
65 
66       if (I->getOpcode() == Instruction::Add) {
67         // We have X+C.  Check to see if we really have (X*C2)+C1,
68         // where C1 is divisible by C2.
69         unsigned SubScale;
70         Value *SubVal =
71           decomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
72         Offset += RHS->getZExtValue();
73         Scale = SubScale;
74         return SubVal;
75       }
76     }
77   }
78 
79   // Otherwise, we can't look past this.
80   Scale = 1;
81   Offset = 0;
82   return Val;
83 }
84 
85 /// If we find a cast of an allocation instruction, try to eliminate the cast by
86 /// moving the type information into the alloc.
87 Instruction *InstCombinerImpl::PromoteCastOfAllocation(BitCastInst &CI,
88                                                        AllocaInst &AI) {
89   PointerType *PTy = cast<PointerType>(CI.getType());
90   // Opaque pointers don't have an element type we could replace with.
91   if (PTy->isOpaque())
92     return nullptr;
93 
94   IRBuilderBase::InsertPointGuard Guard(Builder);
95   Builder.SetInsertPoint(&AI);
96 
97   // Get the type really allocated and the type casted to.
98   Type *AllocElTy = AI.getAllocatedType();
99   Type *CastElTy = PTy->getNonOpaquePointerElementType();
100   if (!AllocElTy->isSized() || !CastElTy->isSized()) return nullptr;
101 
102   // This optimisation does not work for cases where the cast type
103   // is scalable and the allocated type is not. This because we need to
104   // know how many times the casted type fits into the allocated type.
105   // For the opposite case where the allocated type is scalable and the
106   // cast type is not this leads to poor code quality due to the
107   // introduction of 'vscale' into the calculations. It seems better to
108   // bail out for this case too until we've done a proper cost-benefit
109   // analysis.
110   bool AllocIsScalable = isa<ScalableVectorType>(AllocElTy);
111   bool CastIsScalable = isa<ScalableVectorType>(CastElTy);
112   if (AllocIsScalable != CastIsScalable) return nullptr;
113 
114   Align AllocElTyAlign = DL.getABITypeAlign(AllocElTy);
115   Align CastElTyAlign = DL.getABITypeAlign(CastElTy);
116   if (CastElTyAlign < AllocElTyAlign) return nullptr;
117 
118   // If the allocation has multiple uses, only promote it if we are strictly
119   // increasing the alignment of the resultant allocation.  If we keep it the
120   // same, we open the door to infinite loops of various kinds.
121   if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return nullptr;
122 
123   // The alloc and cast types should be either both fixed or both scalable.
124   uint64_t AllocElTySize = DL.getTypeAllocSize(AllocElTy).getKnownMinSize();
125   uint64_t CastElTySize = DL.getTypeAllocSize(CastElTy).getKnownMinSize();
126   if (CastElTySize == 0 || AllocElTySize == 0) return nullptr;
127 
128   // If the allocation has multiple uses, only promote it if we're not
129   // shrinking the amount of memory being allocated.
130   uint64_t AllocElTyStoreSize = DL.getTypeStoreSize(AllocElTy).getKnownMinSize();
131   uint64_t CastElTyStoreSize = DL.getTypeStoreSize(CastElTy).getKnownMinSize();
132   if (!AI.hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize) return nullptr;
133 
134   // See if we can satisfy the modulus by pulling a scale out of the array
135   // size argument.
136   unsigned ArraySizeScale;
137   uint64_t ArrayOffset;
138   Value *NumElements = // See if the array size is a decomposable linear expr.
139     decomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
140 
141   // If we can now satisfy the modulus, by using a non-1 scale, we really can
142   // do the xform.
143   if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
144       (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return nullptr;
145 
146   // We don't currently support arrays of scalable types.
147   assert(!AllocIsScalable || (ArrayOffset == 1 && ArraySizeScale == 0));
148 
149   unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
150   Value *Amt = nullptr;
151   if (Scale == 1) {
152     Amt = NumElements;
153   } else {
154     Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
155     // Insert before the alloca, not before the cast.
156     Amt = Builder.CreateMul(Amt, NumElements);
157   }
158 
159   if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
160     Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
161                                   Offset, true);
162     Amt = Builder.CreateAdd(Amt, Off);
163   }
164 
165   AllocaInst *New = Builder.CreateAlloca(CastElTy, AI.getAddressSpace(), Amt);
166   New->setAlignment(AI.getAlign());
167   New->takeName(&AI);
168   New->setUsedWithInAlloca(AI.isUsedWithInAlloca());
169   New->setMetadata(LLVMContext::MD_DIAssignID,
170                    AI.getMetadata(LLVMContext::MD_DIAssignID));
171 
172   replaceAllDbgUsesWith(AI, *New, *New, DT);
173 
174   // If the allocation has multiple real uses, insert a cast and change all
175   // things that used it to use the new cast.  This will also hack on CI, but it
176   // will die soon.
177   if (!AI.hasOneUse()) {
178     // New is the allocation instruction, pointer typed. AI is the original
179     // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
180     Value *NewCast = Builder.CreateBitCast(New, AI.getType(), "tmpcast");
181     replaceInstUsesWith(AI, NewCast);
182     eraseInstFromFunction(AI);
183   }
184   return replaceInstUsesWith(CI, New);
185 }
186 
187 /// Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns
188 /// true for, actually insert the code to evaluate the expression.
189 Value *InstCombinerImpl::EvaluateInDifferentType(Value *V, Type *Ty,
190                                                  bool isSigned) {
191   if (Constant *C = dyn_cast<Constant>(V)) {
192     C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
193     // If we got a constantexpr back, try to simplify it with DL info.
194     return ConstantFoldConstant(C, DL, &TLI);
195   }
196 
197   // Otherwise, it must be an instruction.
198   Instruction *I = cast<Instruction>(V);
199   Instruction *Res = nullptr;
200   unsigned Opc = I->getOpcode();
201   switch (Opc) {
202   case Instruction::Add:
203   case Instruction::Sub:
204   case Instruction::Mul:
205   case Instruction::And:
206   case Instruction::Or:
207   case Instruction::Xor:
208   case Instruction::AShr:
209   case Instruction::LShr:
210   case Instruction::Shl:
211   case Instruction::UDiv:
212   case Instruction::URem: {
213     Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
214     Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
215     Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
216     break;
217   }
218   case Instruction::Trunc:
219   case Instruction::ZExt:
220   case Instruction::SExt:
221     // If the source type of the cast is the type we're trying for then we can
222     // just return the source.  There's no need to insert it because it is not
223     // new.
224     if (I->getOperand(0)->getType() == Ty)
225       return I->getOperand(0);
226 
227     // Otherwise, must be the same type of cast, so just reinsert a new one.
228     // This also handles the case of zext(trunc(x)) -> zext(x).
229     Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
230                                       Opc == Instruction::SExt);
231     break;
232   case Instruction::Select: {
233     Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
234     Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
235     Res = SelectInst::Create(I->getOperand(0), True, False);
236     break;
237   }
238   case Instruction::PHI: {
239     PHINode *OPN = cast<PHINode>(I);
240     PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues());
241     for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
242       Value *V =
243           EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
244       NPN->addIncoming(V, OPN->getIncomingBlock(i));
245     }
246     Res = NPN;
247     break;
248   }
249   default:
250     // TODO: Can handle more cases here.
251     llvm_unreachable("Unreachable!");
252   }
253 
254   Res->takeName(I);
255   return InsertNewInstWith(Res, *I);
256 }
257 
258 Instruction::CastOps
259 InstCombinerImpl::isEliminableCastPair(const CastInst *CI1,
260                                        const CastInst *CI2) {
261   Type *SrcTy = CI1->getSrcTy();
262   Type *MidTy = CI1->getDestTy();
263   Type *DstTy = CI2->getDestTy();
264 
265   Instruction::CastOps firstOp = CI1->getOpcode();
266   Instruction::CastOps secondOp = CI2->getOpcode();
267   Type *SrcIntPtrTy =
268       SrcTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(SrcTy) : nullptr;
269   Type *MidIntPtrTy =
270       MidTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(MidTy) : nullptr;
271   Type *DstIntPtrTy =
272       DstTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(DstTy) : nullptr;
273   unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
274                                                 DstTy, SrcIntPtrTy, MidIntPtrTy,
275                                                 DstIntPtrTy);
276 
277   // We don't want to form an inttoptr or ptrtoint that converts to an integer
278   // type that differs from the pointer size.
279   if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
280       (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
281     Res = 0;
282 
283   return Instruction::CastOps(Res);
284 }
285 
286 /// Implement the transforms common to all CastInst visitors.
287 Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
288   Value *Src = CI.getOperand(0);
289   Type *Ty = CI.getType();
290 
291   // Try to eliminate a cast of a cast.
292   if (auto *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast
293     if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) {
294       // The first cast (CSrc) is eliminable so we need to fix up or replace
295       // the second cast (CI). CSrc will then have a good chance of being dead.
296       auto *Res = CastInst::Create(NewOpc, CSrc->getOperand(0), Ty);
297       // Point debug users of the dying cast to the new one.
298       if (CSrc->hasOneUse())
299         replaceAllDbgUsesWith(*CSrc, *Res, CI, DT);
300       return Res;
301     }
302   }
303 
304   if (auto *Sel = dyn_cast<SelectInst>(Src)) {
305     // We are casting a select. Try to fold the cast into the select if the
306     // select does not have a compare instruction with matching operand types
307     // or the select is likely better done in a narrow type.
308     // Creating a select with operands that are different sizes than its
309     // condition may inhibit other folds and lead to worse codegen.
310     auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition());
311     if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType() ||
312         (CI.getOpcode() == Instruction::Trunc &&
313          shouldChangeType(CI.getSrcTy(), CI.getType()))) {
314       if (Instruction *NV = FoldOpIntoSelect(CI, Sel)) {
315         replaceAllDbgUsesWith(*Sel, *NV, CI, DT);
316         return NV;
317       }
318     }
319   }
320 
321   // If we are casting a PHI, then fold the cast into the PHI.
322   if (auto *PN = dyn_cast<PHINode>(Src)) {
323     // Don't do this if it would create a PHI node with an illegal type from a
324     // legal type.
325     if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() ||
326         shouldChangeType(CI.getSrcTy(), CI.getType()))
327       if (Instruction *NV = foldOpIntoPhi(CI, PN))
328         return NV;
329   }
330 
331   // Canonicalize a unary shuffle after the cast if neither operation changes
332   // the size or element size of the input vector.
333   // TODO: We could allow size-changing ops if that doesn't harm codegen.
334   // cast (shuffle X, Mask) --> shuffle (cast X), Mask
335   Value *X;
336   ArrayRef<int> Mask;
337   if (match(Src, m_OneUse(m_Shuffle(m_Value(X), m_Undef(), m_Mask(Mask))))) {
338     // TODO: Allow scalable vectors?
339     auto *SrcTy = dyn_cast<FixedVectorType>(X->getType());
340     auto *DestTy = dyn_cast<FixedVectorType>(Ty);
341     if (SrcTy && DestTy &&
342         SrcTy->getNumElements() == DestTy->getNumElements() &&
343         SrcTy->getPrimitiveSizeInBits() == DestTy->getPrimitiveSizeInBits()) {
344       Value *CastX = Builder.CreateCast(CI.getOpcode(), X, DestTy);
345       return new ShuffleVectorInst(CastX, Mask);
346     }
347   }
348 
349   return nullptr;
350 }
351 
352 /// Constants and extensions/truncates from the destination type are always
353 /// free to be evaluated in that type. This is a helper for canEvaluate*.
354 static bool canAlwaysEvaluateInType(Value *V, Type *Ty) {
355   if (isa<Constant>(V))
356     return true;
357   Value *X;
358   if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) &&
359       X->getType() == Ty)
360     return true;
361 
362   return false;
363 }
364 
365 /// Filter out values that we can not evaluate in the destination type for free.
366 /// This is a helper for canEvaluate*.
367 static bool canNotEvaluateInType(Value *V, Type *Ty) {
368   assert(!isa<Constant>(V) && "Constant should already be handled.");
369   if (!isa<Instruction>(V))
370     return true;
371   // We don't extend or shrink something that has multiple uses --  doing so
372   // would require duplicating the instruction which isn't profitable.
373   if (!V->hasOneUse())
374     return true;
375 
376   return false;
377 }
378 
379 /// Return true if we can evaluate the specified expression tree as type Ty
380 /// instead of its larger type, and arrive with the same value.
381 /// This is used by code that tries to eliminate truncates.
382 ///
383 /// Ty will always be a type smaller than V.  We should return true if trunc(V)
384 /// can be computed by computing V in the smaller type.  If V is an instruction,
385 /// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
386 /// makes sense if x and y can be efficiently truncated.
387 ///
388 /// This function works on both vectors and scalars.
389 ///
390 static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombinerImpl &IC,
391                                  Instruction *CxtI) {
392   if (canAlwaysEvaluateInType(V, Ty))
393     return true;
394   if (canNotEvaluateInType(V, Ty))
395     return false;
396 
397   auto *I = cast<Instruction>(V);
398   Type *OrigTy = V->getType();
399   switch (I->getOpcode()) {
400   case Instruction::Add:
401   case Instruction::Sub:
402   case Instruction::Mul:
403   case Instruction::And:
404   case Instruction::Or:
405   case Instruction::Xor:
406     // These operators can all arbitrarily be extended or truncated.
407     return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
408            canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
409 
410   case Instruction::UDiv:
411   case Instruction::URem: {
412     // UDiv and URem can be truncated if all the truncated bits are zero.
413     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
414     uint32_t BitWidth = Ty->getScalarSizeInBits();
415     assert(BitWidth < OrigBitWidth && "Unexpected bitwidths!");
416     APInt Mask = APInt::getBitsSetFrom(OrigBitWidth, BitWidth);
417     if (IC.MaskedValueIsZero(I->getOperand(0), Mask, 0, CxtI) &&
418         IC.MaskedValueIsZero(I->getOperand(1), Mask, 0, CxtI)) {
419       return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
420              canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
421     }
422     break;
423   }
424   case Instruction::Shl: {
425     // If we are truncating the result of this SHL, and if it's a shift of an
426     // inrange amount, we can always perform a SHL in a smaller type.
427     uint32_t BitWidth = Ty->getScalarSizeInBits();
428     KnownBits AmtKnownBits =
429         llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout());
430     if (AmtKnownBits.getMaxValue().ult(BitWidth))
431       return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
432              canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
433     break;
434   }
435   case Instruction::LShr: {
436     // If this is a truncate of a logical shr, we can truncate it to a smaller
437     // lshr iff we know that the bits we would otherwise be shifting in are
438     // already zeros.
439     // TODO: It is enough to check that the bits we would be shifting in are
440     //       zero - use AmtKnownBits.getMaxValue().
441     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
442     uint32_t BitWidth = Ty->getScalarSizeInBits();
443     KnownBits AmtKnownBits =
444         llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout());
445     APInt ShiftedBits = APInt::getBitsSetFrom(OrigBitWidth, BitWidth);
446     if (AmtKnownBits.getMaxValue().ult(BitWidth) &&
447         IC.MaskedValueIsZero(I->getOperand(0), ShiftedBits, 0, CxtI)) {
448       return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
449              canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
450     }
451     break;
452   }
453   case Instruction::AShr: {
454     // If this is a truncate of an arithmetic shr, we can truncate it to a
455     // smaller ashr iff we know that all the bits from the sign bit of the
456     // original type and the sign bit of the truncate type are similar.
457     // TODO: It is enough to check that the bits we would be shifting in are
458     //       similar to sign bit of the truncate type.
459     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
460     uint32_t BitWidth = Ty->getScalarSizeInBits();
461     KnownBits AmtKnownBits =
462         llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout());
463     unsigned ShiftedBits = OrigBitWidth - BitWidth;
464     if (AmtKnownBits.getMaxValue().ult(BitWidth) &&
465         ShiftedBits < IC.ComputeNumSignBits(I->getOperand(0), 0, CxtI))
466       return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
467              canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
468     break;
469   }
470   case Instruction::Trunc:
471     // trunc(trunc(x)) -> trunc(x)
472     return true;
473   case Instruction::ZExt:
474   case Instruction::SExt:
475     // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
476     // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest
477     return true;
478   case Instruction::Select: {
479     SelectInst *SI = cast<SelectInst>(I);
480     return canEvaluateTruncated(SI->getTrueValue(), Ty, IC, CxtI) &&
481            canEvaluateTruncated(SI->getFalseValue(), Ty, IC, CxtI);
482   }
483   case Instruction::PHI: {
484     // We can change a phi if we can change all operands.  Note that we never
485     // get into trouble with cyclic PHIs here because we only consider
486     // instructions with a single use.
487     PHINode *PN = cast<PHINode>(I);
488     for (Value *IncValue : PN->incoming_values())
489       if (!canEvaluateTruncated(IncValue, Ty, IC, CxtI))
490         return false;
491     return true;
492   }
493   default:
494     // TODO: Can handle more cases here.
495     break;
496   }
497 
498   return false;
499 }
500 
501 /// Given a vector that is bitcast to an integer, optionally logically
502 /// right-shifted, and truncated, convert it to an extractelement.
503 /// Example (big endian):
504 ///   trunc (lshr (bitcast <4 x i32> %X to i128), 32) to i32
505 ///   --->
506 ///   extractelement <4 x i32> %X, 1
507 static Instruction *foldVecTruncToExtElt(TruncInst &Trunc,
508                                          InstCombinerImpl &IC) {
509   Value *TruncOp = Trunc.getOperand(0);
510   Type *DestType = Trunc.getType();
511   if (!TruncOp->hasOneUse() || !isa<IntegerType>(DestType))
512     return nullptr;
513 
514   Value *VecInput = nullptr;
515   ConstantInt *ShiftVal = nullptr;
516   if (!match(TruncOp, m_CombineOr(m_BitCast(m_Value(VecInput)),
517                                   m_LShr(m_BitCast(m_Value(VecInput)),
518                                          m_ConstantInt(ShiftVal)))) ||
519       !isa<VectorType>(VecInput->getType()))
520     return nullptr;
521 
522   VectorType *VecType = cast<VectorType>(VecInput->getType());
523   unsigned VecWidth = VecType->getPrimitiveSizeInBits();
524   unsigned DestWidth = DestType->getPrimitiveSizeInBits();
525   unsigned ShiftAmount = ShiftVal ? ShiftVal->getZExtValue() : 0;
526 
527   if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0))
528     return nullptr;
529 
530   // If the element type of the vector doesn't match the result type,
531   // bitcast it to a vector type that we can extract from.
532   unsigned NumVecElts = VecWidth / DestWidth;
533   if (VecType->getElementType() != DestType) {
534     VecType = FixedVectorType::get(DestType, NumVecElts);
535     VecInput = IC.Builder.CreateBitCast(VecInput, VecType, "bc");
536   }
537 
538   unsigned Elt = ShiftAmount / DestWidth;
539   if (IC.getDataLayout().isBigEndian())
540     Elt = NumVecElts - 1 - Elt;
541 
542   return ExtractElementInst::Create(VecInput, IC.Builder.getInt32(Elt));
543 }
544 
545 /// Funnel/Rotate left/right may occur in a wider type than necessary because of
546 /// type promotion rules. Try to narrow the inputs and convert to funnel shift.
547 Instruction *InstCombinerImpl::narrowFunnelShift(TruncInst &Trunc) {
548   assert((isa<VectorType>(Trunc.getSrcTy()) ||
549           shouldChangeType(Trunc.getSrcTy(), Trunc.getType())) &&
550          "Don't narrow to an illegal scalar type");
551 
552   // Bail out on strange types. It is possible to handle some of these patterns
553   // even with non-power-of-2 sizes, but it is not a likely scenario.
554   Type *DestTy = Trunc.getType();
555   unsigned NarrowWidth = DestTy->getScalarSizeInBits();
556   unsigned WideWidth = Trunc.getSrcTy()->getScalarSizeInBits();
557   if (!isPowerOf2_32(NarrowWidth))
558     return nullptr;
559 
560   // First, find an or'd pair of opposite shifts:
561   // trunc (or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1))
562   BinaryOperator *Or0, *Or1;
563   if (!match(Trunc.getOperand(0), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
564     return nullptr;
565 
566   Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
567   if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
568       !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
569       Or0->getOpcode() == Or1->getOpcode())
570     return nullptr;
571 
572   // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
573   if (Or0->getOpcode() == BinaryOperator::LShr) {
574     std::swap(Or0, Or1);
575     std::swap(ShVal0, ShVal1);
576     std::swap(ShAmt0, ShAmt1);
577   }
578   assert(Or0->getOpcode() == BinaryOperator::Shl &&
579          Or1->getOpcode() == BinaryOperator::LShr &&
580          "Illegal or(shift,shift) pair");
581 
582   // Match the shift amount operands for a funnel/rotate pattern. This always
583   // matches a subtraction on the R operand.
584   auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
585     // The shift amounts may add up to the narrow bit width:
586     // (shl ShVal0, L) | (lshr ShVal1, Width - L)
587     // If this is a funnel shift (different operands are shifted), then the
588     // shift amount can not over-shift (create poison) in the narrow type.
589     unsigned MaxShiftAmountWidth = Log2_32(NarrowWidth);
590     APInt HiBitMask = ~APInt::getLowBitsSet(WideWidth, MaxShiftAmountWidth);
591     if (ShVal0 == ShVal1 || MaskedValueIsZero(L, HiBitMask))
592       if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L)))))
593         return L;
594 
595     // The following patterns currently only work for rotation patterns.
596     // TODO: Add more general funnel-shift compatible patterns.
597     if (ShVal0 != ShVal1)
598       return nullptr;
599 
600     // The shift amount may be masked with negation:
601     // (shl ShVal0, (X & (Width - 1))) | (lshr ShVal1, ((-X) & (Width - 1)))
602     Value *X;
603     unsigned Mask = Width - 1;
604     if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
605         match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))
606       return X;
607 
608     // Same as above, but the shift amount may be extended after masking:
609     if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
610         match(R, m_ZExt(m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))))
611       return X;
612 
613     return nullptr;
614   };
615 
616   Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, NarrowWidth);
617   bool IsFshl = true; // Sub on LSHR.
618   if (!ShAmt) {
619     ShAmt = matchShiftAmount(ShAmt1, ShAmt0, NarrowWidth);
620     IsFshl = false; // Sub on SHL.
621   }
622   if (!ShAmt)
623     return nullptr;
624 
625   // The right-shifted value must have high zeros in the wide type (for example
626   // from 'zext', 'and' or 'shift'). High bits of the left-shifted value are
627   // truncated, so those do not matter.
628   APInt HiBitMask = APInt::getHighBitsSet(WideWidth, WideWidth - NarrowWidth);
629   if (!MaskedValueIsZero(ShVal1, HiBitMask, 0, &Trunc))
630     return nullptr;
631 
632   // We have an unnecessarily wide rotate!
633   // trunc (or (shl ShVal0, ShAmt), (lshr ShVal1, BitWidth - ShAmt))
634   // Narrow the inputs and convert to funnel shift intrinsic:
635   // llvm.fshl.i8(trunc(ShVal), trunc(ShVal), trunc(ShAmt))
636   Value *NarrowShAmt = Builder.CreateTrunc(ShAmt, DestTy);
637   Value *X, *Y;
638   X = Y = Builder.CreateTrunc(ShVal0, DestTy);
639   if (ShVal0 != ShVal1)
640     Y = Builder.CreateTrunc(ShVal1, DestTy);
641   Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
642   Function *F = Intrinsic::getDeclaration(Trunc.getModule(), IID, DestTy);
643   return CallInst::Create(F, {X, Y, NarrowShAmt});
644 }
645 
646 /// Try to narrow the width of math or bitwise logic instructions by pulling a
647 /// truncate ahead of binary operators.
648 Instruction *InstCombinerImpl::narrowBinOp(TruncInst &Trunc) {
649   Type *SrcTy = Trunc.getSrcTy();
650   Type *DestTy = Trunc.getType();
651   unsigned SrcWidth = SrcTy->getScalarSizeInBits();
652   unsigned DestWidth = DestTy->getScalarSizeInBits();
653 
654   if (!isa<VectorType>(SrcTy) && !shouldChangeType(SrcTy, DestTy))
655     return nullptr;
656 
657   BinaryOperator *BinOp;
658   if (!match(Trunc.getOperand(0), m_OneUse(m_BinOp(BinOp))))
659     return nullptr;
660 
661   Value *BinOp0 = BinOp->getOperand(0);
662   Value *BinOp1 = BinOp->getOperand(1);
663   switch (BinOp->getOpcode()) {
664   case Instruction::And:
665   case Instruction::Or:
666   case Instruction::Xor:
667   case Instruction::Add:
668   case Instruction::Sub:
669   case Instruction::Mul: {
670     Constant *C;
671     if (match(BinOp0, m_Constant(C))) {
672       // trunc (binop C, X) --> binop (trunc C', X)
673       Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
674       Value *TruncX = Builder.CreateTrunc(BinOp1, DestTy);
675       return BinaryOperator::Create(BinOp->getOpcode(), NarrowC, TruncX);
676     }
677     if (match(BinOp1, m_Constant(C))) {
678       // trunc (binop X, C) --> binop (trunc X, C')
679       Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
680       Value *TruncX = Builder.CreateTrunc(BinOp0, DestTy);
681       return BinaryOperator::Create(BinOp->getOpcode(), TruncX, NarrowC);
682     }
683     Value *X;
684     if (match(BinOp0, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) {
685       // trunc (binop (ext X), Y) --> binop X, (trunc Y)
686       Value *NarrowOp1 = Builder.CreateTrunc(BinOp1, DestTy);
687       return BinaryOperator::Create(BinOp->getOpcode(), X, NarrowOp1);
688     }
689     if (match(BinOp1, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) {
690       // trunc (binop Y, (ext X)) --> binop (trunc Y), X
691       Value *NarrowOp0 = Builder.CreateTrunc(BinOp0, DestTy);
692       return BinaryOperator::Create(BinOp->getOpcode(), NarrowOp0, X);
693     }
694     break;
695   }
696   case Instruction::LShr:
697   case Instruction::AShr: {
698     // trunc (*shr (trunc A), C) --> trunc(*shr A, C)
699     Value *A;
700     Constant *C;
701     if (match(BinOp0, m_Trunc(m_Value(A))) && match(BinOp1, m_Constant(C))) {
702       unsigned MaxShiftAmt = SrcWidth - DestWidth;
703       // If the shift is small enough, all zero/sign bits created by the shift
704       // are removed by the trunc.
705       if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULE,
706                                       APInt(SrcWidth, MaxShiftAmt)))) {
707         auto *OldShift = cast<Instruction>(Trunc.getOperand(0));
708         bool IsExact = OldShift->isExact();
709         auto *ShAmt = ConstantExpr::getIntegerCast(C, A->getType(), true);
710         ShAmt = Constant::mergeUndefsWith(ShAmt, C);
711         Value *Shift =
712             OldShift->getOpcode() == Instruction::AShr
713                 ? Builder.CreateAShr(A, ShAmt, OldShift->getName(), IsExact)
714                 : Builder.CreateLShr(A, ShAmt, OldShift->getName(), IsExact);
715         return CastInst::CreateTruncOrBitCast(Shift, DestTy);
716       }
717     }
718     break;
719   }
720   default: break;
721   }
722 
723   if (Instruction *NarrowOr = narrowFunnelShift(Trunc))
724     return NarrowOr;
725 
726   return nullptr;
727 }
728 
729 /// Try to narrow the width of a splat shuffle. This could be generalized to any
730 /// shuffle with a constant operand, but we limit the transform to avoid
731 /// creating a shuffle type that targets may not be able to lower effectively.
732 static Instruction *shrinkSplatShuffle(TruncInst &Trunc,
733                                        InstCombiner::BuilderTy &Builder) {
734   auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
735   if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) &&
736       all_equal(Shuf->getShuffleMask()) &&
737       Shuf->getType() == Shuf->getOperand(0)->getType()) {
738     // trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask
739     // trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask
740     Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType());
741     return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask());
742   }
743 
744   return nullptr;
745 }
746 
747 /// Try to narrow the width of an insert element. This could be generalized for
748 /// any vector constant, but we limit the transform to insertion into undef to
749 /// avoid potential backend problems from unsupported insertion widths. This
750 /// could also be extended to handle the case of inserting a scalar constant
751 /// into a vector variable.
752 static Instruction *shrinkInsertElt(CastInst &Trunc,
753                                     InstCombiner::BuilderTy &Builder) {
754   Instruction::CastOps Opcode = Trunc.getOpcode();
755   assert((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) &&
756          "Unexpected instruction for shrinking");
757 
758   auto *InsElt = dyn_cast<InsertElementInst>(Trunc.getOperand(0));
759   if (!InsElt || !InsElt->hasOneUse())
760     return nullptr;
761 
762   Type *DestTy = Trunc.getType();
763   Type *DestScalarTy = DestTy->getScalarType();
764   Value *VecOp = InsElt->getOperand(0);
765   Value *ScalarOp = InsElt->getOperand(1);
766   Value *Index = InsElt->getOperand(2);
767 
768   if (match(VecOp, m_Undef())) {
769     // trunc   (inselt undef, X, Index) --> inselt undef,   (trunc X), Index
770     // fptrunc (inselt undef, X, Index) --> inselt undef, (fptrunc X), Index
771     UndefValue *NarrowUndef = UndefValue::get(DestTy);
772     Value *NarrowOp = Builder.CreateCast(Opcode, ScalarOp, DestScalarTy);
773     return InsertElementInst::Create(NarrowUndef, NarrowOp, Index);
774   }
775 
776   return nullptr;
777 }
778 
779 Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
780   if (Instruction *Result = commonCastTransforms(Trunc))
781     return Result;
782 
783   Value *Src = Trunc.getOperand(0);
784   Type *DestTy = Trunc.getType(), *SrcTy = Src->getType();
785   unsigned DestWidth = DestTy->getScalarSizeInBits();
786   unsigned SrcWidth = SrcTy->getScalarSizeInBits();
787 
788   // Attempt to truncate the entire input expression tree to the destination
789   // type.   Only do this if the dest type is a simple type, don't convert the
790   // expression tree to something weird like i93 unless the source is also
791   // strange.
792   if ((DestTy->isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
793       canEvaluateTruncated(Src, DestTy, *this, &Trunc)) {
794 
795     // If this cast is a truncate, evaluting in a different type always
796     // eliminates the cast, so it is always a win.
797     LLVM_DEBUG(
798         dbgs() << "ICE: EvaluateInDifferentType converting expression type"
799                   " to avoid cast: "
800                << Trunc << '\n');
801     Value *Res = EvaluateInDifferentType(Src, DestTy, false);
802     assert(Res->getType() == DestTy);
803     return replaceInstUsesWith(Trunc, Res);
804   }
805 
806   // For integer types, check if we can shorten the entire input expression to
807   // DestWidth * 2, which won't allow removing the truncate, but reducing the
808   // width may enable further optimizations, e.g. allowing for larger
809   // vectorization factors.
810   if (auto *DestITy = dyn_cast<IntegerType>(DestTy)) {
811     if (DestWidth * 2 < SrcWidth) {
812       auto *NewDestTy = DestITy->getExtendedType();
813       if (shouldChangeType(SrcTy, NewDestTy) &&
814           canEvaluateTruncated(Src, NewDestTy, *this, &Trunc)) {
815         LLVM_DEBUG(
816             dbgs() << "ICE: EvaluateInDifferentType converting expression type"
817                       " to reduce the width of operand of"
818                    << Trunc << '\n');
819         Value *Res = EvaluateInDifferentType(Src, NewDestTy, false);
820         return new TruncInst(Res, DestTy);
821       }
822     }
823   }
824 
825   // Test if the trunc is the user of a select which is part of a
826   // minimum or maximum operation. If so, don't do any more simplification.
827   // Even simplifying demanded bits can break the canonical form of a
828   // min/max.
829   Value *LHS, *RHS;
830   if (SelectInst *Sel = dyn_cast<SelectInst>(Src))
831     if (matchSelectPattern(Sel, LHS, RHS).Flavor != SPF_UNKNOWN)
832       return nullptr;
833 
834   // See if we can simplify any instructions used by the input whose sole
835   // purpose is to compute bits we don't care about.
836   if (SimplifyDemandedInstructionBits(Trunc))
837     return &Trunc;
838 
839   if (DestWidth == 1) {
840     Value *Zero = Constant::getNullValue(SrcTy);
841     if (DestTy->isIntegerTy()) {
842       // Canonicalize trunc x to i1 -> icmp ne (and x, 1), 0 (scalar only).
843       // TODO: We canonicalize to more instructions here because we are probably
844       // lacking equivalent analysis for trunc relative to icmp. There may also
845       // be codegen concerns. If those trunc limitations were removed, we could
846       // remove this transform.
847       Value *And = Builder.CreateAnd(Src, ConstantInt::get(SrcTy, 1));
848       return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
849     }
850 
851     // For vectors, we do not canonicalize all truncs to icmp, so optimize
852     // patterns that would be covered within visitICmpInst.
853     Value *X;
854     Constant *C;
855     if (match(Src, m_OneUse(m_LShr(m_Value(X), m_Constant(C))))) {
856       // trunc (lshr X, C) to i1 --> icmp ne (and X, C'), 0
857       Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1));
858       Constant *MaskC = ConstantExpr::getShl(One, C);
859       Value *And = Builder.CreateAnd(X, MaskC);
860       return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
861     }
862     if (match(Src, m_OneUse(m_c_Or(m_LShr(m_Value(X), m_Constant(C)),
863                                    m_Deferred(X))))) {
864       // trunc (or (lshr X, C), X) to i1 --> icmp ne (and X, C'), 0
865       Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1));
866       Constant *MaskC = ConstantExpr::getShl(One, C);
867       MaskC = ConstantExpr::getOr(MaskC, One);
868       Value *And = Builder.CreateAnd(X, MaskC);
869       return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
870     }
871   }
872 
873   Value *A, *B;
874   Constant *C;
875   if (match(Src, m_LShr(m_SExt(m_Value(A)), m_Constant(C)))) {
876     unsigned AWidth = A->getType()->getScalarSizeInBits();
877     unsigned MaxShiftAmt = SrcWidth - std::max(DestWidth, AWidth);
878     auto *OldSh = cast<Instruction>(Src);
879     bool IsExact = OldSh->isExact();
880 
881     // If the shift is small enough, all zero bits created by the shift are
882     // removed by the trunc.
883     if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULE,
884                                     APInt(SrcWidth, MaxShiftAmt)))) {
885       // trunc (lshr (sext A), C) --> ashr A, C
886       if (A->getType() == DestTy) {
887         Constant *MaxAmt = ConstantInt::get(SrcTy, DestWidth - 1, false);
888         Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt);
889         ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType());
890         ShAmt = Constant::mergeUndefsWith(ShAmt, C);
891         return IsExact ? BinaryOperator::CreateExactAShr(A, ShAmt)
892                        : BinaryOperator::CreateAShr(A, ShAmt);
893       }
894       // The types are mismatched, so create a cast after shifting:
895       // trunc (lshr (sext A), C) --> sext/trunc (ashr A, C)
896       if (Src->hasOneUse()) {
897         Constant *MaxAmt = ConstantInt::get(SrcTy, AWidth - 1, false);
898         Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt);
899         ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType());
900         Value *Shift = Builder.CreateAShr(A, ShAmt, "", IsExact);
901         return CastInst::CreateIntegerCast(Shift, DestTy, true);
902       }
903     }
904     // TODO: Mask high bits with 'and'.
905   }
906 
907   if (Instruction *I = narrowBinOp(Trunc))
908     return I;
909 
910   if (Instruction *I = shrinkSplatShuffle(Trunc, Builder))
911     return I;
912 
913   if (Instruction *I = shrinkInsertElt(Trunc, Builder))
914     return I;
915 
916   if (Src->hasOneUse() &&
917       (isa<VectorType>(SrcTy) || shouldChangeType(SrcTy, DestTy))) {
918     // Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the
919     // dest type is native and cst < dest size.
920     if (match(Src, m_Shl(m_Value(A), m_Constant(C))) &&
921         !match(A, m_Shr(m_Value(), m_Constant()))) {
922       // Skip shifts of shift by constants. It undoes a combine in
923       // FoldShiftByConstant and is the extend in reg pattern.
924       APInt Threshold = APInt(C->getType()->getScalarSizeInBits(), DestWidth);
925       if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold))) {
926         Value *NewTrunc = Builder.CreateTrunc(A, DestTy, A->getName() + ".tr");
927         return BinaryOperator::Create(Instruction::Shl, NewTrunc,
928                                       ConstantExpr::getTrunc(C, DestTy));
929       }
930     }
931   }
932 
933   if (Instruction *I = foldVecTruncToExtElt(Trunc, *this))
934     return I;
935 
936   // Whenever an element is extracted from a vector, and then truncated,
937   // canonicalize by converting it to a bitcast followed by an
938   // extractelement.
939   //
940   // Example (little endian):
941   //   trunc (extractelement <4 x i64> %X, 0) to i32
942   //   --->
943   //   extractelement <8 x i32> (bitcast <4 x i64> %X to <8 x i32>), i32 0
944   Value *VecOp;
945   ConstantInt *Cst;
946   if (match(Src, m_OneUse(m_ExtractElt(m_Value(VecOp), m_ConstantInt(Cst))))) {
947     auto *VecOpTy = cast<VectorType>(VecOp->getType());
948     auto VecElts = VecOpTy->getElementCount();
949 
950     // A badly fit destination size would result in an invalid cast.
951     if (SrcWidth % DestWidth == 0) {
952       uint64_t TruncRatio = SrcWidth / DestWidth;
953       uint64_t BitCastNumElts = VecElts.getKnownMinValue() * TruncRatio;
954       uint64_t VecOpIdx = Cst->getZExtValue();
955       uint64_t NewIdx = DL.isBigEndian() ? (VecOpIdx + 1) * TruncRatio - 1
956                                          : VecOpIdx * TruncRatio;
957       assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() &&
958              "overflow 32-bits");
959 
960       auto *BitCastTo =
961           VectorType::get(DestTy, BitCastNumElts, VecElts.isScalable());
962       Value *BitCast = Builder.CreateBitCast(VecOp, BitCastTo);
963       return ExtractElementInst::Create(BitCast, Builder.getInt32(NewIdx));
964     }
965   }
966 
967   // trunc (ctlz_i32(zext(A), B) --> add(ctlz_i16(A, B), C)
968   if (match(Src, m_OneUse(m_Intrinsic<Intrinsic::ctlz>(m_ZExt(m_Value(A)),
969                                                        m_Value(B))))) {
970     unsigned AWidth = A->getType()->getScalarSizeInBits();
971     if (AWidth == DestWidth && AWidth > Log2_32(SrcWidth)) {
972       Value *WidthDiff = ConstantInt::get(A->getType(), SrcWidth - AWidth);
973       Value *NarrowCtlz =
974           Builder.CreateIntrinsic(Intrinsic::ctlz, {Trunc.getType()}, {A, B});
975       return BinaryOperator::CreateAdd(NarrowCtlz, WidthDiff);
976     }
977   }
978 
979   if (match(Src, m_VScale(DL))) {
980     if (Trunc.getFunction() &&
981         Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
982       Attribute Attr =
983           Trunc.getFunction()->getFnAttribute(Attribute::VScaleRange);
984       if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
985         if (Log2_32(*MaxVScale) < DestWidth) {
986           Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
987           return replaceInstUsesWith(Trunc, VScale);
988         }
989       }
990     }
991   }
992 
993   return nullptr;
994 }
995 
996 Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext) {
997   // If we are just checking for a icmp eq of a single bit and zext'ing it
998   // to an integer, then shift the bit to the appropriate place and then
999   // cast to integer to avoid the comparison.
1000 
1001   // FIXME: This set of transforms does not check for extra uses and/or creates
1002   //        an extra instruction (an optional final cast is not included
1003   //        in the transform comments). We may also want to favor icmp over
1004   //        shifts in cases of equal instructions because icmp has better
1005   //        analysis in general (invert the transform).
1006 
1007   const APInt *Op1CV;
1008   if (match(Cmp->getOperand(1), m_APInt(Op1CV))) {
1009 
1010     // zext (x <s  0) to i32 --> x>>u31      true if signbit set.
1011     if (Cmp->getPredicate() == ICmpInst::ICMP_SLT && Op1CV->isZero()) {
1012       Value *In = Cmp->getOperand(0);
1013       Value *Sh = ConstantInt::get(In->getType(),
1014                                    In->getType()->getScalarSizeInBits() - 1);
1015       In = Builder.CreateLShr(In, Sh, In->getName() + ".lobit");
1016       if (In->getType() != Zext.getType())
1017         In = Builder.CreateIntCast(In, Zext.getType(), false /*ZExt*/);
1018 
1019       return replaceInstUsesWith(Zext, In);
1020     }
1021 
1022     // zext (X == 0) to i32 --> X^1      iff X has only the low bit set.
1023     // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
1024     // zext (X != 0) to i32 --> X        iff X has only the low bit set.
1025     // zext (X != 0) to i32 --> X>>1     iff X has only the 2nd bit set.
1026     if (Op1CV->isZero() && Cmp->isEquality()) {
1027       // If Op1C some other power of two, convert:
1028       KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
1029 
1030       APInt KnownZeroMask(~Known.Zero);
1031       if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
1032         bool isNE = Cmp->getPredicate() == ICmpInst::ICMP_NE;
1033         uint32_t ShAmt = KnownZeroMask.logBase2();
1034         Value *In = Cmp->getOperand(0);
1035         if (ShAmt) {
1036           // Perform a logical shr by shiftamt.
1037           // Insert the shift to put the result in the low bit.
1038           In = Builder.CreateLShr(In, ConstantInt::get(In->getType(), ShAmt),
1039                                   In->getName() + ".lobit");
1040         }
1041 
1042         if (!isNE) { // Toggle the low bit.
1043           Constant *One = ConstantInt::get(In->getType(), 1);
1044           In = Builder.CreateXor(In, One);
1045         }
1046 
1047         if (Zext.getType() == In->getType())
1048           return replaceInstUsesWith(Zext, In);
1049 
1050         Value *IntCast = Builder.CreateIntCast(In, Zext.getType(), false);
1051         return replaceInstUsesWith(Zext, IntCast);
1052       }
1053     }
1054   }
1055 
1056   if (Cmp->isEquality() && Zext.getType() == Cmp->getOperand(0)->getType()) {
1057     // Test if a bit is clear/set using a shifted-one mask:
1058     // zext (icmp eq (and X, (1 << ShAmt)), 0) --> and (lshr (not X), ShAmt), 1
1059     // zext (icmp ne (and X, (1 << ShAmt)), 0) --> and (lshr X, ShAmt), 1
1060     Value *X, *ShAmt;
1061     if (Cmp->hasOneUse() && match(Cmp->getOperand(1), m_ZeroInt()) &&
1062         match(Cmp->getOperand(0),
1063               m_OneUse(m_c_And(m_Shl(m_One(), m_Value(ShAmt)), m_Value(X))))) {
1064       if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
1065         X = Builder.CreateNot(X);
1066       Value *Lshr = Builder.CreateLShr(X, ShAmt);
1067       Value *And1 = Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1));
1068       return replaceInstUsesWith(Zext, And1);
1069     }
1070 
1071     // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
1072     // It is also profitable to transform icmp eq into not(xor(A, B)) because
1073     // that may lead to additional simplifications.
1074     if (IntegerType *ITy = dyn_cast<IntegerType>(Zext.getType())) {
1075       Value *LHS = Cmp->getOperand(0);
1076       Value *RHS = Cmp->getOperand(1);
1077 
1078       KnownBits KnownLHS = computeKnownBits(LHS, 0, &Zext);
1079       KnownBits KnownRHS = computeKnownBits(RHS, 0, &Zext);
1080 
1081       if (KnownLHS == KnownRHS) {
1082         APInt KnownBits = KnownLHS.Zero | KnownLHS.One;
1083         APInt UnknownBit = ~KnownBits;
1084         if (UnknownBit.countPopulation() == 1) {
1085           Value *Result = Builder.CreateXor(LHS, RHS);
1086 
1087           // Mask off any bits that are set and won't be shifted away.
1088           if (KnownLHS.One.uge(UnknownBit))
1089             Result = Builder.CreateAnd(Result,
1090                                         ConstantInt::get(ITy, UnknownBit));
1091 
1092           // Shift the bit we're testing down to the lsb.
1093           Result = Builder.CreateLShr(
1094                Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
1095 
1096           if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
1097             Result = Builder.CreateXor(Result, ConstantInt::get(ITy, 1));
1098           Result->takeName(Cmp);
1099           return replaceInstUsesWith(Zext, Result);
1100         }
1101       }
1102     }
1103   }
1104 
1105   return nullptr;
1106 }
1107 
1108 /// Determine if the specified value can be computed in the specified wider type
1109 /// and produce the same low bits. If not, return false.
1110 ///
1111 /// If this function returns true, it can also return a non-zero number of bits
1112 /// (in BitsToClear) which indicates that the value it computes is correct for
1113 /// the zero extend, but that the additional BitsToClear bits need to be zero'd
1114 /// out.  For example, to promote something like:
1115 ///
1116 ///   %B = trunc i64 %A to i32
1117 ///   %C = lshr i32 %B, 8
1118 ///   %E = zext i32 %C to i64
1119 ///
1120 /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be
1121 /// set to 8 to indicate that the promoted value needs to have bits 24-31
1122 /// cleared in addition to bits 32-63.  Since an 'and' will be generated to
1123 /// clear the top bits anyway, doing this has no extra cost.
1124 ///
1125 /// This function works on both vectors and scalars.
1126 static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear,
1127                              InstCombinerImpl &IC, Instruction *CxtI) {
1128   BitsToClear = 0;
1129   if (canAlwaysEvaluateInType(V, Ty))
1130     return true;
1131   if (canNotEvaluateInType(V, Ty))
1132     return false;
1133 
1134   auto *I = cast<Instruction>(V);
1135   unsigned Tmp;
1136   switch (I->getOpcode()) {
1137   case Instruction::ZExt:  // zext(zext(x)) -> zext(x).
1138   case Instruction::SExt:  // zext(sext(x)) -> sext(x).
1139   case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x)
1140     return true;
1141   case Instruction::And:
1142   case Instruction::Or:
1143   case Instruction::Xor:
1144   case Instruction::Add:
1145   case Instruction::Sub:
1146   case Instruction::Mul:
1147     if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI) ||
1148         !canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI))
1149       return false;
1150     // These can all be promoted if neither operand has 'bits to clear'.
1151     if (BitsToClear == 0 && Tmp == 0)
1152       return true;
1153 
1154     // If the operation is an AND/OR/XOR and the bits to clear are zero in the
1155     // other side, BitsToClear is ok.
1156     if (Tmp == 0 && I->isBitwiseLogicOp()) {
1157       // We use MaskedValueIsZero here for generality, but the case we care
1158       // about the most is constant RHS.
1159       unsigned VSize = V->getType()->getScalarSizeInBits();
1160       if (IC.MaskedValueIsZero(I->getOperand(1),
1161                                APInt::getHighBitsSet(VSize, BitsToClear),
1162                                0, CxtI)) {
1163         // If this is an And instruction and all of the BitsToClear are
1164         // known to be zero we can reset BitsToClear.
1165         if (I->getOpcode() == Instruction::And)
1166           BitsToClear = 0;
1167         return true;
1168       }
1169     }
1170 
1171     // Otherwise, we don't know how to analyze this BitsToClear case yet.
1172     return false;
1173 
1174   case Instruction::Shl: {
1175     // We can promote shl(x, cst) if we can promote x.  Since shl overwrites the
1176     // upper bits we can reduce BitsToClear by the shift amount.
1177     const APInt *Amt;
1178     if (match(I->getOperand(1), m_APInt(Amt))) {
1179       if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
1180         return false;
1181       uint64_t ShiftAmt = Amt->getZExtValue();
1182       BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
1183       return true;
1184     }
1185     return false;
1186   }
1187   case Instruction::LShr: {
1188     // We can promote lshr(x, cst) if we can promote x.  This requires the
1189     // ultimate 'and' to clear out the high zero bits we're clearing out though.
1190     const APInt *Amt;
1191     if (match(I->getOperand(1), m_APInt(Amt))) {
1192       if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
1193         return false;
1194       BitsToClear += Amt->getZExtValue();
1195       if (BitsToClear > V->getType()->getScalarSizeInBits())
1196         BitsToClear = V->getType()->getScalarSizeInBits();
1197       return true;
1198     }
1199     // Cannot promote variable LSHR.
1200     return false;
1201   }
1202   case Instruction::Select:
1203     if (!canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI) ||
1204         !canEvaluateZExtd(I->getOperand(2), Ty, BitsToClear, IC, CxtI) ||
1205         // TODO: If important, we could handle the case when the BitsToClear are
1206         // known zero in the disagreeing side.
1207         Tmp != BitsToClear)
1208       return false;
1209     return true;
1210 
1211   case Instruction::PHI: {
1212     // We can change a phi if we can change all operands.  Note that we never
1213     // get into trouble with cyclic PHIs here because we only consider
1214     // instructions with a single use.
1215     PHINode *PN = cast<PHINode>(I);
1216     if (!canEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear, IC, CxtI))
1217       return false;
1218     for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
1219       if (!canEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp, IC, CxtI) ||
1220           // TODO: If important, we could handle the case when the BitsToClear
1221           // are known zero in the disagreeing input.
1222           Tmp != BitsToClear)
1223         return false;
1224     return true;
1225   }
1226   default:
1227     // TODO: Can handle more cases here.
1228     return false;
1229   }
1230 }
1231 
1232 Instruction *InstCombinerImpl::visitZExt(ZExtInst &CI) {
1233   // If this zero extend is only used by a truncate, let the truncate be
1234   // eliminated before we try to optimize this zext.
1235   if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
1236     return nullptr;
1237 
1238   // If one of the common conversion will work, do it.
1239   if (Instruction *Result = commonCastTransforms(CI))
1240     return Result;
1241 
1242   Value *Src = CI.getOperand(0);
1243   Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1244 
1245   // Try to extend the entire expression tree to the wide destination type.
1246   unsigned BitsToClear;
1247   if (shouldChangeType(SrcTy, DestTy) &&
1248       canEvaluateZExtd(Src, DestTy, BitsToClear, *this, &CI)) {
1249     assert(BitsToClear <= SrcTy->getScalarSizeInBits() &&
1250            "Can't clear more bits than in SrcTy");
1251 
1252     // Okay, we can transform this!  Insert the new expression now.
1253     LLVM_DEBUG(
1254         dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1255                   " to avoid zero extend: "
1256                << CI << '\n');
1257     Value *Res = EvaluateInDifferentType(Src, DestTy, false);
1258     assert(Res->getType() == DestTy);
1259 
1260     // Preserve debug values referring to Src if the zext is its last use.
1261     if (auto *SrcOp = dyn_cast<Instruction>(Src))
1262       if (SrcOp->hasOneUse())
1263         replaceAllDbgUsesWith(*SrcOp, *Res, CI, DT);
1264 
1265     uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
1266     uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1267 
1268     // If the high bits are already filled with zeros, just replace this
1269     // cast with the result.
1270     if (MaskedValueIsZero(Res,
1271                           APInt::getHighBitsSet(DestBitSize,
1272                                                 DestBitSize-SrcBitsKept),
1273                              0, &CI))
1274       return replaceInstUsesWith(CI, Res);
1275 
1276     // We need to emit an AND to clear the high bits.
1277     Constant *C = ConstantInt::get(Res->getType(),
1278                                APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
1279     return BinaryOperator::CreateAnd(Res, C);
1280   }
1281 
1282   // If this is a TRUNC followed by a ZEXT then we are dealing with integral
1283   // types and if the sizes are just right we can convert this into a logical
1284   // 'and' which will be much cheaper than the pair of casts.
1285   if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
1286     // TODO: Subsume this into EvaluateInDifferentType.
1287 
1288     // Get the sizes of the types involved.  We know that the intermediate type
1289     // will be smaller than A or C, but don't know the relation between A and C.
1290     Value *A = CSrc->getOperand(0);
1291     unsigned SrcSize = A->getType()->getScalarSizeInBits();
1292     unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
1293     unsigned DstSize = CI.getType()->getScalarSizeInBits();
1294     // If we're actually extending zero bits, then if
1295     // SrcSize <  DstSize: zext(a & mask)
1296     // SrcSize == DstSize: a & mask
1297     // SrcSize  > DstSize: trunc(a) & mask
1298     if (SrcSize < DstSize) {
1299       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
1300       Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
1301       Value *And = Builder.CreateAnd(A, AndConst, CSrc->getName() + ".mask");
1302       return new ZExtInst(And, CI.getType());
1303     }
1304 
1305     if (SrcSize == DstSize) {
1306       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
1307       return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
1308                                                            AndValue));
1309     }
1310     if (SrcSize > DstSize) {
1311       Value *Trunc = Builder.CreateTrunc(A, CI.getType());
1312       APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
1313       return BinaryOperator::CreateAnd(Trunc,
1314                                        ConstantInt::get(Trunc->getType(),
1315                                                         AndValue));
1316     }
1317   }
1318 
1319   if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Src))
1320     return transformZExtICmp(Cmp, CI);
1321 
1322   // zext(trunc(X) & C) -> (X & zext(C)).
1323   Constant *C;
1324   Value *X;
1325   if (match(Src, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) &&
1326       X->getType() == CI.getType())
1327     return BinaryOperator::CreateAnd(X, ConstantExpr::getZExt(C, CI.getType()));
1328 
1329   // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)).
1330   Value *And;
1331   if (match(Src, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) &&
1332       match(And, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Specific(C)))) &&
1333       X->getType() == CI.getType()) {
1334     Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
1335     return BinaryOperator::CreateXor(Builder.CreateAnd(X, ZC), ZC);
1336   }
1337 
1338   // If we are truncating, masking, and then zexting back to the original type,
1339   // that's just a mask. This is not handled by canEvaluateZextd if the
1340   // intermediate values have extra uses. This could be generalized further for
1341   // a non-constant mask operand.
1342   // zext (and (trunc X), C) --> and X, (zext C)
1343   if (match(Src, m_And(m_Trunc(m_Value(X)), m_Constant(C))) &&
1344       X->getType() == DestTy) {
1345     Constant *ZextC = ConstantExpr::getZExt(C, DestTy);
1346     return BinaryOperator::CreateAnd(X, ZextC);
1347   }
1348 
1349   if (match(Src, m_VScale(DL))) {
1350     if (CI.getFunction() &&
1351         CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1352       Attribute Attr = CI.getFunction()->getFnAttribute(Attribute::VScaleRange);
1353       if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
1354         unsigned TypeWidth = Src->getType()->getScalarSizeInBits();
1355         if (Log2_32(*MaxVScale) < TypeWidth) {
1356           Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
1357           return replaceInstUsesWith(CI, VScale);
1358         }
1359       }
1360     }
1361   }
1362 
1363   return nullptr;
1364 }
1365 
1366 /// Transform (sext icmp) to bitwise / integer operations to eliminate the icmp.
1367 Instruction *InstCombinerImpl::transformSExtICmp(ICmpInst *ICI,
1368                                                  Instruction &CI) {
1369   Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1);
1370   ICmpInst::Predicate Pred = ICI->getPredicate();
1371 
1372   // Don't bother if Op1 isn't of vector or integer type.
1373   if (!Op1->getType()->isIntOrIntVectorTy())
1374     return nullptr;
1375 
1376   if ((Pred == ICmpInst::ICMP_SLT && match(Op1, m_ZeroInt())) ||
1377       (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))) {
1378     // (x <s  0) ? -1 : 0 -> ashr x, 31        -> all ones if negative
1379     // (x >s -1) ? -1 : 0 -> not (ashr x, 31)  -> all ones if positive
1380     Value *Sh = ConstantInt::get(Op0->getType(),
1381                                  Op0->getType()->getScalarSizeInBits() - 1);
1382     Value *In = Builder.CreateAShr(Op0, Sh, Op0->getName() + ".lobit");
1383     if (In->getType() != CI.getType())
1384       In = Builder.CreateIntCast(In, CI.getType(), true /*SExt*/);
1385 
1386     if (Pred == ICmpInst::ICMP_SGT)
1387       In = Builder.CreateNot(In, In->getName() + ".not");
1388     return replaceInstUsesWith(CI, In);
1389   }
1390 
1391   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1392     // If we know that only one bit of the LHS of the icmp can be set and we
1393     // have an equality comparison with zero or a power of 2, we can transform
1394     // the icmp and sext into bitwise/integer operations.
1395     if (ICI->hasOneUse() &&
1396         ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
1397       KnownBits Known = computeKnownBits(Op0, 0, &CI);
1398 
1399       APInt KnownZeroMask(~Known.Zero);
1400       if (KnownZeroMask.isPowerOf2()) {
1401         Value *In = ICI->getOperand(0);
1402 
1403         // If the icmp tests for a known zero bit we can constant fold it.
1404         if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
1405           Value *V = Pred == ICmpInst::ICMP_NE ?
1406                        ConstantInt::getAllOnesValue(CI.getType()) :
1407                        ConstantInt::getNullValue(CI.getType());
1408           return replaceInstUsesWith(CI, V);
1409         }
1410 
1411         if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
1412           // sext ((x & 2^n) == 0)   -> (x >> n) - 1
1413           // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
1414           unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
1415           // Perform a right shift to place the desired bit in the LSB.
1416           if (ShiftAmt)
1417             In = Builder.CreateLShr(In,
1418                                     ConstantInt::get(In->getType(), ShiftAmt));
1419 
1420           // At this point "In" is either 1 or 0. Subtract 1 to turn
1421           // {1, 0} -> {0, -1}.
1422           In = Builder.CreateAdd(In,
1423                                  ConstantInt::getAllOnesValue(In->getType()),
1424                                  "sext");
1425         } else {
1426           // sext ((x & 2^n) != 0)   -> (x << bitwidth-n) a>> bitwidth-1
1427           // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
1428           unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
1429           // Perform a left shift to place the desired bit in the MSB.
1430           if (ShiftAmt)
1431             In = Builder.CreateShl(In,
1432                                    ConstantInt::get(In->getType(), ShiftAmt));
1433 
1434           // Distribute the bit over the whole bit width.
1435           In = Builder.CreateAShr(In, ConstantInt::get(In->getType(),
1436                                   KnownZeroMask.getBitWidth() - 1), "sext");
1437         }
1438 
1439         if (CI.getType() == In->getType())
1440           return replaceInstUsesWith(CI, In);
1441         return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/);
1442       }
1443     }
1444   }
1445 
1446   return nullptr;
1447 }
1448 
1449 /// Return true if we can take the specified value and return it as type Ty
1450 /// without inserting any new casts and without changing the value of the common
1451 /// low bits.  This is used by code that tries to promote integer operations to
1452 /// a wider types will allow us to eliminate the extension.
1453 ///
1454 /// This function works on both vectors and scalars.
1455 ///
1456 static bool canEvaluateSExtd(Value *V, Type *Ty) {
1457   assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
1458          "Can't sign extend type to a smaller type");
1459   if (canAlwaysEvaluateInType(V, Ty))
1460     return true;
1461   if (canNotEvaluateInType(V, Ty))
1462     return false;
1463 
1464   auto *I = cast<Instruction>(V);
1465   switch (I->getOpcode()) {
1466   case Instruction::SExt:  // sext(sext(x)) -> sext(x)
1467   case Instruction::ZExt:  // sext(zext(x)) -> zext(x)
1468   case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
1469     return true;
1470   case Instruction::And:
1471   case Instruction::Or:
1472   case Instruction::Xor:
1473   case Instruction::Add:
1474   case Instruction::Sub:
1475   case Instruction::Mul:
1476     // These operators can all arbitrarily be extended if their inputs can.
1477     return canEvaluateSExtd(I->getOperand(0), Ty) &&
1478            canEvaluateSExtd(I->getOperand(1), Ty);
1479 
1480   //case Instruction::Shl:   TODO
1481   //case Instruction::LShr:  TODO
1482 
1483   case Instruction::Select:
1484     return canEvaluateSExtd(I->getOperand(1), Ty) &&
1485            canEvaluateSExtd(I->getOperand(2), Ty);
1486 
1487   case Instruction::PHI: {
1488     // We can change a phi if we can change all operands.  Note that we never
1489     // get into trouble with cyclic PHIs here because we only consider
1490     // instructions with a single use.
1491     PHINode *PN = cast<PHINode>(I);
1492     for (Value *IncValue : PN->incoming_values())
1493       if (!canEvaluateSExtd(IncValue, Ty)) return false;
1494     return true;
1495   }
1496   default:
1497     // TODO: Can handle more cases here.
1498     break;
1499   }
1500 
1501   return false;
1502 }
1503 
1504 Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) {
1505   // If this sign extend is only used by a truncate, let the truncate be
1506   // eliminated before we try to optimize this sext.
1507   if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
1508     return nullptr;
1509 
1510   if (Instruction *I = commonCastTransforms(CI))
1511     return I;
1512 
1513   Value *Src = CI.getOperand(0);
1514   Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1515   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1516   unsigned DestBitSize = DestTy->getScalarSizeInBits();
1517 
1518   // If the value being extended is zero or positive, use a zext instead.
1519   if (isKnownNonNegative(Src, DL, 0, &AC, &CI, &DT))
1520     return CastInst::Create(Instruction::ZExt, Src, DestTy);
1521 
1522   // Try to extend the entire expression tree to the wide destination type.
1523   if (shouldChangeType(SrcTy, DestTy) && canEvaluateSExtd(Src, DestTy)) {
1524     // Okay, we can transform this!  Insert the new expression now.
1525     LLVM_DEBUG(
1526         dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1527                   " to avoid sign extend: "
1528                << CI << '\n');
1529     Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1530     assert(Res->getType() == DestTy);
1531 
1532     // If the high bits are already filled with sign bit, just replace this
1533     // cast with the result.
1534     if (ComputeNumSignBits(Res, 0, &CI) > DestBitSize - SrcBitSize)
1535       return replaceInstUsesWith(CI, Res);
1536 
1537     // We need to emit a shl + ashr to do the sign extend.
1538     Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1539     return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt, "sext"),
1540                                       ShAmt);
1541   }
1542 
1543   Value *X;
1544   if (match(Src, m_Trunc(m_Value(X)))) {
1545     // If the input has more sign bits than bits truncated, then convert
1546     // directly to final type.
1547     unsigned XBitSize = X->getType()->getScalarSizeInBits();
1548     if (ComputeNumSignBits(X, 0, &CI) > XBitSize - SrcBitSize)
1549       return CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true);
1550 
1551     // If input is a trunc from the destination type, then convert into shifts.
1552     if (Src->hasOneUse() && X->getType() == DestTy) {
1553       // sext (trunc X) --> ashr (shl X, C), C
1554       Constant *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
1555       return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt);
1556     }
1557 
1558     // If we are replacing shifted-in high zero bits with sign bits, convert
1559     // the logic shift to arithmetic shift and eliminate the cast to
1560     // intermediate type:
1561     // sext (trunc (lshr Y, C)) --> sext/trunc (ashr Y, C)
1562     Value *Y;
1563     if (Src->hasOneUse() &&
1564         match(X, m_LShr(m_Value(Y),
1565                         m_SpecificIntAllowUndef(XBitSize - SrcBitSize)))) {
1566       Value *Ashr = Builder.CreateAShr(Y, XBitSize - SrcBitSize);
1567       return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true);
1568     }
1569   }
1570 
1571   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1572     return transformSExtICmp(ICI, CI);
1573 
1574   // If the input is a shl/ashr pair of a same constant, then this is a sign
1575   // extension from a smaller value.  If we could trust arbitrary bitwidth
1576   // integers, we could turn this into a truncate to the smaller bit and then
1577   // use a sext for the whole extension.  Since we don't, look deeper and check
1578   // for a truncate.  If the source and dest are the same type, eliminate the
1579   // trunc and extend and just do shifts.  For example, turn:
1580   //   %a = trunc i32 %i to i8
1581   //   %b = shl i8 %a, C
1582   //   %c = ashr i8 %b, C
1583   //   %d = sext i8 %c to i32
1584   // into:
1585   //   %a = shl i32 %i, 32-(8-C)
1586   //   %d = ashr i32 %a, 32-(8-C)
1587   Value *A = nullptr;
1588   // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
1589   Constant *BA = nullptr, *CA = nullptr;
1590   if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_Constant(BA)),
1591                         m_Constant(CA))) &&
1592       BA->isElementWiseEqual(CA) && A->getType() == DestTy) {
1593     Constant *WideCurrShAmt = ConstantExpr::getSExt(CA, DestTy);
1594     Constant *NumLowbitsLeft = ConstantExpr::getSub(
1595         ConstantInt::get(DestTy, SrcTy->getScalarSizeInBits()), WideCurrShAmt);
1596     Constant *NewShAmt = ConstantExpr::getSub(
1597         ConstantInt::get(DestTy, DestTy->getScalarSizeInBits()),
1598         NumLowbitsLeft);
1599     NewShAmt =
1600         Constant::mergeUndefsWith(Constant::mergeUndefsWith(NewShAmt, BA), CA);
1601     A = Builder.CreateShl(A, NewShAmt, CI.getName());
1602     return BinaryOperator::CreateAShr(A, NewShAmt);
1603   }
1604 
1605   // Splatting a bit of constant-index across a value:
1606   // sext (ashr (trunc iN X to iM), M-1) to iN --> ashr (shl X, N-M), N-1
1607   // If the dest type is different, use a cast (adjust use check).
1608   if (match(Src, m_OneUse(m_AShr(m_Trunc(m_Value(X)),
1609                                  m_SpecificInt(SrcBitSize - 1))))) {
1610     Type *XTy = X->getType();
1611     unsigned XBitSize = XTy->getScalarSizeInBits();
1612     Constant *ShlAmtC = ConstantInt::get(XTy, XBitSize - SrcBitSize);
1613     Constant *AshrAmtC = ConstantInt::get(XTy, XBitSize - 1);
1614     if (XTy == DestTy)
1615       return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShlAmtC),
1616                                         AshrAmtC);
1617     if (cast<BinaryOperator>(Src)->getOperand(0)->hasOneUse()) {
1618       Value *Ashr = Builder.CreateAShr(Builder.CreateShl(X, ShlAmtC), AshrAmtC);
1619       return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true);
1620     }
1621   }
1622 
1623   if (match(Src, m_VScale(DL))) {
1624     if (CI.getFunction() &&
1625         CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1626       Attribute Attr = CI.getFunction()->getFnAttribute(Attribute::VScaleRange);
1627       if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
1628         if (Log2_32(*MaxVScale) < (SrcBitSize - 1)) {
1629           Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
1630           return replaceInstUsesWith(CI, VScale);
1631         }
1632       }
1633     }
1634   }
1635 
1636   return nullptr;
1637 }
1638 
1639 /// Return a Constant* for the specified floating-point constant if it fits
1640 /// in the specified FP type without changing its value.
1641 static bool fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1642   bool losesInfo;
1643   APFloat F = CFP->getValueAPF();
1644   (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1645   return !losesInfo;
1646 }
1647 
1648 static Type *shrinkFPConstant(ConstantFP *CFP) {
1649   if (CFP->getType() == Type::getPPC_FP128Ty(CFP->getContext()))
1650     return nullptr;  // No constant folding of this.
1651   // See if the value can be truncated to half and then reextended.
1652   if (fitsInFPType(CFP, APFloat::IEEEhalf()))
1653     return Type::getHalfTy(CFP->getContext());
1654   // See if the value can be truncated to float and then reextended.
1655   if (fitsInFPType(CFP, APFloat::IEEEsingle()))
1656     return Type::getFloatTy(CFP->getContext());
1657   if (CFP->getType()->isDoubleTy())
1658     return nullptr;  // Won't shrink.
1659   if (fitsInFPType(CFP, APFloat::IEEEdouble()))
1660     return Type::getDoubleTy(CFP->getContext());
1661   // Don't try to shrink to various long double types.
1662   return nullptr;
1663 }
1664 
1665 // Determine if this is a vector of ConstantFPs and if so, return the minimal
1666 // type we can safely truncate all elements to.
1667 static Type *shrinkFPConstantVector(Value *V) {
1668   auto *CV = dyn_cast<Constant>(V);
1669   auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
1670   if (!CV || !CVVTy)
1671     return nullptr;
1672 
1673   Type *MinType = nullptr;
1674 
1675   unsigned NumElts = CVVTy->getNumElements();
1676 
1677   // For fixed-width vectors we find the minimal type by looking
1678   // through the constant values of the vector.
1679   for (unsigned i = 0; i != NumElts; ++i) {
1680     if (isa<UndefValue>(CV->getAggregateElement(i)))
1681       continue;
1682 
1683     auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
1684     if (!CFP)
1685       return nullptr;
1686 
1687     Type *T = shrinkFPConstant(CFP);
1688     if (!T)
1689       return nullptr;
1690 
1691     // If we haven't found a type yet or this type has a larger mantissa than
1692     // our previous type, this is our new minimal type.
1693     if (!MinType || T->getFPMantissaWidth() > MinType->getFPMantissaWidth())
1694       MinType = T;
1695   }
1696 
1697   // Make a vector type from the minimal type.
1698   return MinType ? FixedVectorType::get(MinType, NumElts) : nullptr;
1699 }
1700 
1701 /// Find the minimum FP type we can safely truncate to.
1702 static Type *getMinimumFPType(Value *V) {
1703   if (auto *FPExt = dyn_cast<FPExtInst>(V))
1704     return FPExt->getOperand(0)->getType();
1705 
1706   // If this value is a constant, return the constant in the smallest FP type
1707   // that can accurately represent it.  This allows us to turn
1708   // (float)((double)X+2.0) into x+2.0f.
1709   if (auto *CFP = dyn_cast<ConstantFP>(V))
1710     if (Type *T = shrinkFPConstant(CFP))
1711       return T;
1712 
1713   // We can only correctly find a minimum type for a scalable vector when it is
1714   // a splat. For splats of constant values the fpext is wrapped up as a
1715   // ConstantExpr.
1716   if (auto *FPCExt = dyn_cast<ConstantExpr>(V))
1717     if (FPCExt->getOpcode() == Instruction::FPExt)
1718       return FPCExt->getOperand(0)->getType();
1719 
1720   // Try to shrink a vector of FP constants. This returns nullptr on scalable
1721   // vectors
1722   if (Type *T = shrinkFPConstantVector(V))
1723     return T;
1724 
1725   return V->getType();
1726 }
1727 
1728 /// Return true if the cast from integer to FP can be proven to be exact for all
1729 /// possible inputs (the conversion does not lose any precision).
1730 static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) {
1731   CastInst::CastOps Opcode = I.getOpcode();
1732   assert((Opcode == CastInst::SIToFP || Opcode == CastInst::UIToFP) &&
1733          "Unexpected cast");
1734   Value *Src = I.getOperand(0);
1735   Type *SrcTy = Src->getType();
1736   Type *FPTy = I.getType();
1737   bool IsSigned = Opcode == Instruction::SIToFP;
1738   int SrcSize = (int)SrcTy->getScalarSizeInBits() - IsSigned;
1739 
1740   // Easy case - if the source integer type has less bits than the FP mantissa,
1741   // then the cast must be exact.
1742   int DestNumSigBits = FPTy->getFPMantissaWidth();
1743   if (SrcSize <= DestNumSigBits)
1744     return true;
1745 
1746   // Cast from FP to integer and back to FP is independent of the intermediate
1747   // integer width because of poison on overflow.
1748   Value *F;
1749   if (match(Src, m_FPToSI(m_Value(F))) || match(Src, m_FPToUI(m_Value(F)))) {
1750     // If this is uitofp (fptosi F), the source needs an extra bit to avoid
1751     // potential rounding of negative FP input values.
1752     int SrcNumSigBits = F->getType()->getFPMantissaWidth();
1753     if (!IsSigned && match(Src, m_FPToSI(m_Value())))
1754       SrcNumSigBits++;
1755 
1756     // [su]itofp (fpto[su]i F) --> exact if the source type has less or equal
1757     // significant bits than the destination (and make sure neither type is
1758     // weird -- ppc_fp128).
1759     if (SrcNumSigBits > 0 && DestNumSigBits > 0 &&
1760         SrcNumSigBits <= DestNumSigBits)
1761       return true;
1762   }
1763 
1764   // TODO:
1765   // Try harder to find if the source integer type has less significant bits.
1766   // For example, compute number of sign bits.
1767   KnownBits SrcKnown = IC.computeKnownBits(Src, 0, &I);
1768   int SigBits = (int)SrcTy->getScalarSizeInBits() -
1769                 SrcKnown.countMinLeadingZeros() -
1770                 SrcKnown.countMinTrailingZeros();
1771   if (SigBits <= DestNumSigBits)
1772     return true;
1773 
1774   return false;
1775 }
1776 
1777 Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
1778   if (Instruction *I = commonCastTransforms(FPT))
1779     return I;
1780 
1781   // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to
1782   // simplify this expression to avoid one or more of the trunc/extend
1783   // operations if we can do so without changing the numerical results.
1784   //
1785   // The exact manner in which the widths of the operands interact to limit
1786   // what we can and cannot do safely varies from operation to operation, and
1787   // is explained below in the various case statements.
1788   Type *Ty = FPT.getType();
1789   auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand(0));
1790   if (BO && BO->hasOneUse()) {
1791     Type *LHSMinType = getMinimumFPType(BO->getOperand(0));
1792     Type *RHSMinType = getMinimumFPType(BO->getOperand(1));
1793     unsigned OpWidth = BO->getType()->getFPMantissaWidth();
1794     unsigned LHSWidth = LHSMinType->getFPMantissaWidth();
1795     unsigned RHSWidth = RHSMinType->getFPMantissaWidth();
1796     unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1797     unsigned DstWidth = Ty->getFPMantissaWidth();
1798     switch (BO->getOpcode()) {
1799       default: break;
1800       case Instruction::FAdd:
1801       case Instruction::FSub:
1802         // For addition and subtraction, the infinitely precise result can
1803         // essentially be arbitrarily wide; proving that double rounding
1804         // will not occur because the result of OpI is exact (as we will for
1805         // FMul, for example) is hopeless.  However, we *can* nonetheless
1806         // frequently know that double rounding cannot occur (or that it is
1807         // innocuous) by taking advantage of the specific structure of
1808         // infinitely-precise results that admit double rounding.
1809         //
1810         // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient
1811         // to represent both sources, we can guarantee that the double
1812         // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis,
1813         // "A Rigorous Framework for Fully Supporting the IEEE Standard ..."
1814         // for proof of this fact).
1815         //
1816         // Note: Figueroa does not consider the case where DstFormat !=
1817         // SrcFormat.  It's possible (likely even!) that this analysis
1818         // could be tightened for those cases, but they are rare (the main
1819         // case of interest here is (float)((double)float + float)).
1820         if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1821           Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1822           Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1823           Instruction *RI = BinaryOperator::Create(BO->getOpcode(), LHS, RHS);
1824           RI->copyFastMathFlags(BO);
1825           return RI;
1826         }
1827         break;
1828       case Instruction::FMul:
1829         // For multiplication, the infinitely precise result has at most
1830         // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient
1831         // that such a value can be exactly represented, then no double
1832         // rounding can possibly occur; we can safely perform the operation
1833         // in the destination format if it can represent both sources.
1834         if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1835           Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1836           Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1837           return BinaryOperator::CreateFMulFMF(LHS, RHS, BO);
1838         }
1839         break;
1840       case Instruction::FDiv:
1841         // For division, we use again use the bound from Figueroa's
1842         // dissertation.  I am entirely certain that this bound can be
1843         // tightened in the unbalanced operand case by an analysis based on
1844         // the diophantine rational approximation bound, but the well-known
1845         // condition used here is a good conservative first pass.
1846         // TODO: Tighten bound via rigorous analysis of the unbalanced case.
1847         if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1848           Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1849           Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1850           return BinaryOperator::CreateFDivFMF(LHS, RHS, BO);
1851         }
1852         break;
1853       case Instruction::FRem: {
1854         // Remainder is straightforward.  Remainder is always exact, so the
1855         // type of OpI doesn't enter into things at all.  We simply evaluate
1856         // in whichever source type is larger, then convert to the
1857         // destination type.
1858         if (SrcWidth == OpWidth)
1859           break;
1860         Value *LHS, *RHS;
1861         if (LHSWidth == SrcWidth) {
1862            LHS = Builder.CreateFPTrunc(BO->getOperand(0), LHSMinType);
1863            RHS = Builder.CreateFPTrunc(BO->getOperand(1), LHSMinType);
1864         } else {
1865            LHS = Builder.CreateFPTrunc(BO->getOperand(0), RHSMinType);
1866            RHS = Builder.CreateFPTrunc(BO->getOperand(1), RHSMinType);
1867         }
1868 
1869         Value *ExactResult = Builder.CreateFRemFMF(LHS, RHS, BO);
1870         return CastInst::CreateFPCast(ExactResult, Ty);
1871       }
1872     }
1873   }
1874 
1875   // (fptrunc (fneg x)) -> (fneg (fptrunc x))
1876   Value *X;
1877   Instruction *Op = dyn_cast<Instruction>(FPT.getOperand(0));
1878   if (Op && Op->hasOneUse()) {
1879     // FIXME: The FMF should propagate from the fptrunc, not the source op.
1880     IRBuilder<>::FastMathFlagGuard FMFG(Builder);
1881     if (isa<FPMathOperator>(Op))
1882       Builder.setFastMathFlags(Op->getFastMathFlags());
1883 
1884     if (match(Op, m_FNeg(m_Value(X)))) {
1885       Value *InnerTrunc = Builder.CreateFPTrunc(X, Ty);
1886 
1887       return UnaryOperator::CreateFNegFMF(InnerTrunc, Op);
1888     }
1889 
1890     // If we are truncating a select that has an extended operand, we can
1891     // narrow the other operand and do the select as a narrow op.
1892     Value *Cond, *X, *Y;
1893     if (match(Op, m_Select(m_Value(Cond), m_FPExt(m_Value(X)), m_Value(Y))) &&
1894         X->getType() == Ty) {
1895       // fptrunc (select Cond, (fpext X), Y --> select Cond, X, (fptrunc Y)
1896       Value *NarrowY = Builder.CreateFPTrunc(Y, Ty);
1897       Value *Sel = Builder.CreateSelect(Cond, X, NarrowY, "narrow.sel", Op);
1898       return replaceInstUsesWith(FPT, Sel);
1899     }
1900     if (match(Op, m_Select(m_Value(Cond), m_Value(Y), m_FPExt(m_Value(X)))) &&
1901         X->getType() == Ty) {
1902       // fptrunc (select Cond, Y, (fpext X) --> select Cond, (fptrunc Y), X
1903       Value *NarrowY = Builder.CreateFPTrunc(Y, Ty);
1904       Value *Sel = Builder.CreateSelect(Cond, NarrowY, X, "narrow.sel", Op);
1905       return replaceInstUsesWith(FPT, Sel);
1906     }
1907   }
1908 
1909   if (auto *II = dyn_cast<IntrinsicInst>(FPT.getOperand(0))) {
1910     switch (II->getIntrinsicID()) {
1911     default: break;
1912     case Intrinsic::ceil:
1913     case Intrinsic::fabs:
1914     case Intrinsic::floor:
1915     case Intrinsic::nearbyint:
1916     case Intrinsic::rint:
1917     case Intrinsic::round:
1918     case Intrinsic::roundeven:
1919     case Intrinsic::trunc: {
1920       Value *Src = II->getArgOperand(0);
1921       if (!Src->hasOneUse())
1922         break;
1923 
1924       // Except for fabs, this transformation requires the input of the unary FP
1925       // operation to be itself an fpext from the type to which we're
1926       // truncating.
1927       if (II->getIntrinsicID() != Intrinsic::fabs) {
1928         FPExtInst *FPExtSrc = dyn_cast<FPExtInst>(Src);
1929         if (!FPExtSrc || FPExtSrc->getSrcTy() != Ty)
1930           break;
1931       }
1932 
1933       // Do unary FP operation on smaller type.
1934       // (fptrunc (fabs x)) -> (fabs (fptrunc x))
1935       Value *InnerTrunc = Builder.CreateFPTrunc(Src, Ty);
1936       Function *Overload = Intrinsic::getDeclaration(FPT.getModule(),
1937                                                      II->getIntrinsicID(), Ty);
1938       SmallVector<OperandBundleDef, 1> OpBundles;
1939       II->getOperandBundlesAsDefs(OpBundles);
1940       CallInst *NewCI =
1941           CallInst::Create(Overload, {InnerTrunc}, OpBundles, II->getName());
1942       NewCI->copyFastMathFlags(II);
1943       return NewCI;
1944     }
1945     }
1946   }
1947 
1948   if (Instruction *I = shrinkInsertElt(FPT, Builder))
1949     return I;
1950 
1951   Value *Src = FPT.getOperand(0);
1952   if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1953     auto *FPCast = cast<CastInst>(Src);
1954     if (isKnownExactCastIntToFP(*FPCast, *this))
1955       return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
1956   }
1957 
1958   return nullptr;
1959 }
1960 
1961 Instruction *InstCombinerImpl::visitFPExt(CastInst &FPExt) {
1962   // If the source operand is a cast from integer to FP and known exact, then
1963   // cast the integer operand directly to the destination type.
1964   Type *Ty = FPExt.getType();
1965   Value *Src = FPExt.getOperand(0);
1966   if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1967     auto *FPCast = cast<CastInst>(Src);
1968     if (isKnownExactCastIntToFP(*FPCast, *this))
1969       return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
1970   }
1971 
1972   return commonCastTransforms(FPExt);
1973 }
1974 
1975 /// fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X)
1976 /// This is safe if the intermediate type has enough bits in its mantissa to
1977 /// accurately represent all values of X.  For example, this won't work with
1978 /// i64 -> float -> i64.
1979 Instruction *InstCombinerImpl::foldItoFPtoI(CastInst &FI) {
1980   if (!isa<UIToFPInst>(FI.getOperand(0)) && !isa<SIToFPInst>(FI.getOperand(0)))
1981     return nullptr;
1982 
1983   auto *OpI = cast<CastInst>(FI.getOperand(0));
1984   Value *X = OpI->getOperand(0);
1985   Type *XType = X->getType();
1986   Type *DestType = FI.getType();
1987   bool IsOutputSigned = isa<FPToSIInst>(FI);
1988 
1989   // Since we can assume the conversion won't overflow, our decision as to
1990   // whether the input will fit in the float should depend on the minimum
1991   // of the input range and output range.
1992 
1993   // This means this is also safe for a signed input and unsigned output, since
1994   // a negative input would lead to undefined behavior.
1995   if (!isKnownExactCastIntToFP(*OpI, *this)) {
1996     // The first cast may not round exactly based on the source integer width
1997     // and FP width, but the overflow UB rules can still allow this to fold.
1998     // If the destination type is narrow, that means the intermediate FP value
1999     // must be large enough to hold the source value exactly.
2000     // For example, (uint8_t)((float)(uint32_t 16777217) is undefined behavior.
2001     int OutputSize = (int)DestType->getScalarSizeInBits();
2002     if (OutputSize > OpI->getType()->getFPMantissaWidth())
2003       return nullptr;
2004   }
2005 
2006   if (DestType->getScalarSizeInBits() > XType->getScalarSizeInBits()) {
2007     bool IsInputSigned = isa<SIToFPInst>(OpI);
2008     if (IsInputSigned && IsOutputSigned)
2009       return new SExtInst(X, DestType);
2010     return new ZExtInst(X, DestType);
2011   }
2012   if (DestType->getScalarSizeInBits() < XType->getScalarSizeInBits())
2013     return new TruncInst(X, DestType);
2014 
2015   assert(XType == DestType && "Unexpected types for int to FP to int casts");
2016   return replaceInstUsesWith(FI, X);
2017 }
2018 
2019 Instruction *InstCombinerImpl::visitFPToUI(FPToUIInst &FI) {
2020   if (Instruction *I = foldItoFPtoI(FI))
2021     return I;
2022 
2023   return commonCastTransforms(FI);
2024 }
2025 
2026 Instruction *InstCombinerImpl::visitFPToSI(FPToSIInst &FI) {
2027   if (Instruction *I = foldItoFPtoI(FI))
2028     return I;
2029 
2030   return commonCastTransforms(FI);
2031 }
2032 
2033 Instruction *InstCombinerImpl::visitUIToFP(CastInst &CI) {
2034   return commonCastTransforms(CI);
2035 }
2036 
2037 Instruction *InstCombinerImpl::visitSIToFP(CastInst &CI) {
2038   return commonCastTransforms(CI);
2039 }
2040 
2041 Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) {
2042   // If the source integer type is not the intptr_t type for this target, do a
2043   // trunc or zext to the intptr_t type, then inttoptr of it.  This allows the
2044   // cast to be exposed to other transforms.
2045   unsigned AS = CI.getAddressSpace();
2046   if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
2047       DL.getPointerSizeInBits(AS)) {
2048     Type *Ty = CI.getOperand(0)->getType()->getWithNewType(
2049         DL.getIntPtrType(CI.getContext(), AS));
2050     Value *P = Builder.CreateZExtOrTrunc(CI.getOperand(0), Ty);
2051     return new IntToPtrInst(P, CI.getType());
2052   }
2053 
2054   if (Instruction *I = commonCastTransforms(CI))
2055     return I;
2056 
2057   return nullptr;
2058 }
2059 
2060 /// Implement the transforms for cast of pointer (bitcast/ptrtoint)
2061 Instruction *InstCombinerImpl::commonPointerCastTransforms(CastInst &CI) {
2062   Value *Src = CI.getOperand(0);
2063 
2064   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
2065     // If casting the result of a getelementptr instruction with no offset, turn
2066     // this into a cast of the original pointer!
2067     if (GEP->hasAllZeroIndices() &&
2068         // If CI is an addrspacecast and GEP changes the poiner type, merging
2069         // GEP into CI would undo canonicalizing addrspacecast with different
2070         // pointer types, causing infinite loops.
2071         (!isa<AddrSpaceCastInst>(CI) ||
2072          GEP->getType() == GEP->getPointerOperandType())) {
2073       // Changing the cast operand is usually not a good idea but it is safe
2074       // here because the pointer operand is being replaced with another
2075       // pointer operand so the opcode doesn't need to change.
2076       return replaceOperand(CI, 0, GEP->getOperand(0));
2077     }
2078   }
2079 
2080   return commonCastTransforms(CI);
2081 }
2082 
2083 Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
2084   // If the destination integer type is not the intptr_t type for this target,
2085   // do a ptrtoint to intptr_t then do a trunc or zext.  This allows the cast
2086   // to be exposed to other transforms.
2087   Value *SrcOp = CI.getPointerOperand();
2088   Type *SrcTy = SrcOp->getType();
2089   Type *Ty = CI.getType();
2090   unsigned AS = CI.getPointerAddressSpace();
2091   unsigned TySize = Ty->getScalarSizeInBits();
2092   unsigned PtrSize = DL.getPointerSizeInBits(AS);
2093   if (TySize != PtrSize) {
2094     Type *IntPtrTy =
2095         SrcTy->getWithNewType(DL.getIntPtrType(CI.getContext(), AS));
2096     Value *P = Builder.CreatePtrToInt(SrcOp, IntPtrTy);
2097     return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
2098   }
2099 
2100   if (auto *GEP = dyn_cast<GetElementPtrInst>(SrcOp)) {
2101     // Fold ptrtoint(gep null, x) to multiply + constant if the GEP has one use.
2102     // While this can increase the number of instructions it doesn't actually
2103     // increase the overall complexity since the arithmetic is just part of
2104     // the GEP otherwise.
2105     if (GEP->hasOneUse() &&
2106         isa<ConstantPointerNull>(GEP->getPointerOperand())) {
2107       return replaceInstUsesWith(CI,
2108                                  Builder.CreateIntCast(EmitGEPOffset(GEP), Ty,
2109                                                        /*isSigned=*/false));
2110     }
2111   }
2112 
2113   Value *Vec, *Scalar, *Index;
2114   if (match(SrcOp, m_OneUse(m_InsertElt(m_IntToPtr(m_Value(Vec)),
2115                                         m_Value(Scalar), m_Value(Index)))) &&
2116       Vec->getType() == Ty) {
2117     assert(Vec->getType()->getScalarSizeInBits() == PtrSize && "Wrong type");
2118     // Convert the scalar to int followed by insert to eliminate one cast:
2119     // p2i (ins (i2p Vec), Scalar, Index --> ins Vec, (p2i Scalar), Index
2120     Value *NewCast = Builder.CreatePtrToInt(Scalar, Ty->getScalarType());
2121     return InsertElementInst::Create(Vec, NewCast, Index);
2122   }
2123 
2124   return commonPointerCastTransforms(CI);
2125 }
2126 
2127 /// This input value (which is known to have vector type) is being zero extended
2128 /// or truncated to the specified vector type. Since the zext/trunc is done
2129 /// using an integer type, we have a (bitcast(cast(bitcast))) pattern,
2130 /// endianness will impact which end of the vector that is extended or
2131 /// truncated.
2132 ///
2133 /// A vector is always stored with index 0 at the lowest address, which
2134 /// corresponds to the most significant bits for a big endian stored integer and
2135 /// the least significant bits for little endian. A trunc/zext of an integer
2136 /// impacts the big end of the integer. Thus, we need to add/remove elements at
2137 /// the front of the vector for big endian targets, and the back of the vector
2138 /// for little endian targets.
2139 ///
2140 /// Try to replace it with a shuffle (and vector/vector bitcast) if possible.
2141 ///
2142 /// The source and destination vector types may have different element types.
2143 static Instruction *
2144 optimizeVectorResizeWithIntegerBitCasts(Value *InVal, VectorType *DestTy,
2145                                         InstCombinerImpl &IC) {
2146   // We can only do this optimization if the output is a multiple of the input
2147   // element size, or the input is a multiple of the output element size.
2148   // Convert the input type to have the same element type as the output.
2149   VectorType *SrcTy = cast<VectorType>(InVal->getType());
2150 
2151   if (SrcTy->getElementType() != DestTy->getElementType()) {
2152     // The input types don't need to be identical, but for now they must be the
2153     // same size.  There is no specific reason we couldn't handle things like
2154     // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
2155     // there yet.
2156     if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
2157         DestTy->getElementType()->getPrimitiveSizeInBits())
2158       return nullptr;
2159 
2160     SrcTy =
2161         FixedVectorType::get(DestTy->getElementType(),
2162                              cast<FixedVectorType>(SrcTy)->getNumElements());
2163     InVal = IC.Builder.CreateBitCast(InVal, SrcTy);
2164   }
2165 
2166   bool IsBigEndian = IC.getDataLayout().isBigEndian();
2167   unsigned SrcElts = cast<FixedVectorType>(SrcTy)->getNumElements();
2168   unsigned DestElts = cast<FixedVectorType>(DestTy)->getNumElements();
2169 
2170   assert(SrcElts != DestElts && "Element counts should be different.");
2171 
2172   // Now that the element types match, get the shuffle mask and RHS of the
2173   // shuffle to use, which depends on whether we're increasing or decreasing the
2174   // size of the input.
2175   auto ShuffleMaskStorage = llvm::to_vector<16>(llvm::seq<int>(0, SrcElts));
2176   ArrayRef<int> ShuffleMask;
2177   Value *V2;
2178 
2179   if (SrcElts > DestElts) {
2180     // If we're shrinking the number of elements (rewriting an integer
2181     // truncate), just shuffle in the elements corresponding to the least
2182     // significant bits from the input and use poison as the second shuffle
2183     // input.
2184     V2 = PoisonValue::get(SrcTy);
2185     // Make sure the shuffle mask selects the "least significant bits" by
2186     // keeping elements from back of the src vector for big endian, and from the
2187     // front for little endian.
2188     ShuffleMask = ShuffleMaskStorage;
2189     if (IsBigEndian)
2190       ShuffleMask = ShuffleMask.take_back(DestElts);
2191     else
2192       ShuffleMask = ShuffleMask.take_front(DestElts);
2193   } else {
2194     // If we're increasing the number of elements (rewriting an integer zext),
2195     // shuffle in all of the elements from InVal. Fill the rest of the result
2196     // elements with zeros from a constant zero.
2197     V2 = Constant::getNullValue(SrcTy);
2198     // Use first elt from V2 when indicating zero in the shuffle mask.
2199     uint32_t NullElt = SrcElts;
2200     // Extend with null values in the "most significant bits" by adding elements
2201     // in front of the src vector for big endian, and at the back for little
2202     // endian.
2203     unsigned DeltaElts = DestElts - SrcElts;
2204     if (IsBigEndian)
2205       ShuffleMaskStorage.insert(ShuffleMaskStorage.begin(), DeltaElts, NullElt);
2206     else
2207       ShuffleMaskStorage.append(DeltaElts, NullElt);
2208     ShuffleMask = ShuffleMaskStorage;
2209   }
2210 
2211   return new ShuffleVectorInst(InVal, V2, ShuffleMask);
2212 }
2213 
2214 static bool isMultipleOfTypeSize(unsigned Value, Type *Ty) {
2215   return Value % Ty->getPrimitiveSizeInBits() == 0;
2216 }
2217 
2218 static unsigned getTypeSizeIndex(unsigned Value, Type *Ty) {
2219   return Value / Ty->getPrimitiveSizeInBits();
2220 }
2221 
2222 /// V is a value which is inserted into a vector of VecEltTy.
2223 /// Look through the value to see if we can decompose it into
2224 /// insertions into the vector.  See the example in the comment for
2225 /// OptimizeIntegerToVectorInsertions for the pattern this handles.
2226 /// The type of V is always a non-zero multiple of VecEltTy's size.
2227 /// Shift is the number of bits between the lsb of V and the lsb of
2228 /// the vector.
2229 ///
2230 /// This returns false if the pattern can't be matched or true if it can,
2231 /// filling in Elements with the elements found here.
2232 static bool collectInsertionElements(Value *V, unsigned Shift,
2233                                      SmallVectorImpl<Value *> &Elements,
2234                                      Type *VecEltTy, bool isBigEndian) {
2235   assert(isMultipleOfTypeSize(Shift, VecEltTy) &&
2236          "Shift should be a multiple of the element type size");
2237 
2238   // Undef values never contribute useful bits to the result.
2239   if (isa<UndefValue>(V)) return true;
2240 
2241   // If we got down to a value of the right type, we win, try inserting into the
2242   // right element.
2243   if (V->getType() == VecEltTy) {
2244     // Inserting null doesn't actually insert any elements.
2245     if (Constant *C = dyn_cast<Constant>(V))
2246       if (C->isNullValue())
2247         return true;
2248 
2249     unsigned ElementIndex = getTypeSizeIndex(Shift, VecEltTy);
2250     if (isBigEndian)
2251       ElementIndex = Elements.size() - ElementIndex - 1;
2252 
2253     // Fail if multiple elements are inserted into this slot.
2254     if (Elements[ElementIndex])
2255       return false;
2256 
2257     Elements[ElementIndex] = V;
2258     return true;
2259   }
2260 
2261   if (Constant *C = dyn_cast<Constant>(V)) {
2262     // Figure out the # elements this provides, and bitcast it or slice it up
2263     // as required.
2264     unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(),
2265                                         VecEltTy);
2266     // If the constant is the size of a vector element, we just need to bitcast
2267     // it to the right type so it gets properly inserted.
2268     if (NumElts == 1)
2269       return collectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy),
2270                                       Shift, Elements, VecEltTy, isBigEndian);
2271 
2272     // Okay, this is a constant that covers multiple elements.  Slice it up into
2273     // pieces and insert each element-sized piece into the vector.
2274     if (!isa<IntegerType>(C->getType()))
2275       C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(),
2276                                        C->getType()->getPrimitiveSizeInBits()));
2277     unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
2278     Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
2279 
2280     for (unsigned i = 0; i != NumElts; ++i) {
2281       unsigned ShiftI = Shift+i*ElementSize;
2282       Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(),
2283                                                                   ShiftI));
2284       Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
2285       if (!collectInsertionElements(Piece, ShiftI, Elements, VecEltTy,
2286                                     isBigEndian))
2287         return false;
2288     }
2289     return true;
2290   }
2291 
2292   if (!V->hasOneUse()) return false;
2293 
2294   Instruction *I = dyn_cast<Instruction>(V);
2295   if (!I) return false;
2296   switch (I->getOpcode()) {
2297   default: return false; // Unhandled case.
2298   case Instruction::BitCast:
2299     if (I->getOperand(0)->getType()->isVectorTy())
2300       return false;
2301     return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2302                                     isBigEndian);
2303   case Instruction::ZExt:
2304     if (!isMultipleOfTypeSize(
2305                           I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
2306                               VecEltTy))
2307       return false;
2308     return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2309                                     isBigEndian);
2310   case Instruction::Or:
2311     return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2312                                     isBigEndian) &&
2313            collectInsertionElements(I->getOperand(1), Shift, Elements, VecEltTy,
2314                                     isBigEndian);
2315   case Instruction::Shl: {
2316     // Must be shifting by a constant that is a multiple of the element size.
2317     ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
2318     if (!CI) return false;
2319     Shift += CI->getZExtValue();
2320     if (!isMultipleOfTypeSize(Shift, VecEltTy)) return false;
2321     return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2322                                     isBigEndian);
2323   }
2324 
2325   }
2326 }
2327 
2328 
2329 /// If the input is an 'or' instruction, we may be doing shifts and ors to
2330 /// assemble the elements of the vector manually.
2331 /// Try to rip the code out and replace it with insertelements.  This is to
2332 /// optimize code like this:
2333 ///
2334 ///    %tmp37 = bitcast float %inc to i32
2335 ///    %tmp38 = zext i32 %tmp37 to i64
2336 ///    %tmp31 = bitcast float %inc5 to i32
2337 ///    %tmp32 = zext i32 %tmp31 to i64
2338 ///    %tmp33 = shl i64 %tmp32, 32
2339 ///    %ins35 = or i64 %tmp33, %tmp38
2340 ///    %tmp43 = bitcast i64 %ins35 to <2 x float>
2341 ///
2342 /// Into two insertelements that do "buildvector{%inc, %inc5}".
2343 static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI,
2344                                                 InstCombinerImpl &IC) {
2345   auto *DestVecTy = cast<FixedVectorType>(CI.getType());
2346   Value *IntInput = CI.getOperand(0);
2347 
2348   SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
2349   if (!collectInsertionElements(IntInput, 0, Elements,
2350                                 DestVecTy->getElementType(),
2351                                 IC.getDataLayout().isBigEndian()))
2352     return nullptr;
2353 
2354   // If we succeeded, we know that all of the element are specified by Elements
2355   // or are zero if Elements has a null entry.  Recast this as a set of
2356   // insertions.
2357   Value *Result = Constant::getNullValue(CI.getType());
2358   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
2359     if (!Elements[i]) continue;  // Unset element.
2360 
2361     Result = IC.Builder.CreateInsertElement(Result, Elements[i],
2362                                             IC.Builder.getInt32(i));
2363   }
2364 
2365   return Result;
2366 }
2367 
2368 /// Canonicalize scalar bitcasts of extracted elements into a bitcast of the
2369 /// vector followed by extract element. The backend tends to handle bitcasts of
2370 /// vectors better than bitcasts of scalars because vector registers are
2371 /// usually not type-specific like scalar integer or scalar floating-point.
2372 static Instruction *canonicalizeBitCastExtElt(BitCastInst &BitCast,
2373                                               InstCombinerImpl &IC) {
2374   Value *VecOp, *Index;
2375   if (!match(BitCast.getOperand(0),
2376              m_OneUse(m_ExtractElt(m_Value(VecOp), m_Value(Index)))))
2377     return nullptr;
2378 
2379   // The bitcast must be to a vectorizable type, otherwise we can't make a new
2380   // type to extract from.
2381   Type *DestType = BitCast.getType();
2382   VectorType *VecType = cast<VectorType>(VecOp->getType());
2383   if (VectorType::isValidElementType(DestType)) {
2384     auto *NewVecType = VectorType::get(DestType, VecType);
2385     auto *NewBC = IC.Builder.CreateBitCast(VecOp, NewVecType, "bc");
2386     return ExtractElementInst::Create(NewBC, Index);
2387   }
2388 
2389   // Only solve DestType is vector to avoid inverse transform in visitBitCast.
2390   // bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest)
2391   auto *FixedVType = dyn_cast<FixedVectorType>(VecType);
2392   if (DestType->isVectorTy() && FixedVType && FixedVType->getNumElements() == 1)
2393     return CastInst::Create(Instruction::BitCast, VecOp, DestType);
2394 
2395   return nullptr;
2396 }
2397 
2398 /// Change the type of a bitwise logic operation if we can eliminate a bitcast.
2399 static Instruction *foldBitCastBitwiseLogic(BitCastInst &BitCast,
2400                                             InstCombiner::BuilderTy &Builder) {
2401   Type *DestTy = BitCast.getType();
2402   BinaryOperator *BO;
2403 
2404   if (!match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))) ||
2405       !BO->isBitwiseLogicOp())
2406     return nullptr;
2407 
2408   // FIXME: This transform is restricted to vector types to avoid backend
2409   // problems caused by creating potentially illegal operations. If a fix-up is
2410   // added to handle that situation, we can remove this check.
2411   if (!DestTy->isVectorTy() || !BO->getType()->isVectorTy())
2412     return nullptr;
2413 
2414   if (DestTy->isFPOrFPVectorTy()) {
2415     Value *X, *Y;
2416     // bitcast(logic(bitcast(X), bitcast(Y))) -> bitcast'(logic(bitcast'(X), Y))
2417     if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) &&
2418         match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(Y))))) {
2419       if (X->getType()->isFPOrFPVectorTy() &&
2420           Y->getType()->isIntOrIntVectorTy()) {
2421         Value *CastedOp =
2422             Builder.CreateBitCast(BO->getOperand(0), Y->getType());
2423         Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, Y);
2424         return CastInst::CreateBitOrPointerCast(NewBO, DestTy);
2425       }
2426       if (X->getType()->isIntOrIntVectorTy() &&
2427           Y->getType()->isFPOrFPVectorTy()) {
2428         Value *CastedOp =
2429             Builder.CreateBitCast(BO->getOperand(1), X->getType());
2430         Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, X);
2431         return CastInst::CreateBitOrPointerCast(NewBO, DestTy);
2432       }
2433     }
2434     return nullptr;
2435   }
2436 
2437   if (!DestTy->isIntOrIntVectorTy())
2438     return nullptr;
2439 
2440   Value *X;
2441   if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) &&
2442       X->getType() == DestTy && !isa<Constant>(X)) {
2443     // bitcast(logic(bitcast(X), Y)) --> logic'(X, bitcast(Y))
2444     Value *CastedOp1 = Builder.CreateBitCast(BO->getOperand(1), DestTy);
2445     return BinaryOperator::Create(BO->getOpcode(), X, CastedOp1);
2446   }
2447 
2448   if (match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(X)))) &&
2449       X->getType() == DestTy && !isa<Constant>(X)) {
2450     // bitcast(logic(Y, bitcast(X))) --> logic'(bitcast(Y), X)
2451     Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
2452     return BinaryOperator::Create(BO->getOpcode(), CastedOp0, X);
2453   }
2454 
2455   // Canonicalize vector bitcasts to come before vector bitwise logic with a
2456   // constant. This eases recognition of special constants for later ops.
2457   // Example:
2458   // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
2459   Constant *C;
2460   if (match(BO->getOperand(1), m_Constant(C))) {
2461     // bitcast (logic X, C) --> logic (bitcast X, C')
2462     Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
2463     Value *CastedC = Builder.CreateBitCast(C, DestTy);
2464     return BinaryOperator::Create(BO->getOpcode(), CastedOp0, CastedC);
2465   }
2466 
2467   return nullptr;
2468 }
2469 
2470 /// Change the type of a select if we can eliminate a bitcast.
2471 static Instruction *foldBitCastSelect(BitCastInst &BitCast,
2472                                       InstCombiner::BuilderTy &Builder) {
2473   Value *Cond, *TVal, *FVal;
2474   if (!match(BitCast.getOperand(0),
2475              m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))))
2476     return nullptr;
2477 
2478   // A vector select must maintain the same number of elements in its operands.
2479   Type *CondTy = Cond->getType();
2480   Type *DestTy = BitCast.getType();
2481   if (auto *CondVTy = dyn_cast<VectorType>(CondTy))
2482     if (!DestTy->isVectorTy() ||
2483         CondVTy->getElementCount() !=
2484             cast<VectorType>(DestTy)->getElementCount())
2485       return nullptr;
2486 
2487   // FIXME: This transform is restricted from changing the select between
2488   // scalars and vectors to avoid backend problems caused by creating
2489   // potentially illegal operations. If a fix-up is added to handle that
2490   // situation, we can remove this check.
2491   if (DestTy->isVectorTy() != TVal->getType()->isVectorTy())
2492     return nullptr;
2493 
2494   auto *Sel = cast<Instruction>(BitCast.getOperand(0));
2495   Value *X;
2496   if (match(TVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy &&
2497       !isa<Constant>(X)) {
2498     // bitcast(select(Cond, bitcast(X), Y)) --> select'(Cond, X, bitcast(Y))
2499     Value *CastedVal = Builder.CreateBitCast(FVal, DestTy);
2500     return SelectInst::Create(Cond, X, CastedVal, "", nullptr, Sel);
2501   }
2502 
2503   if (match(FVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy &&
2504       !isa<Constant>(X)) {
2505     // bitcast(select(Cond, Y, bitcast(X))) --> select'(Cond, bitcast(Y), X)
2506     Value *CastedVal = Builder.CreateBitCast(TVal, DestTy);
2507     return SelectInst::Create(Cond, CastedVal, X, "", nullptr, Sel);
2508   }
2509 
2510   return nullptr;
2511 }
2512 
2513 /// Check if all users of CI are StoreInsts.
2514 static bool hasStoreUsersOnly(CastInst &CI) {
2515   for (User *U : CI.users()) {
2516     if (!isa<StoreInst>(U))
2517       return false;
2518   }
2519   return true;
2520 }
2521 
2522 /// This function handles following case
2523 ///
2524 ///     A  ->  B    cast
2525 ///     PHI
2526 ///     B  ->  A    cast
2527 ///
2528 /// All the related PHI nodes can be replaced by new PHI nodes with type A.
2529 /// The uses of \p CI can be changed to the new PHI node corresponding to \p PN.
2530 Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI,
2531                                                       PHINode *PN) {
2532   // BitCast used by Store can be handled in InstCombineLoadStoreAlloca.cpp.
2533   if (hasStoreUsersOnly(CI))
2534     return nullptr;
2535 
2536   Value *Src = CI.getOperand(0);
2537   Type *SrcTy = Src->getType();         // Type B
2538   Type *DestTy = CI.getType();          // Type A
2539 
2540   SmallVector<PHINode *, 4> PhiWorklist;
2541   SmallSetVector<PHINode *, 4> OldPhiNodes;
2542 
2543   // Find all of the A->B casts and PHI nodes.
2544   // We need to inspect all related PHI nodes, but PHIs can be cyclic, so
2545   // OldPhiNodes is used to track all known PHI nodes, before adding a new
2546   // PHI to PhiWorklist, it is checked against and added to OldPhiNodes first.
2547   PhiWorklist.push_back(PN);
2548   OldPhiNodes.insert(PN);
2549   while (!PhiWorklist.empty()) {
2550     auto *OldPN = PhiWorklist.pop_back_val();
2551     for (Value *IncValue : OldPN->incoming_values()) {
2552       if (isa<Constant>(IncValue))
2553         continue;
2554 
2555       if (auto *LI = dyn_cast<LoadInst>(IncValue)) {
2556         // If there is a sequence of one or more load instructions, each loaded
2557         // value is used as address of later load instruction, bitcast is
2558         // necessary to change the value type, don't optimize it. For
2559         // simplicity we give up if the load address comes from another load.
2560         Value *Addr = LI->getOperand(0);
2561         if (Addr == &CI || isa<LoadInst>(Addr))
2562           return nullptr;
2563         // Don't tranform "load <256 x i32>, <256 x i32>*" to
2564         // "load x86_amx, x86_amx*", because x86_amx* is invalid.
2565         // TODO: Remove this check when bitcast between vector and x86_amx
2566         // is replaced with a specific intrinsic.
2567         if (DestTy->isX86_AMXTy())
2568           return nullptr;
2569         if (LI->hasOneUse() && LI->isSimple())
2570           continue;
2571         // If a LoadInst has more than one use, changing the type of loaded
2572         // value may create another bitcast.
2573         return nullptr;
2574       }
2575 
2576       if (auto *PNode = dyn_cast<PHINode>(IncValue)) {
2577         if (OldPhiNodes.insert(PNode))
2578           PhiWorklist.push_back(PNode);
2579         continue;
2580       }
2581 
2582       auto *BCI = dyn_cast<BitCastInst>(IncValue);
2583       // We can't handle other instructions.
2584       if (!BCI)
2585         return nullptr;
2586 
2587       // Verify it's a A->B cast.
2588       Type *TyA = BCI->getOperand(0)->getType();
2589       Type *TyB = BCI->getType();
2590       if (TyA != DestTy || TyB != SrcTy)
2591         return nullptr;
2592     }
2593   }
2594 
2595   // Check that each user of each old PHI node is something that we can
2596   // rewrite, so that all of the old PHI nodes can be cleaned up afterwards.
2597   for (auto *OldPN : OldPhiNodes) {
2598     for (User *V : OldPN->users()) {
2599       if (auto *SI = dyn_cast<StoreInst>(V)) {
2600         if (!SI->isSimple() || SI->getOperand(0) != OldPN)
2601           return nullptr;
2602       } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2603         // Verify it's a B->A cast.
2604         Type *TyB = BCI->getOperand(0)->getType();
2605         Type *TyA = BCI->getType();
2606         if (TyA != DestTy || TyB != SrcTy)
2607           return nullptr;
2608       } else if (auto *PHI = dyn_cast<PHINode>(V)) {
2609         // As long as the user is another old PHI node, then even if we don't
2610         // rewrite it, the PHI web we're considering won't have any users
2611         // outside itself, so it'll be dead.
2612         if (!OldPhiNodes.contains(PHI))
2613           return nullptr;
2614       } else {
2615         return nullptr;
2616       }
2617     }
2618   }
2619 
2620   // For each old PHI node, create a corresponding new PHI node with a type A.
2621   SmallDenseMap<PHINode *, PHINode *> NewPNodes;
2622   for (auto *OldPN : OldPhiNodes) {
2623     Builder.SetInsertPoint(OldPN);
2624     PHINode *NewPN = Builder.CreatePHI(DestTy, OldPN->getNumOperands());
2625     NewPNodes[OldPN] = NewPN;
2626   }
2627 
2628   // Fill in the operands of new PHI nodes.
2629   for (auto *OldPN : OldPhiNodes) {
2630     PHINode *NewPN = NewPNodes[OldPN];
2631     for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
2632       Value *V = OldPN->getOperand(j);
2633       Value *NewV = nullptr;
2634       if (auto *C = dyn_cast<Constant>(V)) {
2635         NewV = ConstantExpr::getBitCast(C, DestTy);
2636       } else if (auto *LI = dyn_cast<LoadInst>(V)) {
2637         // Explicitly perform load combine to make sure no opposing transform
2638         // can remove the bitcast in the meantime and trigger an infinite loop.
2639         Builder.SetInsertPoint(LI);
2640         NewV = combineLoadToNewType(*LI, DestTy);
2641         // Remove the old load and its use in the old phi, which itself becomes
2642         // dead once the whole transform finishes.
2643         replaceInstUsesWith(*LI, PoisonValue::get(LI->getType()));
2644         eraseInstFromFunction(*LI);
2645       } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2646         NewV = BCI->getOperand(0);
2647       } else if (auto *PrevPN = dyn_cast<PHINode>(V)) {
2648         NewV = NewPNodes[PrevPN];
2649       }
2650       assert(NewV);
2651       NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j));
2652     }
2653   }
2654 
2655   // Traverse all accumulated PHI nodes and process its users,
2656   // which are Stores and BitcCasts. Without this processing
2657   // NewPHI nodes could be replicated and could lead to extra
2658   // moves generated after DeSSA.
2659   // If there is a store with type B, change it to type A.
2660 
2661 
2662   // Replace users of BitCast B->A with NewPHI. These will help
2663   // later to get rid off a closure formed by OldPHI nodes.
2664   Instruction *RetVal = nullptr;
2665   for (auto *OldPN : OldPhiNodes) {
2666     PHINode *NewPN = NewPNodes[OldPN];
2667     for (User *V : make_early_inc_range(OldPN->users())) {
2668       if (auto *SI = dyn_cast<StoreInst>(V)) {
2669         assert(SI->isSimple() && SI->getOperand(0) == OldPN);
2670         Builder.SetInsertPoint(SI);
2671         auto *NewBC =
2672           cast<BitCastInst>(Builder.CreateBitCast(NewPN, SrcTy));
2673         SI->setOperand(0, NewBC);
2674         Worklist.push(SI);
2675         assert(hasStoreUsersOnly(*NewBC));
2676       }
2677       else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2678         Type *TyB = BCI->getOperand(0)->getType();
2679         Type *TyA = BCI->getType();
2680         assert(TyA == DestTy && TyB == SrcTy);
2681         (void) TyA;
2682         (void) TyB;
2683         Instruction *I = replaceInstUsesWith(*BCI, NewPN);
2684         if (BCI == &CI)
2685           RetVal = I;
2686       } else if (auto *PHI = dyn_cast<PHINode>(V)) {
2687         assert(OldPhiNodes.contains(PHI));
2688         (void) PHI;
2689       } else {
2690         llvm_unreachable("all uses should be handled");
2691       }
2692     }
2693   }
2694 
2695   return RetVal;
2696 }
2697 
2698 static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder,
2699                                         const DataLayout &DL) {
2700   Value *Src = CI.getOperand(0);
2701   PointerType *SrcPTy = cast<PointerType>(Src->getType());
2702   PointerType *DstPTy = cast<PointerType>(CI.getType());
2703 
2704   // Bitcasts involving opaque pointers cannot be converted into a GEP.
2705   if (SrcPTy->isOpaque() || DstPTy->isOpaque())
2706     return nullptr;
2707 
2708   Type *DstElTy = DstPTy->getNonOpaquePointerElementType();
2709   Type *SrcElTy = SrcPTy->getNonOpaquePointerElementType();
2710 
2711   // When the type pointed to is not sized the cast cannot be
2712   // turned into a gep.
2713   if (!SrcElTy->isSized())
2714     return nullptr;
2715 
2716   // If the source and destination are pointers, and this cast is equivalent
2717   // to a getelementptr X, 0, 0, 0...  turn it into the appropriate gep.
2718   // This can enhance SROA and other transforms that want type-safe pointers.
2719   unsigned NumZeros = 0;
2720   while (SrcElTy && SrcElTy != DstElTy) {
2721     SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0);
2722     ++NumZeros;
2723   }
2724 
2725   // If we found a path from the src to dest, create the getelementptr now.
2726   if (SrcElTy == DstElTy) {
2727     SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0));
2728     GetElementPtrInst *GEP = GetElementPtrInst::Create(
2729         SrcPTy->getNonOpaquePointerElementType(), Src, Idxs);
2730 
2731     // If the source pointer is dereferenceable, then assume it points to an
2732     // allocated object and apply "inbounds" to the GEP.
2733     bool CanBeNull, CanBeFreed;
2734     if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) {
2735       // In a non-default address space (not 0), a null pointer can not be
2736       // assumed inbounds, so ignore that case (dereferenceable_or_null).
2737       // The reason is that 'null' is not treated differently in these address
2738       // spaces, and we consequently ignore the 'gep inbounds' special case
2739       // for 'null' which allows 'inbounds' on 'null' if the indices are
2740       // zeros.
2741       if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
2742         GEP->setIsInBounds();
2743     }
2744     return GEP;
2745   }
2746   return nullptr;
2747 }
2748 
2749 Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
2750   // If the operands are integer typed then apply the integer transforms,
2751   // otherwise just apply the common ones.
2752   Value *Src = CI.getOperand(0);
2753   Type *SrcTy = Src->getType();
2754   Type *DestTy = CI.getType();
2755 
2756   // Get rid of casts from one type to the same type. These are useless and can
2757   // be replaced by the operand.
2758   if (DestTy == Src->getType())
2759     return replaceInstUsesWith(CI, Src);
2760 
2761   if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy)) {
2762     // If we are casting a alloca to a pointer to a type of the same
2763     // size, rewrite the allocation instruction to allocate the "right" type.
2764     // There is no need to modify malloc calls because it is their bitcast that
2765     // needs to be cleaned up.
2766     if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
2767       if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
2768         return V;
2769 
2770     if (Instruction *I = convertBitCastToGEP(CI, Builder, DL))
2771       return I;
2772   }
2773 
2774   if (FixedVectorType *DestVTy = dyn_cast<FixedVectorType>(DestTy)) {
2775     // Beware: messing with this target-specific oddity may cause trouble.
2776     if (DestVTy->getNumElements() == 1 && SrcTy->isX86_MMXTy()) {
2777       Value *Elem = Builder.CreateBitCast(Src, DestVTy->getElementType());
2778       return InsertElementInst::Create(PoisonValue::get(DestTy), Elem,
2779                      Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
2780     }
2781 
2782     if (isa<IntegerType>(SrcTy)) {
2783       // If this is a cast from an integer to vector, check to see if the input
2784       // is a trunc or zext of a bitcast from vector.  If so, we can replace all
2785       // the casts with a shuffle and (potentially) a bitcast.
2786       if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
2787         CastInst *SrcCast = cast<CastInst>(Src);
2788         if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
2789           if (isa<VectorType>(BCIn->getOperand(0)->getType()))
2790             if (Instruction *I = optimizeVectorResizeWithIntegerBitCasts(
2791                     BCIn->getOperand(0), cast<VectorType>(DestTy), *this))
2792               return I;
2793       }
2794 
2795       // If the input is an 'or' instruction, we may be doing shifts and ors to
2796       // assemble the elements of the vector manually.  Try to rip the code out
2797       // and replace it with insertelements.
2798       if (Value *V = optimizeIntegerToVectorInsertions(CI, *this))
2799         return replaceInstUsesWith(CI, V);
2800     }
2801   }
2802 
2803   if (FixedVectorType *SrcVTy = dyn_cast<FixedVectorType>(SrcTy)) {
2804     if (SrcVTy->getNumElements() == 1) {
2805       // If our destination is not a vector, then make this a straight
2806       // scalar-scalar cast.
2807       if (!DestTy->isVectorTy()) {
2808         Value *Elem =
2809           Builder.CreateExtractElement(Src,
2810                      Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
2811         return CastInst::Create(Instruction::BitCast, Elem, DestTy);
2812       }
2813 
2814       // Otherwise, see if our source is an insert. If so, then use the scalar
2815       // component directly:
2816       // bitcast (inselt <1 x elt> V, X, 0) to <n x m> --> bitcast X to <n x m>
2817       if (auto *InsElt = dyn_cast<InsertElementInst>(Src))
2818         return new BitCastInst(InsElt->getOperand(1), DestTy);
2819     }
2820 
2821     // Convert an artificial vector insert into more analyzable bitwise logic.
2822     unsigned BitWidth = DestTy->getScalarSizeInBits();
2823     Value *X, *Y;
2824     uint64_t IndexC;
2825     if (match(Src, m_OneUse(m_InsertElt(m_OneUse(m_BitCast(m_Value(X))),
2826                                         m_Value(Y), m_ConstantInt(IndexC)))) &&
2827         DestTy->isIntegerTy() && X->getType() == DestTy &&
2828         Y->getType()->isIntegerTy() && isDesirableIntType(BitWidth)) {
2829       // Adjust for big endian - the LSBs are at the high index.
2830       if (DL.isBigEndian())
2831         IndexC = SrcVTy->getNumElements() - 1 - IndexC;
2832 
2833       // We only handle (endian-normalized) insert to index 0. Any other insert
2834       // would require a left-shift, so that is an extra instruction.
2835       if (IndexC == 0) {
2836         // bitcast (inselt (bitcast X), Y, 0) --> or (and X, MaskC), (zext Y)
2837         unsigned EltWidth = Y->getType()->getScalarSizeInBits();
2838         APInt MaskC = APInt::getHighBitsSet(BitWidth, BitWidth - EltWidth);
2839         Value *AndX = Builder.CreateAnd(X, MaskC);
2840         Value *ZextY = Builder.CreateZExt(Y, DestTy);
2841         return BinaryOperator::CreateOr(AndX, ZextY);
2842       }
2843     }
2844   }
2845 
2846   if (auto *Shuf = dyn_cast<ShuffleVectorInst>(Src)) {
2847     // Okay, we have (bitcast (shuffle ..)).  Check to see if this is
2848     // a bitcast to a vector with the same # elts.
2849     Value *ShufOp0 = Shuf->getOperand(0);
2850     Value *ShufOp1 = Shuf->getOperand(1);
2851     auto ShufElts = cast<VectorType>(Shuf->getType())->getElementCount();
2852     auto SrcVecElts = cast<VectorType>(ShufOp0->getType())->getElementCount();
2853     if (Shuf->hasOneUse() && DestTy->isVectorTy() &&
2854         cast<VectorType>(DestTy)->getElementCount() == ShufElts &&
2855         ShufElts == SrcVecElts) {
2856       BitCastInst *Tmp;
2857       // If either of the operands is a cast from CI.getType(), then
2858       // evaluating the shuffle in the casted destination's type will allow
2859       // us to eliminate at least one cast.
2860       if (((Tmp = dyn_cast<BitCastInst>(ShufOp0)) &&
2861            Tmp->getOperand(0)->getType() == DestTy) ||
2862           ((Tmp = dyn_cast<BitCastInst>(ShufOp1)) &&
2863            Tmp->getOperand(0)->getType() == DestTy)) {
2864         Value *LHS = Builder.CreateBitCast(ShufOp0, DestTy);
2865         Value *RHS = Builder.CreateBitCast(ShufOp1, DestTy);
2866         // Return a new shuffle vector.  Use the same element ID's, as we
2867         // know the vector types match #elts.
2868         return new ShuffleVectorInst(LHS, RHS, Shuf->getShuffleMask());
2869       }
2870     }
2871 
2872     // A bitcasted-to-scalar and byte/bit reversing shuffle is better recognized
2873     // as a byte/bit swap:
2874     // bitcast <N x i8> (shuf X, undef, <N, N-1,...0>) -> bswap (bitcast X)
2875     // bitcast <N x i1> (shuf X, undef, <N, N-1,...0>) -> bitreverse (bitcast X)
2876     if (DestTy->isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 &&
2877         Shuf->hasOneUse() && Shuf->isReverse()) {
2878       unsigned IntrinsicNum = 0;
2879       if (DL.isLegalInteger(DestTy->getScalarSizeInBits()) &&
2880           SrcTy->getScalarSizeInBits() == 8) {
2881         IntrinsicNum = Intrinsic::bswap;
2882       } else if (SrcTy->getScalarSizeInBits() == 1) {
2883         IntrinsicNum = Intrinsic::bitreverse;
2884       }
2885       if (IntrinsicNum != 0) {
2886         assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask");
2887         assert(match(ShufOp1, m_Undef()) && "Unexpected shuffle op");
2888         Function *BswapOrBitreverse =
2889             Intrinsic::getDeclaration(CI.getModule(), IntrinsicNum, DestTy);
2890         Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy);
2891         return CallInst::Create(BswapOrBitreverse, {ScalarX});
2892       }
2893     }
2894   }
2895 
2896   // Handle the A->B->A cast, and there is an intervening PHI node.
2897   if (PHINode *PN = dyn_cast<PHINode>(Src))
2898     if (Instruction *I = optimizeBitCastFromPhi(CI, PN))
2899       return I;
2900 
2901   if (Instruction *I = canonicalizeBitCastExtElt(CI, *this))
2902     return I;
2903 
2904   if (Instruction *I = foldBitCastBitwiseLogic(CI, Builder))
2905     return I;
2906 
2907   if (Instruction *I = foldBitCastSelect(CI, Builder))
2908     return I;
2909 
2910   if (SrcTy->isPointerTy())
2911     return commonPointerCastTransforms(CI);
2912   return commonCastTransforms(CI);
2913 }
2914 
2915 Instruction *InstCombinerImpl::visitAddrSpaceCast(AddrSpaceCastInst &CI) {
2916   // If the destination pointer element type is not the same as the source's
2917   // first do a bitcast to the destination type, and then the addrspacecast.
2918   // This allows the cast to be exposed to other transforms.
2919   Value *Src = CI.getOperand(0);
2920   PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType());
2921   PointerType *DestTy = cast<PointerType>(CI.getType()->getScalarType());
2922 
2923   if (!SrcTy->hasSameElementTypeAs(DestTy)) {
2924     Type *MidTy =
2925         PointerType::getWithSamePointeeType(DestTy, SrcTy->getAddressSpace());
2926     // Handle vectors of pointers.
2927     if (VectorType *VT = dyn_cast<VectorType>(CI.getType()))
2928       MidTy = VectorType::get(MidTy, VT->getElementCount());
2929 
2930     Value *NewBitCast = Builder.CreateBitCast(Src, MidTy);
2931     return new AddrSpaceCastInst(NewBitCast, CI.getType());
2932   }
2933 
2934   return commonPointerCastTransforms(CI);
2935 }
2936