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