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
59 // Assume that pointers are never more than 64 bits wide, and only use this
60 // for the middle type. Otherwise we could end up folding away illegal
61 // bitcasts between address spaces with different sizes.
62 IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
63
64 // Let CastInst::isEliminableCastPair do the heavy lifting.
65 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
66 nullptr, FakeIntPtrTy, nullptr);
67}
68
69static Constant *FoldBitCast(Constant *V, Type *DestTy) {
70 Type *SrcTy = V->getType();
71 if (SrcTy == DestTy)
72 return V; // no-op cast
73
74 if (V->isAllOnesValue())
75 return Constant::getAllOnesValue(DestTy);
76
77 // Handle ConstantInt -> ConstantFP
78 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
79 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
80 // This allows for other simplifications (although some of them
81 // can only be handled by Analysis/ConstantFolding.cpp).
82 if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
84
85 // Make sure dest type is compatible with the folded fp constant.
86 // See note below regarding the PPC_FP128 restriction.
87 if (!DestTy->isFPOrFPVectorTy() || DestTy->isPPC_FP128Ty() ||
88 DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
89 return nullptr;
90
91 return ConstantFP::get(
92 DestTy,
93 APFloat(DestTy->getScalarType()->getFltSemantics(), CI->getValue()));
94 }
95
96 // Handle ConstantFP -> ConstantInt
97 if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
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.
152 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
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(),
203 APFloat::rmNearestTiesToEven, &ignored);
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);
213 if (APFloat::opInvalidOp ==
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,
229 APFloat::rmNearestTiesToEven);
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.
270 if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
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
299 if (isa<PoisonValue>(Cond))
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
325 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||
326 isa<ConstantPointerNull>(C) || isa<Function>(C))
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
416 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
417 if (!CIdx) return nullptr;
418
419 // Do not iterate on scalable vector. The num of elements is unknown at
420 // compile-time.
421 if (isa<ScalableVectorType>(Val->getType()))
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, [](int Elt) { return Elt == 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, [](int Elt) { return Elt == 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 isa<GlobalValue>(CE1->getOperand(0))) {
753 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
754
755 Align GVAlign; // defaults to 1
756
757 if (Module *TheModule = GV->getParent()) {
758 const DataLayout &DL = TheModule->getDataLayout();
759 GVAlign = GV->getPointerAlignment(DL);
760
761 // If the function alignment is not specified then assume that it
762 // is 4.
763 // This is dangerous; on x86, the alignment of the pointer
764 // corresponds to the alignment of the function, but might be less
765 // than 4 if it isn't explicitly specified.
766 // However, a fix for this behaviour was reverted because it
767 // increased code size (see https://reviews.llvm.org/D55115)
768 // FIXME: This code should be deleted once existing targets have
769 // appropriate defaults
770 if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
771 GVAlign = Align(4);
772 } else if (isa<GlobalVariable>(GV)) {
773 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
774 }
775
776 if (GVAlign > 1) {
777 unsigned DstWidth = CI2->getBitWidth();
778 unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
779 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
780
781 // If checking bits we know are clear, return zero.
782 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
783 return Constant::getNullValue(CI2->getType());
784 }
785 }
786 }
787 break;
788 }
789 } else if (isa<ConstantInt>(C1)) {
790 // If C1 is a ConstantInt and C2 is not, swap the operands.
791 if (Instruction::isCommutative(Opcode))
792 return ConstantExpr::isDesirableBinOp(Opcode)
793 ? ConstantExpr::get(Opcode, C2, C1)
794 : ConstantFoldBinaryInstruction(Opcode, C2, C1);
795 }
796
797 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
798 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
799 const APInt &C1V = CI1->getValue();
800 const APInt &C2V = CI2->getValue();
801 switch (Opcode) {
802 default:
803 break;
804 case Instruction::Add:
805 return ConstantInt::get(C1->getType(), C1V + C2V);
806 case Instruction::Sub:
807 return ConstantInt::get(C1->getType(), C1V - C2V);
808 case Instruction::Mul:
809 return ConstantInt::get(C1->getType(), C1V * C2V);
810 case Instruction::UDiv:
811 assert(!CI2->isZero() && "Div by zero handled above");
812 return ConstantInt::get(CI1->getType(), C1V.udiv(C2V));
813 case Instruction::SDiv:
814 assert(!CI2->isZero() && "Div by zero handled above");
815 if (C2V.isAllOnes() && C1V.isMinSignedValue())
816 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison
817 return ConstantInt::get(CI1->getType(), C1V.sdiv(C2V));
818 case Instruction::URem:
819 assert(!CI2->isZero() && "Div by zero handled above");
820 return ConstantInt::get(C1->getType(), C1V.urem(C2V));
821 case Instruction::SRem:
822 assert(!CI2->isZero() && "Div by zero handled above");
823 if (C2V.isAllOnes() && C1V.isMinSignedValue())
824 return PoisonValue::get(C1->getType()); // MIN_INT % -1 -> poison
825 return ConstantInt::get(C1->getType(), C1V.srem(C2V));
826 case Instruction::And:
827 return ConstantInt::get(C1->getType(), C1V & C2V);
828 case Instruction::Or:
829 return ConstantInt::get(C1->getType(), C1V | C2V);
830 case Instruction::Xor:
831 return ConstantInt::get(C1->getType(), C1V ^ C2V);
832 case Instruction::Shl:
833 if (C2V.ult(C1V.getBitWidth()))
834 return ConstantInt::get(C1->getType(), C1V.shl(C2V));
835 return PoisonValue::get(C1->getType()); // too big shift is poison
836 case Instruction::LShr:
837 if (C2V.ult(C1V.getBitWidth()))
838 return ConstantInt::get(C1->getType(), C1V.lshr(C2V));
839 return PoisonValue::get(C1->getType()); // too big shift is poison
840 case Instruction::AShr:
841 if (C2V.ult(C1V.getBitWidth()))
842 return ConstantInt::get(C1->getType(), C1V.ashr(C2V));
843 return PoisonValue::get(C1->getType()); // too big shift is poison
844 }
845 }
846
847 if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(),
848 /*AllowLHSConstant*/ true))
849 return C1;
850 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
851 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
852 const APFloat &C1V = CFP1->getValueAPF();
853 const APFloat &C2V = CFP2->getValueAPF();
854 APFloat C3V = C1V; // copy for modification
855 switch (Opcode) {
856 default:
857 break;
858 case Instruction::FAdd:
859 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
860 return ConstantFP::get(C1->getType(), C3V);
861 case Instruction::FSub:
862 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
863 return ConstantFP::get(C1->getType(), C3V);
864 case Instruction::FMul:
865 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
866 return ConstantFP::get(C1->getType(), C3V);
867 case Instruction::FDiv:
868 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
869 return ConstantFP::get(C1->getType(), C3V);
870 case Instruction::FRem:
871 (void)C3V.mod(C2V);
872 return ConstantFP::get(C1->getType(), C3V);
873 }
874 }
875 }
876
877 if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
878 // Fast path for splatted constants.
879 if (Constant *C2Splat = C2->getSplatValue()) {
880 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
881 return PoisonValue::get(VTy);
882 if (Constant *C1Splat = C1->getSplatValue()) {
883 Constant *Res =
885 ? ConstantExpr::get(Opcode, C1Splat, C2Splat)
886 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat);
887 if (!Res)
888 return nullptr;
889 return ConstantVector::getSplat(VTy->getElementCount(), Res);
890 }
891 }
892
893 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
894 // Fold each element and create a vector constant from those constants.
896 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
897 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
898 Constant *ExtractIdx = ConstantInt::get(Ty, i);
902 ? ConstantExpr::get(Opcode, LHS, RHS)
904 if (!Res)
905 return nullptr;
906 Result.push_back(Res);
907 }
908
909 return ConstantVector::get(Result);
910 }
911 }
912
913 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
914 // There are many possible foldings we could do here. We should probably
915 // at least fold add of a pointer with an integer into the appropriate
916 // getelementptr. This will improve alias analysis a bit.
917
918 // Given ((a + b) + c), if (b + c) folds to something interesting, return
919 // (a + (b + c)).
920 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
921 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
922 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
923 return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
924 }
925 } else if (isa<ConstantExpr>(C2)) {
926 // If C2 is a constant expr and C1 isn't, flop them around and fold the
927 // other way if possible.
928 if (Instruction::isCommutative(Opcode))
929 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
930 }
931
932 // i1 can be simplified in many cases.
933 if (C1->getType()->isIntegerTy(1)) {
934 switch (Opcode) {
935 case Instruction::Add:
936 case Instruction::Sub:
937 return ConstantExpr::getXor(C1, C2);
938 case Instruction::Shl:
939 case Instruction::LShr:
940 case Instruction::AShr:
941 // We can assume that C2 == 0. If it were one the result would be
942 // undefined because the shift value is as large as the bitwidth.
943 return C1;
944 case Instruction::SDiv:
945 case Instruction::UDiv:
946 // We can assume that C2 == 1. If it were zero the result would be
947 // undefined through division by zero.
948 return C1;
949 case Instruction::URem:
950 case Instruction::SRem:
951 // We can assume that C2 == 1. If it were zero the result would be
952 // undefined through division by zero.
953 return ConstantInt::getFalse(C1->getContext());
954 default:
955 break;
956 }
957 }
958
959 // We don't know how to fold this.
960 return nullptr;
961}
962
964 const GlobalValue *GV2) {
965 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
966 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
967 return true;
968 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
969 Type *Ty = GVar->getValueType();
970 // A global with opaque type might end up being zero sized.
971 if (!Ty->isSized())
972 return true;
973 // A global with an empty type might lie at the address of any other
974 // global.
975 if (Ty->isEmptyTy())
976 return true;
977 }
978 return false;
979 };
980 // Don't try to decide equality of aliases.
981 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
982 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
983 return ICmpInst::ICMP_NE;
984 return ICmpInst::BAD_ICMP_PREDICATE;
985}
986
987/// This function determines if there is anything we can decide about the two
988/// constants provided. This doesn't need to handle simple things like integer
989/// comparisons, but should instead handle ConstantExprs and GlobalValues.
990/// If we can determine that the two constants have a particular relation to
991/// each other, we should return the corresponding ICmp predicate, otherwise
992/// return ICmpInst::BAD_ICMP_PREDICATE.
994 assert(V1->getType() == V2->getType() &&
995 "Cannot compare different types of values!");
996 if (V1 == V2) return ICmpInst::ICMP_EQ;
997
998 // The following folds only apply to pointers.
999 if (!V1->getType()->isPointerTy())
1000 return ICmpInst::BAD_ICMP_PREDICATE;
1001
1002 // To simplify this code we canonicalize the relation so that the first
1003 // operand is always the most "complex" of the two. We consider simple
1004 // constants (like ConstantPointerNull) to be the simplest, followed by
1005 // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
1006 auto GetComplexity = [](Constant *V) {
1007 if (isa<ConstantExpr>(V))
1008 return 3;
1009 if (isa<GlobalValue>(V))
1010 return 2;
1011 if (isa<BlockAddress>(V))
1012 return 1;
1013 return 0;
1014 };
1015 if (GetComplexity(V1) < GetComplexity(V2)) {
1016 ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
1017 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1018 return ICmpInst::getSwappedPredicate(SwappedRelation);
1019 return ICmpInst::BAD_ICMP_PREDICATE;
1020 }
1021
1022 if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1023 // Now we know that the RHS is a BlockAddress or simple constant.
1024 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1025 // Block address in another function can't equal this one, but block
1026 // addresses in the current function might be the same if blocks are
1027 // empty.
1028 if (BA2->getFunction() != BA->getFunction())
1029 return ICmpInst::ICMP_NE;
1030 } else if (isa<ConstantPointerNull>(V2)) {
1031 return ICmpInst::ICMP_NE;
1032 }
1033 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1034 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1035 // constant.
1036 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1037 return areGlobalsPotentiallyEqual(GV, GV2);
1038 } else if (isa<BlockAddress>(V2)) {
1039 return ICmpInst::ICMP_NE; // Globals never equal labels.
1040 } else if (isa<ConstantPointerNull>(V2)) {
1041 // GlobalVals can never be null unless they have external weak linkage.
1042 // We don't try to evaluate aliases here.
1043 // NOTE: We should not be doing this constant folding if null pointer
1044 // is considered valid for the function. But currently there is no way to
1045 // query it from the Constant type.
1046 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1047 !NullPointerIsDefined(nullptr /* F */,
1048 GV->getType()->getAddressSpace()))
1049 return ICmpInst::ICMP_UGT;
1050 }
1051 } else if (auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
1052 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1053 // constantexpr, a global, block address, or a simple constant.
1054 Constant *CE1Op0 = CE1->getOperand(0);
1055
1056 switch (CE1->getOpcode()) {
1057 case Instruction::GetElementPtr: {
1058 GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1059 // Ok, since this is a getelementptr, we know that the constant has a
1060 // pointer type. Check the various cases.
1061 if (isa<ConstantPointerNull>(V2)) {
1062 // If we are comparing a GEP to a null pointer, check to see if the base
1063 // of the GEP equals the null pointer.
1064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1065 // If its not weak linkage, the GVal must have a non-zero address
1066 // so the result is greater-than
1067 if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
1068 return ICmpInst::ICMP_UGT;
1069 }
1070 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1071 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1072 if (GV != GV2) {
1073 if (CE1GEP->hasAllZeroIndices())
1074 return areGlobalsPotentiallyEqual(GV, GV2);
1075 return ICmpInst::BAD_ICMP_PREDICATE;
1076 }
1077 }
1078 } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1079 // By far the most common case to handle is when the base pointers are
1080 // obviously to the same global.
1081 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1082 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1083 // Don't know relative ordering, but check for inequality.
1084 if (CE1Op0 != CE2Op0) {
1085 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1086 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
1087 cast<GlobalValue>(CE2Op0));
1088 return ICmpInst::BAD_ICMP_PREDICATE;
1089 }
1090 }
1091 }
1092 break;
1093 }
1094 default:
1095 break;
1096 }
1097 }
1098
1099 return ICmpInst::BAD_ICMP_PREDICATE;
1100}
1101
1103 Constant *C1, Constant *C2) {
1104 Type *ResultTy;
1105 if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1106 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1107 VT->getElementCount());
1108 else
1109 ResultTy = Type::getInt1Ty(C1->getContext());
1110
1111 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1112 if (Predicate == FCmpInst::FCMP_FALSE)
1113 return Constant::getNullValue(ResultTy);
1114
1115 if (Predicate == FCmpInst::FCMP_TRUE)
1116 return Constant::getAllOnesValue(ResultTy);
1117
1118 // Handle some degenerate cases first
1119 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1120 return PoisonValue::get(ResultTy);
1121
1122 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1123 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1124 // For EQ and NE, we can always pick a value for the undef to make the
1125 // predicate pass or fail, so we can return undef.
1126 // Also, if both operands are undef, we can return undef for int comparison.
1127 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1128 return UndefValue::get(ResultTy);
1129
1130 // Otherwise, for integer compare, pick the same value as the non-undef
1131 // operand, and fold it to true or false.
1132 if (isIntegerPredicate)
1133 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1134
1135 // Choosing NaN for the undef will always make unordered comparison succeed
1136 // and ordered comparison fails.
1137 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1138 }
1139
1140 if (C2->isNullValue()) {
1141 // The caller is expected to commute the operands if the constant expression
1142 // is C2.
1143 // C1 >= 0 --> true
1144 if (Predicate == ICmpInst::ICMP_UGE)
1145 return Constant::getAllOnesValue(ResultTy);
1146 // C1 < 0 --> false
1147 if (Predicate == ICmpInst::ICMP_ULT)
1148 return Constant::getNullValue(ResultTy);
1149 }
1150
1151 // If the comparison is a comparison between two i1's, simplify it.
1152 if (C1->getType()->isIntOrIntVectorTy(1)) {
1153 switch (Predicate) {
1154 case ICmpInst::ICMP_EQ:
1155 if (isa<ConstantExpr>(C1))
1158 case ICmpInst::ICMP_NE:
1159 return ConstantExpr::getXor(C1, C2);
1160 default:
1161 break;
1162 }
1163 }
1164
1165 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1166 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1167 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1168 return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate));
1169 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1170 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1171 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1172 return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
1173 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
1174
1175 // Fast path for splatted constants.
1176 if (Constant *C1Splat = C1->getSplatValue())
1177 if (Constant *C2Splat = C2->getSplatValue())
1178 if (Constant *Elt =
1179 ConstantFoldCompareInstruction(Predicate, C1Splat, C2Splat))
1180 return ConstantVector::getSplat(C1VTy->getElementCount(), Elt);
1181
1182 // Do not iterate on scalable vector. The number of elements is unknown at
1183 // compile-time.
1184 if (isa<ScalableVectorType>(C1VTy))
1185 return nullptr;
1186
1187 // If we can constant fold the comparison of each element, constant fold
1188 // the whole vector comparison.
1190 Type *Ty = IntegerType::get(C1->getContext(), 32);
1191 // Compare the elements, producing an i1 result or constant expr.
1192 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1193 I != E; ++I) {
1194 Constant *C1E =
1195 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I));
1196 Constant *C2E =
1197 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I));
1198 Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E);
1199 if (!Elt)
1200 return nullptr;
1201
1202 ResElts.push_back(Elt);
1203 }
1204
1205 return ConstantVector::get(ResElts);
1206 }
1207
1208 if (C1->getType()->isFPOrFPVectorTy()) {
1209 if (C1 == C2) {
1210 // We know that C1 == C2 || isUnordered(C1, C2).
1211 if (Predicate == FCmpInst::FCMP_ONE)
1212 return ConstantInt::getFalse(ResultTy);
1213 else if (Predicate == FCmpInst::FCMP_UEQ)
1214 return ConstantInt::getTrue(ResultTy);
1215 }
1216 } else {
1217 // Evaluate the relation between the two constants, per the predicate.
1218 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1219 switch (evaluateICmpRelation(C1, C2)) {
1220 default: llvm_unreachable("Unknown relational!");
1221 case ICmpInst::BAD_ICMP_PREDICATE:
1222 break; // Couldn't determine anything about these constants.
1223 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1224 // If we know the constants are equal, we can decide the result of this
1225 // computation precisely.
1226 Result = ICmpInst::isTrueWhenEqual(Predicate);
1227 break;
1228 case ICmpInst::ICMP_ULT:
1229 switch (Predicate) {
1230 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
1231 Result = 1; break;
1232 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
1233 Result = 0; break;
1234 default:
1235 break;
1236 }
1237 break;
1238 case ICmpInst::ICMP_SLT:
1239 switch (Predicate) {
1240 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
1241 Result = 1; break;
1242 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
1243 Result = 0; break;
1244 default:
1245 break;
1246 }
1247 break;
1248 case ICmpInst::ICMP_UGT:
1249 switch (Predicate) {
1250 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
1251 Result = 1; break;
1252 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
1253 Result = 0; break;
1254 default:
1255 break;
1256 }
1257 break;
1258 case ICmpInst::ICMP_SGT:
1259 switch (Predicate) {
1260 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
1261 Result = 1; break;
1262 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
1263 Result = 0; break;
1264 default:
1265 break;
1266 }
1267 break;
1268 case ICmpInst::ICMP_ULE:
1269 if (Predicate == ICmpInst::ICMP_UGT)
1270 Result = 0;
1271 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1272 Result = 1;
1273 break;
1274 case ICmpInst::ICMP_SLE:
1275 if (Predicate == ICmpInst::ICMP_SGT)
1276 Result = 0;
1277 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1278 Result = 1;
1279 break;
1280 case ICmpInst::ICMP_UGE:
1281 if (Predicate == ICmpInst::ICMP_ULT)
1282 Result = 0;
1283 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1284 Result = 1;
1285 break;
1286 case ICmpInst::ICMP_SGE:
1287 if (Predicate == ICmpInst::ICMP_SLT)
1288 Result = 0;
1289 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1290 Result = 1;
1291 break;
1292 case ICmpInst::ICMP_NE:
1293 if (Predicate == ICmpInst::ICMP_EQ)
1294 Result = 0;
1295 if (Predicate == ICmpInst::ICMP_NE)
1296 Result = 1;
1297 break;
1298 }
1299
1300 // If we evaluated the result, return it now.
1301 if (Result != -1)
1302 return ConstantInt::get(ResultTy, Result);
1303
1304 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1305 (C1->isNullValue() && !C2->isNullValue())) {
1306 // If C2 is a constant expr and C1 isn't, flip them around and fold the
1307 // other way if possible.
1308 // Also, if C1 is null and C2 isn't, flip them around.
1309 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1310 return ConstantFoldCompareInstruction(Predicate, C2, C1);
1311 }
1312 }
1313 return nullptr;
1314}
1315
1317 std::optional<ConstantRange> InRange,
1318 ArrayRef<Value *> Idxs) {
1319 if (Idxs.empty()) return C;
1320
1322 C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
1323
1324 if (isa<PoisonValue>(C))
1325 return PoisonValue::get(GEPTy);
1326
1327 if (isa<UndefValue>(C))
1328 return UndefValue::get(GEPTy);
1329
1330 auto IsNoOp = [&]() {
1331 // Avoid losing inrange information.
1332 if (InRange)
1333 return false;
1334
1335 return all_of(Idxs, [](Value *Idx) {
1336 Constant *IdxC = cast<Constant>(Idx);
1337 return IdxC->isNullValue() || isa<UndefValue>(IdxC);
1338 });
1339 };
1340 if (IsNoOp())
1341 return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
1343 cast<VectorType>(GEPTy)->getElementCount(), C)
1344 : C;
1345
1346 return nullptr;
1347}
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...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
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
Value * RHS
Value * LHS
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, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
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:678
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:944
static LLVM_ABI bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1314
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)
Definition: Constants.cpp:2564
static LLVM_ABI bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
Definition: Constants.cpp:2443
static LLVM_ABI Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
Definition: Constants.cpp:2765
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:2213
static LLVM_ABI Constant * getNot(Constant *C)
Definition: Constants.cpp:2641
static LLVM_ABI Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2661
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.
Definition: Constants.cpp:2347
static LLVM_ABI bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2389
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2328
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2694
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:277
static LLVM_ABI Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
Definition: Constants.cpp:1026
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
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)
Definition: Constants.cpp:1380
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.
Definition: Constants.cpp:1474
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1423
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.
Definition: Constants.cpp:1713
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
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:318
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...
Definition: GlobalValue.h:663
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isEquality() const
Return true if this predicate is either EQ or NE.
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
Definition: Instruction.h:317
bool isUnaryOp() const
Definition: Instruction.h:316
bool isIntDivRem() const
Definition: Instruction.h:318
Class to represent integer types.
Definition: DerivedTypes.h:42
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.
Definition: Constants.cpp:1885
void reserve(size_type N)
Definition: SmallVector.h:664
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Class to represent struct types.
Definition: DerivedTypes.h:218
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
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:246
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVM_ABI bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:165
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:311
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool isX86_AMXTy() const
Return true if this is X86 AMX.
Definition: Type.h:200
LLVM_ABI const fltSemantics & getFltSemantics() const
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
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
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
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:953
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
Type * getElementType() const
Definition: DerivedTypes.h:463
#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
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:784
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1744
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
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 ...
Definition: Function.cpp:1172
LLVM_ABI Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
constexpr int PoisonMaskElem
LLVM_ABI Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
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:208
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