LLVM 22.0.0git
ConstantFold.cpp
Go to the documentation of this file.
1//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
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 folding of constants for LLVM. This implements the
10// (internal) ConstantFold.h interface, which is used by the
11// ConstantExpr::get* methods to automatically fold constants when possible.
12//
13// The current constant folding implementation is implemented in two pieces: the
14// pieces that don't need DataLayout, and the pieces that do. This is to avoid
15// a dependence in IR on Target.
16//
17//===----------------------------------------------------------------------===//
18
20#include "llvm/ADT/APSInt.h"
22#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/GlobalAlias.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Operator.h"
32using namespace llvm;
33using namespace llvm::PatternMatch;
34
35//===----------------------------------------------------------------------===//
36// ConstantFold*Instruction Implementations
37//===----------------------------------------------------------------------===//
38
39/// This function determines which opcode to use to fold two constant cast
40/// expressions together. It uses CastInst::isEliminableCastPair to determine
41/// the opcode. Consequently its just a wrapper around that function.
42/// Determine if it is valid to fold a cast of a cast
43static unsigned
45 unsigned opc, ///< opcode of the second cast constant expression
46 ConstantExpr *Op, ///< the first cast constant expression
47 Type *DstTy ///< destination type of the first cast
48) {
49 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
50 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
51 assert(CastInst::isCast(opc) && "Invalid cast opcode");
52
53 // The types and opcodes for the two Cast constant expressions
54 Type *SrcTy = Op->getOperand(0)->getType();
55 Type *MidTy = Op->getType();
56 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
58 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
59 /*DL=*/nullptr);
60}
61
62static Constant *FoldBitCast(Constant *V, Type *DestTy) {
63 Type *SrcTy = V->getType();
64 if (SrcTy == DestTy)
65 return V; // no-op cast
66
67 if (V->isAllOnesValue())
68 return Constant::getAllOnesValue(DestTy);
69
70 // Handle ConstantInt -> ConstantFP
72 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
73 // This allows for other simplifications (although some of them
74 // can only be handled by Analysis/ConstantFolding.cpp).
75 if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
77
78 // Make sure dest type is compatible with the folded fp constant.
79 // See note below regarding the PPC_FP128 restriction.
80 if (!DestTy->isFPOrFPVectorTy() || DestTy->isPPC_FP128Ty() ||
81 DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
82 return nullptr;
83
84 return ConstantFP::get(
85 DestTy,
86 APFloat(DestTy->getScalarType()->getFltSemantics(), CI->getValue()));
87 }
88
89 // Handle ConstantFP -> ConstantInt
91 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
92 // This allows for other simplifications (although some of them
93 // can only be handled by Analysis/ConstantFolding.cpp).
94 if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
96
97 // PPC_FP128 is really the sum of two consecutive doubles, where the first
98 // double is always stored first in memory, regardless of the target
99 // endianness. The memory layout of i128, however, depends on the target
100 // endianness, and so we can't fold this without target endianness
101 // information. This should instead be handled by
102 // Analysis/ConstantFolding.cpp
103 if (SrcTy->isPPC_FP128Ty())
104 return nullptr;
105
106 // Make sure dest type is compatible with the folded integer constant.
107 if (!DestTy->isIntOrIntVectorTy() ||
108 DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
109 return nullptr;
110
111 return ConstantInt::get(DestTy, FP->getValueAPF().bitcastToAPInt());
112 }
113
114 return nullptr;
115}
116
118 Type *DestTy) {
120 ? ConstantExpr::getCast(opc, V, DestTy)
121 : ConstantFoldCastInstruction(opc, V, DestTy);
122}
123
125 Type *DestTy) {
126 if (isa<PoisonValue>(V))
127 return PoisonValue::get(DestTy);
128
129 if (isa<UndefValue>(V)) {
130 // zext(undef) = 0, because the top bits will be zero.
131 // sext(undef) = 0, because the top bits will all be the same.
132 // [us]itofp(undef) = 0, because the result value is bounded.
133 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
134 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
135 return Constant::getNullValue(DestTy);
136 return UndefValue::get(DestTy);
137 }
138
139 if (V->isNullValue() && !DestTy->isX86_AMXTy() &&
140 opc != Instruction::AddrSpaceCast)
141 return Constant::getNullValue(DestTy);
142
143 // If the cast operand is a constant expression, there's a few things we can
144 // do to try to simplify it.
146 if (CE->isCast()) {
147 // Try hard to fold cast of cast because they are often eliminable.
148 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
149 return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
150 }
151 }
152
153 // If the cast operand is a constant vector, perform the cast by
154 // operating on each element. In the cast of bitcasts, the element
155 // count may be mismatched; don't attempt to handle that here.
156 if (DestTy->isVectorTy() && V->getType()->isVectorTy() &&
157 cast<VectorType>(DestTy)->getElementCount() ==
158 cast<VectorType>(V->getType())->getElementCount()) {
159 VectorType *DestVecTy = cast<VectorType>(DestTy);
160 Type *DstEltTy = DestVecTy->getElementType();
161 // Fast path for splatted constants.
162 if (Constant *Splat = V->getSplatValue()) {
163 Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
164 if (!Res)
165 return nullptr;
167 cast<VectorType>(DestTy)->getElementCount(), Res);
168 }
169 if (isa<ScalableVectorType>(DestTy))
170 return nullptr;
172 Type *Ty = IntegerType::get(V->getContext(), 32);
173 for (unsigned i = 0,
174 e = cast<FixedVectorType>(V->getType())->getNumElements();
175 i != e; ++i) {
176 Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
177 Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
178 if (!Casted)
179 return nullptr;
180 res.push_back(Casted);
181 }
182 return ConstantVector::get(res);
183 }
184
185 // We actually have to do a cast now. Perform the cast according to the
186 // opcode specified.
187 switch (opc) {
188 default:
189 llvm_unreachable("Failed to cast constant expression");
190 case Instruction::FPTrunc:
191 case Instruction::FPExt:
192 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
193 bool ignored;
194 APFloat Val = FPC->getValueAPF();
195 Val.convert(DestTy->getScalarType()->getFltSemantics(),
197 return ConstantFP::get(DestTy, Val);
198 }
199 return nullptr; // Can't fold.
200 case Instruction::FPToUI:
201 case Instruction::FPToSI:
202 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
203 const APFloat &V = FPC->getValueAPF();
204 bool ignored;
205 APSInt IntVal(DestTy->getScalarSizeInBits(), opc == Instruction::FPToUI);
207 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
208 // Undefined behavior invoked - the destination type can't represent
209 // the input constant.
210 return PoisonValue::get(DestTy);
211 }
212 return ConstantInt::get(DestTy, IntVal);
213 }
214 return nullptr; // Can't fold.
215 case Instruction::UIToFP:
216 case Instruction::SIToFP:
217 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
218 const APInt &api = CI->getValue();
219 APFloat apf(DestTy->getScalarType()->getFltSemantics(),
221 apf.convertFromAPInt(api, opc==Instruction::SIToFP,
223 return ConstantFP::get(DestTy, apf);
224 }
225 return nullptr;
226 case Instruction::ZExt:
227 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
229 return ConstantInt::get(DestTy, CI->getValue().zext(BitWidth));
230 }
231 return nullptr;
232 case Instruction::SExt:
233 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
235 return ConstantInt::get(DestTy, CI->getValue().sext(BitWidth));
236 }
237 return nullptr;
238 case Instruction::Trunc: {
239 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
241 return ConstantInt::get(DestTy, CI->getValue().trunc(BitWidth));
242 }
243
244 return nullptr;
245 }
246 case Instruction::BitCast:
247 return FoldBitCast(V, DestTy);
248 case Instruction::AddrSpaceCast:
249 case Instruction::IntToPtr:
250 case Instruction::PtrToAddr:
251 case Instruction::PtrToInt:
252 return nullptr;
253 }
254}
255
257 Constant *V1, Constant *V2) {
258 // Check for i1 and vector true/false conditions.
259 if (Cond->isNullValue()) return V2;
260 if (Cond->isAllOnesValue()) return V1;
261
262 // If the condition is a vector constant, fold the result elementwise.
264 auto *V1VTy = CondV->getType();
266 Type *Ty = IntegerType::get(CondV->getContext(), 32);
267 for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
268 Constant *V;
270 ConstantInt::get(Ty, i));
272 ConstantInt::get(Ty, i));
273 auto *Cond = cast<Constant>(CondV->getOperand(i));
274 if (isa<PoisonValue>(Cond)) {
275 V = PoisonValue::get(V1Element->getType());
276 } else if (V1Element == V2Element) {
277 V = V1Element;
278 } else if (isa<UndefValue>(Cond)) {
279 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
280 } else {
281 if (!isa<ConstantInt>(Cond)) break;
282 V = Cond->isNullValue() ? V2Element : V1Element;
283 }
284 Result.push_back(V);
285 }
286
287 // If we were able to build the vector, return it.
288 if (Result.size() == V1VTy->getNumElements())
289 return ConstantVector::get(Result);
290 }
291
293 return PoisonValue::get(V1->getType());
294
295 if (isa<UndefValue>(Cond)) {
296 if (isa<UndefValue>(V1)) return V1;
297 return V2;
298 }
299
300 if (V1 == V2) return V1;
301
302 if (isa<PoisonValue>(V1))
303 return V2;
304 if (isa<PoisonValue>(V2))
305 return V1;
306
307 // If the true or false value is undef, we can fold to the other value as
308 // long as the other value isn't poison.
309 auto NotPoison = [](Constant *C) {
310 if (isa<PoisonValue>(C))
311 return false;
312
313 // TODO: We can analyze ConstExpr by opcode to determine if there is any
314 // possibility of poison.
315 if (isa<ConstantExpr>(C))
316 return false;
317
320 return true;
321
322 if (C->getType()->isVectorTy())
323 return !C->containsPoisonElement() && !C->containsConstantExpression();
324
325 // TODO: Recursively analyze aggregates or other constants.
326 return false;
327 };
328 if (isa<UndefValue>(V1) && NotPoison(V2)) return V2;
329 if (isa<UndefValue>(V2) && NotPoison(V1)) return V1;
330
331 return nullptr;
332}
333
335 Constant *Idx) {
336 auto *ValVTy = cast<VectorType>(Val->getType());
337
338 // extractelt poison, C -> poison
339 // extractelt C, undef -> poison
340 if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx))
341 return PoisonValue::get(ValVTy->getElementType());
342
343 // extractelt undef, C -> undef
344 if (isa<UndefValue>(Val))
345 return UndefValue::get(ValVTy->getElementType());
346
347 auto *CIdx = dyn_cast<ConstantInt>(Idx);
348 if (!CIdx)
349 return nullptr;
350
351 if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) {
352 // ee({w,x,y,z}, wrong_value) -> poison
353 if (CIdx->uge(ValFVTy->getNumElements()))
354 return PoisonValue::get(ValFVTy->getElementType());
355 }
356
357 // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...)
358 if (auto *CE = dyn_cast<ConstantExpr>(Val)) {
359 if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
361 Ops.reserve(CE->getNumOperands());
362 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
363 Constant *Op = CE->getOperand(i);
364 if (Op->getType()->isVectorTy()) {
366 if (!ScalarOp)
367 return nullptr;
368 Ops.push_back(ScalarOp);
369 } else
370 Ops.push_back(Op);
371 }
372 return CE->getWithOperands(Ops, ValVTy->getElementType(), false,
373 GEP->getSourceElementType());
374 } else if (CE->getOpcode() == Instruction::InsertElement) {
375 if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
376 if (APSInt::isSameValue(APSInt(IEIdx->getValue()),
377 APSInt(CIdx->getValue()))) {
378 return CE->getOperand(1);
379 } else {
380 return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx);
381 }
382 }
383 }
384 }
385
386 if (Constant *C = Val->getAggregateElement(CIdx))
387 return C;
388
389 // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x
390 if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
391 if (Constant *SplatVal = Val->getSplatValue())
392 return SplatVal;
393 }
394
395 return nullptr;
396}
397
399 Constant *Elt,
400 Constant *Idx) {
401 if (isa<UndefValue>(Idx))
402 return PoisonValue::get(Val->getType());
403
404 // Inserting null into all zeros is still all zeros.
405 // TODO: This is true for undef and poison splats too.
406 if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue())
407 return Val;
408
410 if (!CIdx) return nullptr;
411
412 // Do not iterate on scalable vector. The num of elements is unknown at
413 // compile-time.
415 return nullptr;
416
417 auto *ValTy = cast<FixedVectorType>(Val->getType());
418
419 unsigned NumElts = ValTy->getNumElements();
420 if (CIdx->uge(NumElts))
421 return PoisonValue::get(Val->getType());
422
424 Result.reserve(NumElts);
425 auto *Ty = Type::getInt32Ty(Val->getContext());
426 uint64_t IdxVal = CIdx->getZExtValue();
427 for (unsigned i = 0; i != NumElts; ++i) {
428 if (i == IdxVal) {
429 Result.push_back(Elt);
430 continue;
431 }
432
433 Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
434 Result.push_back(C);
435 }
436
437 return ConstantVector::get(Result);
438}
439
441 ArrayRef<int> Mask) {
442 auto *V1VTy = cast<VectorType>(V1->getType());
443 unsigned MaskNumElts = Mask.size();
444 auto MaskEltCount =
445 ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
446 Type *EltTy = V1VTy->getElementType();
447
448 // Poison shuffle mask -> poison value.
449 if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
450 return PoisonValue::get(VectorType::get(EltTy, MaskEltCount));
451 }
452
453 // If the mask is all zeros this is a splat, no need to go through all
454 // elements.
455 if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
456 Type *Ty = IntegerType::get(V1->getContext(), 32);
457 Constant *Elt =
458 ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0));
459
460 // For scalable vectors, make sure this doesn't fold back into a
461 // shufflevector.
462 if (!MaskEltCount.isScalable() || Elt->isNullValue() || isa<UndefValue>(Elt))
463 return ConstantVector::getSplat(MaskEltCount, Elt);
464 }
465
466 // Do not iterate on scalable vector. The num of elements is unknown at
467 // compile-time.
468 if (isa<ScalableVectorType>(V1VTy))
469 return nullptr;
470
471 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
472
473 // Loop over the shuffle mask, evaluating each element.
475 for (unsigned i = 0; i != MaskNumElts; ++i) {
476 int Elt = Mask[i];
477 if (Elt == -1) {
478 Result.push_back(UndefValue::get(EltTy));
479 continue;
480 }
481 Constant *InElt;
482 if (unsigned(Elt) >= SrcNumElts*2)
483 InElt = UndefValue::get(EltTy);
484 else if (unsigned(Elt) >= SrcNumElts) {
485 Type *Ty = IntegerType::get(V2->getContext(), 32);
486 InElt =
488 ConstantInt::get(Ty, Elt - SrcNumElts));
489 } else {
490 Type *Ty = IntegerType::get(V1->getContext(), 32);
491 InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
492 }
493 Result.push_back(InElt);
494 }
495
496 return ConstantVector::get(Result);
497}
498
500 ArrayRef<unsigned> Idxs) {
501 // Base case: no indices, so return the entire value.
502 if (Idxs.empty())
503 return Agg;
504
505 if (Constant *C = Agg->getAggregateElement(Idxs[0]))
507
508 return nullptr;
509}
510
512 Constant *Val,
513 ArrayRef<unsigned> Idxs) {
514 // Base case: no indices, so replace the entire value.
515 if (Idxs.empty())
516 return Val;
517
518 unsigned NumElts;
519 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
520 NumElts = ST->getNumElements();
521 else
522 NumElts = cast<ArrayType>(Agg->getType())->getNumElements();
523
525 for (unsigned i = 0; i != NumElts; ++i) {
526 Constant *C = Agg->getAggregateElement(i);
527 if (!C) return nullptr;
528
529 if (Idxs[0] == i)
531
532 Result.push_back(C);
533 }
534
535 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
536 return ConstantStruct::get(ST, Result);
537 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result);
538}
539
541 assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
542
543 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
544 // vectors are always evaluated per element.
545 bool IsScalableVector = isa<ScalableVectorType>(C->getType());
546 bool HasScalarUndefOrScalableVectorUndef =
547 (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C);
548
549 if (HasScalarUndefOrScalableVectorUndef) {
550 switch (static_cast<Instruction::UnaryOps>(Opcode)) {
551 case Instruction::FNeg:
552 return C; // -undef -> undef
553 case Instruction::UnaryOpsEnd:
554 llvm_unreachable("Invalid UnaryOp");
555 }
556 }
557
558 // Constant should not be UndefValue, unless these are vector constants.
559 assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue");
560 // We only have FP UnaryOps right now.
561 assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
562
563 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
564 const APFloat &CV = CFP->getValueAPF();
565 switch (Opcode) {
566 default:
567 break;
568 case Instruction::FNeg:
569 return ConstantFP::get(C->getType(), neg(CV));
570 }
571 } else if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
572 // Fast path for splatted constants.
573 if (Constant *Splat = C->getSplatValue())
574 if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat))
575 return ConstantVector::getSplat(VTy->getElementCount(), Elt);
576
577 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
578 // Fold each element and create a vector constant from those constants.
579 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
581 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
582 Constant *ExtractIdx = ConstantInt::get(Ty, i);
583 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
584 Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt);
585 if (!Res)
586 return nullptr;
587 Result.push_back(Res);
588 }
589
590 return ConstantVector::get(Result);
591 }
592 }
593
594 // We don't know how to fold this.
595 return nullptr;
596}
597
599 Constant *C2) {
600 assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");
601
602 // Simplify BinOps with their identity values first. They are no-ops and we
603 // can always return the other value, including undef or poison values.
605 Opcode, C1->getType(), /*AllowRHSIdentity*/ false)) {
606 if (C1 == Identity)
607 return C2;
608 if (C2 == Identity)
609 return C1;
610 } else if (Constant *Identity = ConstantExpr::getBinOpIdentity(
611 Opcode, C1->getType(), /*AllowRHSIdentity*/ true)) {
612 if (C2 == Identity)
613 return C1;
614 }
615
616 // Binary operations propagate poison.
617 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
618 return PoisonValue::get(C1->getType());
619
620 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
621 // vectors are always evaluated per element.
622 bool IsScalableVector = isa<ScalableVectorType>(C1->getType());
623 bool HasScalarUndefOrScalableVectorUndef =
624 (!C1->getType()->isVectorTy() || IsScalableVector) &&
625 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
626 if (HasScalarUndefOrScalableVectorUndef) {
627 switch (static_cast<Instruction::BinaryOps>(Opcode)) {
628 case Instruction::Xor:
629 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
630 // Handle undef ^ undef -> 0 special case. This is a common
631 // idiom (misuse).
632 return Constant::getNullValue(C1->getType());
633 [[fallthrough]];
634 case Instruction::Add:
635 case Instruction::Sub:
636 return UndefValue::get(C1->getType());
637 case Instruction::And:
638 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
639 return C1;
640 return Constant::getNullValue(C1->getType()); // undef & X -> 0
641 case Instruction::Mul: {
642 // undef * undef -> undef
643 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
644 return C1;
645 const APInt *CV;
646 // X * undef -> undef if X is odd
647 if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
648 if ((*CV)[0])
649 return UndefValue::get(C1->getType());
650
651 // X * undef -> 0 otherwise
652 return Constant::getNullValue(C1->getType());
653 }
654 case Instruction::SDiv:
655 case Instruction::UDiv:
656 // X / undef -> poison
657 // X / 0 -> poison
658 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
659 return PoisonValue::get(C2->getType());
660 // undef / X -> 0 otherwise
661 return Constant::getNullValue(C1->getType());
662 case Instruction::URem:
663 case Instruction::SRem:
664 // X % undef -> poison
665 // X % 0 -> poison
666 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
667 return PoisonValue::get(C2->getType());
668 // undef % X -> 0 otherwise
669 return Constant::getNullValue(C1->getType());
670 case Instruction::Or: // X | undef -> -1
671 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
672 return C1;
673 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
674 case Instruction::LShr:
675 // X >>l undef -> poison
676 if (isa<UndefValue>(C2))
677 return PoisonValue::get(C2->getType());
678 // undef >>l X -> 0
679 return Constant::getNullValue(C1->getType());
680 case Instruction::AShr:
681 // X >>a undef -> poison
682 if (isa<UndefValue>(C2))
683 return PoisonValue::get(C2->getType());
684 // TODO: undef >>a X -> poison if the shift is exact
685 // undef >>a X -> 0
686 return Constant::getNullValue(C1->getType());
687 case Instruction::Shl:
688 // X << undef -> undef
689 if (isa<UndefValue>(C2))
690 return PoisonValue::get(C2->getType());
691 // undef << X -> 0
692 return Constant::getNullValue(C1->getType());
693 case Instruction::FSub:
694 // -0.0 - undef --> undef (consistent with "fneg undef")
695 if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
696 return C2;
697 [[fallthrough]];
698 case Instruction::FAdd:
699 case Instruction::FMul:
700 case Instruction::FDiv:
701 case Instruction::FRem:
702 // [any flop] undef, undef -> undef
703 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
704 return C1;
705 // [any flop] C, undef -> NaN
706 // [any flop] undef, C -> NaN
707 // We could potentially specialize NaN/Inf constants vs. 'normal'
708 // constants (possibly differently depending on opcode and operand). This
709 // would allow returning undef sometimes. But it is always safe to fold to
710 // NaN because we can choose the undef operand as NaN, and any FP opcode
711 // with a NaN operand will propagate NaN.
712 return ConstantFP::getNaN(C1->getType());
713 case Instruction::BinaryOpsEnd:
714 llvm_unreachable("Invalid BinaryOp");
715 }
716 }
717
718 // Neither constant should be UndefValue, unless these are vector constants.
719 assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue");
720
721 // Handle simplifications when the RHS is a constant int.
722 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
723 if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(),
724 /*AllowLHSConstant*/ false))
725 return C2;
726
727 switch (Opcode) {
728 case Instruction::UDiv:
729 case Instruction::SDiv:
730 if (CI2->isZero())
731 return PoisonValue::get(CI2->getType()); // X / 0 == poison
732 break;
733 case Instruction::URem:
734 case Instruction::SRem:
735 if (CI2->isOne())
736 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
737 if (CI2->isZero())
738 return PoisonValue::get(CI2->getType()); // X % 0 == poison
739 break;
740 case Instruction::And:
741 assert(!CI2->isZero() && "And zero handled above");
742 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
743 // If and'ing the address of a global with a constant, fold it.
744 if (CE1->getOpcode() == Instruction::PtrToInt &&
745 isa<GlobalValue>(CE1->getOperand(0))) {
746 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
747
748 Align GVAlign; // defaults to 1
749
750 if (Module *TheModule = GV->getParent()) {
751 const DataLayout &DL = TheModule->getDataLayout();
752 GVAlign = GV->getPointerAlignment(DL);
753
754 // If the function alignment is not specified then assume that it
755 // is 4.
756 // This is dangerous; on x86, the alignment of the pointer
757 // corresponds to the alignment of the function, but might be less
758 // than 4 if it isn't explicitly specified.
759 // However, a fix for this behaviour was reverted because it
760 // increased code size (see https://reviews.llvm.org/D55115)
761 // FIXME: This code should be deleted once existing targets have
762 // appropriate defaults
763 if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
764 GVAlign = Align(4);
765 } else if (isa<GlobalVariable>(GV)) {
766 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
767 }
768
769 if (GVAlign > 1) {
770 unsigned DstWidth = CI2->getBitWidth();
771 unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
772 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
773
774 // If checking bits we know are clear, return zero.
775 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
776 return Constant::getNullValue(CI2->getType());
777 }
778 }
779 }
780 break;
781 }
782 } else if (isa<ConstantInt>(C1)) {
783 // If C1 is a ConstantInt and C2 is not, swap the operands.
784 if (Instruction::isCommutative(Opcode))
785 return ConstantExpr::isDesirableBinOp(Opcode)
786 ? ConstantExpr::get(Opcode, C2, C1)
787 : ConstantFoldBinaryInstruction(Opcode, C2, C1);
788 }
789
790 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
791 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
792 const APInt &C1V = CI1->getValue();
793 const APInt &C2V = CI2->getValue();
794 switch (Opcode) {
795 default:
796 break;
797 case Instruction::Add:
798 return ConstantInt::get(C1->getType(), C1V + C2V);
799 case Instruction::Sub:
800 return ConstantInt::get(C1->getType(), C1V - C2V);
801 case Instruction::Mul:
802 return ConstantInt::get(C1->getType(), C1V * C2V);
803 case Instruction::UDiv:
804 assert(!CI2->isZero() && "Div by zero handled above");
805 return ConstantInt::get(CI1->getType(), C1V.udiv(C2V));
806 case Instruction::SDiv:
807 assert(!CI2->isZero() && "Div by zero handled above");
808 if (C2V.isAllOnes() && C1V.isMinSignedValue())
809 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison
810 return ConstantInt::get(CI1->getType(), C1V.sdiv(C2V));
811 case Instruction::URem:
812 assert(!CI2->isZero() && "Div by zero handled above");
813 return ConstantInt::get(C1->getType(), C1V.urem(C2V));
814 case Instruction::SRem:
815 assert(!CI2->isZero() && "Div by zero handled above");
816 if (C2V.isAllOnes() && C1V.isMinSignedValue())
817 return PoisonValue::get(C1->getType()); // MIN_INT % -1 -> poison
818 return ConstantInt::get(C1->getType(), C1V.srem(C2V));
819 case Instruction::And:
820 return ConstantInt::get(C1->getType(), C1V & C2V);
821 case Instruction::Or:
822 return ConstantInt::get(C1->getType(), C1V | C2V);
823 case Instruction::Xor:
824 return ConstantInt::get(C1->getType(), C1V ^ C2V);
825 case Instruction::Shl:
826 if (C2V.ult(C1V.getBitWidth()))
827 return ConstantInt::get(C1->getType(), C1V.shl(C2V));
828 return PoisonValue::get(C1->getType()); // too big shift is poison
829 case Instruction::LShr:
830 if (C2V.ult(C1V.getBitWidth()))
831 return ConstantInt::get(C1->getType(), C1V.lshr(C2V));
832 return PoisonValue::get(C1->getType()); // too big shift is poison
833 case Instruction::AShr:
834 if (C2V.ult(C1V.getBitWidth()))
835 return ConstantInt::get(C1->getType(), C1V.ashr(C2V));
836 return PoisonValue::get(C1->getType()); // too big shift is poison
837 }
838 }
839
840 if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(),
841 /*AllowLHSConstant*/ true))
842 return C1;
843 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
844 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
845 const APFloat &C1V = CFP1->getValueAPF();
846 const APFloat &C2V = CFP2->getValueAPF();
847 APFloat C3V = C1V; // copy for modification
848 switch (Opcode) {
849 default:
850 break;
851 case Instruction::FAdd:
852 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
853 return ConstantFP::get(C1->getType(), C3V);
854 case Instruction::FSub:
856 return ConstantFP::get(C1->getType(), C3V);
857 case Instruction::FMul:
859 return ConstantFP::get(C1->getType(), C3V);
860 case Instruction::FDiv:
861 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
862 return ConstantFP::get(C1->getType(), C3V);
863 case Instruction::FRem:
864 (void)C3V.mod(C2V);
865 return ConstantFP::get(C1->getType(), C3V);
866 }
867 }
868 }
869
870 if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
871 // Fast path for splatted constants.
872 if (Constant *C2Splat = C2->getSplatValue()) {
873 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
874 return PoisonValue::get(VTy);
875 if (Constant *C1Splat = C1->getSplatValue()) {
876 Constant *Res =
878 ? ConstantExpr::get(Opcode, C1Splat, C2Splat)
879 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat);
880 if (!Res)
881 return nullptr;
882 return ConstantVector::getSplat(VTy->getElementCount(), Res);
883 }
884 }
885
886 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
887 // Fold each element and create a vector constant from those constants.
889 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
890 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
891 Constant *ExtractIdx = ConstantInt::get(Ty, i);
892 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
893 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
895 ? ConstantExpr::get(Opcode, LHS, RHS)
896 : ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
897 if (!Res)
898 return nullptr;
899 Result.push_back(Res);
900 }
901
902 return ConstantVector::get(Result);
903 }
904 }
905
906 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
907 // There are many possible foldings we could do here. We should probably
908 // at least fold add of a pointer with an integer into the appropriate
909 // getelementptr. This will improve alias analysis a bit.
910
911 // Given ((a + b) + c), if (b + c) folds to something interesting, return
912 // (a + (b + c)).
913 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
914 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
915 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
916 return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
917 }
918 } else if (isa<ConstantExpr>(C2)) {
919 // If C2 is a constant expr and C1 isn't, flop them around and fold the
920 // other way if possible.
921 if (Instruction::isCommutative(Opcode))
922 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
923 }
924
925 // i1 can be simplified in many cases.
926 if (C1->getType()->isIntegerTy(1)) {
927 switch (Opcode) {
928 case Instruction::Add:
929 case Instruction::Sub:
930 return ConstantExpr::getXor(C1, C2);
931 case Instruction::Shl:
932 case Instruction::LShr:
933 case Instruction::AShr:
934 // We can assume that C2 == 0. If it were one the result would be
935 // undefined because the shift value is as large as the bitwidth.
936 return C1;
937 case Instruction::SDiv:
938 case Instruction::UDiv:
939 // We can assume that C2 == 1. If it were zero the result would be
940 // undefined through division by zero.
941 return C1;
942 case Instruction::URem:
943 case Instruction::SRem:
944 // We can assume that C2 == 1. If it were zero the result would be
945 // undefined through division by zero.
946 return ConstantInt::getFalse(C1->getContext());
947 default:
948 break;
949 }
950 }
951
952 // We don't know how to fold this.
953 return nullptr;
954}
955
957 const GlobalValue *GV2) {
958 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
959 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
960 return true;
961 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
962 Type *Ty = GVar->getValueType();
963 // A global with opaque type might end up being zero sized.
964 if (!Ty->isSized())
965 return true;
966 // A global with an empty type might lie at the address of any other
967 // global.
968 if (Ty->isEmptyTy())
969 return true;
970 }
971 return false;
972 };
973 // Don't try to decide equality of aliases.
974 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
975 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
976 return ICmpInst::ICMP_NE;
978}
979
980/// This function determines if there is anything we can decide about the two
981/// constants provided. This doesn't need to handle simple things like integer
982/// comparisons, but should instead handle ConstantExprs and GlobalValues.
983/// If we can determine that the two constants have a particular relation to
984/// each other, we should return the corresponding ICmp predicate, otherwise
985/// return ICmpInst::BAD_ICMP_PREDICATE.
987 assert(V1->getType() == V2->getType() &&
988 "Cannot compare different types of values!");
989 if (V1 == V2) return ICmpInst::ICMP_EQ;
990
991 // The following folds only apply to pointers.
992 if (!V1->getType()->isPointerTy())
994
995 // To simplify this code we canonicalize the relation so that the first
996 // operand is always the most "complex" of the two. We consider simple
997 // constants (like ConstantPointerNull) to be the simplest, followed by
998 // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
999 auto GetComplexity = [](Constant *V) {
1000 if (isa<ConstantExpr>(V))
1001 return 3;
1002 if (isa<GlobalValue>(V))
1003 return 2;
1004 if (isa<BlockAddress>(V))
1005 return 1;
1006 return 0;
1007 };
1008 if (GetComplexity(V1) < GetComplexity(V2)) {
1009 ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
1010 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1011 return ICmpInst::getSwappedPredicate(SwappedRelation);
1013 }
1014
1015 if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1016 // Now we know that the RHS is a BlockAddress or simple constant.
1017 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1018 // Block address in another function can't equal this one, but block
1019 // addresses in the current function might be the same if blocks are
1020 // empty.
1021 if (BA2->getFunction() != BA->getFunction())
1022 return ICmpInst::ICMP_NE;
1023 } else if (isa<ConstantPointerNull>(V2)) {
1024 return ICmpInst::ICMP_NE;
1025 }
1026 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1027 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1028 // constant.
1029 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1030 return areGlobalsPotentiallyEqual(GV, GV2);
1031 } else if (isa<BlockAddress>(V2)) {
1032 return ICmpInst::ICMP_NE; // Globals never equal labels.
1033 } else if (isa<ConstantPointerNull>(V2)) {
1034 // GlobalVals can never be null unless they have external weak linkage.
1035 // We don't try to evaluate aliases here.
1036 // NOTE: We should not be doing this constant folding if null pointer
1037 // is considered valid for the function. But currently there is no way to
1038 // query it from the Constant type.
1039 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1040 !NullPointerIsDefined(nullptr /* F */,
1041 GV->getType()->getAddressSpace()))
1042 return ICmpInst::ICMP_UGT;
1043 }
1044 } else if (auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
1045 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1046 // constantexpr, a global, block address, or a simple constant.
1047 Constant *CE1Op0 = CE1->getOperand(0);
1048
1049 switch (CE1->getOpcode()) {
1050 case Instruction::GetElementPtr: {
1051 GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1052 // Ok, since this is a getelementptr, we know that the constant has a
1053 // pointer type. Check the various cases.
1054 if (isa<ConstantPointerNull>(V2)) {
1055 // If we are comparing a GEP to a null pointer, check to see if the base
1056 // of the GEP equals the null pointer.
1057 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1058 // If its not weak linkage, the GVal must have a non-zero address
1059 // so the result is greater-than
1060 if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
1061 return ICmpInst::ICMP_UGT;
1062 }
1063 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1065 if (GV != GV2) {
1066 if (CE1GEP->hasAllZeroIndices())
1067 return areGlobalsPotentiallyEqual(GV, GV2);
1069 }
1070 }
1071 } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1072 // By far the most common case to handle is when the base pointers are
1073 // obviously to the same global.
1074 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1075 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1076 // Don't know relative ordering, but check for inequality.
1077 if (CE1Op0 != CE2Op0) {
1078 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1080 cast<GlobalValue>(CE2Op0));
1082 }
1083 }
1084 }
1085 break;
1086 }
1087 default:
1088 break;
1089 }
1090 }
1091
1093}
1094
1096 Constant *C1, Constant *C2) {
1097 Type *ResultTy;
1098 if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1099 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1100 VT->getElementCount());
1101 else
1102 ResultTy = Type::getInt1Ty(C1->getContext());
1103
1104 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1105 if (Predicate == FCmpInst::FCMP_FALSE)
1106 return Constant::getNullValue(ResultTy);
1107
1108 if (Predicate == FCmpInst::FCMP_TRUE)
1109 return Constant::getAllOnesValue(ResultTy);
1110
1111 // Handle some degenerate cases first
1112 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1113 return PoisonValue::get(ResultTy);
1114
1115 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1116 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1117 // For EQ and NE, we can always pick a value for the undef to make the
1118 // predicate pass or fail, so we can return undef.
1119 // Also, if both operands are undef, we can return undef for int comparison.
1120 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1121 return UndefValue::get(ResultTy);
1122
1123 // Otherwise, for integer compare, pick the same value as the non-undef
1124 // operand, and fold it to true or false.
1125 if (isIntegerPredicate)
1126 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1127
1128 // Choosing NaN for the undef will always make unordered comparison succeed
1129 // and ordered comparison fails.
1130 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1131 }
1132
1133 if (C2->isNullValue()) {
1134 // The caller is expected to commute the operands if the constant expression
1135 // is C2.
1136 // C1 >= 0 --> true
1137 if (Predicate == ICmpInst::ICMP_UGE)
1138 return Constant::getAllOnesValue(ResultTy);
1139 // C1 < 0 --> false
1140 if (Predicate == ICmpInst::ICMP_ULT)
1141 return Constant::getNullValue(ResultTy);
1142 }
1143
1144 // If the comparison is a comparison between two i1's, simplify it.
1145 if (C1->getType()->isIntOrIntVectorTy(1)) {
1146 switch (Predicate) {
1147 case ICmpInst::ICMP_EQ:
1148 if (isa<ConstantExpr>(C1))
1151 case ICmpInst::ICMP_NE:
1152 return ConstantExpr::getXor(C1, C2);
1153 default:
1154 break;
1155 }
1156 }
1157
1158 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1159 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1160 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1161 return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate));
1162 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1163 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1164 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1165 return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
1166 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
1167
1168 // Fast path for splatted constants.
1169 if (Constant *C1Splat = C1->getSplatValue())
1170 if (Constant *C2Splat = C2->getSplatValue())
1171 if (Constant *Elt =
1172 ConstantFoldCompareInstruction(Predicate, C1Splat, C2Splat))
1173 return ConstantVector::getSplat(C1VTy->getElementCount(), Elt);
1174
1175 // Do not iterate on scalable vector. The number of elements is unknown at
1176 // compile-time.
1177 if (isa<ScalableVectorType>(C1VTy))
1178 return nullptr;
1179
1180 // If we can constant fold the comparison of each element, constant fold
1181 // the whole vector comparison.
1183 Type *Ty = IntegerType::get(C1->getContext(), 32);
1184 // Compare the elements, producing an i1 result or constant expr.
1185 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1186 I != E; ++I) {
1187 Constant *C1E =
1188 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I));
1189 Constant *C2E =
1190 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I));
1191 Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E);
1192 if (!Elt)
1193 return nullptr;
1194
1195 ResElts.push_back(Elt);
1196 }
1197
1198 return ConstantVector::get(ResElts);
1199 }
1200
1201 if (C1->getType()->isFPOrFPVectorTy()) {
1202 if (C1 == C2) {
1203 // We know that C1 == C2 || isUnordered(C1, C2).
1204 if (Predicate == FCmpInst::FCMP_ONE)
1205 return ConstantInt::getFalse(ResultTy);
1206 else if (Predicate == FCmpInst::FCMP_UEQ)
1207 return ConstantInt::getTrue(ResultTy);
1208 }
1209 } else {
1210 // Evaluate the relation between the two constants, per the predicate.
1211 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1212 switch (evaluateICmpRelation(C1, C2)) {
1213 default: llvm_unreachable("Unknown relational!");
1215 break; // Couldn't determine anything about these constants.
1216 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1217 // If we know the constants are equal, we can decide the result of this
1218 // computation precisely.
1219 Result = ICmpInst::isTrueWhenEqual(Predicate);
1220 break;
1221 case ICmpInst::ICMP_ULT:
1222 switch (Predicate) {
1224 Result = 1; break;
1226 Result = 0; break;
1227 default:
1228 break;
1229 }
1230 break;
1231 case ICmpInst::ICMP_SLT:
1232 switch (Predicate) {
1234 Result = 1; break;
1236 Result = 0; break;
1237 default:
1238 break;
1239 }
1240 break;
1241 case ICmpInst::ICMP_UGT:
1242 switch (Predicate) {
1244 Result = 1; break;
1246 Result = 0; break;
1247 default:
1248 break;
1249 }
1250 break;
1251 case ICmpInst::ICMP_SGT:
1252 switch (Predicate) {
1254 Result = 1; break;
1256 Result = 0; break;
1257 default:
1258 break;
1259 }
1260 break;
1261 case ICmpInst::ICMP_ULE:
1262 if (Predicate == ICmpInst::ICMP_UGT)
1263 Result = 0;
1264 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1265 Result = 1;
1266 break;
1267 case ICmpInst::ICMP_SLE:
1268 if (Predicate == ICmpInst::ICMP_SGT)
1269 Result = 0;
1270 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1271 Result = 1;
1272 break;
1273 case ICmpInst::ICMP_UGE:
1274 if (Predicate == ICmpInst::ICMP_ULT)
1275 Result = 0;
1276 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1277 Result = 1;
1278 break;
1279 case ICmpInst::ICMP_SGE:
1280 if (Predicate == ICmpInst::ICMP_SLT)
1281 Result = 0;
1282 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1283 Result = 1;
1284 break;
1285 case ICmpInst::ICMP_NE:
1286 if (Predicate == ICmpInst::ICMP_EQ)
1287 Result = 0;
1288 if (Predicate == ICmpInst::ICMP_NE)
1289 Result = 1;
1290 break;
1291 }
1292
1293 // If we evaluated the result, return it now.
1294 if (Result != -1)
1295 return ConstantInt::get(ResultTy, Result);
1296
1297 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1298 (C1->isNullValue() && !C2->isNullValue())) {
1299 // If C2 is a constant expr and C1 isn't, flip them around and fold the
1300 // other way if possible.
1301 // Also, if C1 is null and C2 isn't, flip them around.
1302 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1303 return ConstantFoldCompareInstruction(Predicate, C2, C1);
1304 }
1305 }
1306 return nullptr;
1307}
1308
1310 std::optional<ConstantRange> InRange,
1311 ArrayRef<Value *> Idxs) {
1312 if (Idxs.empty()) return C;
1313
1315 C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
1316
1317 if (isa<PoisonValue>(C))
1318 return PoisonValue::get(GEPTy);
1319
1320 if (isa<UndefValue>(C))
1321 return UndefValue::get(GEPTy);
1322
1323 auto IsNoOp = [&]() {
1324 // Avoid losing inrange information.
1325 if (InRange)
1326 return false;
1327
1328 return all_of(Idxs, [](Value *Idx) {
1329 Constant *IdxC = cast<Constant>(Idx);
1330 return IdxC->isNullValue() || isa<UndefValue>(IdxC);
1331 });
1332 };
1333 if (IsNoOp())
1334 return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
1336 cast<VectorType>(GEPTy)->getElementCount(), C)
1337 : C;
1338
1339 return nullptr;
1340}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static unsigned foldConstantCastPair(unsigned opc, ConstantExpr *Op, Type *DstTy)
This function determines which opcode to use to fold two constant cast expressions together.
static Constant * foldMaybeUndesirableCast(unsigned opc, Constant *V, Type *DestTy)
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2)
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2)
This function determines if there is anything we can decide about the two constants provided.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define I(x, y, z)
Definition MD5.cpp:58
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
#define T
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1208
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6057
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1347
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1199
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1226
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:423
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
static bool isSameValue(const APSInt &I1, const APSInt &I2)
Determine if two APSInts have the same value, zero- or sign-extending as needed.
Definition APSInt.h:320
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:144
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
The address of a basic block.
Definition Constants.h:899
static LLVM_ABI unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, const DataLayout *DL)
Determine how a pair of casts can be eliminated, if they can be at all.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static LLVM_ABI bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static LLVM_ABI Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static LLVM_ABI Constant * getNot(Constant *C)
static LLVM_ABI Constant * getXor(Constant *C1, Constant *C2)
static LLVM_ABI Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static LLVM_ABI bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
static LLVM_ABI Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(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
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
Definition Constants.h:257
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
Constant Vector Declarations.
Definition Constants.h:517
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
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 Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:316
static LLVM_ABI bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition Operator.h:430
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
Definition Operator.h:491
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value * > IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers.
Module * getParent()
Get the module that this global value is contained inside of...
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isCast() const
LLVM_ABI bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isBinaryOp() const
bool isUnaryOp() const
bool isIntDivRem() const
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 Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
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
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:165
LLVM_ABI bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition Type.cpp:250
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
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
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:294
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
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
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
LLVM_ABI Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition Value.cpp:956
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
Base class of all SIMD vector types.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Type * getElementType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
auto m_Undef()
Match an arbitrary undef constant.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
This is an optimization pass for GlobalISel generic memory operations.
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:1727
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:644
LLVM_ABI Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
LLVM_ABI Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
LLVM_ABI Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
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
constexpr int PoisonMaskElem
LLVM_ABI Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition APFloat.h:1569
LLVM_ABI Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:304
static constexpr roundingMode rmTowardZero
Definition APFloat.h:308
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39