Reapply r237453 with a fix for the test timeouts.
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineCasts.cpp
1 //===- InstCombineCasts.cpp -----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the visit functions for cast operations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "InstCombineInternal.h"
15 #include "llvm/Analysis/ConstantFolding.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/PatternMatch.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 using namespace llvm;
20 using namespace PatternMatch;
21
22 #define DEBUG_TYPE "instcombine"
23
24 /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
25 /// expression.  If so, decompose it, returning some value X, such that Val is
26 /// X*Scale+Offset.
27 ///
28 static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
29                                         uint64_t &Offset) {
30   if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
31     Offset = CI->getZExtValue();
32     Scale  = 0;
33     return ConstantInt::get(Val->getType(), 0);
34   }
35
36   if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
37     // Cannot look past anything that might overflow.
38     OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val);
39     if (OBI && !OBI->hasNoUnsignedWrap() && !OBI->hasNoSignedWrap()) {
40       Scale = 1;
41       Offset = 0;
42       return Val;
43     }
44
45     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
46       if (I->getOpcode() == Instruction::Shl) {
47         // This is a value scaled by '1 << the shift amt'.
48         Scale = UINT64_C(1) << RHS->getZExtValue();
49         Offset = 0;
50         return I->getOperand(0);
51       }
52
53       if (I->getOpcode() == Instruction::Mul) {
54         // This value is scaled by 'RHS'.
55         Scale = RHS->getZExtValue();
56         Offset = 0;
57         return I->getOperand(0);
58       }
59
60       if (I->getOpcode() == Instruction::Add) {
61         // We have X+C.  Check to see if we really have (X*C2)+C1,
62         // where C1 is divisible by C2.
63         unsigned SubScale;
64         Value *SubVal =
65           DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
66         Offset += RHS->getZExtValue();
67         Scale = SubScale;
68         return SubVal;
69       }
70     }
71   }
72
73   // Otherwise, we can't look past this.
74   Scale = 1;
75   Offset = 0;
76   return Val;
77 }
78
79 /// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
80 /// try to eliminate the cast by moving the type information into the alloc.
81 Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
82                                                    AllocaInst &AI) {
83   PointerType *PTy = cast<PointerType>(CI.getType());
84
85   BuilderTy AllocaBuilder(*Builder);
86   AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
87
88   // Get the type really allocated and the type casted to.
89   Type *AllocElTy = AI.getAllocatedType();
90   Type *CastElTy = PTy->getElementType();
91   if (!AllocElTy->isSized() || !CastElTy->isSized()) return nullptr;
92
93   unsigned AllocElTyAlign = DL.getABITypeAlignment(AllocElTy);
94   unsigned CastElTyAlign = DL.getABITypeAlignment(CastElTy);
95   if (CastElTyAlign < AllocElTyAlign) return nullptr;
96
97   // If the allocation has multiple uses, only promote it if we are strictly
98   // increasing the alignment of the resultant allocation.  If we keep it the
99   // same, we open the door to infinite loops of various kinds.
100   if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return nullptr;
101
102   uint64_t AllocElTySize = DL.getTypeAllocSize(AllocElTy);
103   uint64_t CastElTySize = DL.getTypeAllocSize(CastElTy);
104   if (CastElTySize == 0 || AllocElTySize == 0) return nullptr;
105
106   // If the allocation has multiple uses, only promote it if we're not
107   // shrinking the amount of memory being allocated.
108   uint64_t AllocElTyStoreSize = DL.getTypeStoreSize(AllocElTy);
109   uint64_t CastElTyStoreSize = DL.getTypeStoreSize(CastElTy);
110   if (!AI.hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize) return nullptr;
111
112   // See if we can satisfy the modulus by pulling a scale out of the array
113   // size argument.
114   unsigned ArraySizeScale;
115   uint64_t ArrayOffset;
116   Value *NumElements = // See if the array size is a decomposable linear expr.
117     DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
118
119   // If we can now satisfy the modulus, by using a non-1 scale, we really can
120   // do the xform.
121   if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
122       (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return nullptr;
123
124   unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
125   Value *Amt = nullptr;
126   if (Scale == 1) {
127     Amt = NumElements;
128   } else {
129     Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
130     // Insert before the alloca, not before the cast.
131     Amt = AllocaBuilder.CreateMul(Amt, NumElements);
132   }
133
134   if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
135     Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
136                                   Offset, true);
137     Amt = AllocaBuilder.CreateAdd(Amt, Off);
138   }
139
140   AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
141   New->setAlignment(AI.getAlignment());
142   New->takeName(&AI);
143   New->setUsedWithInAlloca(AI.isUsedWithInAlloca());
144
145   // If the allocation has multiple real uses, insert a cast and change all
146   // things that used it to use the new cast.  This will also hack on CI, but it
147   // will die soon.
148   if (!AI.hasOneUse()) {
149     // New is the allocation instruction, pointer typed. AI is the original
150     // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
151     Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
152     ReplaceInstUsesWith(AI, NewCast);
153   }
154   return ReplaceInstUsesWith(CI, New);
155 }
156
157 /// EvaluateInDifferentType - Given an expression that
158 /// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
159 /// insert the code to evaluate the expression.
160 Value *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty,
161                                              bool isSigned) {
162   if (Constant *C = dyn_cast<Constant>(V)) {
163     C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
164     // If we got a constantexpr back, try to simplify it with DL info.
165     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
166       C = ConstantFoldConstantExpression(CE, DL, TLI);
167     return C;
168   }
169
170   // Otherwise, it must be an instruction.
171   Instruction *I = cast<Instruction>(V);
172   Instruction *Res = nullptr;
173   unsigned Opc = I->getOpcode();
174   switch (Opc) {
175   case Instruction::Add:
176   case Instruction::Sub:
177   case Instruction::Mul:
178   case Instruction::And:
179   case Instruction::Or:
180   case Instruction::Xor:
181   case Instruction::AShr:
182   case Instruction::LShr:
183   case Instruction::Shl:
184   case Instruction::UDiv:
185   case Instruction::URem: {
186     Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
187     Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
188     Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
189     break;
190   }
191   case Instruction::Trunc:
192   case Instruction::ZExt:
193   case Instruction::SExt:
194     // If the source type of the cast is the type we're trying for then we can
195     // just return the source.  There's no need to insert it because it is not
196     // new.
197     if (I->getOperand(0)->getType() == Ty)
198       return I->getOperand(0);
199
200     // Otherwise, must be the same type of cast, so just reinsert a new one.
201     // This also handles the case of zext(trunc(x)) -> zext(x).
202     Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
203                                       Opc == Instruction::SExt);
204     break;
205   case Instruction::Select: {
206     Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
207     Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
208     Res = SelectInst::Create(I->getOperand(0), True, False);
209     break;
210   }
211   case Instruction::PHI: {
212     PHINode *OPN = cast<PHINode>(I);
213     PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues());
214     for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
215       Value *V =
216           EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
217       NPN->addIncoming(V, OPN->getIncomingBlock(i));
218     }
219     Res = NPN;
220     break;
221   }
222   default:
223     // TODO: Can handle more cases here.
224     llvm_unreachable("Unreachable!");
225   }
226
227   Res->takeName(I);
228   return InsertNewInstWith(Res, *I);
229 }
230
231
232 /// This function is a wrapper around CastInst::isEliminableCastPair. It
233 /// simply extracts arguments and returns what that function returns.
234 static Instruction::CastOps
235 isEliminableCastPair(const CastInst *CI, ///< First cast instruction
236                      unsigned opcode,    ///< Opcode for the second cast
237                      Type *DstTy,        ///< Target type for the second cast
238                      const DataLayout &DL) {
239   Type *SrcTy = CI->getOperand(0)->getType();   // A from above
240   Type *MidTy = CI->getType();                  // B from above
241
242   // Get the opcodes of the two Cast instructions
243   Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
244   Instruction::CastOps secondOp = Instruction::CastOps(opcode);
245   Type *SrcIntPtrTy =
246       SrcTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(SrcTy) : nullptr;
247   Type *MidIntPtrTy =
248       MidTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(MidTy) : nullptr;
249   Type *DstIntPtrTy =
250       DstTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(DstTy) : nullptr;
251   unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
252                                                 DstTy, SrcIntPtrTy, MidIntPtrTy,
253                                                 DstIntPtrTy);
254
255   // We don't want to form an inttoptr or ptrtoint that converts to an integer
256   // type that differs from the pointer size.
257   if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
258       (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
259     Res = 0;
260
261   return Instruction::CastOps(Res);
262 }
263
264 /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
265 /// results in any code being generated and is interesting to optimize out. If
266 /// the cast can be eliminated by some other simple transformation, we prefer
267 /// to do the simplification first.
268 bool InstCombiner::ShouldOptimizeCast(Instruction::CastOps opc, const Value *V,
269                                       Type *Ty) {
270   // Noop casts and casts of constants should be eliminated trivially.
271   if (V->getType() == Ty || isa<Constant>(V)) return false;
272
273   // If this is another cast that can be eliminated, we prefer to have it
274   // eliminated.
275   if (const CastInst *CI = dyn_cast<CastInst>(V))
276     if (isEliminableCastPair(CI, opc, Ty, DL))
277       return false;
278
279   // If this is a vector sext from a compare, then we don't want to break the
280   // idiom where each element of the extended vector is either zero or all ones.
281   if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy())
282     return false;
283
284   return true;
285 }
286
287
288 /// @brief Implement the transforms common to all CastInst visitors.
289 Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
290   Value *Src = CI.getOperand(0);
291
292   // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
293   // eliminate it now.
294   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast
295     if (Instruction::CastOps opc =
296             isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), DL)) {
297       // The first cast (CSrc) is eliminable so we need to fix up or replace
298       // the second cast (CI). CSrc will then have a good chance of being dead.
299       return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
300     }
301   }
302
303   // If we are casting a select then fold the cast into the select
304   if (SelectInst *SI = dyn_cast<SelectInst>(Src))
305     if (Instruction *NV = FoldOpIntoSelect(CI, SI))
306       return NV;
307
308   // If we are casting a PHI then fold the cast into the PHI
309   if (isa<PHINode>(Src)) {
310     // We don't do this if this would create a PHI node with an illegal type if
311     // it is currently legal.
312     if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() ||
313         ShouldChangeType(CI.getType(), Src->getType()))
314       if (Instruction *NV = FoldOpIntoPhi(CI))
315         return NV;
316   }
317
318   return nullptr;
319 }
320
321 /// CanEvaluateTruncated - Return true if we can evaluate the specified
322 /// expression tree as type Ty instead of its larger type, and arrive with the
323 /// same value.  This is used by code that tries to eliminate truncates.
324 ///
325 /// Ty will always be a type smaller than V.  We should return true if trunc(V)
326 /// can be computed by computing V in the smaller type.  If V is an instruction,
327 /// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
328 /// makes sense if x and y can be efficiently truncated.
329 ///
330 /// This function works on both vectors and scalars.
331 ///
332 static bool CanEvaluateTruncated(Value *V, Type *Ty, InstCombiner &IC,
333                                  Instruction *CxtI) {
334   // We can always evaluate constants in another type.
335   if (isa<Constant>(V))
336     return true;
337
338   Instruction *I = dyn_cast<Instruction>(V);
339   if (!I) return false;
340
341   Type *OrigTy = V->getType();
342
343   // If this is an extension from the dest type, we can eliminate it, even if it
344   // has multiple uses.
345   if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
346       I->getOperand(0)->getType() == Ty)
347     return true;
348
349   // We can't extend or shrink something that has multiple uses: doing so would
350   // require duplicating the instruction in general, which isn't profitable.
351   if (!I->hasOneUse()) return false;
352
353   unsigned Opc = I->getOpcode();
354   switch (Opc) {
355   case Instruction::Add:
356   case Instruction::Sub:
357   case Instruction::Mul:
358   case Instruction::And:
359   case Instruction::Or:
360   case Instruction::Xor:
361     // These operators can all arbitrarily be extended or truncated.
362     return CanEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
363            CanEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
364
365   case Instruction::UDiv:
366   case Instruction::URem: {
367     // UDiv and URem can be truncated if all the truncated bits are zero.
368     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
369     uint32_t BitWidth = Ty->getScalarSizeInBits();
370     if (BitWidth < OrigBitWidth) {
371       APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
372       if (IC.MaskedValueIsZero(I->getOperand(0), Mask, 0, CxtI) &&
373           IC.MaskedValueIsZero(I->getOperand(1), Mask, 0, CxtI)) {
374         return CanEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
375                CanEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
376       }
377     }
378     break;
379   }
380   case Instruction::Shl:
381     // If we are truncating the result of this SHL, and if it's a shift of a
382     // constant amount, we can always perform a SHL in a smaller type.
383     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
384       uint32_t BitWidth = Ty->getScalarSizeInBits();
385       if (CI->getLimitedValue(BitWidth) < BitWidth)
386         return CanEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI);
387     }
388     break;
389   case Instruction::LShr:
390     // If this is a truncate of a logical shr, we can truncate it to a smaller
391     // lshr iff we know that the bits we would otherwise be shifting in are
392     // already zeros.
393     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
394       uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
395       uint32_t BitWidth = Ty->getScalarSizeInBits();
396       if (IC.MaskedValueIsZero(I->getOperand(0),
397             APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth), 0, CxtI) &&
398           CI->getLimitedValue(BitWidth) < BitWidth) {
399         return CanEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI);
400       }
401     }
402     break;
403   case Instruction::Trunc:
404     // trunc(trunc(x)) -> trunc(x)
405     return true;
406   case Instruction::ZExt:
407   case Instruction::SExt:
408     // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
409     // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest
410     return true;
411   case Instruction::Select: {
412     SelectInst *SI = cast<SelectInst>(I);
413     return CanEvaluateTruncated(SI->getTrueValue(), Ty, IC, CxtI) &&
414            CanEvaluateTruncated(SI->getFalseValue(), Ty, IC, CxtI);
415   }
416   case Instruction::PHI: {
417     // We can change a phi if we can change all operands.  Note that we never
418     // get into trouble with cyclic PHIs here because we only consider
419     // instructions with a single use.
420     PHINode *PN = cast<PHINode>(I);
421     for (Value *IncValue : PN->incoming_values())
422       if (!CanEvaluateTruncated(IncValue, Ty, IC, CxtI))
423         return false;
424     return true;
425   }
426   default:
427     // TODO: Can handle more cases here.
428     break;
429   }
430
431   return false;
432 }
433
434 Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
435   if (Instruction *Result = commonCastTransforms(CI))
436     return Result;
437
438   // Test if the trunc is the user of a select which is part of a
439   // minimum or maximum operation. If so, don't do any more simplification.
440   // Even simplifying demanded bits can break the canonical form of a 
441   // min/max.
442   Value *LHS, *RHS;
443   if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)))
444     if (matchSelectPattern(SI, LHS, RHS) != SPF_UNKNOWN)
445       return nullptr;
446   
447   // See if we can simplify any instructions used by the input whose sole
448   // purpose is to compute bits we don't care about.
449   if (SimplifyDemandedInstructionBits(CI))
450     return &CI;
451
452   Value *Src = CI.getOperand(0);
453   Type *DestTy = CI.getType(), *SrcTy = Src->getType();
454
455   // Attempt to truncate the entire input expression tree to the destination
456   // type.   Only do this if the dest type is a simple type, don't convert the
457   // expression tree to something weird like i93 unless the source is also
458   // strange.
459   if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
460       CanEvaluateTruncated(Src, DestTy, *this, &CI)) {
461
462     // If this cast is a truncate, evaluting in a different type always
463     // eliminates the cast, so it is always a win.
464     DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
465           " to avoid cast: " << CI << '\n');
466     Value *Res = EvaluateInDifferentType(Src, DestTy, false);
467     assert(Res->getType() == DestTy);
468     return ReplaceInstUsesWith(CI, Res);
469   }
470
471   // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
472   if (DestTy->getScalarSizeInBits() == 1) {
473     Constant *One = ConstantInt::get(Src->getType(), 1);
474     Src = Builder->CreateAnd(Src, One);
475     Value *Zero = Constant::getNullValue(Src->getType());
476     return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
477   }
478
479   // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion.
480   Value *A = nullptr; ConstantInt *Cst = nullptr;
481   if (Src->hasOneUse() &&
482       match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) {
483     // We have three types to worry about here, the type of A, the source of
484     // the truncate (MidSize), and the destination of the truncate. We know that
485     // ASize < MidSize   and MidSize > ResultSize, but don't know the relation
486     // between ASize and ResultSize.
487     unsigned ASize = A->getType()->getPrimitiveSizeInBits();
488
489     // If the shift amount is larger than the size of A, then the result is
490     // known to be zero because all the input bits got shifted out.
491     if (Cst->getZExtValue() >= ASize)
492       return ReplaceInstUsesWith(CI, Constant::getNullValue(CI.getType()));
493
494     // Since we're doing an lshr and a zero extend, and know that the shift
495     // amount is smaller than ASize, it is always safe to do the shift in A's
496     // type, then zero extend or truncate to the result.
497     Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue());
498     Shift->takeName(Src);
499     return CastInst::CreateIntegerCast(Shift, CI.getType(), false);
500   }
501
502   // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
503   // type isn't non-native.
504   if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) &&
505       ShouldChangeType(Src->getType(), CI.getType()) &&
506       match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
507     Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr");
508     return BinaryOperator::CreateAnd(NewTrunc,
509                                      ConstantExpr::getTrunc(Cst, CI.getType()));
510   }
511
512   return nullptr;
513 }
514
515 /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
516 /// in order to eliminate the icmp.
517 Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
518                                              bool DoXform) {
519   // If we are just checking for a icmp eq of a single bit and zext'ing it
520   // to an integer, then shift the bit to the appropriate place and then
521   // cast to integer to avoid the comparison.
522   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
523     const APInt &Op1CV = Op1C->getValue();
524
525     // zext (x <s  0) to i32 --> x>>u31      true if signbit set.
526     // zext (x >s -1) to i32 --> (x>>u31)^1  true if signbit clear.
527     if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
528         (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
529       if (!DoXform) return ICI;
530
531       Value *In = ICI->getOperand(0);
532       Value *Sh = ConstantInt::get(In->getType(),
533                                    In->getType()->getScalarSizeInBits()-1);
534       In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
535       if (In->getType() != CI.getType())
536         In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/);
537
538       if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
539         Constant *One = ConstantInt::get(In->getType(), 1);
540         In = Builder->CreateXor(In, One, In->getName()+".not");
541       }
542
543       return ReplaceInstUsesWith(CI, In);
544     }
545
546     // zext (X == 0) to i32 --> X^1      iff X has only the low bit set.
547     // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
548     // zext (X == 1) to i32 --> X        iff X has only the low bit set.
549     // zext (X == 2) to i32 --> X>>1     iff X has only the 2nd bit set.
550     // zext (X != 0) to i32 --> X        iff X has only the low bit set.
551     // zext (X != 0) to i32 --> X>>1     iff X has only the 2nd bit set.
552     // zext (X != 1) to i32 --> X^1      iff X has only the low bit set.
553     // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
554     if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
555         // This only works for EQ and NE
556         ICI->isEquality()) {
557       // If Op1C some other power of two, convert:
558       uint32_t BitWidth = Op1C->getType()->getBitWidth();
559       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
560       computeKnownBits(ICI->getOperand(0), KnownZero, KnownOne, 0, &CI);
561
562       APInt KnownZeroMask(~KnownZero);
563       if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
564         if (!DoXform) return ICI;
565
566         bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
567         if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
568           // (X&4) == 2 --> false
569           // (X&4) != 2 --> true
570           Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()),
571                                            isNE);
572           Res = ConstantExpr::getZExt(Res, CI.getType());
573           return ReplaceInstUsesWith(CI, Res);
574         }
575
576         uint32_t ShiftAmt = KnownZeroMask.logBase2();
577         Value *In = ICI->getOperand(0);
578         if (ShiftAmt) {
579           // Perform a logical shr by shiftamt.
580           // Insert the shift to put the result in the low bit.
581           In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
582                                    In->getName()+".lobit");
583         }
584
585         if ((Op1CV != 0) == isNE) { // Toggle the low bit.
586           Constant *One = ConstantInt::get(In->getType(), 1);
587           In = Builder->CreateXor(In, One);
588         }
589
590         if (CI.getType() == In->getType())
591           return ReplaceInstUsesWith(CI, In);
592         return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
593       }
594     }
595   }
596
597   // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
598   // It is also profitable to transform icmp eq into not(xor(A, B)) because that
599   // may lead to additional simplifications.
600   if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) {
601     if (IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) {
602       uint32_t BitWidth = ITy->getBitWidth();
603       Value *LHS = ICI->getOperand(0);
604       Value *RHS = ICI->getOperand(1);
605
606       APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
607       APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
608       computeKnownBits(LHS, KnownZeroLHS, KnownOneLHS, 0, &CI);
609       computeKnownBits(RHS, KnownZeroRHS, KnownOneRHS, 0, &CI);
610
611       if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
612         APInt KnownBits = KnownZeroLHS | KnownOneLHS;
613         APInt UnknownBit = ~KnownBits;
614         if (UnknownBit.countPopulation() == 1) {
615           if (!DoXform) return ICI;
616
617           Value *Result = Builder->CreateXor(LHS, RHS);
618
619           // Mask off any bits that are set and won't be shifted away.
620           if (KnownOneLHS.uge(UnknownBit))
621             Result = Builder->CreateAnd(Result,
622                                         ConstantInt::get(ITy, UnknownBit));
623
624           // Shift the bit we're testing down to the lsb.
625           Result = Builder->CreateLShr(
626                Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
627
628           if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
629             Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1));
630           Result->takeName(ICI);
631           return ReplaceInstUsesWith(CI, Result);
632         }
633       }
634     }
635   }
636
637   return nullptr;
638 }
639
640 /// CanEvaluateZExtd - Determine if the specified value can be computed in the
641 /// specified wider type and produce the same low bits.  If not, return false.
642 ///
643 /// If this function returns true, it can also return a non-zero number of bits
644 /// (in BitsToClear) which indicates that the value it computes is correct for
645 /// the zero extend, but that the additional BitsToClear bits need to be zero'd
646 /// out.  For example, to promote something like:
647 ///
648 ///   %B = trunc i64 %A to i32
649 ///   %C = lshr i32 %B, 8
650 ///   %E = zext i32 %C to i64
651 ///
652 /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be
653 /// set to 8 to indicate that the promoted value needs to have bits 24-31
654 /// cleared in addition to bits 32-63.  Since an 'and' will be generated to
655 /// clear the top bits anyway, doing this has no extra cost.
656 ///
657 /// This function works on both vectors and scalars.
658 static bool CanEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear,
659                              InstCombiner &IC, Instruction *CxtI) {
660   BitsToClear = 0;
661   if (isa<Constant>(V))
662     return true;
663
664   Instruction *I = dyn_cast<Instruction>(V);
665   if (!I) return false;
666
667   // If the input is a truncate from the destination type, we can trivially
668   // eliminate it.
669   if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
670     return true;
671
672   // We can't extend or shrink something that has multiple uses: doing so would
673   // require duplicating the instruction in general, which isn't profitable.
674   if (!I->hasOneUse()) return false;
675
676   unsigned Opc = I->getOpcode(), Tmp;
677   switch (Opc) {
678   case Instruction::ZExt:  // zext(zext(x)) -> zext(x).
679   case Instruction::SExt:  // zext(sext(x)) -> sext(x).
680   case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x)
681     return true;
682   case Instruction::And:
683   case Instruction::Or:
684   case Instruction::Xor:
685   case Instruction::Add:
686   case Instruction::Sub:
687   case Instruction::Mul:
688     if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI) ||
689         !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI))
690       return false;
691     // These can all be promoted if neither operand has 'bits to clear'.
692     if (BitsToClear == 0 && Tmp == 0)
693       return true;
694
695     // If the operation is an AND/OR/XOR and the bits to clear are zero in the
696     // other side, BitsToClear is ok.
697     if (Tmp == 0 &&
698         (Opc == Instruction::And || Opc == Instruction::Or ||
699          Opc == Instruction::Xor)) {
700       // We use MaskedValueIsZero here for generality, but the case we care
701       // about the most is constant RHS.
702       unsigned VSize = V->getType()->getScalarSizeInBits();
703       if (IC.MaskedValueIsZero(I->getOperand(1),
704                                APInt::getHighBitsSet(VSize, BitsToClear),
705                                0, CxtI))
706         return true;
707     }
708
709     // Otherwise, we don't know how to analyze this BitsToClear case yet.
710     return false;
711
712   case Instruction::Shl:
713     // We can promote shl(x, cst) if we can promote x.  Since shl overwrites the
714     // upper bits we can reduce BitsToClear by the shift amount.
715     if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) {
716       if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
717         return false;
718       uint64_t ShiftAmt = Amt->getZExtValue();
719       BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
720       return true;
721     }
722     return false;
723   case Instruction::LShr:
724     // We can promote lshr(x, cst) if we can promote x.  This requires the
725     // ultimate 'and' to clear out the high zero bits we're clearing out though.
726     if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) {
727       if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
728         return false;
729       BitsToClear += Amt->getZExtValue();
730       if (BitsToClear > V->getType()->getScalarSizeInBits())
731         BitsToClear = V->getType()->getScalarSizeInBits();
732       return true;
733     }
734     // Cannot promote variable LSHR.
735     return false;
736   case Instruction::Select:
737     if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI) ||
738         !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear, IC, CxtI) ||
739         // TODO: If important, we could handle the case when the BitsToClear are
740         // known zero in the disagreeing side.
741         Tmp != BitsToClear)
742       return false;
743     return true;
744
745   case Instruction::PHI: {
746     // We can change a phi if we can change all operands.  Note that we never
747     // get into trouble with cyclic PHIs here because we only consider
748     // instructions with a single use.
749     PHINode *PN = cast<PHINode>(I);
750     if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear, IC, CxtI))
751       return false;
752     for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
753       if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp, IC, CxtI) ||
754           // TODO: If important, we could handle the case when the BitsToClear
755           // are known zero in the disagreeing input.
756           Tmp != BitsToClear)
757         return false;
758     return true;
759   }
760   default:
761     // TODO: Can handle more cases here.
762     return false;
763   }
764 }
765
766 Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
767   // If this zero extend is only used by a truncate, let the truncate be
768   // eliminated before we try to optimize this zext.
769   if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
770     return nullptr;
771
772   // If one of the common conversion will work, do it.
773   if (Instruction *Result = commonCastTransforms(CI))
774     return Result;
775
776   // See if we can simplify any instructions used by the input whose sole
777   // purpose is to compute bits we don't care about.
778   if (SimplifyDemandedInstructionBits(CI))
779     return &CI;
780
781   Value *Src = CI.getOperand(0);
782   Type *SrcTy = Src->getType(), *DestTy = CI.getType();
783
784   // Attempt to extend the entire input expression tree to the destination
785   // type.   Only do this if the dest type is a simple type, don't convert the
786   // expression tree to something weird like i93 unless the source is also
787   // strange.
788   unsigned BitsToClear;
789   if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
790       CanEvaluateZExtd(Src, DestTy, BitsToClear, *this, &CI)) {
791     assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
792            "Unreasonable BitsToClear");
793
794     // Okay, we can transform this!  Insert the new expression now.
795     DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
796           " to avoid zero extend: " << CI);
797     Value *Res = EvaluateInDifferentType(Src, DestTy, false);
798     assert(Res->getType() == DestTy);
799
800     uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
801     uint32_t DestBitSize = DestTy->getScalarSizeInBits();
802
803     // If the high bits are already filled with zeros, just replace this
804     // cast with the result.
805     if (MaskedValueIsZero(Res,
806                           APInt::getHighBitsSet(DestBitSize,
807                                                 DestBitSize-SrcBitsKept),
808                              0, &CI))
809       return ReplaceInstUsesWith(CI, Res);
810
811     // We need to emit an AND to clear the high bits.
812     Constant *C = ConstantInt::get(Res->getType(),
813                                APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
814     return BinaryOperator::CreateAnd(Res, C);
815   }
816
817   // If this is a TRUNC followed by a ZEXT then we are dealing with integral
818   // types and if the sizes are just right we can convert this into a logical
819   // 'and' which will be much cheaper than the pair of casts.
820   if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
821     // TODO: Subsume this into EvaluateInDifferentType.
822
823     // Get the sizes of the types involved.  We know that the intermediate type
824     // will be smaller than A or C, but don't know the relation between A and C.
825     Value *A = CSrc->getOperand(0);
826     unsigned SrcSize = A->getType()->getScalarSizeInBits();
827     unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
828     unsigned DstSize = CI.getType()->getScalarSizeInBits();
829     // If we're actually extending zero bits, then if
830     // SrcSize <  DstSize: zext(a & mask)
831     // SrcSize == DstSize: a & mask
832     // SrcSize  > DstSize: trunc(a) & mask
833     if (SrcSize < DstSize) {
834       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
835       Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
836       Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
837       return new ZExtInst(And, CI.getType());
838     }
839
840     if (SrcSize == DstSize) {
841       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
842       return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
843                                                            AndValue));
844     }
845     if (SrcSize > DstSize) {
846       Value *Trunc = Builder->CreateTrunc(A, CI.getType());
847       APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
848       return BinaryOperator::CreateAnd(Trunc,
849                                        ConstantInt::get(Trunc->getType(),
850                                                         AndValue));
851     }
852   }
853
854   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
855     return transformZExtICmp(ICI, CI);
856
857   BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
858   if (SrcI && SrcI->getOpcode() == Instruction::Or) {
859     // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
860     // of the (zext icmp) will be transformed.
861     ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
862     ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
863     if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
864         (transformZExtICmp(LHS, CI, false) ||
865          transformZExtICmp(RHS, CI, false))) {
866       Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
867       Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
868       return BinaryOperator::Create(Instruction::Or, LCast, RCast);
869     }
870   }
871
872   // zext(trunc(X) & C) -> (X & zext(C)).
873   Constant *C;
874   Value *X;
875   if (SrcI &&
876       match(SrcI, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) &&
877       X->getType() == CI.getType())
878     return BinaryOperator::CreateAnd(X, ConstantExpr::getZExt(C, CI.getType()));
879
880   // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)).
881   Value *And;
882   if (SrcI && match(SrcI, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) &&
883       match(And, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Specific(C)))) &&
884       X->getType() == CI.getType()) {
885     Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
886     return BinaryOperator::CreateXor(Builder->CreateAnd(X, ZC), ZC);
887   }
888
889   // zext (xor i1 X, true) to i32  --> xor (zext i1 X to i32), 1
890   if (SrcI && SrcI->hasOneUse() &&
891       SrcI->getType()->getScalarType()->isIntegerTy(1) &&
892       match(SrcI, m_Not(m_Value(X))) && (!X->hasOneUse() || !isa<CmpInst>(X))) {
893     Value *New = Builder->CreateZExt(X, CI.getType());
894     return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
895   }
896
897   return nullptr;
898 }
899
900 /// transformSExtICmp - Transform (sext icmp) to bitwise / integer operations
901 /// in order to eliminate the icmp.
902 Instruction *InstCombiner::transformSExtICmp(ICmpInst *ICI, Instruction &CI) {
903   Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1);
904   ICmpInst::Predicate Pred = ICI->getPredicate();
905
906   // Don't bother if Op1 isn't of vector or integer type.
907   if (!Op1->getType()->isIntOrIntVectorTy())
908     return nullptr;
909
910   if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
911     // (x <s  0) ? -1 : 0 -> ashr x, 31        -> all ones if negative
912     // (x >s -1) ? -1 : 0 -> not (ashr x, 31)  -> all ones if positive
913     if ((Pred == ICmpInst::ICMP_SLT && Op1C->isNullValue()) ||
914         (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) {
915
916       Value *Sh = ConstantInt::get(Op0->getType(),
917                                    Op0->getType()->getScalarSizeInBits()-1);
918       Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit");
919       if (In->getType() != CI.getType())
920         In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/);
921
922       if (Pred == ICmpInst::ICMP_SGT)
923         In = Builder->CreateNot(In, In->getName()+".not");
924       return ReplaceInstUsesWith(CI, In);
925     }
926   }
927
928   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
929     // If we know that only one bit of the LHS of the icmp can be set and we
930     // have an equality comparison with zero or a power of 2, we can transform
931     // the icmp and sext into bitwise/integer operations.
932     if (ICI->hasOneUse() &&
933         ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
934       unsigned BitWidth = Op1C->getType()->getBitWidth();
935       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
936       computeKnownBits(Op0, KnownZero, KnownOne, 0, &CI);
937
938       APInt KnownZeroMask(~KnownZero);
939       if (KnownZeroMask.isPowerOf2()) {
940         Value *In = ICI->getOperand(0);
941
942         // If the icmp tests for a known zero bit we can constant fold it.
943         if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
944           Value *V = Pred == ICmpInst::ICMP_NE ?
945                        ConstantInt::getAllOnesValue(CI.getType()) :
946                        ConstantInt::getNullValue(CI.getType());
947           return ReplaceInstUsesWith(CI, V);
948         }
949
950         if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
951           // sext ((x & 2^n) == 0)   -> (x >> n) - 1
952           // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
953           unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
954           // Perform a right shift to place the desired bit in the LSB.
955           if (ShiftAmt)
956             In = Builder->CreateLShr(In,
957                                      ConstantInt::get(In->getType(), ShiftAmt));
958
959           // At this point "In" is either 1 or 0. Subtract 1 to turn
960           // {1, 0} -> {0, -1}.
961           In = Builder->CreateAdd(In,
962                                   ConstantInt::getAllOnesValue(In->getType()),
963                                   "sext");
964         } else {
965           // sext ((x & 2^n) != 0)   -> (x << bitwidth-n) a>> bitwidth-1
966           // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
967           unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
968           // Perform a left shift to place the desired bit in the MSB.
969           if (ShiftAmt)
970             In = Builder->CreateShl(In,
971                                     ConstantInt::get(In->getType(), ShiftAmt));
972
973           // Distribute the bit over the whole bit width.
974           In = Builder->CreateAShr(In, ConstantInt::get(In->getType(),
975                                                         BitWidth - 1), "sext");
976         }
977
978         if (CI.getType() == In->getType())
979           return ReplaceInstUsesWith(CI, In);
980         return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/);
981       }
982     }
983   }
984
985   return nullptr;
986 }
987
988 /// CanEvaluateSExtd - Return true if we can take the specified value
989 /// and return it as type Ty without inserting any new casts and without
990 /// changing the value of the common low bits.  This is used by code that tries
991 /// to promote integer operations to a wider types will allow us to eliminate
992 /// the extension.
993 ///
994 /// This function works on both vectors and scalars.
995 ///
996 static bool CanEvaluateSExtd(Value *V, Type *Ty) {
997   assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
998          "Can't sign extend type to a smaller type");
999   // If this is a constant, it can be trivially promoted.
1000   if (isa<Constant>(V))
1001     return true;
1002
1003   Instruction *I = dyn_cast<Instruction>(V);
1004   if (!I) return false;
1005
1006   // If this is a truncate from the dest type, we can trivially eliminate it.
1007   if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
1008     return true;
1009
1010   // We can't extend or shrink something that has multiple uses: doing so would
1011   // require duplicating the instruction in general, which isn't profitable.
1012   if (!I->hasOneUse()) return false;
1013
1014   switch (I->getOpcode()) {
1015   case Instruction::SExt:  // sext(sext(x)) -> sext(x)
1016   case Instruction::ZExt:  // sext(zext(x)) -> zext(x)
1017   case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
1018     return true;
1019   case Instruction::And:
1020   case Instruction::Or:
1021   case Instruction::Xor:
1022   case Instruction::Add:
1023   case Instruction::Sub:
1024   case Instruction::Mul:
1025     // These operators can all arbitrarily be extended if their inputs can.
1026     return CanEvaluateSExtd(I->getOperand(0), Ty) &&
1027            CanEvaluateSExtd(I->getOperand(1), Ty);
1028
1029   //case Instruction::Shl:   TODO
1030   //case Instruction::LShr:  TODO
1031
1032   case Instruction::Select:
1033     return CanEvaluateSExtd(I->getOperand(1), Ty) &&
1034            CanEvaluateSExtd(I->getOperand(2), Ty);
1035
1036   case Instruction::PHI: {
1037     // We can change a phi if we can change all operands.  Note that we never
1038     // get into trouble with cyclic PHIs here because we only consider
1039     // instructions with a single use.
1040     PHINode *PN = cast<PHINode>(I);
1041     for (Value *IncValue : PN->incoming_values())
1042       if (!CanEvaluateSExtd(IncValue, Ty)) return false;
1043     return true;
1044   }
1045   default:
1046     // TODO: Can handle more cases here.
1047     break;
1048   }
1049
1050   return false;
1051 }
1052
1053 Instruction *InstCombiner::visitSExt(SExtInst &CI) {
1054   // If this sign extend is only used by a truncate, let the truncate be
1055   // eliminated before we try to optimize this sext.
1056   if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
1057     return nullptr;
1058
1059   if (Instruction *I = commonCastTransforms(CI))
1060     return I;
1061
1062   // See if we can simplify any instructions used by the input whose sole
1063   // purpose is to compute bits we don't care about.
1064   if (SimplifyDemandedInstructionBits(CI))
1065     return &CI;
1066
1067   Value *Src = CI.getOperand(0);
1068   Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1069
1070   // If we know that the value being extended is positive, we can use a zext
1071   // instead. 
1072   bool KnownZero, KnownOne;
1073   ComputeSignBit(Src, KnownZero, KnownOne, 0, &CI);
1074   if (KnownZero) {
1075     Value *ZExt = Builder->CreateZExt(Src, DestTy);
1076     return ReplaceInstUsesWith(CI, ZExt);
1077   }
1078
1079   // Attempt to extend the entire input expression tree to the destination
1080   // type.   Only do this if the dest type is a simple type, don't convert the
1081   // expression tree to something weird like i93 unless the source is also
1082   // strange.
1083   if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
1084       CanEvaluateSExtd(Src, DestTy)) {
1085     // Okay, we can transform this!  Insert the new expression now.
1086     DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1087           " to avoid sign extend: " << CI);
1088     Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1089     assert(Res->getType() == DestTy);
1090
1091     uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1092     uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1093
1094     // If the high bits are already filled with sign bit, just replace this
1095     // cast with the result.
1096     if (ComputeNumSignBits(Res, 0, &CI) > DestBitSize - SrcBitSize)
1097       return ReplaceInstUsesWith(CI, Res);
1098
1099     // We need to emit a shl + ashr to do the sign extend.
1100     Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1101     return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
1102                                       ShAmt);
1103   }
1104
1105   // If this input is a trunc from our destination, then turn sext(trunc(x))
1106   // into shifts.
1107   if (TruncInst *TI = dyn_cast<TruncInst>(Src))
1108     if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
1109       uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1110       uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1111
1112       // We need to emit a shl + ashr to do the sign extend.
1113       Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1114       Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext");
1115       return BinaryOperator::CreateAShr(Res, ShAmt);
1116     }
1117
1118   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1119     return transformSExtICmp(ICI, CI);
1120
1121   // If the input is a shl/ashr pair of a same constant, then this is a sign
1122   // extension from a smaller value.  If we could trust arbitrary bitwidth
1123   // integers, we could turn this into a truncate to the smaller bit and then
1124   // use a sext for the whole extension.  Since we don't, look deeper and check
1125   // for a truncate.  If the source and dest are the same type, eliminate the
1126   // trunc and extend and just do shifts.  For example, turn:
1127   //   %a = trunc i32 %i to i8
1128   //   %b = shl i8 %a, 6
1129   //   %c = ashr i8 %b, 6
1130   //   %d = sext i8 %c to i32
1131   // into:
1132   //   %a = shl i32 %i, 30
1133   //   %d = ashr i32 %a, 30
1134   Value *A = nullptr;
1135   // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
1136   ConstantInt *BA = nullptr, *CA = nullptr;
1137   if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)),
1138                         m_ConstantInt(CA))) &&
1139       BA == CA && A->getType() == CI.getType()) {
1140     unsigned MidSize = Src->getType()->getScalarSizeInBits();
1141     unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
1142     unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1143     Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
1144     A = Builder->CreateShl(A, ShAmtV, CI.getName());
1145     return BinaryOperator::CreateAShr(A, ShAmtV);
1146   }
1147
1148   return nullptr;
1149 }
1150
1151
1152 /// FitsInFPType - Return a Constant* for the specified FP constant if it fits
1153 /// in the specified FP type without changing its value.
1154 static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1155   bool losesInfo;
1156   APFloat F = CFP->getValueAPF();
1157   (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1158   if (!losesInfo)
1159     return ConstantFP::get(CFP->getContext(), F);
1160   return nullptr;
1161 }
1162
1163 /// LookThroughFPExtensions - If this is an fp extension instruction, look
1164 /// through it until we get the source value.
1165 static Value *LookThroughFPExtensions(Value *V) {
1166   if (Instruction *I = dyn_cast<Instruction>(V))
1167     if (I->getOpcode() == Instruction::FPExt)
1168       return LookThroughFPExtensions(I->getOperand(0));
1169
1170   // If this value is a constant, return the constant in the smallest FP type
1171   // that can accurately represent it.  This allows us to turn
1172   // (float)((double)X+2.0) into x+2.0f.
1173   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1174     if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext()))
1175       return V;  // No constant folding of this.
1176     // See if the value can be truncated to half and then reextended.
1177     if (Value *V = FitsInFPType(CFP, APFloat::IEEEhalf))
1178       return V;
1179     // See if the value can be truncated to float and then reextended.
1180     if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
1181       return V;
1182     if (CFP->getType()->isDoubleTy())
1183       return V;  // Won't shrink.
1184     if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
1185       return V;
1186     // Don't try to shrink to various long double types.
1187   }
1188
1189   return V;
1190 }
1191
1192 Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
1193   if (Instruction *I = commonCastTransforms(CI))
1194     return I;
1195   // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to
1196   // simpilify this expression to avoid one or more of the trunc/extend
1197   // operations if we can do so without changing the numerical results.
1198   //
1199   // The exact manner in which the widths of the operands interact to limit
1200   // what we can and cannot do safely varies from operation to operation, and
1201   // is explained below in the various case statements.
1202   BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
1203   if (OpI && OpI->hasOneUse()) {
1204     Value *LHSOrig = LookThroughFPExtensions(OpI->getOperand(0));
1205     Value *RHSOrig = LookThroughFPExtensions(OpI->getOperand(1));
1206     unsigned OpWidth = OpI->getType()->getFPMantissaWidth();
1207     unsigned LHSWidth = LHSOrig->getType()->getFPMantissaWidth();
1208     unsigned RHSWidth = RHSOrig->getType()->getFPMantissaWidth();
1209     unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1210     unsigned DstWidth = CI.getType()->getFPMantissaWidth();
1211     switch (OpI->getOpcode()) {
1212       default: break;
1213       case Instruction::FAdd:
1214       case Instruction::FSub:
1215         // For addition and subtraction, the infinitely precise result can
1216         // essentially be arbitrarily wide; proving that double rounding
1217         // will not occur because the result of OpI is exact (as we will for
1218         // FMul, for example) is hopeless.  However, we *can* nonetheless
1219         // frequently know that double rounding cannot occur (or that it is
1220         // innocuous) by taking advantage of the specific structure of
1221         // infinitely-precise results that admit double rounding.
1222         //
1223         // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient
1224         // to represent both sources, we can guarantee that the double
1225         // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis,
1226         // "A Rigorous Framework for Fully Supporting the IEEE Standard ..."
1227         // for proof of this fact).
1228         //
1229         // Note: Figueroa does not consider the case where DstFormat !=
1230         // SrcFormat.  It's possible (likely even!) that this analysis
1231         // could be tightened for those cases, but they are rare (the main
1232         // case of interest here is (float)((double)float + float)).
1233         if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1234           if (LHSOrig->getType() != CI.getType())
1235             LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1236           if (RHSOrig->getType() != CI.getType())
1237             RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1238           Instruction *RI =
1239             BinaryOperator::Create(OpI->getOpcode(), LHSOrig, RHSOrig);
1240           RI->copyFastMathFlags(OpI);
1241           return RI;
1242         }
1243         break;
1244       case Instruction::FMul:
1245         // For multiplication, the infinitely precise result has at most
1246         // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient
1247         // that such a value can be exactly represented, then no double
1248         // rounding can possibly occur; we can safely perform the operation
1249         // in the destination format if it can represent both sources.
1250         if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1251           if (LHSOrig->getType() != CI.getType())
1252             LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1253           if (RHSOrig->getType() != CI.getType())
1254             RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1255           Instruction *RI =
1256             BinaryOperator::CreateFMul(LHSOrig, RHSOrig);
1257           RI->copyFastMathFlags(OpI);
1258           return RI;
1259         }
1260         break;
1261       case Instruction::FDiv:
1262         // For division, we use again use the bound from Figueroa's
1263         // dissertation.  I am entirely certain that this bound can be
1264         // tightened in the unbalanced operand case by an analysis based on
1265         // the diophantine rational approximation bound, but the well-known
1266         // condition used here is a good conservative first pass.
1267         // TODO: Tighten bound via rigorous analysis of the unbalanced case.
1268         if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1269           if (LHSOrig->getType() != CI.getType())
1270             LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1271           if (RHSOrig->getType() != CI.getType())
1272             RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1273           Instruction *RI =
1274             BinaryOperator::CreateFDiv(LHSOrig, RHSOrig);
1275           RI->copyFastMathFlags(OpI);
1276           return RI;
1277         }
1278         break;
1279       case Instruction::FRem:
1280         // Remainder is straightforward.  Remainder is always exact, so the
1281         // type of OpI doesn't enter into things at all.  We simply evaluate
1282         // in whichever source type is larger, then convert to the
1283         // destination type.
1284         if (SrcWidth == OpWidth)
1285           break;
1286         if (LHSWidth < SrcWidth)
1287           LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
1288         else if (RHSWidth <= SrcWidth)
1289           RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType());
1290         if (LHSOrig != OpI->getOperand(0) || RHSOrig != OpI->getOperand(1)) {
1291           Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig);
1292           if (Instruction *RI = dyn_cast<Instruction>(ExactResult))
1293             RI->copyFastMathFlags(OpI);
1294           return CastInst::CreateFPCast(ExactResult, CI.getType());
1295         }
1296     }
1297
1298     // (fptrunc (fneg x)) -> (fneg (fptrunc x))
1299     if (BinaryOperator::isFNeg(OpI)) {
1300       Value *InnerTrunc = Builder->CreateFPTrunc(OpI->getOperand(1),
1301                                                  CI.getType());
1302       Instruction *RI = BinaryOperator::CreateFNeg(InnerTrunc);
1303       RI->copyFastMathFlags(OpI);
1304       return RI;
1305     }
1306   }
1307
1308   // (fptrunc (select cond, R1, Cst)) -->
1309   // (select cond, (fptrunc R1), (fptrunc Cst))
1310   SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0));
1311   if (SI &&
1312       (isa<ConstantFP>(SI->getOperand(1)) ||
1313        isa<ConstantFP>(SI->getOperand(2)))) {
1314     Value *LHSTrunc = Builder->CreateFPTrunc(SI->getOperand(1),
1315                                              CI.getType());
1316     Value *RHSTrunc = Builder->CreateFPTrunc(SI->getOperand(2),
1317                                              CI.getType());
1318     return SelectInst::Create(SI->getOperand(0), LHSTrunc, RHSTrunc);
1319   }
1320
1321   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI.getOperand(0));
1322   if (II) {
1323     switch (II->getIntrinsicID()) {
1324       default: break;
1325       case Intrinsic::fabs: {
1326         // (fptrunc (fabs x)) -> (fabs (fptrunc x))
1327         Value *InnerTrunc = Builder->CreateFPTrunc(II->getArgOperand(0),
1328                                                    CI.getType());
1329         Type *IntrinsicType[] = { CI.getType() };
1330         Function *Overload =
1331           Intrinsic::getDeclaration(CI.getParent()->getParent()->getParent(),
1332                                     II->getIntrinsicID(), IntrinsicType);
1333
1334         Value *Args[] = { InnerTrunc };
1335         return CallInst::Create(Overload, Args, II->getName());
1336       }
1337     }
1338   }
1339
1340   return nullptr;
1341 }
1342
1343 Instruction *InstCombiner::visitFPExt(CastInst &CI) {
1344   return commonCastTransforms(CI);
1345 }
1346
1347 // fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X)
1348 // This is safe if the intermediate type has enough bits in its mantissa to
1349 // accurately represent all values of X.  For example, this won't work with
1350 // i64 -> float -> i64.
1351 Instruction *InstCombiner::FoldItoFPtoI(Instruction &FI) {
1352   if (!isa<UIToFPInst>(FI.getOperand(0)) && !isa<SIToFPInst>(FI.getOperand(0)))
1353     return nullptr;
1354   Instruction *OpI = cast<Instruction>(FI.getOperand(0));
1355
1356   Value *SrcI = OpI->getOperand(0);
1357   Type *FITy = FI.getType();
1358   Type *OpITy = OpI->getType();
1359   Type *SrcTy = SrcI->getType();
1360   bool IsInputSigned = isa<SIToFPInst>(OpI);
1361   bool IsOutputSigned = isa<FPToSIInst>(FI);
1362
1363   // We can safely assume the conversion won't overflow the output range,
1364   // because (for example) (uint8_t)18293.f is undefined behavior.
1365
1366   // Since we can assume the conversion won't overflow, our decision as to
1367   // whether the input will fit in the float should depend on the minimum
1368   // of the input range and output range.
1369
1370   // This means this is also safe for a signed input and unsigned output, since
1371   // a negative input would lead to undefined behavior.
1372   int InputSize = (int)SrcTy->getScalarSizeInBits() - IsInputSigned;
1373   int OutputSize = (int)FITy->getScalarSizeInBits() - IsOutputSigned;
1374   int ActualSize = std::min(InputSize, OutputSize);
1375
1376   if (ActualSize <= OpITy->getFPMantissaWidth()) {
1377     if (FITy->getScalarSizeInBits() > SrcTy->getScalarSizeInBits()) {
1378       if (IsInputSigned && IsOutputSigned)
1379         return new SExtInst(SrcI, FITy);
1380       return new ZExtInst(SrcI, FITy);
1381     }
1382     if (FITy->getScalarSizeInBits() < SrcTy->getScalarSizeInBits())
1383       return new TruncInst(SrcI, FITy);
1384     if (SrcTy == FITy)
1385       return ReplaceInstUsesWith(FI, SrcI);
1386     return new BitCastInst(SrcI, FITy);
1387   }
1388   return nullptr;
1389 }
1390
1391 Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
1392   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1393   if (!OpI)
1394     return commonCastTransforms(FI);
1395
1396   if (Instruction *I = FoldItoFPtoI(FI))
1397     return I;
1398
1399   return commonCastTransforms(FI);
1400 }
1401
1402 Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
1403   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1404   if (!OpI)
1405     return commonCastTransforms(FI);
1406
1407   if (Instruction *I = FoldItoFPtoI(FI))
1408     return I;
1409
1410   return commonCastTransforms(FI);
1411 }
1412
1413 Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
1414   return commonCastTransforms(CI);
1415 }
1416
1417 Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
1418   return commonCastTransforms(CI);
1419 }
1420
1421 Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
1422   // If the source integer type is not the intptr_t type for this target, do a
1423   // trunc or zext to the intptr_t type, then inttoptr of it.  This allows the
1424   // cast to be exposed to other transforms.
1425   unsigned AS = CI.getAddressSpace();
1426   if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
1427       DL.getPointerSizeInBits(AS)) {
1428     Type *Ty = DL.getIntPtrType(CI.getContext(), AS);
1429     if (CI.getType()->isVectorTy()) // Handle vectors of pointers.
1430       Ty = VectorType::get(Ty, CI.getType()->getVectorNumElements());
1431
1432     Value *P = Builder->CreateZExtOrTrunc(CI.getOperand(0), Ty);
1433     return new IntToPtrInst(P, CI.getType());
1434   }
1435
1436   if (Instruction *I = commonCastTransforms(CI))
1437     return I;
1438
1439   return nullptr;
1440 }
1441
1442 /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
1443 Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
1444   Value *Src = CI.getOperand(0);
1445
1446   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
1447     // If casting the result of a getelementptr instruction with no offset, turn
1448     // this into a cast of the original pointer!
1449     if (GEP->hasAllZeroIndices() &&
1450         // If CI is an addrspacecast and GEP changes the poiner type, merging
1451         // GEP into CI would undo canonicalizing addrspacecast with different
1452         // pointer types, causing infinite loops.
1453         (!isa<AddrSpaceCastInst>(CI) ||
1454           GEP->getType() == GEP->getPointerOperand()->getType())) {
1455       // Changing the cast operand is usually not a good idea but it is safe
1456       // here because the pointer operand is being replaced with another
1457       // pointer operand so the opcode doesn't need to change.
1458       Worklist.Add(GEP);
1459       CI.setOperand(0, GEP->getOperand(0));
1460       return &CI;
1461     }
1462   }
1463
1464   return commonCastTransforms(CI);
1465 }
1466
1467 Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
1468   // If the destination integer type is not the intptr_t type for this target,
1469   // do a ptrtoint to intptr_t then do a trunc or zext.  This allows the cast
1470   // to be exposed to other transforms.
1471
1472   Type *Ty = CI.getType();
1473   unsigned AS = CI.getPointerAddressSpace();
1474
1475   if (Ty->getScalarSizeInBits() == DL.getPointerSizeInBits(AS))
1476     return commonPointerCastTransforms(CI);
1477
1478   Type *PtrTy = DL.getIntPtrType(CI.getContext(), AS);
1479   if (Ty->isVectorTy()) // Handle vectors of pointers.
1480     PtrTy = VectorType::get(PtrTy, Ty->getVectorNumElements());
1481
1482   Value *P = Builder->CreatePtrToInt(CI.getOperand(0), PtrTy);
1483   return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
1484 }
1485
1486 /// OptimizeVectorResize - This input value (which is known to have vector type)
1487 /// is being zero extended or truncated to the specified vector type.  Try to
1488 /// replace it with a shuffle (and vector/vector bitcast) if possible.
1489 ///
1490 /// The source and destination vector types may have different element types.
1491 static Instruction *OptimizeVectorResize(Value *InVal, VectorType *DestTy,
1492                                          InstCombiner &IC) {
1493   // We can only do this optimization if the output is a multiple of the input
1494   // element size, or the input is a multiple of the output element size.
1495   // Convert the input type to have the same element type as the output.
1496   VectorType *SrcTy = cast<VectorType>(InVal->getType());
1497
1498   if (SrcTy->getElementType() != DestTy->getElementType()) {
1499     // The input types don't need to be identical, but for now they must be the
1500     // same size.  There is no specific reason we couldn't handle things like
1501     // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
1502     // there yet.
1503     if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
1504         DestTy->getElementType()->getPrimitiveSizeInBits())
1505       return nullptr;
1506
1507     SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements());
1508     InVal = IC.Builder->CreateBitCast(InVal, SrcTy);
1509   }
1510
1511   // Now that the element types match, get the shuffle mask and RHS of the
1512   // shuffle to use, which depends on whether we're increasing or decreasing the
1513   // size of the input.
1514   SmallVector<uint32_t, 16> ShuffleMask;
1515   Value *V2;
1516
1517   if (SrcTy->getNumElements() > DestTy->getNumElements()) {
1518     // If we're shrinking the number of elements, just shuffle in the low
1519     // elements from the input and use undef as the second shuffle input.
1520     V2 = UndefValue::get(SrcTy);
1521     for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i)
1522       ShuffleMask.push_back(i);
1523
1524   } else {
1525     // If we're increasing the number of elements, shuffle in all of the
1526     // elements from InVal and fill the rest of the result elements with zeros
1527     // from a constant zero.
1528     V2 = Constant::getNullValue(SrcTy);
1529     unsigned SrcElts = SrcTy->getNumElements();
1530     for (unsigned i = 0, e = SrcElts; i != e; ++i)
1531       ShuffleMask.push_back(i);
1532
1533     // The excess elements reference the first element of the zero input.
1534     for (unsigned i = 0, e = DestTy->getNumElements()-SrcElts; i != e; ++i)
1535       ShuffleMask.push_back(SrcElts);
1536   }
1537
1538   return new ShuffleVectorInst(InVal, V2,
1539                                ConstantDataVector::get(V2->getContext(),
1540                                                        ShuffleMask));
1541 }
1542
1543 static bool isMultipleOfTypeSize(unsigned Value, Type *Ty) {
1544   return Value % Ty->getPrimitiveSizeInBits() == 0;
1545 }
1546
1547 static unsigned getTypeSizeIndex(unsigned Value, Type *Ty) {
1548   return Value / Ty->getPrimitiveSizeInBits();
1549 }
1550
1551 /// CollectInsertionElements - V is a value which is inserted into a vector of
1552 /// VecEltTy.  Look through the value to see if we can decompose it into
1553 /// insertions into the vector.  See the example in the comment for
1554 /// OptimizeIntegerToVectorInsertions for the pattern this handles.
1555 /// The type of V is always a non-zero multiple of VecEltTy's size.
1556 /// Shift is the number of bits between the lsb of V and the lsb of
1557 /// the vector.
1558 ///
1559 /// This returns false if the pattern can't be matched or true if it can,
1560 /// filling in Elements with the elements found here.
1561 static bool CollectInsertionElements(Value *V, unsigned Shift,
1562                                      SmallVectorImpl<Value *> &Elements,
1563                                      Type *VecEltTy, bool isBigEndian) {
1564   assert(isMultipleOfTypeSize(Shift, VecEltTy) &&
1565          "Shift should be a multiple of the element type size");
1566
1567   // Undef values never contribute useful bits to the result.
1568   if (isa<UndefValue>(V)) return true;
1569
1570   // If we got down to a value of the right type, we win, try inserting into the
1571   // right element.
1572   if (V->getType() == VecEltTy) {
1573     // Inserting null doesn't actually insert any elements.
1574     if (Constant *C = dyn_cast<Constant>(V))
1575       if (C->isNullValue())
1576         return true;
1577
1578     unsigned ElementIndex = getTypeSizeIndex(Shift, VecEltTy);
1579     if (isBigEndian)
1580       ElementIndex = Elements.size() - ElementIndex - 1;
1581
1582     // Fail if multiple elements are inserted into this slot.
1583     if (Elements[ElementIndex])
1584       return false;
1585
1586     Elements[ElementIndex] = V;
1587     return true;
1588   }
1589
1590   if (Constant *C = dyn_cast<Constant>(V)) {
1591     // Figure out the # elements this provides, and bitcast it or slice it up
1592     // as required.
1593     unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(),
1594                                         VecEltTy);
1595     // If the constant is the size of a vector element, we just need to bitcast
1596     // it to the right type so it gets properly inserted.
1597     if (NumElts == 1)
1598       return CollectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy),
1599                                       Shift, Elements, VecEltTy, isBigEndian);
1600
1601     // Okay, this is a constant that covers multiple elements.  Slice it up into
1602     // pieces and insert each element-sized piece into the vector.
1603     if (!isa<IntegerType>(C->getType()))
1604       C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(),
1605                                        C->getType()->getPrimitiveSizeInBits()));
1606     unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
1607     Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
1608
1609     for (unsigned i = 0; i != NumElts; ++i) {
1610       unsigned ShiftI = Shift+i*ElementSize;
1611       Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(),
1612                                                                   ShiftI));
1613       Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
1614       if (!CollectInsertionElements(Piece, ShiftI, Elements, VecEltTy,
1615                                     isBigEndian))
1616         return false;
1617     }
1618     return true;
1619   }
1620
1621   if (!V->hasOneUse()) return false;
1622
1623   Instruction *I = dyn_cast<Instruction>(V);
1624   if (!I) return false;
1625   switch (I->getOpcode()) {
1626   default: return false; // Unhandled case.
1627   case Instruction::BitCast:
1628     return CollectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
1629                                     isBigEndian);
1630   case Instruction::ZExt:
1631     if (!isMultipleOfTypeSize(
1632                           I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
1633                               VecEltTy))
1634       return false;
1635     return CollectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
1636                                     isBigEndian);
1637   case Instruction::Or:
1638     return CollectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
1639                                     isBigEndian) &&
1640            CollectInsertionElements(I->getOperand(1), Shift, Elements, VecEltTy,
1641                                     isBigEndian);
1642   case Instruction::Shl: {
1643     // Must be shifting by a constant that is a multiple of the element size.
1644     ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1645     if (!CI) return false;
1646     Shift += CI->getZExtValue();
1647     if (!isMultipleOfTypeSize(Shift, VecEltTy)) return false;
1648     return CollectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
1649                                     isBigEndian);
1650   }
1651
1652   }
1653 }
1654
1655
1656 /// OptimizeIntegerToVectorInsertions - If the input is an 'or' instruction, we
1657 /// may be doing shifts and ors to assemble the elements of the vector manually.
1658 /// Try to rip the code out and replace it with insertelements.  This is to
1659 /// optimize code like this:
1660 ///
1661 ///    %tmp37 = bitcast float %inc to i32
1662 ///    %tmp38 = zext i32 %tmp37 to i64
1663 ///    %tmp31 = bitcast float %inc5 to i32
1664 ///    %tmp32 = zext i32 %tmp31 to i64
1665 ///    %tmp33 = shl i64 %tmp32, 32
1666 ///    %ins35 = or i64 %tmp33, %tmp38
1667 ///    %tmp43 = bitcast i64 %ins35 to <2 x float>
1668 ///
1669 /// Into two insertelements that do "buildvector{%inc, %inc5}".
1670 static Value *OptimizeIntegerToVectorInsertions(BitCastInst &CI,
1671                                                 InstCombiner &IC) {
1672   VectorType *DestVecTy = cast<VectorType>(CI.getType());
1673   Value *IntInput = CI.getOperand(0);
1674
1675   SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
1676   if (!CollectInsertionElements(IntInput, 0, Elements,
1677                                 DestVecTy->getElementType(),
1678                                 IC.getDataLayout().isBigEndian()))
1679     return nullptr;
1680
1681   // If we succeeded, we know that all of the element are specified by Elements
1682   // or are zero if Elements has a null entry.  Recast this as a set of
1683   // insertions.
1684   Value *Result = Constant::getNullValue(CI.getType());
1685   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
1686     if (!Elements[i]) continue;  // Unset element.
1687
1688     Result = IC.Builder->CreateInsertElement(Result, Elements[i],
1689                                              IC.Builder->getInt32(i));
1690   }
1691
1692   return Result;
1693 }
1694
1695
1696 /// OptimizeIntToFloatBitCast - See if we can optimize an integer->float/double
1697 /// bitcast.  The various long double bitcasts can't get in here.
1698 static Instruction *OptimizeIntToFloatBitCast(BitCastInst &CI, InstCombiner &IC,
1699                                               const DataLayout &DL) {
1700   Value *Src = CI.getOperand(0);
1701   Type *DestTy = CI.getType();
1702
1703   // If this is a bitcast from int to float, check to see if the int is an
1704   // extraction from a vector.
1705   Value *VecInput = nullptr;
1706   // bitcast(trunc(bitcast(somevector)))
1707   if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) &&
1708       isa<VectorType>(VecInput->getType())) {
1709     VectorType *VecTy = cast<VectorType>(VecInput->getType());
1710     unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1711
1712     if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) {
1713       // If the element type of the vector doesn't match the result type,
1714       // bitcast it to be a vector type we can extract from.
1715       if (VecTy->getElementType() != DestTy) {
1716         VecTy = VectorType::get(DestTy,
1717                                 VecTy->getPrimitiveSizeInBits() / DestWidth);
1718         VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1719       }
1720
1721       unsigned Elt = 0;
1722       if (DL.isBigEndian())
1723         Elt = VecTy->getPrimitiveSizeInBits() / DestWidth - 1;
1724       return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
1725     }
1726   }
1727
1728   // bitcast(trunc(lshr(bitcast(somevector), cst))
1729   ConstantInt *ShAmt = nullptr;
1730   if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)),
1731                                 m_ConstantInt(ShAmt)))) &&
1732       isa<VectorType>(VecInput->getType())) {
1733     VectorType *VecTy = cast<VectorType>(VecInput->getType());
1734     unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1735     if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 &&
1736         ShAmt->getZExtValue() % DestWidth == 0) {
1737       // If the element type of the vector doesn't match the result type,
1738       // bitcast it to be a vector type we can extract from.
1739       if (VecTy->getElementType() != DestTy) {
1740         VecTy = VectorType::get(DestTy,
1741                                 VecTy->getPrimitiveSizeInBits() / DestWidth);
1742         VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1743       }
1744
1745       unsigned Elt = ShAmt->getZExtValue() / DestWidth;
1746       if (DL.isBigEndian())
1747         Elt = VecTy->getPrimitiveSizeInBits() / DestWidth - 1 - Elt;
1748       return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
1749     }
1750   }
1751   return nullptr;
1752 }
1753
1754 Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
1755   // If the operands are integer typed then apply the integer transforms,
1756   // otherwise just apply the common ones.
1757   Value *Src = CI.getOperand(0);
1758   Type *SrcTy = Src->getType();
1759   Type *DestTy = CI.getType();
1760
1761   // Get rid of casts from one type to the same type. These are useless and can
1762   // be replaced by the operand.
1763   if (DestTy == Src->getType())
1764     return ReplaceInstUsesWith(CI, Src);
1765
1766   if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
1767     PointerType *SrcPTy = cast<PointerType>(SrcTy);
1768     Type *DstElTy = DstPTy->getElementType();
1769     Type *SrcElTy = SrcPTy->getElementType();
1770
1771     // If we are casting a alloca to a pointer to a type of the same
1772     // size, rewrite the allocation instruction to allocate the "right" type.
1773     // There is no need to modify malloc calls because it is their bitcast that
1774     // needs to be cleaned up.
1775     if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
1776       if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
1777         return V;
1778
1779     // If the source and destination are pointers, and this cast is equivalent
1780     // to a getelementptr X, 0, 0, 0...  turn it into the appropriate gep.
1781     // This can enhance SROA and other transforms that want type-safe pointers.
1782     unsigned NumZeros = 0;
1783     while (SrcElTy != DstElTy &&
1784            isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() &&
1785            SrcElTy->getNumContainedTypes() /* not "{}" */) {
1786       SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(0U);
1787       ++NumZeros;
1788     }
1789
1790     // If we found a path from the src to dest, create the getelementptr now.
1791     if (SrcElTy == DstElTy) {
1792       SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder->getInt32(0));
1793       return GetElementPtrInst::CreateInBounds(Src, Idxs);
1794     }
1795   }
1796
1797   // Try to optimize int -> float bitcasts.
1798   if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy))
1799     if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this, DL))
1800       return I;
1801
1802   if (VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
1803     if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) {
1804       Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
1805       return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
1806                      Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
1807       // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
1808     }
1809
1810     if (isa<IntegerType>(SrcTy)) {
1811       // If this is a cast from an integer to vector, check to see if the input
1812       // is a trunc or zext of a bitcast from vector.  If so, we can replace all
1813       // the casts with a shuffle and (potentially) a bitcast.
1814       if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
1815         CastInst *SrcCast = cast<CastInst>(Src);
1816         if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
1817           if (isa<VectorType>(BCIn->getOperand(0)->getType()))
1818             if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0),
1819                                                cast<VectorType>(DestTy), *this))
1820               return I;
1821       }
1822
1823       // If the input is an 'or' instruction, we may be doing shifts and ors to
1824       // assemble the elements of the vector manually.  Try to rip the code out
1825       // and replace it with insertelements.
1826       if (Value *V = OptimizeIntegerToVectorInsertions(CI, *this))
1827         return ReplaceInstUsesWith(CI, V);
1828     }
1829   }
1830
1831   if (VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
1832     if (SrcVTy->getNumElements() == 1) {
1833       // If our destination is not a vector, then make this a straight
1834       // scalar-scalar cast.
1835       if (!DestTy->isVectorTy()) {
1836         Value *Elem =
1837           Builder->CreateExtractElement(Src,
1838                      Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
1839         return CastInst::Create(Instruction::BitCast, Elem, DestTy);
1840       }
1841
1842       // Otherwise, see if our source is an insert. If so, then use the scalar
1843       // component directly.
1844       if (InsertElementInst *IEI =
1845             dyn_cast<InsertElementInst>(CI.getOperand(0)))
1846         return CastInst::Create(Instruction::BitCast, IEI->getOperand(1),
1847                                 DestTy);
1848     }
1849   }
1850
1851   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
1852     // Okay, we have (bitcast (shuffle ..)).  Check to see if this is
1853     // a bitcast to a vector with the same # elts.
1854     if (SVI->hasOneUse() && DestTy->isVectorTy() &&
1855         DestTy->getVectorNumElements() == SVI->getType()->getNumElements() &&
1856         SVI->getType()->getNumElements() ==
1857         SVI->getOperand(0)->getType()->getVectorNumElements()) {
1858       BitCastInst *Tmp;
1859       // If either of the operands is a cast from CI.getType(), then
1860       // evaluating the shuffle in the casted destination's type will allow
1861       // us to eliminate at least one cast.
1862       if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
1863            Tmp->getOperand(0)->getType() == DestTy) ||
1864           ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
1865            Tmp->getOperand(0)->getType() == DestTy)) {
1866         Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
1867         Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
1868         // Return a new shuffle vector.  Use the same element ID's, as we
1869         // know the vector types match #elts.
1870         return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
1871       }
1872     }
1873   }
1874
1875   if (SrcTy->isPointerTy())
1876     return commonPointerCastTransforms(CI);
1877   return commonCastTransforms(CI);
1878 }
1879
1880 Instruction *InstCombiner::visitAddrSpaceCast(AddrSpaceCastInst &CI) {
1881   // If the destination pointer element type is not the same as the source's
1882   // first do a bitcast to the destination type, and then the addrspacecast.
1883   // This allows the cast to be exposed to other transforms.
1884   Value *Src = CI.getOperand(0);
1885   PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType());
1886   PointerType *DestTy = cast<PointerType>(CI.getType()->getScalarType());
1887
1888   Type *DestElemTy = DestTy->getElementType();
1889   if (SrcTy->getElementType() != DestElemTy) {
1890     Type *MidTy = PointerType::get(DestElemTy, SrcTy->getAddressSpace());
1891     if (VectorType *VT = dyn_cast<VectorType>(CI.getType())) {
1892       // Handle vectors of pointers.
1893       MidTy = VectorType::get(MidTy, VT->getNumElements());
1894     }
1895
1896     Value *NewBitCast = Builder->CreateBitCast(Src, MidTy);
1897     return new AddrSpaceCastInst(NewBitCast, CI.getType());
1898   }
1899
1900   return commonPointerCastTransforms(CI);
1901 }