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