LLVM 18.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"
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
60 // Assume that pointers are never more than 64 bits wide, and only use this
61 // for the middle type. Otherwise we could end up folding away illegal
62 // bitcasts between address spaces with different sizes.
63 IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
64
65 // Let CastInst::isEliminableCastPair do the heavy lifting.
66 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
67 nullptr, FakeIntPtrTy, nullptr);
68}
69
70static Constant *FoldBitCast(Constant *V, Type *DestTy) {
71 Type *SrcTy = V->getType();
72 if (SrcTy == DestTy)
73 return V; // no-op cast
74
75 // Handle casts from one vector constant to another. We know that the src
76 // and dest type have the same size (otherwise its an illegal cast).
77 if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
78 if (V->isAllOnesValue())
79 return Constant::getAllOnesValue(DestTy);
80
81 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
82 // This allows for other simplifications (although some of them
83 // can only be handled by Analysis/ConstantFolding.cpp).
84 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
86 return nullptr;
87 }
88
89 // Handle integral constant input.
90 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
91 // See note below regarding the PPC_FP128 restriction.
92 if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
93 return ConstantFP::get(DestTy->getContext(),
94 APFloat(DestTy->getFltSemantics(),
95 CI->getValue()));
96
97 // Otherwise, can't fold this (vector?)
98 return nullptr;
99 }
100
101 // Handle ConstantFP input: FP -> Integral.
102 if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
103 // PPC_FP128 is really the sum of two consecutive doubles, where the first
104 // double is always stored first in memory, regardless of the target
105 // endianness. The memory layout of i128, however, depends on the target
106 // endianness, and so we can't fold this without target endianness
107 // information. This should instead be handled by
108 // Analysis/ConstantFolding.cpp
109 if (FP->getType()->isPPC_FP128Ty())
110 return nullptr;
111
112 // Make sure dest type is compatible with the folded integer constant.
113 if (!DestTy->isIntegerTy())
114 return nullptr;
115
116 return ConstantInt::get(FP->getContext(),
117 FP->getValueAPF().bitcastToAPInt());
118 }
119
120 return nullptr;
121}
122
123
124/// V is an integer constant which only has a subset of its bytes used.
125/// The bytes used are indicated by ByteStart (which is the first byte used,
126/// counting from the least significant byte) and ByteSize, which is the number
127/// of bytes used.
128///
129/// This function analyzes the specified constant to see if the specified byte
130/// range can be returned as a simplified constant. If so, the constant is
131/// returned, otherwise null is returned.
132static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
133 unsigned ByteSize) {
134 assert(C->getType()->isIntegerTy() &&
135 (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
136 "Non-byte sized integer input");
137 [[maybe_unused]] unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
138 assert(ByteSize && "Must be accessing some piece");
139 assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
140 assert(ByteSize != CSize && "Should not extract everything");
141
142 // Constant Integers are simple.
143 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
144 APInt V = CI->getValue();
145 if (ByteStart)
146 V.lshrInPlace(ByteStart*8);
147 V = V.trunc(ByteSize*8);
148 return ConstantInt::get(CI->getContext(), V);
149 }
150
151 // In the input is a constant expr, we might be able to recursively simplify.
152 // If not, we definitely can't do anything.
153 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
154 if (!CE) return nullptr;
155
156 switch (CE->getOpcode()) {
157 default: return nullptr;
158 case Instruction::Shl: {
159 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
160 if (!Amt)
161 return nullptr;
162 APInt ShAmt = Amt->getValue();
163 // Cannot analyze non-byte shifts.
164 if ((ShAmt & 7) != 0)
165 return nullptr;
166 ShAmt.lshrInPlace(3);
167
168 // If the extract is known to be all zeros, return zero.
169 if (ShAmt.uge(ByteStart + ByteSize))
171 IntegerType::get(CE->getContext(), ByteSize * 8));
172 // If the extract is known to be fully in the input, extract it.
173 if (ShAmt.ule(ByteStart))
174 return ExtractConstantBytes(CE->getOperand(0),
175 ByteStart - ShAmt.getZExtValue(), ByteSize);
176
177 // TODO: Handle the 'partially zero' case.
178 return nullptr;
179 }
180 }
181}
182
184 Type *DestTy) {
186 ? ConstantExpr::getCast(opc, V, DestTy)
187 : ConstantFoldCastInstruction(opc, V, DestTy);
188}
189
191 Type *DestTy) {
192 if (isa<PoisonValue>(V))
193 return PoisonValue::get(DestTy);
194
195 if (isa<UndefValue>(V)) {
196 // zext(undef) = 0, because the top bits will be zero.
197 // sext(undef) = 0, because the top bits will all be the same.
198 // [us]itofp(undef) = 0, because the result value is bounded.
199 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
200 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
201 return Constant::getNullValue(DestTy);
202 return UndefValue::get(DestTy);
203 }
204
205 if (V->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() &&
206 opc != Instruction::AddrSpaceCast)
207 return Constant::getNullValue(DestTy);
208
209 // If the cast operand is a constant expression, there's a few things we can
210 // do to try to simplify it.
211 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
212 if (CE->isCast()) {
213 // Try hard to fold cast of cast because they are often eliminable.
214 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
215 return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
216 }
217 }
218
219 // If the cast operand is a constant vector, perform the cast by
220 // operating on each element. In the cast of bitcasts, the element
221 // count may be mismatched; don't attempt to handle that here.
222 if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
223 DestTy->isVectorTy() &&
224 cast<FixedVectorType>(DestTy)->getNumElements() ==
225 cast<FixedVectorType>(V->getType())->getNumElements()) {
226 VectorType *DestVecTy = cast<VectorType>(DestTy);
227 Type *DstEltTy = DestVecTy->getElementType();
228 // Fast path for splatted constants.
229 if (Constant *Splat = V->getSplatValue()) {
230 Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
231 if (!Res)
232 return nullptr;
234 cast<VectorType>(DestTy)->getElementCount(), Res);
235 }
237 Type *Ty = IntegerType::get(V->getContext(), 32);
238 for (unsigned i = 0,
239 e = cast<FixedVectorType>(V->getType())->getNumElements();
240 i != e; ++i) {
242 Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
243 if (!Casted)
244 return nullptr;
245 res.push_back(Casted);
246 }
247 return ConstantVector::get(res);
248 }
249
250 // We actually have to do a cast now. Perform the cast according to the
251 // opcode specified.
252 switch (opc) {
253 default:
254 llvm_unreachable("Failed to cast constant expression");
255 case Instruction::FPTrunc:
256 case Instruction::FPExt:
257 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
258 bool ignored;
259 APFloat Val = FPC->getValueAPF();
260 Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
261 &ignored);
262 return ConstantFP::get(V->getContext(), Val);
263 }
264 return nullptr; // Can't fold.
265 case Instruction::FPToUI:
266 case Instruction::FPToSI:
267 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
268 const APFloat &V = FPC->getValueAPF();
269 bool ignored;
270 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
271 APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
272 if (APFloat::opInvalidOp ==
273 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
274 // Undefined behavior invoked - the destination type can't represent
275 // the input constant.
276 return PoisonValue::get(DestTy);
277 }
278 return ConstantInt::get(FPC->getContext(), IntVal);
279 }
280 return nullptr; // Can't fold.
281 case Instruction::UIToFP:
282 case Instruction::SIToFP:
283 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
284 const APInt &api = CI->getValue();
285 APFloat apf(DestTy->getFltSemantics(),
287 apf.convertFromAPInt(api, opc==Instruction::SIToFP,
288 APFloat::rmNearestTiesToEven);
289 return ConstantFP::get(V->getContext(), apf);
290 }
291 return nullptr;
292 case Instruction::ZExt:
293 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
294 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
295 return ConstantInt::get(V->getContext(),
296 CI->getValue().zext(BitWidth));
297 }
298 return nullptr;
299 case Instruction::SExt:
300 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
301 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
302 return ConstantInt::get(V->getContext(),
303 CI->getValue().sext(BitWidth));
304 }
305 return nullptr;
306 case Instruction::Trunc: {
307 if (V->getType()->isVectorTy())
308 return nullptr;
309
310 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
311 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
312 return ConstantInt::get(V->getContext(),
313 CI->getValue().trunc(DestBitWidth));
314 }
315
316 // The input must be a constantexpr. See if we can simplify this based on
317 // the bytes we are demanding. Only do this if the source and dest are an
318 // even multiple of a byte.
319 if ((DestBitWidth & 7) == 0 &&
320 (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
321 if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
322 return Res;
323
324 return nullptr;
325 }
326 case Instruction::BitCast:
327 return FoldBitCast(V, DestTy);
328 case Instruction::AddrSpaceCast:
329 case Instruction::IntToPtr:
330 case Instruction::PtrToInt:
331 return nullptr;
332 }
333}
334
336 Constant *V1, Constant *V2) {
337 // Check for i1 and vector true/false conditions.
338 if (Cond->isNullValue()) return V2;
339 if (Cond->isAllOnesValue()) return V1;
340
341 // If the condition is a vector constant, fold the result elementwise.
342 if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
343 auto *V1VTy = CondV->getType();
345 Type *Ty = IntegerType::get(CondV->getContext(), 32);
346 for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
347 Constant *V;
349 ConstantInt::get(Ty, i));
351 ConstantInt::get(Ty, i));
352 auto *Cond = cast<Constant>(CondV->getOperand(i));
353 if (isa<PoisonValue>(Cond)) {
354 V = PoisonValue::get(V1Element->getType());
355 } else if (V1Element == V2Element) {
356 V = V1Element;
357 } else if (isa<UndefValue>(Cond)) {
358 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
359 } else {
360 if (!isa<ConstantInt>(Cond)) break;
361 V = Cond->isNullValue() ? V2Element : V1Element;
362 }
363 Result.push_back(V);
364 }
365
366 // If we were able to build the vector, return it.
367 if (Result.size() == V1VTy->getNumElements())
368 return ConstantVector::get(Result);
369 }
370
371 if (isa<PoisonValue>(Cond))
372 return PoisonValue::get(V1->getType());
373
374 if (isa<UndefValue>(Cond)) {
375 if (isa<UndefValue>(V1)) return V1;
376 return V2;
377 }
378
379 if (V1 == V2) return V1;
380
381 if (isa<PoisonValue>(V1))
382 return V2;
383 if (isa<PoisonValue>(V2))
384 return V1;
385
386 // If the true or false value is undef, we can fold to the other value as
387 // long as the other value isn't poison.
388 auto NotPoison = [](Constant *C) {
389 if (isa<PoisonValue>(C))
390 return false;
391
392 // TODO: We can analyze ConstExpr by opcode to determine if there is any
393 // possibility of poison.
394 if (isa<ConstantExpr>(C))
395 return false;
396
397 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||
398 isa<ConstantPointerNull>(C) || isa<Function>(C))
399 return true;
400
401 if (C->getType()->isVectorTy())
402 return !C->containsPoisonElement() && !C->containsConstantExpression();
403
404 // TODO: Recursively analyze aggregates or other constants.
405 return false;
406 };
407 if (isa<UndefValue>(V1) && NotPoison(V2)) return V2;
408 if (isa<UndefValue>(V2) && NotPoison(V1)) return V1;
409
410 return nullptr;
411}
412
414 Constant *Idx) {
415 auto *ValVTy = cast<VectorType>(Val->getType());
416
417 // extractelt poison, C -> poison
418 // extractelt C, undef -> poison
419 if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx))
420 return PoisonValue::get(ValVTy->getElementType());
421
422 // extractelt undef, C -> undef
423 if (isa<UndefValue>(Val))
424 return UndefValue::get(ValVTy->getElementType());
425
426 auto *CIdx = dyn_cast<ConstantInt>(Idx);
427 if (!CIdx)
428 return nullptr;
429
430 if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) {
431 // ee({w,x,y,z}, wrong_value) -> poison
432 if (CIdx->uge(ValFVTy->getNumElements()))
433 return PoisonValue::get(ValFVTy->getElementType());
434 }
435
436 // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...)
437 if (auto *CE = dyn_cast<ConstantExpr>(Val)) {
438 if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
440 Ops.reserve(CE->getNumOperands());
441 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
442 Constant *Op = CE->getOperand(i);
443 if (Op->getType()->isVectorTy()) {
445 if (!ScalarOp)
446 return nullptr;
447 Ops.push_back(ScalarOp);
448 } else
449 Ops.push_back(Op);
450 }
451 return CE->getWithOperands(Ops, ValVTy->getElementType(), false,
452 GEP->getSourceElementType());
453 } else if (CE->getOpcode() == Instruction::InsertElement) {
454 if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
455 if (APSInt::isSameValue(APSInt(IEIdx->getValue()),
456 APSInt(CIdx->getValue()))) {
457 return CE->getOperand(1);
458 } else {
459 return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx);
460 }
461 }
462 }
463 }
464
465 if (Constant *C = Val->getAggregateElement(CIdx))
466 return C;
467
468 // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x
469 if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
470 if (Constant *SplatVal = Val->getSplatValue())
471 return SplatVal;
472 }
473
474 return nullptr;
475}
476
478 Constant *Elt,
479 Constant *Idx) {
480 if (isa<UndefValue>(Idx))
481 return PoisonValue::get(Val->getType());
482
483 // Inserting null into all zeros is still all zeros.
484 // TODO: This is true for undef and poison splats too.
485 if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue())
486 return Val;
487
488 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
489 if (!CIdx) return nullptr;
490
491 // Do not iterate on scalable vector. The num of elements is unknown at
492 // compile-time.
493 if (isa<ScalableVectorType>(Val->getType()))
494 return nullptr;
495
496 auto *ValTy = cast<FixedVectorType>(Val->getType());
497
498 unsigned NumElts = ValTy->getNumElements();
499 if (CIdx->uge(NumElts))
500 return PoisonValue::get(Val->getType());
501
503 Result.reserve(NumElts);
504 auto *Ty = Type::getInt32Ty(Val->getContext());
505 uint64_t IdxVal = CIdx->getZExtValue();
506 for (unsigned i = 0; i != NumElts; ++i) {
507 if (i == IdxVal) {
508 Result.push_back(Elt);
509 continue;
510 }
511
513 Result.push_back(C);
514 }
515
516 return ConstantVector::get(Result);
517}
518
520 ArrayRef<int> Mask) {
521 auto *V1VTy = cast<VectorType>(V1->getType());
522 unsigned MaskNumElts = Mask.size();
523 auto MaskEltCount =
524 ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
525 Type *EltTy = V1VTy->getElementType();
526
527 // Poison shuffle mask -> poison value.
528 if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
529 return PoisonValue::get(VectorType::get(EltTy, MaskEltCount));
530 }
531
532 // If the mask is all zeros this is a splat, no need to go through all
533 // elements.
534 if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
535 Type *Ty = IntegerType::get(V1->getContext(), 32);
536 Constant *Elt =
538
539 if (Elt->isNullValue()) {
540 auto *VTy = VectorType::get(EltTy, MaskEltCount);
541 return ConstantAggregateZero::get(VTy);
542 } else if (!MaskEltCount.isScalable())
543 return ConstantVector::getSplat(MaskEltCount, Elt);
544 }
545 // Do not iterate on scalable vector. The num of elements is unknown at
546 // compile-time.
547 if (isa<ScalableVectorType>(V1VTy))
548 return nullptr;
549
550 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
551
552 // Loop over the shuffle mask, evaluating each element.
554 for (unsigned i = 0; i != MaskNumElts; ++i) {
555 int Elt = Mask[i];
556 if (Elt == -1) {
557 Result.push_back(UndefValue::get(EltTy));
558 continue;
559 }
560 Constant *InElt;
561 if (unsigned(Elt) >= SrcNumElts*2)
562 InElt = UndefValue::get(EltTy);
563 else if (unsigned(Elt) >= SrcNumElts) {
564 Type *Ty = IntegerType::get(V2->getContext(), 32);
565 InElt =
567 ConstantInt::get(Ty, Elt - SrcNumElts));
568 } else {
569 Type *Ty = IntegerType::get(V1->getContext(), 32);
571 }
572 Result.push_back(InElt);
573 }
574
575 return ConstantVector::get(Result);
576}
577
579 ArrayRef<unsigned> Idxs) {
580 // Base case: no indices, so return the entire value.
581 if (Idxs.empty())
582 return Agg;
583
584 if (Constant *C = Agg->getAggregateElement(Idxs[0]))
586
587 return nullptr;
588}
589
591 Constant *Val,
592 ArrayRef<unsigned> Idxs) {
593 // Base case: no indices, so replace the entire value.
594 if (Idxs.empty())
595 return Val;
596
597 unsigned NumElts;
598 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
599 NumElts = ST->getNumElements();
600 else
601 NumElts = cast<ArrayType>(Agg->getType())->getNumElements();
602
604 for (unsigned i = 0; i != NumElts; ++i) {
605 Constant *C = Agg->getAggregateElement(i);
606 if (!C) return nullptr;
607
608 if (Idxs[0] == i)
610
611 Result.push_back(C);
612 }
613
614 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
615 return ConstantStruct::get(ST, Result);
616 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result);
617}
618
620 assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
621
622 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
623 // vectors are always evaluated per element.
624 bool IsScalableVector = isa<ScalableVectorType>(C->getType());
625 bool HasScalarUndefOrScalableVectorUndef =
626 (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C);
627
628 if (HasScalarUndefOrScalableVectorUndef) {
629 switch (static_cast<Instruction::UnaryOps>(Opcode)) {
630 case Instruction::FNeg:
631 return C; // -undef -> undef
632 case Instruction::UnaryOpsEnd:
633 llvm_unreachable("Invalid UnaryOp");
634 }
635 }
636
637 // Constant should not be UndefValue, unless these are vector constants.
638 assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue");
639 // We only have FP UnaryOps right now.
640 assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
641
642 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
643 const APFloat &CV = CFP->getValueAPF();
644 switch (Opcode) {
645 default:
646 break;
647 case Instruction::FNeg:
648 return ConstantFP::get(C->getContext(), neg(CV));
649 }
650 } else if (auto *VTy = dyn_cast<FixedVectorType>(C->getType())) {
651
652 Type *Ty = IntegerType::get(VTy->getContext(), 32);
653 // Fast path for splatted constants.
654 if (Constant *Splat = C->getSplatValue())
656 return ConstantVector::getSplat(VTy->getElementCount(), Elt);
657
658 // Fold each element and create a vector constant from those constants.
660 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
661 Constant *ExtractIdx = ConstantInt::get(Ty, i);
662 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
664 if (!Res)
665 return nullptr;
666 Result.push_back(Res);
667 }
668
669 return ConstantVector::get(Result);
670 }
671
672 // We don't know how to fold this.
673 return nullptr;
674}
675
677 Constant *C2) {
678 assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");
679
680 // Simplify BinOps with their identity values first. They are no-ops and we
681 // can always return the other value, including undef or poison values.
682 // FIXME: remove unnecessary duplicated identity patterns below.
683 // FIXME: Use AllowRHSConstant with getBinOpIdentity to handle additional ops,
684 // like X << 0 = X.
686 if (Identity) {
687 if (C1 == Identity)
688 return C2;
689 if (C2 == Identity)
690 return C1;
691 }
692
693 // Binary operations propagate poison.
694 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
695 return PoisonValue::get(C1->getType());
696
697 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
698 // vectors are always evaluated per element.
699 bool IsScalableVector = isa<ScalableVectorType>(C1->getType());
700 bool HasScalarUndefOrScalableVectorUndef =
701 (!C1->getType()->isVectorTy() || IsScalableVector) &&
702 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
703 if (HasScalarUndefOrScalableVectorUndef) {
704 switch (static_cast<Instruction::BinaryOps>(Opcode)) {
705 case Instruction::Xor:
706 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
707 // Handle undef ^ undef -> 0 special case. This is a common
708 // idiom (misuse).
709 return Constant::getNullValue(C1->getType());
710 [[fallthrough]];
711 case Instruction::Add:
712 case Instruction::Sub:
713 return UndefValue::get(C1->getType());
714 case Instruction::And:
715 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
716 return C1;
717 return Constant::getNullValue(C1->getType()); // undef & X -> 0
718 case Instruction::Mul: {
719 // undef * undef -> undef
720 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
721 return C1;
722 const APInt *CV;
723 // X * undef -> undef if X is odd
724 if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
725 if ((*CV)[0])
726 return UndefValue::get(C1->getType());
727
728 // X * undef -> 0 otherwise
729 return Constant::getNullValue(C1->getType());
730 }
731 case Instruction::SDiv:
732 case Instruction::UDiv:
733 // X / undef -> poison
734 // X / 0 -> poison
735 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
736 return PoisonValue::get(C2->getType());
737 // undef / 1 -> undef
738 if (match(C2, m_One()))
739 return C1;
740 // undef / X -> 0 otherwise
741 return Constant::getNullValue(C1->getType());
742 case Instruction::URem:
743 case Instruction::SRem:
744 // X % undef -> poison
745 // X % 0 -> poison
746 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
747 return PoisonValue::get(C2->getType());
748 // undef % X -> 0 otherwise
749 return Constant::getNullValue(C1->getType());
750 case Instruction::Or: // X | undef -> -1
751 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
752 return C1;
753 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
754 case Instruction::LShr:
755 // X >>l undef -> poison
756 if (isa<UndefValue>(C2))
757 return PoisonValue::get(C2->getType());
758 // undef >>l 0 -> undef
759 if (match(C2, m_Zero()))
760 return C1;
761 // undef >>l X -> 0
762 return Constant::getNullValue(C1->getType());
763 case Instruction::AShr:
764 // X >>a undef -> poison
765 if (isa<UndefValue>(C2))
766 return PoisonValue::get(C2->getType());
767 // undef >>a 0 -> undef
768 if (match(C2, m_Zero()))
769 return C1;
770 // TODO: undef >>a X -> poison if the shift is exact
771 // undef >>a X -> 0
772 return Constant::getNullValue(C1->getType());
773 case Instruction::Shl:
774 // X << undef -> undef
775 if (isa<UndefValue>(C2))
776 return PoisonValue::get(C2->getType());
777 // undef << 0 -> undef
778 if (match(C2, m_Zero()))
779 return C1;
780 // undef << X -> 0
781 return Constant::getNullValue(C1->getType());
782 case Instruction::FSub:
783 // -0.0 - undef --> undef (consistent with "fneg undef")
784 if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
785 return C2;
786 [[fallthrough]];
787 case Instruction::FAdd:
788 case Instruction::FMul:
789 case Instruction::FDiv:
790 case Instruction::FRem:
791 // [any flop] undef, undef -> undef
792 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
793 return C1;
794 // [any flop] C, undef -> NaN
795 // [any flop] undef, C -> NaN
796 // We could potentially specialize NaN/Inf constants vs. 'normal'
797 // constants (possibly differently depending on opcode and operand). This
798 // would allow returning undef sometimes. But it is always safe to fold to
799 // NaN because we can choose the undef operand as NaN, and any FP opcode
800 // with a NaN operand will propagate NaN.
801 return ConstantFP::getNaN(C1->getType());
802 case Instruction::BinaryOpsEnd:
803 llvm_unreachable("Invalid BinaryOp");
804 }
805 }
806
807 // Neither constant should be UndefValue, unless these are vector constants.
808 assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue");
809
810 // Handle simplifications when the RHS is a constant int.
811 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
812 switch (Opcode) {
813 case Instruction::Add:
814 if (CI2->isZero()) return C1; // X + 0 == X
815 break;
816 case Instruction::Sub:
817 if (CI2->isZero()) return C1; // X - 0 == X
818 break;
819 case Instruction::Mul:
820 if (CI2->isZero()) return C2; // X * 0 == 0
821 if (CI2->isOne())
822 return C1; // X * 1 == X
823 break;
824 case Instruction::UDiv:
825 case Instruction::SDiv:
826 if (CI2->isOne())
827 return C1; // X / 1 == X
828 if (CI2->isZero())
829 return PoisonValue::get(CI2->getType()); // X / 0 == poison
830 break;
831 case Instruction::URem:
832 case Instruction::SRem:
833 if (CI2->isOne())
834 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
835 if (CI2->isZero())
836 return PoisonValue::get(CI2->getType()); // X % 0 == poison
837 break;
838 case Instruction::And:
839 if (CI2->isZero()) return C2; // X & 0 == 0
840 if (CI2->isMinusOne())
841 return C1; // X & -1 == X
842
843 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
844 // If and'ing the address of a global with a constant, fold it.
845 if (CE1->getOpcode() == Instruction::PtrToInt &&
846 isa<GlobalValue>(CE1->getOperand(0))) {
847 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
848
849 Align GVAlign; // defaults to 1
850
851 if (Module *TheModule = GV->getParent()) {
852 const DataLayout &DL = TheModule->getDataLayout();
853 GVAlign = GV->getPointerAlignment(DL);
854
855 // If the function alignment is not specified then assume that it
856 // is 4.
857 // This is dangerous; on x86, the alignment of the pointer
858 // corresponds to the alignment of the function, but might be less
859 // than 4 if it isn't explicitly specified.
860 // However, a fix for this behaviour was reverted because it
861 // increased code size (see https://reviews.llvm.org/D55115)
862 // FIXME: This code should be deleted once existing targets have
863 // appropriate defaults
864 if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
865 GVAlign = Align(4);
866 } else if (isa<GlobalVariable>(GV)) {
867 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
868 }
869
870 if (GVAlign > 1) {
871 unsigned DstWidth = CI2->getType()->getBitWidth();
872 unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
873 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
874
875 // If checking bits we know are clear, return zero.
876 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
877 return Constant::getNullValue(CI2->getType());
878 }
879 }
880 }
881 break;
882 case Instruction::Or:
883 if (CI2->isZero()) return C1; // X | 0 == X
884 if (CI2->isMinusOne())
885 return C2; // X | -1 == -1
886 break;
887 case Instruction::Xor:
888 if (CI2->isZero()) return C1; // X ^ 0 == X
889
890 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
891 switch (CE1->getOpcode()) {
892 default: break;
893 case Instruction::ICmp:
894 case Instruction::FCmp:
895 // cmp pred ^ true -> cmp !pred
896 assert(CI2->isOne());
897 CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
899 return ConstantExpr::getCompare(pred, CE1->getOperand(0),
900 CE1->getOperand(1));
901 }
902 }
903 break;
904 }
905 } else if (isa<ConstantInt>(C1)) {
906 // If C1 is a ConstantInt and C2 is not, swap the operands.
909 ? ConstantExpr::get(Opcode, C2, C1)
911 }
912
913 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
914 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
915 const APInt &C1V = CI1->getValue();
916 const APInt &C2V = CI2->getValue();
917 switch (Opcode) {
918 default:
919 break;
920 case Instruction::Add:
921 return ConstantInt::get(CI1->getContext(), C1V + C2V);
922 case Instruction::Sub:
923 return ConstantInt::get(CI1->getContext(), C1V - C2V);
924 case Instruction::Mul:
925 return ConstantInt::get(CI1->getContext(), C1V * C2V);
926 case Instruction::UDiv:
927 assert(!CI2->isZero() && "Div by zero handled above");
928 return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
929 case Instruction::SDiv:
930 assert(!CI2->isZero() && "Div by zero handled above");
931 if (C2V.isAllOnes() && C1V.isMinSignedValue())
932 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison
933 return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
934 case Instruction::URem:
935 assert(!CI2->isZero() && "Div by zero handled above");
936 return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
937 case Instruction::SRem:
938 assert(!CI2->isZero() && "Div by zero handled above");
939 if (C2V.isAllOnes() && C1V.isMinSignedValue())
940 return PoisonValue::get(CI1->getType()); // MIN_INT % -1 -> poison
941 return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
942 case Instruction::And:
943 return ConstantInt::get(CI1->getContext(), C1V & C2V);
944 case Instruction::Or:
945 return ConstantInt::get(CI1->getContext(), C1V | C2V);
946 case Instruction::Xor:
947 return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
948 case Instruction::Shl:
949 if (C2V.ult(C1V.getBitWidth()))
950 return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
951 return PoisonValue::get(C1->getType()); // too big shift is poison
952 case Instruction::LShr:
953 if (C2V.ult(C1V.getBitWidth()))
954 return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
955 return PoisonValue::get(C1->getType()); // too big shift is poison
956 case Instruction::AShr:
957 if (C2V.ult(C1V.getBitWidth()))
958 return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
959 return PoisonValue::get(C1->getType()); // too big shift is poison
960 }
961 }
962
963 switch (Opcode) {
964 case Instruction::SDiv:
965 case Instruction::UDiv:
966 case Instruction::URem:
967 case Instruction::SRem:
968 case Instruction::LShr:
969 case Instruction::AShr:
970 case Instruction::Shl:
971 if (CI1->isZero()) return C1;
972 break;
973 default:
974 break;
975 }
976 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
977 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
978 const APFloat &C1V = CFP1->getValueAPF();
979 const APFloat &C2V = CFP2->getValueAPF();
980 APFloat C3V = C1V; // copy for modification
981 switch (Opcode) {
982 default:
983 break;
984 case Instruction::FAdd:
985 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
986 return ConstantFP::get(C1->getContext(), C3V);
987 case Instruction::FSub:
988 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
989 return ConstantFP::get(C1->getContext(), C3V);
990 case Instruction::FMul:
991 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
992 return ConstantFP::get(C1->getContext(), C3V);
993 case Instruction::FDiv:
994 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
995 return ConstantFP::get(C1->getContext(), C3V);
996 case Instruction::FRem:
997 (void)C3V.mod(C2V);
998 return ConstantFP::get(C1->getContext(), C3V);
999 }
1000 }
1001 } else if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
1002 // Fast path for splatted constants.
1003 if (Constant *C2Splat = C2->getSplatValue()) {
1004 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
1005 return PoisonValue::get(VTy);
1006 if (Constant *C1Splat = C1->getSplatValue()) {
1007 Constant *Res =
1009 ? ConstantExpr::get(Opcode, C1Splat, C2Splat)
1010 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat);
1011 if (!Res)
1012 return nullptr;
1013 return ConstantVector::getSplat(VTy->getElementCount(), Res);
1014 }
1015 }
1016
1017 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
1018 // Fold each element and create a vector constant from those constants.
1020 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
1021 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
1022 Constant *ExtractIdx = ConstantInt::get(Ty, i);
1023 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
1024 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
1025
1026 // If any element of a divisor vector is zero, the whole op is poison.
1027 if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue())
1028 return PoisonValue::get(VTy);
1029
1033 if (!Res)
1034 return nullptr;
1035 Result.push_back(Res);
1036 }
1037
1038 return ConstantVector::get(Result);
1039 }
1040 }
1041
1042 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1043 // There are many possible foldings we could do here. We should probably
1044 // at least fold add of a pointer with an integer into the appropriate
1045 // getelementptr. This will improve alias analysis a bit.
1046
1047 // Given ((a + b) + c), if (b + c) folds to something interesting, return
1048 // (a + (b + c)).
1049 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
1050 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
1051 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
1052 return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
1053 }
1054 } else if (isa<ConstantExpr>(C2)) {
1055 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1056 // other way if possible.
1058 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
1059 }
1060
1061 // i1 can be simplified in many cases.
1062 if (C1->getType()->isIntegerTy(1)) {
1063 switch (Opcode) {
1064 case Instruction::Add:
1065 case Instruction::Sub:
1066 return ConstantExpr::getXor(C1, C2);
1067 case Instruction::Shl:
1068 case Instruction::LShr:
1069 case Instruction::AShr:
1070 // We can assume that C2 == 0. If it were one the result would be
1071 // undefined because the shift value is as large as the bitwidth.
1072 return C1;
1073 case Instruction::SDiv:
1074 case Instruction::UDiv:
1075 // We can assume that C2 == 1. If it were zero the result would be
1076 // undefined through division by zero.
1077 return C1;
1078 case Instruction::URem:
1079 case Instruction::SRem:
1080 // We can assume that C2 == 1. If it were zero the result would be
1081 // undefined through division by zero.
1082 return ConstantInt::getFalse(C1->getContext());
1083 default:
1084 break;
1085 }
1086 }
1087
1088 // We don't know how to fold this.
1089 return nullptr;
1090}
1091
1093 const GlobalValue *GV2) {
1094 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
1095 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
1096 return true;
1097 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1098 Type *Ty = GVar->getValueType();
1099 // A global with opaque type might end up being zero sized.
1100 if (!Ty->isSized())
1101 return true;
1102 // A global with an empty type might lie at the address of any other
1103 // global.
1104 if (Ty->isEmptyTy())
1105 return true;
1106 }
1107 return false;
1108 };
1109 // Don't try to decide equality of aliases.
1110 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1111 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1112 return ICmpInst::ICMP_NE;
1113 return ICmpInst::BAD_ICMP_PREDICATE;
1114}
1115
1116/// This function determines if there is anything we can decide about the two
1117/// constants provided. This doesn't need to handle simple things like integer
1118/// comparisons, but should instead handle ConstantExprs and GlobalValues.
1119/// If we can determine that the two constants have a particular relation to
1120/// each other, we should return the corresponding ICmp predicate, otherwise
1121/// return ICmpInst::BAD_ICMP_PREDICATE.
1123 assert(V1->getType() == V2->getType() &&
1124 "Cannot compare different types of values!");
1125 if (V1 == V2) return ICmpInst::ICMP_EQ;
1126
1127 // The following folds only apply to pointers.
1128 if (!V1->getType()->isPointerTy())
1129 return ICmpInst::BAD_ICMP_PREDICATE;
1130
1131 // To simplify this code we canonicalize the relation so that the first
1132 // operand is always the most "complex" of the two. We consider simple
1133 // constants (like ConstantPointerNull) to be the simplest, followed by
1134 // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
1135 auto GetComplexity = [](Constant *V) {
1136 if (isa<ConstantExpr>(V))
1137 return 3;
1138 if (isa<GlobalValue>(V))
1139 return 2;
1140 if (isa<BlockAddress>(V))
1141 return 1;
1142 return 0;
1143 };
1144 if (GetComplexity(V1) < GetComplexity(V2)) {
1145 ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
1146 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1147 return ICmpInst::getSwappedPredicate(SwappedRelation);
1148 return ICmpInst::BAD_ICMP_PREDICATE;
1149 }
1150
1151 if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1152 // Now we know that the RHS is a BlockAddress or simple constant.
1153 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1154 // Block address in another function can't equal this one, but block
1155 // addresses in the current function might be the same if blocks are
1156 // empty.
1157 if (BA2->getFunction() != BA->getFunction())
1158 return ICmpInst::ICMP_NE;
1159 } else if (isa<ConstantPointerNull>(V2)) {
1160 return ICmpInst::ICMP_NE;
1161 }
1162 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1163 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1164 // constant.
1165 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1166 return areGlobalsPotentiallyEqual(GV, GV2);
1167 } else if (isa<BlockAddress>(V2)) {
1168 return ICmpInst::ICMP_NE; // Globals never equal labels.
1169 } else if (isa<ConstantPointerNull>(V2)) {
1170 // GlobalVals can never be null unless they have external weak linkage.
1171 // We don't try to evaluate aliases here.
1172 // NOTE: We should not be doing this constant folding if null pointer
1173 // is considered valid for the function. But currently there is no way to
1174 // query it from the Constant type.
1175 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1176 !NullPointerIsDefined(nullptr /* F */,
1177 GV->getType()->getAddressSpace()))
1178 return ICmpInst::ICMP_UGT;
1179 }
1180 } else {
1181 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1182 // constantexpr, a global, block address, or a simple constant.
1183 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1184 Constant *CE1Op0 = CE1->getOperand(0);
1185
1186 switch (CE1->getOpcode()) {
1187 case Instruction::GetElementPtr: {
1188 GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1189 // Ok, since this is a getelementptr, we know that the constant has a
1190 // pointer type. Check the various cases.
1191 if (isa<ConstantPointerNull>(V2)) {
1192 // If we are comparing a GEP to a null pointer, check to see if the base
1193 // of the GEP equals the null pointer.
1194 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1195 // If its not weak linkage, the GVal must have a non-zero address
1196 // so the result is greater-than
1197 if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
1198 return ICmpInst::ICMP_UGT;
1199 }
1200 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1201 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1202 if (GV != GV2) {
1203 if (CE1GEP->hasAllZeroIndices())
1204 return areGlobalsPotentiallyEqual(GV, GV2);
1205 return ICmpInst::BAD_ICMP_PREDICATE;
1206 }
1207 }
1208 } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1209 // By far the most common case to handle is when the base pointers are
1210 // obviously to the same global.
1211 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1212 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1213 // Don't know relative ordering, but check for inequality.
1214 if (CE1Op0 != CE2Op0) {
1215 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1216 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
1217 cast<GlobalValue>(CE2Op0));
1218 return ICmpInst::BAD_ICMP_PREDICATE;
1219 }
1220 }
1221 }
1222 break;
1223 }
1224 default:
1225 break;
1226 }
1227 }
1228
1229 return ICmpInst::BAD_ICMP_PREDICATE;
1230}
1231
1233 Constant *C1, Constant *C2) {
1234 Type *ResultTy;
1235 if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1236 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1237 VT->getElementCount());
1238 else
1239 ResultTy = Type::getInt1Ty(C1->getContext());
1240
1241 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1242 if (Predicate == FCmpInst::FCMP_FALSE)
1243 return Constant::getNullValue(ResultTy);
1244
1245 if (Predicate == FCmpInst::FCMP_TRUE)
1246 return Constant::getAllOnesValue(ResultTy);
1247
1248 // Handle some degenerate cases first
1249 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1250 return PoisonValue::get(ResultTy);
1251
1252 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1253 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1254 // For EQ and NE, we can always pick a value for the undef to make the
1255 // predicate pass or fail, so we can return undef.
1256 // Also, if both operands are undef, we can return undef for int comparison.
1257 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1258 return UndefValue::get(ResultTy);
1259
1260 // Otherwise, for integer compare, pick the same value as the non-undef
1261 // operand, and fold it to true or false.
1262 if (isIntegerPredicate)
1263 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1264
1265 // Choosing NaN for the undef will always make unordered comparison succeed
1266 // and ordered comparison fails.
1267 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1268 }
1269
1270 if (C2->isNullValue()) {
1271 // The caller is expected to commute the operands if the constant expression
1272 // is C2.
1273 // C1 >= 0 --> true
1274 if (Predicate == ICmpInst::ICMP_UGE)
1275 return Constant::getAllOnesValue(ResultTy);
1276 // C1 < 0 --> false
1277 if (Predicate == ICmpInst::ICMP_ULT)
1278 return Constant::getNullValue(ResultTy);
1279 }
1280
1281 // If the comparison is a comparison between two i1's, simplify it.
1282 if (C1->getType()->isIntegerTy(1)) {
1283 switch (Predicate) {
1284 case ICmpInst::ICMP_EQ:
1285 if (isa<ConstantInt>(C2))
1288 case ICmpInst::ICMP_NE:
1289 return ConstantExpr::getXor(C1, C2);
1290 default:
1291 break;
1292 }
1293 }
1294
1295 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1296 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1297 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1298 return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate));
1299 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1300 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1301 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1302 return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
1303 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
1304
1305 // Fast path for splatted constants.
1306 if (Constant *C1Splat = C1->getSplatValue())
1307 if (Constant *C2Splat = C2->getSplatValue())
1309 C1VTy->getElementCount(),
1310 ConstantExpr::getCompare(Predicate, C1Splat, C2Splat));
1311
1312 // Do not iterate on scalable vector. The number of elements is unknown at
1313 // compile-time.
1314 if (isa<ScalableVectorType>(C1VTy))
1315 return nullptr;
1316
1317 // If we can constant fold the comparison of each element, constant fold
1318 // the whole vector comparison.
1320 Type *Ty = IntegerType::get(C1->getContext(), 32);
1321 // Compare the elements, producing an i1 result or constant expr.
1322 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1323 I != E; ++I) {
1324 Constant *C1E =
1326 Constant *C2E =
1328
1329 ResElts.push_back(ConstantExpr::getCompare(Predicate, C1E, C2E));
1330 }
1331
1332 return ConstantVector::get(ResElts);
1333 }
1334
1335 if (C1->getType()->isFPOrFPVectorTy()) {
1336 if (C1 == C2) {
1337 // We know that C1 == C2 || isUnordered(C1, C2).
1338 if (Predicate == FCmpInst::FCMP_ONE)
1339 return ConstantInt::getFalse(ResultTy);
1340 else if (Predicate == FCmpInst::FCMP_UEQ)
1341 return ConstantInt::getTrue(ResultTy);
1342 }
1343 } else {
1344 // Evaluate the relation between the two constants, per the predicate.
1345 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1346 switch (evaluateICmpRelation(C1, C2)) {
1347 default: llvm_unreachable("Unknown relational!");
1348 case ICmpInst::BAD_ICMP_PREDICATE:
1349 break; // Couldn't determine anything about these constants.
1350 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1351 // If we know the constants are equal, we can decide the result of this
1352 // computation precisely.
1353 Result = ICmpInst::isTrueWhenEqual(Predicate);
1354 break;
1355 case ICmpInst::ICMP_ULT:
1356 switch (Predicate) {
1357 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
1358 Result = 1; break;
1359 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
1360 Result = 0; break;
1361 default:
1362 break;
1363 }
1364 break;
1365 case ICmpInst::ICMP_SLT:
1366 switch (Predicate) {
1367 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
1368 Result = 1; break;
1369 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
1370 Result = 0; break;
1371 default:
1372 break;
1373 }
1374 break;
1375 case ICmpInst::ICMP_UGT:
1376 switch (Predicate) {
1377 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
1378 Result = 1; break;
1379 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
1380 Result = 0; break;
1381 default:
1382 break;
1383 }
1384 break;
1385 case ICmpInst::ICMP_SGT:
1386 switch (Predicate) {
1387 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
1388 Result = 1; break;
1389 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
1390 Result = 0; break;
1391 default:
1392 break;
1393 }
1394 break;
1395 case ICmpInst::ICMP_ULE:
1396 if (Predicate == ICmpInst::ICMP_UGT)
1397 Result = 0;
1398 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1399 Result = 1;
1400 break;
1401 case ICmpInst::ICMP_SLE:
1402 if (Predicate == ICmpInst::ICMP_SGT)
1403 Result = 0;
1404 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1405 Result = 1;
1406 break;
1407 case ICmpInst::ICMP_UGE:
1408 if (Predicate == ICmpInst::ICMP_ULT)
1409 Result = 0;
1410 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1411 Result = 1;
1412 break;
1413 case ICmpInst::ICMP_SGE:
1414 if (Predicate == ICmpInst::ICMP_SLT)
1415 Result = 0;
1416 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1417 Result = 1;
1418 break;
1419 case ICmpInst::ICMP_NE:
1420 if (Predicate == ICmpInst::ICMP_EQ)
1421 Result = 0;
1422 if (Predicate == ICmpInst::ICMP_NE)
1423 Result = 1;
1424 break;
1425 }
1426
1427 // If we evaluated the result, return it now.
1428 if (Result != -1)
1429 return ConstantInt::get(ResultTy, Result);
1430
1431 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1432 (C1->isNullValue() && !C2->isNullValue())) {
1433 // If C2 is a constant expr and C1 isn't, flip them around and fold the
1434 // other way if possible.
1435 // Also, if C1 is null and C2 isn't, flip them around.
1436 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1437 return ConstantExpr::getICmp(Predicate, C2, C1);
1438 }
1439 }
1440 return nullptr;
1441}
1442
1443/// Test whether the given sequence of *normalized* indices is "inbounds".
1444template<typename IndexTy>
1446 // No indices means nothing that could be out of bounds.
1447 if (Idxs.empty()) return true;
1448
1449 // If the first index is zero, it's in bounds.
1450 if (cast<Constant>(Idxs[0])->isNullValue()) return true;
1451
1452 // If the first index is one and all the rest are zero, it's in bounds,
1453 // by the one-past-the-end rule.
1454 if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
1455 if (!CI->isOne())
1456 return false;
1457 } else {
1458 auto *CV = cast<ConstantDataVector>(Idxs[0]);
1459 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
1460 if (!CI || !CI->isOne())
1461 return false;
1462 }
1463
1464 for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
1465 if (!cast<Constant>(Idxs[i])->isNullValue())
1466 return false;
1467 return true;
1468}
1469
1470/// Test whether a given ConstantInt is in-range for a SequentialType.
1471static bool isIndexInRangeOfArrayType(uint64_t NumElements,
1472 const ConstantInt *CI) {
1473 // We cannot bounds check the index if it doesn't fit in an int64_t.
1474 if (CI->getValue().getSignificantBits() > 64)
1475 return false;
1476
1477 // A negative index or an index past the end of our sequential type is
1478 // considered out-of-range.
1479 int64_t IndexVal = CI->getSExtValue();
1480 if (IndexVal < 0 || (IndexVal != 0 && (uint64_t)IndexVal >= NumElements))
1481 return false;
1482
1483 // Otherwise, it is in-range.
1484 return true;
1485}
1486
1487// Combine Indices - If the source pointer to this getelementptr instruction
1488// is a getelementptr instruction, combine the indices of the two
1489// getelementptr instructions into a single instruction.
1490static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
1491 ArrayRef<Value *> Idxs) {
1492 if (PointeeTy != GEP->getResultElementType())
1493 return nullptr;
1494
1495 Constant *Idx0 = cast<Constant>(Idxs[0]);
1496 if (Idx0->isNullValue()) {
1497 // Handle the simple case of a zero index.
1498 SmallVector<Value*, 16> NewIndices;
1499 NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1500 NewIndices.append(GEP->idx_begin(), GEP->idx_end());
1501 NewIndices.append(Idxs.begin() + 1, Idxs.end());
1503 GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1504 NewIndices, InBounds && GEP->isInBounds(), GEP->getInRangeIndex());
1505 }
1506
1509 I != E; ++I)
1510 LastI = I;
1511
1512 // We can't combine GEPs if the last index is a struct type.
1513 if (!LastI.isSequential())
1514 return nullptr;
1515 // We could perform the transform with non-constant index, but prefer leaving
1516 // it as GEP of GEP rather than GEP of add for now.
1517 ConstantInt *CI = dyn_cast<ConstantInt>(Idx0);
1518 if (!CI)
1519 return nullptr;
1520
1521 // TODO: This code may be extended to handle vectors as well.
1522 auto *LastIdx = cast<Constant>(GEP->getOperand(GEP->getNumOperands()-1));
1523 Type *LastIdxTy = LastIdx->getType();
1524 if (LastIdxTy->isVectorTy())
1525 return nullptr;
1526
1527 SmallVector<Value*, 16> NewIndices;
1528 NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1529 NewIndices.append(GEP->idx_begin(), GEP->idx_end() - 1);
1530
1531 // Add the last index of the source with the first index of the new GEP.
1532 // Make sure to handle the case when they are actually different types.
1533 if (LastIdxTy != Idx0->getType()) {
1534 unsigned CommonExtendedWidth =
1535 std::max(LastIdxTy->getIntegerBitWidth(),
1536 Idx0->getType()->getIntegerBitWidth());
1537 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1538
1539 Type *CommonTy =
1540 Type::getIntNTy(LastIdxTy->getContext(), CommonExtendedWidth);
1541 if (Idx0->getType() != CommonTy)
1542 Idx0 = ConstantFoldCastInstruction(Instruction::SExt, Idx0, CommonTy);
1543 if (LastIdx->getType() != CommonTy)
1544 LastIdx =
1545 ConstantFoldCastInstruction(Instruction::SExt, LastIdx, CommonTy);
1546 if (!Idx0 || !LastIdx)
1547 return nullptr;
1548 }
1549
1550 NewIndices.push_back(ConstantExpr::get(Instruction::Add, Idx0, LastIdx));
1551 NewIndices.append(Idxs.begin() + 1, Idxs.end());
1552
1553 // The combined GEP normally inherits its index inrange attribute from
1554 // the inner GEP, but if the inner GEP's last index was adjusted by the
1555 // outer GEP, any inbounds attribute on that index is invalidated.
1556 std::optional<unsigned> IRIndex = GEP->getInRangeIndex();
1557 if (IRIndex && *IRIndex == GEP->getNumIndices() - 1)
1558 IRIndex = std::nullopt;
1559
1561 GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1562 NewIndices, InBounds && GEP->isInBounds(), IRIndex);
1563}
1564
1566 bool InBounds,
1567 std::optional<unsigned> InRangeIndex,
1568 ArrayRef<Value *> Idxs) {
1569 if (Idxs.empty()) return C;
1570
1572 C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
1573
1574 if (isa<PoisonValue>(C))
1575 return PoisonValue::get(GEPTy);
1576
1577 if (isa<UndefValue>(C))
1578 // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
1579 return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
1580
1581 auto IsNoOp = [&]() {
1582 // Avoid losing inrange information.
1583 if (InRangeIndex)
1584 return false;
1585
1586 return all_of(Idxs, [](Value *Idx) {
1587 Constant *IdxC = cast<Constant>(Idx);
1588 return IdxC->isNullValue() || isa<UndefValue>(IdxC);
1589 });
1590 };
1591 if (IsNoOp())
1592 return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
1594 cast<VectorType>(GEPTy)->getElementCount(), C)
1595 : C;
1596
1597 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
1598 if (auto *GEP = dyn_cast<GEPOperator>(CE))
1599 if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
1600 return C;
1601
1602 // Check to see if any array indices are not within the corresponding
1603 // notional array or vector bounds. If so, try to determine if they can be
1604 // factored out into preceding dimensions.
1606 Type *Ty = PointeeTy;
1607 Type *Prev = C->getType();
1608 auto GEPIter = gep_type_begin(PointeeTy, Idxs);
1609 bool Unknown =
1610 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
1611 for (unsigned i = 1, e = Idxs.size(); i != e;
1612 Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
1613 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
1614 // We don't know if it's in range or not.
1615 Unknown = true;
1616 continue;
1617 }
1618 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
1619 // Skip if the type of the previous index is not supported.
1620 continue;
1621 if (InRangeIndex && i == *InRangeIndex + 1) {
1622 // If an index is marked inrange, we cannot apply this canonicalization to
1623 // the following index, as that will cause the inrange index to point to
1624 // the wrong element.
1625 continue;
1626 }
1627 if (isa<StructType>(Ty)) {
1628 // The verify makes sure that GEPs into a struct are in range.
1629 continue;
1630 }
1631 if (isa<VectorType>(Ty)) {
1632 // There can be awkward padding in after a non-power of two vector.
1633 Unknown = true;
1634 continue;
1635 }
1636 auto *STy = cast<ArrayType>(Ty);
1637 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1638 if (isIndexInRangeOfArrayType(STy->getNumElements(), CI))
1639 // It's in range, skip to the next index.
1640 continue;
1641 if (CI->isNegative()) {
1642 // It's out of range and negative, don't try to factor it.
1643 Unknown = true;
1644 continue;
1645 }
1646 } else {
1647 auto *CV = cast<ConstantDataVector>(Idxs[i]);
1648 bool InRange = true;
1649 for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
1650 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(I));
1651 InRange &= isIndexInRangeOfArrayType(STy->getNumElements(), CI);
1652 if (CI->isNegative()) {
1653 Unknown = true;
1654 break;
1655 }
1656 }
1657 if (InRange || Unknown)
1658 // It's in range, skip to the next index.
1659 // It's out of range and negative, don't try to factor it.
1660 continue;
1661 }
1662 if (isa<StructType>(Prev)) {
1663 // It's out of range, but the prior dimension is a struct
1664 // so we can't do anything about it.
1665 Unknown = true;
1666 continue;
1667 }
1668
1669 // Determine the number of elements in our sequential type.
1670 uint64_t NumElements = STy->getArrayNumElements();
1671 if (!NumElements) {
1672 Unknown = true;
1673 continue;
1674 }
1675
1676 // It's out of range, but we can factor it into the prior
1677 // dimension.
1678 NewIdxs.resize(Idxs.size());
1679
1680 // Expand the current index or the previous index to a vector from a scalar
1681 // if necessary.
1682 Constant *CurrIdx = cast<Constant>(Idxs[i]);
1683 auto *PrevIdx =
1684 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
1685 bool IsCurrIdxVector = CurrIdx->getType()->isVectorTy();
1686 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
1687 bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
1688
1689 if (!IsCurrIdxVector && IsPrevIdxVector)
1691 cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx);
1692
1693 if (!IsPrevIdxVector && IsCurrIdxVector)
1695 cast<FixedVectorType>(CurrIdx->getType())->getNumElements(), PrevIdx);
1696
1697 Constant *Factor =
1698 ConstantInt::get(CurrIdx->getType()->getScalarType(), NumElements);
1699 if (UseVector)
1701 IsPrevIdxVector
1702 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1703 : cast<FixedVectorType>(CurrIdx->getType())->getNumElements(),
1704 Factor);
1705
1706 NewIdxs[i] =
1707 ConstantFoldBinaryInstruction(Instruction::SRem, CurrIdx, Factor);
1708
1709 Constant *Div =
1710 ConstantFoldBinaryInstruction(Instruction::SDiv, CurrIdx, Factor);
1711
1712 // We're working on either ConstantInt or vectors of ConstantInt,
1713 // so these should always fold.
1714 assert(NewIdxs[i] != nullptr && Div != nullptr && "Should have folded");
1715
1716 unsigned CommonExtendedWidth =
1717 std::max(PrevIdx->getType()->getScalarSizeInBits(),
1718 Div->getType()->getScalarSizeInBits());
1719 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1720
1721 // Before adding, extend both operands to i64 to avoid
1722 // overflow trouble.
1723 Type *ExtendedTy = Type::getIntNTy(Div->getContext(), CommonExtendedWidth);
1724 if (UseVector)
1725 ExtendedTy = FixedVectorType::get(
1726 ExtendedTy,
1727 IsPrevIdxVector
1728 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1729 : cast<FixedVectorType>(CurrIdx->getType())->getNumElements());
1730
1731 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1732 PrevIdx =
1733 ConstantFoldCastInstruction(Instruction::SExt, PrevIdx, ExtendedTy);
1734
1735 if (!Div->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1736 Div = ConstantFoldCastInstruction(Instruction::SExt, Div, ExtendedTy);
1737
1738 assert(PrevIdx && Div && "Should have folded");
1739 NewIdxs[i - 1] = ConstantExpr::getAdd(PrevIdx, Div);
1740 }
1741
1742 // If we did any factoring, start over with the adjusted indices.
1743 if (!NewIdxs.empty()) {
1744 for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
1745 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
1746 return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, InBounds,
1747 InRangeIndex);
1748 }
1749
1750 // If all indices are known integers and normalized, we can do a simple
1751 // check for the "inbounds" property.
1752 if (!Unknown && !InBounds)
1753 if (auto *GV = dyn_cast<GlobalVariable>(C))
1754 if (!GV->hasExternalWeakLinkage() && GV->getValueType() == PointeeTy &&
1755 isInBoundsIndices(Idxs))
1756 return ConstantExpr::getGetElementPtr(PointeeTy, C, Idxs,
1757 /*InBounds=*/true, InRangeIndex);
1758
1759 return nullptr;
1760}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 bool isIndexInRangeOfArrayType(uint64_t NumElements, const ConstantInt *CI)
Test whether a given ConstantInt is in-range for a SequentialType.
static Constant * foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds, ArrayRef< Value * > Idxs)
static bool isInBoundsIndices(ArrayRef< IndexTy > Idxs)
Test whether the given sequence of normalized indices is "inbounds".
static Constant * ExtractConstantBytes(Constant *C, unsigned ByteStart, unsigned ByteSize)
V is an integer constant which only has a subset of its bytes used.
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
hexagon gen pred
#define I(x, y, z)
Definition: MD5.cpp:58
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
Module.h This file contains the declarations for the Module class.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:191
Value * RHS
Value * LHS
static constexpr uint32_t Opcode
Definition: aarch32.h:200
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1069
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1051
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1042
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1193
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1060
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1087
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1579
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1485
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1672
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1433
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1650
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
Definition: APInt.h:1476
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1742
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1122
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1193
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
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:319
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
const T * data() const
Definition: ArrayRef.h:162
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:195
The address of a basic block.
Definition: Constants.h:874
static 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:748
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1047
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:862
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1579
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1235
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:2844
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1002
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2370
static bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
Definition: Constants.cpp:2177
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1957
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2447
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
Definition: Constants.cpp:2320
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, std::optional< unsigned > InRangeIndex=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1181
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2474
static 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:2075
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2123
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1232
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2453
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2056
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2514
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2244
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:260
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:927
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
Definition: Constants.cpp:968
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:151
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:145
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:136
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:240
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1300
Constant Vector Declarations.
Definition: Constants.h:492
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1385
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1342
This is an important base class in LLVM.
Definition: Constant.h:41
Constant * getSplatValue(bool AllowUndefs=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1615
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:403
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:356
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:418
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:76
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:110
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition: TypeSize.h:304
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:699
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:387
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
Definition: Operator.h:446
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:652
static 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.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isBinaryOp() const
Definition: Instruction.h:244
bool isUnaryOp() const
Definition: Instruction.h:243
bool isIntDivRem() const
Definition: Instruction.h:245
Class to represent integer types.
Definition: DerivedTypes.h:40
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:285
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
bool empty() const
Definition: SmallVector.h:94
void reserve(size_type N)
Definition: SmallVector.h:667
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
void resize(size_type N)
Definition: SmallVector.h:642
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Class to represent struct types.
Definition: DerivedTypes.h:216
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
static IntegerType * getInt1Ty(LLVMContext &C)
bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
bool isX86_MMXTy() const
Return true if this is X86 MMX.
Definition: Type.h:201
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:166
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition: Type.h:281
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:185
bool isX86_AMXTy() const
Return true if this is X86 AMX.
Definition: Type.h:204
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:216
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1724
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:926
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
Base class of all SIMD vector types.
Definition: DerivedTypes.h:403
Type * getElementType() const
Definition: DerivedTypes.h:436
#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
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:525
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:690
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:278
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:136
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:545
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:218
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:1726
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, std::optional< unsigned > InRangeIndex, ArrayRef< Value * > Idxs)
Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
gep_type_iterator gep_type_end(const User *GEP)
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
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:2007
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
constexpr int PoisonMaskElem
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:191
gep_type_iterator gep_type_begin(const User *GEP)
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition: APFloat.h:1387
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
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
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
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