LLVM 20.0.0git
Instructions.cpp
Go to the documentation of this file.
1//===- Instructions.cpp - Implement the LLVM instructions -----------------===//
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 all of the non-inline methods for the LLVM instruction
10// classes.
11//
12//===----------------------------------------------------------------------===//
13
15#include "LLVMContextImpl.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/Constant.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/MDBuilder.h"
32#include "llvm/IR/Metadata.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/Value.h"
45#include "llvm/Support/ModRef.h"
47#include <algorithm>
48#include <cassert>
49#include <cstdint>
50#include <optional>
51#include <vector>
52
53using namespace llvm;
54
56 "disable-i2p-p2i-opt", cl::init(false),
57 cl::desc("Disables inttoptr/ptrtoint roundtrip optimization"));
58
59//===----------------------------------------------------------------------===//
60// AllocaInst Class
61//===----------------------------------------------------------------------===//
62
63std::optional<TypeSize>
65 TypeSize Size = DL.getTypeAllocSize(getAllocatedType());
66 if (isArrayAllocation()) {
67 auto *C = dyn_cast<ConstantInt>(getArraySize());
68 if (!C)
69 return std::nullopt;
70 assert(!Size.isScalable() && "Array elements cannot have a scalable size");
71 auto CheckedProd =
72 checkedMulUnsigned(Size.getKnownMinValue(), C->getZExtValue());
73 if (!CheckedProd)
74 return std::nullopt;
75 return TypeSize::getFixed(*CheckedProd);
76 }
77 return Size;
78}
79
80std::optional<TypeSize>
82 std::optional<TypeSize> Size = getAllocationSize(DL);
83 if (!Size)
84 return std::nullopt;
85 auto CheckedProd = checkedMulUnsigned(Size->getKnownMinValue(),
86 static_cast<TypeSize::ScalarTy>(8));
87 if (!CheckedProd)
88 return std::nullopt;
89 return TypeSize::get(*CheckedProd, Size->isScalable());
90}
91
92//===----------------------------------------------------------------------===//
93// SelectInst Class
94//===----------------------------------------------------------------------===//
95
96/// areInvalidOperands - Return a string if the specified operands are invalid
97/// for a select operation, otherwise return null.
98const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
99 if (Op1->getType() != Op2->getType())
100 return "both values to select must have same type";
101
102 if (Op1->getType()->isTokenTy())
103 return "select values cannot have token type";
104
105 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
106 // Vector select.
107 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
108 return "vector select condition element type must be i1";
109 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
110 if (!ET)
111 return "selected values for vector select must be vectors";
112 if (ET->getElementCount() != VT->getElementCount())
113 return "vector select requires selected vectors to have "
114 "the same vector length as select condition";
115 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
116 return "select condition must be i1 or <n x i1>";
117 }
118 return nullptr;
119}
120
121//===----------------------------------------------------------------------===//
122// PHINode Class
123//===----------------------------------------------------------------------===//
124
125PHINode::PHINode(const PHINode &PN)
126 : Instruction(PN.getType(), Instruction::PHI, AllocMarker),
127 ReservedSpace(PN.getNumOperands()) {
130 std::copy(PN.op_begin(), PN.op_end(), op_begin());
131 copyIncomingBlocks(make_range(PN.block_begin(), PN.block_end()));
133}
134
135// removeIncomingValue - Remove an incoming value. This is useful if a
136// predecessor basic block is deleted.
137Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
138 Value *Removed = getIncomingValue(Idx);
139
140 // Move everything after this operand down.
141 //
142 // FIXME: we could just swap with the end of the list, then erase. However,
143 // clients might not expect this to happen. The code as it is thrashes the
144 // use/def lists, which is kinda lame.
145 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
147
148 // Nuke the last value.
149 Op<-1>().set(nullptr);
151
152 // If the PHI node is dead, because it has zero entries, nuke it now.
153 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
154 // If anyone is using this PHI, make them use a dummy value instead...
157 }
158 return Removed;
159}
160
161void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate,
162 bool DeletePHIIfEmpty) {
163 SmallDenseSet<unsigned> RemoveIndices;
164 for (unsigned Idx = 0; Idx < getNumIncomingValues(); ++Idx)
165 if (Predicate(Idx))
166 RemoveIndices.insert(Idx);
167
168 if (RemoveIndices.empty())
169 return;
170
171 // Remove operands.
172 auto NewOpEnd = remove_if(operands(), [&](Use &U) {
173 return RemoveIndices.contains(U.getOperandNo());
174 });
175 for (Use &U : make_range(NewOpEnd, op_end()))
176 U.set(nullptr);
177
178 // Remove incoming blocks.
179 (void)std::remove_if(const_cast<block_iterator>(block_begin()),
180 const_cast<block_iterator>(block_end()), [&](BasicBlock *&BB) {
181 return RemoveIndices.contains(&BB - block_begin());
182 });
183
184 setNumHungOffUseOperands(getNumOperands() - RemoveIndices.size());
185
186 // If the PHI node is dead, because it has zero entries, nuke it now.
187 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
188 // If anyone is using this PHI, make them use a dummy value instead...
191 }
192}
193
194/// growOperands - grow operands - This grows the operand list in response
195/// to a push_back style of operation. This grows the number of ops by 1.5
196/// times.
197///
198void PHINode::growOperands() {
199 unsigned e = getNumOperands();
200 unsigned NumOps = e + e / 2;
201 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
202
203 ReservedSpace = NumOps;
204 growHungoffUses(ReservedSpace, /* IsPhi */ true);
205}
206
207/// hasConstantValue - If the specified PHI node always merges together the same
208/// value, return the value, otherwise return null.
210 // Exploit the fact that phi nodes always have at least one entry.
211 Value *ConstantValue = getIncomingValue(0);
212 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
213 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
214 if (ConstantValue != this)
215 return nullptr; // Incoming values not all the same.
216 // The case where the first value is this PHI.
217 ConstantValue = getIncomingValue(i);
218 }
219 if (ConstantValue == this)
220 return PoisonValue::get(getType());
221 return ConstantValue;
222}
223
224/// hasConstantOrUndefValue - Whether the specified PHI node always merges
225/// together the same value, assuming that undefs result in the same value as
226/// non-undefs.
227/// Unlike \ref hasConstantValue, this does not return a value because the
228/// unique non-undef incoming value need not dominate the PHI node.
230 Value *ConstantValue = nullptr;
231 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
233 if (Incoming != this && !isa<UndefValue>(Incoming)) {
234 if (ConstantValue && ConstantValue != Incoming)
235 return false;
236 ConstantValue = Incoming;
237 }
238 }
239 return true;
240}
241
242//===----------------------------------------------------------------------===//
243// LandingPadInst Implementation
244//===----------------------------------------------------------------------===//
245
246LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
247 const Twine &NameStr,
248 InsertPosition InsertBefore)
249 : Instruction(RetTy, Instruction::LandingPad, AllocMarker, InsertBefore) {
250 init(NumReservedValues, NameStr);
251}
252
253LandingPadInst::LandingPadInst(const LandingPadInst &LP)
254 : Instruction(LP.getType(), Instruction::LandingPad, AllocMarker),
255 ReservedSpace(LP.getNumOperands()) {
258 Use *OL = getOperandList();
259 const Use *InOL = LP.getOperandList();
260 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
261 OL[I] = InOL[I];
262
263 setCleanup(LP.isCleanup());
264}
265
266LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
267 const Twine &NameStr,
268 InsertPosition InsertBefore) {
269 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
270}
271
272void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
273 ReservedSpace = NumReservedValues;
275 allocHungoffUses(ReservedSpace);
276 setName(NameStr);
277 setCleanup(false);
278}
279
280/// growOperands - grow operands - This grows the operand list in response to a
281/// push_back style of operation. This grows the number of ops by 2 times.
282void LandingPadInst::growOperands(unsigned Size) {
283 unsigned e = getNumOperands();
284 if (ReservedSpace >= e + Size) return;
285 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
286 growHungoffUses(ReservedSpace);
287}
288
290 unsigned OpNo = getNumOperands();
291 growOperands(1);
292 assert(OpNo < ReservedSpace && "Growing didn't work!");
294 getOperandList()[OpNo] = Val;
295}
296
297//===----------------------------------------------------------------------===//
298// CallBase Implementation
299//===----------------------------------------------------------------------===//
300
302 InsertPosition InsertPt) {
303 switch (CB->getOpcode()) {
304 case Instruction::Call:
305 return CallInst::Create(cast<CallInst>(CB), Bundles, InsertPt);
306 case Instruction::Invoke:
307 return InvokeInst::Create(cast<InvokeInst>(CB), Bundles, InsertPt);
308 case Instruction::CallBr:
309 return CallBrInst::Create(cast<CallBrInst>(CB), Bundles, InsertPt);
310 default:
311 llvm_unreachable("Unknown CallBase sub-class!");
312 }
313}
314
316 InsertPosition InsertPt) {
318 for (unsigned i = 0, e = CI->getNumOperandBundles(); i < e; ++i) {
319 auto ChildOB = CI->getOperandBundleAt(i);
320 if (ChildOB.getTagName() != OpB.getTag())
321 OpDefs.emplace_back(ChildOB);
322 }
323 OpDefs.emplace_back(OpB);
324 return CallBase::Create(CI, OpDefs, InsertPt);
325}
326
327Function *CallBase::getCaller() { return getParent()->getParent(); }
328
330 assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!");
331 return cast<CallBrInst>(this)->getNumIndirectDests() + 1;
332}
333
335 const Value *V = getCalledOperand();
336 if (isa<Function>(V) || isa<Constant>(V))
337 return false;
338 return !isInlineAsm();
339}
340
341/// Tests if this call site must be tail call optimized. Only a CallInst can
342/// be tail call optimized.
344 if (auto *CI = dyn_cast<CallInst>(this))
345 return CI->isMustTailCall();
346 return false;
347}
348
349/// Tests if this call site is marked as a tail call.
351 if (auto *CI = dyn_cast<CallInst>(this))
352 return CI->isTailCall();
353 return false;
354}
355
357 if (auto *F = getCalledFunction())
358 return F->getIntrinsicID();
360}
361
364
365 if (const Function *F = getCalledFunction())
366 Mask |= F->getAttributes().getRetNoFPClass();
367 return Mask;
368}
369
372
373 if (const Function *F = getCalledFunction())
374 Mask |= F->getAttributes().getParamNoFPClass(i);
375 return Mask;
376}
377
378std::optional<ConstantRange> CallBase::getRange() const {
379 const Attribute RangeAttr = getRetAttr(llvm::Attribute::Range);
380 if (RangeAttr.isValid())
381 return RangeAttr.getRange();
382 return std::nullopt;
383}
384
386 if (hasRetAttr(Attribute::NonNull))
387 return true;
388
389 if (getRetDereferenceableBytes() > 0 &&
391 return true;
392
393 return false;
394}
395
397 unsigned Index;
398
399 if (Attrs.hasAttrSomewhere(Kind, &Index))
401 if (const Function *F = getCalledFunction())
402 if (F->getAttributes().hasAttrSomewhere(Kind, &Index))
404
405 return nullptr;
406}
407
408/// Determine whether the argument or parameter has the given attribute.
409bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
410 assert(ArgNo < arg_size() && "Param index out of bounds!");
411
412 if (Attrs.hasParamAttr(ArgNo, Kind))
413 return true;
414
415 const Function *F = getCalledFunction();
416 if (!F)
417 return false;
418
419 if (!F->getAttributes().hasParamAttr(ArgNo, Kind))
420 return false;
421
422 // Take into account mod/ref by operand bundles.
423 switch (Kind) {
424 case Attribute::ReadNone:
426 case Attribute::ReadOnly:
428 case Attribute::WriteOnly:
429 return !hasReadingOperandBundles();
430 default:
431 return true;
432 }
433}
434
435bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
436 if (auto *F = dyn_cast<Function>(getCalledOperand()))
437 return F->getAttributes().hasFnAttr(Kind);
438
439 return false;
440}
441
442bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
443 if (auto *F = dyn_cast<Function>(getCalledOperand()))
444 return F->getAttributes().hasFnAttr(Kind);
445
446 return false;
447}
448
449template <typename AK>
450Attribute CallBase::getFnAttrOnCalledFunction(AK Kind) const {
451 if constexpr (std::is_same_v<AK, Attribute::AttrKind>) {
452 // getMemoryEffects() correctly combines memory effects from the call-site,
453 // operand bundles and function.
454 assert(Kind != Attribute::Memory && "Use getMemoryEffects() instead");
455 }
456
457 if (auto *F = dyn_cast<Function>(getCalledOperand()))
458 return F->getAttributes().getFnAttr(Kind);
459
460 return Attribute();
461}
462
463template Attribute
464CallBase::getFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
465template Attribute CallBase::getFnAttrOnCalledFunction(StringRef Kind) const;
466
467template <typename AK>
468Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
469 AK Kind) const {
471
472 if (auto *F = dyn_cast<Function>(V))
473 return F->getAttributes().getParamAttr(ArgNo, Kind);
474
475 return Attribute();
476}
477template Attribute
478CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
479 Attribute::AttrKind Kind) const;
480template Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
481 StringRef Kind) const;
482
485 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
487}
488
491 const unsigned BeginIndex) {
492 auto It = op_begin() + BeginIndex;
493 for (auto &B : Bundles)
494 It = std::copy(B.input_begin(), B.input_end(), It);
495
496 auto *ContextImpl = getContext().pImpl;
497 auto BI = Bundles.begin();
498 unsigned CurrentIndex = BeginIndex;
499
500 for (auto &BOI : bundle_op_infos()) {
501 assert(BI != Bundles.end() && "Incorrect allocation?");
502
503 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
504 BOI.Begin = CurrentIndex;
505 BOI.End = CurrentIndex + BI->input_size();
506 CurrentIndex = BOI.End;
507 BI++;
508 }
509
510 assert(BI == Bundles.end() && "Incorrect allocation?");
511
512 return It;
513}
514
516 /// When there isn't many bundles, we do a simple linear search.
517 /// Else fallback to a binary-search that use the fact that bundles usually
518 /// have similar number of argument to get faster convergence.
520 for (auto &BOI : bundle_op_infos())
521 if (BOI.Begin <= OpIdx && OpIdx < BOI.End)
522 return BOI;
523
524 llvm_unreachable("Did not find operand bundle for operand!");
525 }
526
527 assert(OpIdx >= arg_size() && "the Idx is not in the operand bundles");
529 OpIdx < std::prev(bundle_op_info_end())->End &&
530 "The Idx isn't in the operand bundle");
531
532 /// We need a decimal number below and to prevent using floating point numbers
533 /// we use an intergal value multiplied by this constant.
534 constexpr unsigned NumberScaling = 1024;
535
538 bundle_op_iterator Current = Begin;
539
540 while (Begin != End) {
541 unsigned ScaledOperandPerBundle =
542 NumberScaling * (std::prev(End)->End - Begin->Begin) / (End - Begin);
543 Current = Begin + (((OpIdx - Begin->Begin) * NumberScaling) /
544 ScaledOperandPerBundle);
545 if (Current >= End)
546 Current = std::prev(End);
547 assert(Current < End && Current >= Begin &&
548 "the operand bundle doesn't cover every value in the range");
549 if (OpIdx >= Current->Begin && OpIdx < Current->End)
550 break;
551 if (OpIdx >= Current->End)
552 Begin = Current + 1;
553 else
554 End = Current;
555 }
556
557 assert(OpIdx >= Current->Begin && OpIdx < Current->End &&
558 "the operand bundle doesn't cover every value in the range");
559 return *Current;
560}
561
564 InsertPosition InsertPt) {
565 if (CB->getOperandBundle(ID))
566 return CB;
567
569 CB->getOperandBundlesAsDefs(Bundles);
570 Bundles.push_back(OB);
571 return Create(CB, Bundles, InsertPt);
572}
573
575 InsertPosition InsertPt) {
577 bool CreateNew = false;
578
579 for (unsigned I = 0, E = CB->getNumOperandBundles(); I != E; ++I) {
580 auto Bundle = CB->getOperandBundleAt(I);
581 if (Bundle.getTagID() == ID) {
582 CreateNew = true;
583 continue;
584 }
585 Bundles.emplace_back(Bundle);
586 }
587
588 return CreateNew ? Create(CB, Bundles, InsertPt) : CB;
589}
590
592 // Implementation note: this is a conservative implementation of operand
593 // bundle semantics, where *any* non-assume operand bundle (other than
594 // ptrauth) forces a callsite to be at least readonly.
597 getIntrinsicID() != Intrinsic::assume;
598}
599
604 getIntrinsicID() != Intrinsic::assume;
605}
606
609 if (auto *Fn = dyn_cast<Function>(getCalledOperand())) {
610 MemoryEffects FnME = Fn->getMemoryEffects();
611 if (hasOperandBundles()) {
612 // TODO: Add a method to get memory effects for operand bundles instead.
614 FnME |= MemoryEffects::readOnly();
616 FnME |= MemoryEffects::writeOnly();
617 }
618 ME &= FnME;
619 }
620 return ME;
621}
624}
625
626/// Determine if the function does not access memory.
629}
632}
633
634/// Determine if the function does not access or only reads memory.
637}
640}
641
642/// Determine if the function does not access or only writes memory.
645}
648}
649
650/// Determine if the call can access memmory only using pointers based
651/// on its arguments.
654}
657}
658
659/// Determine if the function may only access memory that is
660/// inaccessible from the IR.
663}
666}
667
668/// Determine if the function may only access memory that is
669/// either inaccessible from the IR or pointed to by its arguments.
672}
676}
677
678//===----------------------------------------------------------------------===//
679// CallInst Implementation
680//===----------------------------------------------------------------------===//
681
682void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
683 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
684 this->FTy = FTy;
685 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
686 "NumOperands not set up?");
687
688#ifndef NDEBUG
689 assert((Args.size() == FTy->getNumParams() ||
690 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
691 "Calling a function with bad signature!");
692
693 for (unsigned i = 0; i != Args.size(); ++i)
694 assert((i >= FTy->getNumParams() ||
695 FTy->getParamType(i) == Args[i]->getType()) &&
696 "Calling a function with a bad signature!");
697#endif
698
699 // Set operands in order of their index to match use-list-order
700 // prediction.
701 llvm::copy(Args, op_begin());
702 setCalledOperand(Func);
703
704 auto It = populateBundleOperandInfos(Bundles, Args.size());
705 (void)It;
706 assert(It + 1 == op_end() && "Should add up!");
707
708 setName(NameStr);
709}
710
711void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
712 this->FTy = FTy;
713 assert(getNumOperands() == 1 && "NumOperands not set up?");
714 setCalledOperand(Func);
715
716 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
717
718 setName(NameStr);
719}
720
721CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
722 AllocInfo AllocInfo, InsertPosition InsertBefore)
723 : CallBase(Ty->getReturnType(), Instruction::Call, AllocInfo,
724 InsertBefore) {
725 init(Ty, Func, Name);
726}
727
728CallInst::CallInst(const CallInst &CI, AllocInfo AllocInfo)
729 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, AllocInfo) {
731 "Wrong number of operands allocated");
732 setTailCallKind(CI.getTailCallKind());
734
735 std::copy(CI.op_begin(), CI.op_end(), op_begin());
736 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
739}
740
742 InsertPosition InsertPt) {
743 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
744
745 auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledOperand(),
746 Args, OpB, CI->getName(), InsertPt);
747 NewCI->setTailCallKind(CI->getTailCallKind());
748 NewCI->setCallingConv(CI->getCallingConv());
749 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
750 NewCI->setAttributes(CI->getAttributes());
751 NewCI->setDebugLoc(CI->getDebugLoc());
752 return NewCI;
753}
754
755// Update profile weight for call instruction by scaling it using the ratio
756// of S/T. The meaning of "branch_weights" meta data for call instruction is
757// transfered to represent call count.
759 if (T == 0) {
760 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in "
761 "div by 0. Ignoring. Likely the function "
762 << getParent()->getParent()->getName()
763 << " has 0 entry count, and contains call instructions "
764 "with non-zero prof info.");
765 return;
766 }
767 scaleProfData(*this, S, T);
768}
769
770//===----------------------------------------------------------------------===//
771// InvokeInst Implementation
772//===----------------------------------------------------------------------===//
773
774void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
775 BasicBlock *IfException, ArrayRef<Value *> Args,
777 const Twine &NameStr) {
778 this->FTy = FTy;
779
781 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
782 "NumOperands not set up?");
783
784#ifndef NDEBUG
785 assert(((Args.size() == FTy->getNumParams()) ||
786 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
787 "Invoking a function with bad signature");
788
789 for (unsigned i = 0, e = Args.size(); i != e; i++)
790 assert((i >= FTy->getNumParams() ||
791 FTy->getParamType(i) == Args[i]->getType()) &&
792 "Invoking a function with a bad signature!");
793#endif
794
795 // Set operands in order of their index to match use-list-order
796 // prediction.
797 llvm::copy(Args, op_begin());
798 setNormalDest(IfNormal);
799 setUnwindDest(IfException);
801
802 auto It = populateBundleOperandInfos(Bundles, Args.size());
803 (void)It;
804 assert(It + 3 == op_end() && "Should add up!");
805
806 setName(NameStr);
807}
808
809InvokeInst::InvokeInst(const InvokeInst &II, AllocInfo AllocInfo)
810 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, AllocInfo) {
811 assert(getNumOperands() == II.getNumOperands() &&
812 "Wrong number of operands allocated");
813 setCallingConv(II.getCallingConv());
814 std::copy(II.op_begin(), II.op_end(), op_begin());
815 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
817 SubclassOptionalData = II.SubclassOptionalData;
818}
819
821 InsertPosition InsertPt) {
822 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
823
824 auto *NewII = InvokeInst::Create(
825 II->getFunctionType(), II->getCalledOperand(), II->getNormalDest(),
826 II->getUnwindDest(), Args, OpB, II->getName(), InsertPt);
827 NewII->setCallingConv(II->getCallingConv());
828 NewII->SubclassOptionalData = II->SubclassOptionalData;
829 NewII->setAttributes(II->getAttributes());
830 NewII->setDebugLoc(II->getDebugLoc());
831 return NewII;
832}
833
835 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
836}
837
839 if (T == 0) {
840 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in "
841 "div by 0. Ignoring. Likely the function "
842 << getParent()->getParent()->getName()
843 << " has 0 entry count, and contains call instructions "
844 "with non-zero prof info.");
845 return;
846 }
847 scaleProfData(*this, S, T);
848}
849
850//===----------------------------------------------------------------------===//
851// CallBrInst Implementation
852//===----------------------------------------------------------------------===//
853
854void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough,
855 ArrayRef<BasicBlock *> IndirectDests,
858 const Twine &NameStr) {
859 this->FTy = FTy;
860
861 assert(getNumOperands() == ComputeNumOperands(Args.size(),
862 IndirectDests.size(),
863 CountBundleInputs(Bundles)) &&
864 "NumOperands not set up?");
865
866#ifndef NDEBUG
867 assert(((Args.size() == FTy->getNumParams()) ||
868 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
869 "Calling a function with bad signature");
870
871 for (unsigned i = 0, e = Args.size(); i != e; i++)
872 assert((i >= FTy->getNumParams() ||
873 FTy->getParamType(i) == Args[i]->getType()) &&
874 "Calling a function with a bad signature!");
875#endif
876
877 // Set operands in order of their index to match use-list-order
878 // prediction.
879 std::copy(Args.begin(), Args.end(), op_begin());
880 NumIndirectDests = IndirectDests.size();
881 setDefaultDest(Fallthrough);
882 for (unsigned i = 0; i != NumIndirectDests; ++i)
883 setIndirectDest(i, IndirectDests[i]);
885
886 auto It = populateBundleOperandInfos(Bundles, Args.size());
887 (void)It;
888 assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!");
889
890 setName(NameStr);
891}
892
893CallBrInst::CallBrInst(const CallBrInst &CBI, AllocInfo AllocInfo)
894 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr,
895 AllocInfo) {
897 "Wrong number of operands allocated");
899 std::copy(CBI.op_begin(), CBI.op_end(), op_begin());
900 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(),
903 NumIndirectDests = CBI.NumIndirectDests;
904}
905
907 InsertPosition InsertPt) {
908 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end());
909
910 auto *NewCBI = CallBrInst::Create(
911 CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(),
912 CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt);
913 NewCBI->setCallingConv(CBI->getCallingConv());
914 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData;
915 NewCBI->setAttributes(CBI->getAttributes());
916 NewCBI->setDebugLoc(CBI->getDebugLoc());
917 NewCBI->NumIndirectDests = CBI->NumIndirectDests;
918 return NewCBI;
919}
920
921//===----------------------------------------------------------------------===//
922// ReturnInst Implementation
923//===----------------------------------------------------------------------===//
924
925ReturnInst::ReturnInst(const ReturnInst &RI, AllocInfo AllocInfo)
926 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
927 AllocInfo) {
929 "Wrong number of operands allocated");
930 if (RI.getNumOperands())
931 Op<0>() = RI.Op<0>();
933}
934
935ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, AllocInfo AllocInfo,
936 InsertPosition InsertBefore)
937 : Instruction(Type::getVoidTy(C), Instruction::Ret, AllocInfo,
938 InsertBefore) {
939 if (retVal)
940 Op<0>() = retVal;
941}
942
943//===----------------------------------------------------------------------===//
944// ResumeInst Implementation
945//===----------------------------------------------------------------------===//
946
947ResumeInst::ResumeInst(const ResumeInst &RI)
948 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
949 AllocMarker) {
950 Op<0>() = RI.Op<0>();
951}
952
953ResumeInst::ResumeInst(Value *Exn, InsertPosition InsertBefore)
954 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
955 AllocMarker, InsertBefore) {
956 Op<0>() = Exn;
957}
958
959//===----------------------------------------------------------------------===//
960// CleanupReturnInst Implementation
961//===----------------------------------------------------------------------===//
962
963CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI,
965 : Instruction(CRI.getType(), Instruction::CleanupRet, AllocInfo) {
967 "Wrong number of operands allocated");
968 setSubclassData<Instruction::OpaqueField>(
970 Op<0>() = CRI.Op<0>();
971 if (CRI.hasUnwindDest())
972 Op<1>() = CRI.Op<1>();
973}
974
975void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
976 if (UnwindBB)
977 setSubclassData<UnwindDestField>(true);
978
979 Op<0>() = CleanupPad;
980 if (UnwindBB)
981 Op<1>() = UnwindBB;
982}
983
984CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
986 InsertPosition InsertBefore)
987 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
988 Instruction::CleanupRet, AllocInfo, InsertBefore) {
989 init(CleanupPad, UnwindBB);
990}
991
992//===----------------------------------------------------------------------===//
993// CatchReturnInst Implementation
994//===----------------------------------------------------------------------===//
995void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
996 Op<0>() = CatchPad;
997 Op<1>() = BB;
998}
999
1000CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
1001 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
1002 AllocMarker) {
1003 Op<0>() = CRI.Op<0>();
1004 Op<1>() = CRI.Op<1>();
1005}
1006
1007CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
1008 InsertPosition InsertBefore)
1009 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
1010 AllocMarker, InsertBefore) {
1011 init(CatchPad, BB);
1012}
1013
1014//===----------------------------------------------------------------------===//
1015// CatchSwitchInst Implementation
1016//===----------------------------------------------------------------------===//
1017
1018CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1019 unsigned NumReservedValues,
1020 const Twine &NameStr,
1021 InsertPosition InsertBefore)
1022 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, AllocMarker,
1023 InsertBefore) {
1024 if (UnwindDest)
1025 ++NumReservedValues;
1026 init(ParentPad, UnwindDest, NumReservedValues + 1);
1027 setName(NameStr);
1028}
1029
1030CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1031 : Instruction(CSI.getType(), Instruction::CatchSwitch, AllocMarker) {
1033 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1034 setNumHungOffUseOperands(ReservedSpace);
1035 Use *OL = getOperandList();
1036 const Use *InOL = CSI.getOperandList();
1037 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1038 OL[I] = InOL[I];
1039}
1040
1041void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1042 unsigned NumReservedValues) {
1043 assert(ParentPad && NumReservedValues);
1044
1045 ReservedSpace = NumReservedValues;
1046 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1047 allocHungoffUses(ReservedSpace);
1048
1049 Op<0>() = ParentPad;
1050 if (UnwindDest) {
1051 setSubclassData<UnwindDestField>(true);
1052 setUnwindDest(UnwindDest);
1053 }
1054}
1055
1056/// growOperands - grow operands - This grows the operand list in response to a
1057/// push_back style of operation. This grows the number of ops by 2 times.
1058void CatchSwitchInst::growOperands(unsigned Size) {
1059 unsigned NumOperands = getNumOperands();
1060 assert(NumOperands >= 1);
1061 if (ReservedSpace >= NumOperands + Size)
1062 return;
1063 ReservedSpace = (NumOperands + Size / 2) * 2;
1064 growHungoffUses(ReservedSpace);
1065}
1066
1068 unsigned OpNo = getNumOperands();
1069 growOperands(1);
1070 assert(OpNo < ReservedSpace && "Growing didn't work!");
1072 getOperandList()[OpNo] = Handler;
1073}
1074
1076 // Move all subsequent handlers up one.
1077 Use *EndDst = op_end() - 1;
1078 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1079 *CurDst = *(CurDst + 1);
1080 // Null out the last handler use.
1081 *EndDst = nullptr;
1082
1084}
1085
1086//===----------------------------------------------------------------------===//
1087// FuncletPadInst Implementation
1088//===----------------------------------------------------------------------===//
1089void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1090 const Twine &NameStr) {
1091 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1092 llvm::copy(Args, op_begin());
1093 setParentPad(ParentPad);
1094 setName(NameStr);
1095}
1096
1097FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI, AllocInfo AllocInfo)
1098 : Instruction(FPI.getType(), FPI.getOpcode(), AllocInfo) {
1100 "Wrong number of operands allocated");
1101 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1103}
1104
1105FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1107 const Twine &NameStr,
1108 InsertPosition InsertBefore)
1109 : Instruction(ParentPad->getType(), Op, AllocInfo, InsertBefore) {
1110 init(ParentPad, Args, NameStr);
1111}
1112
1113//===----------------------------------------------------------------------===//
1114// UnreachableInst Implementation
1115//===----------------------------------------------------------------------===//
1116
1118 InsertPosition InsertBefore)
1119 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable,
1120 AllocMarker, InsertBefore) {}
1121
1122//===----------------------------------------------------------------------===//
1123// BranchInst Implementation
1124//===----------------------------------------------------------------------===//
1125
1126void BranchInst::AssertOK() {
1127 if (isConditional())
1128 assert(getCondition()->getType()->isIntegerTy(1) &&
1129 "May only branch on boolean predicates!");
1130}
1131
1132BranchInst::BranchInst(BasicBlock *IfTrue, AllocInfo AllocInfo,
1133 InsertPosition InsertBefore)
1134 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1135 AllocInfo, InsertBefore) {
1136 assert(IfTrue && "Branch destination may not be null!");
1137 Op<-1>() = IfTrue;
1138}
1139
1140BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1141 AllocInfo AllocInfo, InsertPosition InsertBefore)
1142 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1143 AllocInfo, InsertBefore) {
1144 // Assign in order of operand index to make use-list order predictable.
1145 Op<-3>() = Cond;
1146 Op<-2>() = IfFalse;
1147 Op<-1>() = IfTrue;
1148#ifndef NDEBUG
1149 AssertOK();
1150#endif
1151}
1152
1153BranchInst::BranchInst(const BranchInst &BI, AllocInfo AllocInfo)
1154 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
1155 AllocInfo) {
1157 "Wrong number of operands allocated");
1158 // Assign in order of operand index to make use-list order predictable.
1159 if (BI.getNumOperands() != 1) {
1160 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
1161 Op<-3>() = BI.Op<-3>();
1162 Op<-2>() = BI.Op<-2>();
1163 }
1164 Op<-1>() = BI.Op<-1>();
1166}
1167
1170 "Cannot swap successors of an unconditional branch");
1171 Op<-1>().swap(Op<-2>());
1172
1173 // Update profile metadata if present and it matches our structural
1174 // expectations.
1176}
1177
1178//===----------------------------------------------------------------------===//
1179// AllocaInst Implementation
1180//===----------------------------------------------------------------------===//
1181
1182static Value *getAISize(LLVMContext &Context, Value *Amt) {
1183 if (!Amt)
1184 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
1185 else {
1186 assert(!isa<BasicBlock>(Amt) &&
1187 "Passed basic block into allocation size parameter! Use other ctor");
1188 assert(Amt->getType()->isIntegerTy() &&
1189 "Allocation array size is not an integer!");
1190 }
1191 return Amt;
1192}
1193
1195 assert(Pos.isValid() &&
1196 "Insertion position cannot be null when alignment not provided!");
1197 BasicBlock *BB = Pos.getBasicBlock();
1198 assert(BB->getParent() &&
1199 "BB must be in a Function when alignment not provided!");
1200 const DataLayout &DL = BB->getDataLayout();
1201 return DL.getPrefTypeAlign(Ty);
1202}
1203
1204AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
1205 InsertPosition InsertBefore)
1206 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
1207
1208AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1209 const Twine &Name, InsertPosition InsertBefore)
1210 : AllocaInst(Ty, AddrSpace, ArraySize,
1211 computeAllocaDefaultAlign(Ty, InsertBefore), Name,
1212 InsertBefore) {}
1213
1214AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1215 Align Align, const Twine &Name,
1216 InsertPosition InsertBefore)
1217 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1218 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1219 AllocatedType(Ty) {
1221 assert(!Ty->isVoidTy() && "Cannot allocate void!");
1222 setName(Name);
1223}
1224
1226 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
1227 return !CI->isOne();
1228 return true;
1229}
1230
1231/// isStaticAlloca - Return true if this alloca is in the entry block of the
1232/// function and is a constant size. If so, the code generator will fold it
1233/// into the prolog/epilog code, so it is basically free.
1235 // Must be constant size.
1236 if (!isa<ConstantInt>(getArraySize())) return false;
1237
1238 // Must be in the entry block.
1239 const BasicBlock *Parent = getParent();
1240 return Parent->isEntryBlock() && !isUsedWithInAlloca();
1241}
1242
1243//===----------------------------------------------------------------------===//
1244// LoadInst Implementation
1245//===----------------------------------------------------------------------===//
1246
1247void LoadInst::AssertOK() {
1249 "Ptr must have pointer type.");
1250}
1251
1253 assert(Pos.isValid() &&
1254 "Insertion position cannot be null when alignment not provided!");
1255 BasicBlock *BB = Pos.getBasicBlock();
1256 assert(BB->getParent() &&
1257 "BB must be in a Function when alignment not provided!");
1258 const DataLayout &DL = BB->getDataLayout();
1259 return DL.getABITypeAlign(Ty);
1260}
1261
1263 InsertPosition InsertBef)
1264 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
1265
1266LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1267 InsertPosition InsertBef)
1268 : LoadInst(Ty, Ptr, Name, isVolatile,
1269 computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {}
1270
1271LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1272 Align Align, InsertPosition InsertBef)
1273 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1274 SyncScope::System, InsertBef) {}
1275
1276LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1278 InsertPosition InsertBef)
1279 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1282 setAtomic(Order, SSID);
1283 AssertOK();
1284 setName(Name);
1285}
1286
1287//===----------------------------------------------------------------------===//
1288// StoreInst Implementation
1289//===----------------------------------------------------------------------===//
1290
1291void StoreInst::AssertOK() {
1292 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1294 "Ptr must have pointer type!");
1295}
1296
1298 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
1299
1300StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1301 InsertPosition InsertBefore)
1302 : StoreInst(val, addr, isVolatile,
1303 computeLoadStoreDefaultAlign(val->getType(), InsertBefore),
1304 InsertBefore) {}
1305
1306StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1307 InsertPosition InsertBefore)
1308 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1309 SyncScope::System, InsertBefore) {}
1310
1311StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1312 AtomicOrdering Order, SyncScope::ID SSID,
1313 InsertPosition InsertBefore)
1314 : Instruction(Type::getVoidTy(val->getContext()), Store, AllocMarker,
1315 InsertBefore) {
1316 Op<0>() = val;
1317 Op<1>() = addr;
1320 setAtomic(Order, SSID);
1321 AssertOK();
1322}
1323
1324//===----------------------------------------------------------------------===//
1325// AtomicCmpXchgInst Implementation
1326//===----------------------------------------------------------------------===//
1327
1328void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1329 Align Alignment, AtomicOrdering SuccessOrdering,
1330 AtomicOrdering FailureOrdering,
1331 SyncScope::ID SSID) {
1332 Op<0>() = Ptr;
1333 Op<1>() = Cmp;
1334 Op<2>() = NewVal;
1335 setSuccessOrdering(SuccessOrdering);
1336 setFailureOrdering(FailureOrdering);
1337 setSyncScopeID(SSID);
1338 setAlignment(Alignment);
1339
1340 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1341 "All operands must be non-null!");
1343 "Ptr must have pointer type!");
1344 assert(getOperand(1)->getType() == getOperand(2)->getType() &&
1345 "Cmp type and NewVal type must be same!");
1346}
1347
1349 Align Alignment,
1350 AtomicOrdering SuccessOrdering,
1351 AtomicOrdering FailureOrdering,
1352 SyncScope::ID SSID,
1353 InsertPosition InsertBefore)
1354 : Instruction(
1355 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
1356 AtomicCmpXchg, AllocMarker, InsertBefore) {
1357 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID);
1358}
1359
1360//===----------------------------------------------------------------------===//
1361// AtomicRMWInst Implementation
1362//===----------------------------------------------------------------------===//
1363
1364void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1365 Align Alignment, AtomicOrdering Ordering,
1366 SyncScope::ID SSID) {
1367 assert(Ordering != AtomicOrdering::NotAtomic &&
1368 "atomicrmw instructions can only be atomic.");
1369 assert(Ordering != AtomicOrdering::Unordered &&
1370 "atomicrmw instructions cannot be unordered.");
1371 Op<0>() = Ptr;
1372 Op<1>() = Val;
1374 setOrdering(Ordering);
1375 setSyncScopeID(SSID);
1376 setAlignment(Alignment);
1377
1378 assert(getOperand(0) && getOperand(1) && "All operands must be non-null!");
1380 "Ptr must have pointer type!");
1381 assert(Ordering != AtomicOrdering::NotAtomic &&
1382 "AtomicRMW instructions must be atomic!");
1383}
1384
1386 Align Alignment, AtomicOrdering Ordering,
1387 SyncScope::ID SSID, InsertPosition InsertBefore)
1388 : Instruction(Val->getType(), AtomicRMW, AllocMarker, InsertBefore) {
1389 Init(Operation, Ptr, Val, Alignment, Ordering, SSID);
1390}
1391
1393 switch (Op) {
1395 return "xchg";
1396 case AtomicRMWInst::Add:
1397 return "add";
1398 case AtomicRMWInst::Sub:
1399 return "sub";
1400 case AtomicRMWInst::And:
1401 return "and";
1403 return "nand";
1404 case AtomicRMWInst::Or:
1405 return "or";
1406 case AtomicRMWInst::Xor:
1407 return "xor";
1408 case AtomicRMWInst::Max:
1409 return "max";
1410 case AtomicRMWInst::Min:
1411 return "min";
1413 return "umax";
1415 return "umin";
1417 return "fadd";
1419 return "fsub";
1421 return "fmax";
1423 return "fmin";
1425 return "uinc_wrap";
1427 return "udec_wrap";
1429 return "usub_cond";
1431 return "usub_sat";
1433 return "<invalid operation>";
1434 }
1435
1436 llvm_unreachable("invalid atomicrmw operation");
1437}
1438
1439//===----------------------------------------------------------------------===//
1440// FenceInst Implementation
1441//===----------------------------------------------------------------------===//
1442
1444 SyncScope::ID SSID, InsertPosition InsertBefore)
1445 : Instruction(Type::getVoidTy(C), Fence, AllocMarker, InsertBefore) {
1446 setOrdering(Ordering);
1447 setSyncScopeID(SSID);
1448}
1449
1450//===----------------------------------------------------------------------===//
1451// GetElementPtrInst Implementation
1452//===----------------------------------------------------------------------===//
1453
1454void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
1455 const Twine &Name) {
1456 assert(getNumOperands() == 1 + IdxList.size() &&
1457 "NumOperands not initialized?");
1458 Op<0>() = Ptr;
1459 llvm::copy(IdxList, op_begin() + 1);
1460 setName(Name);
1461}
1462
1463GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI,
1465 : Instruction(GEPI.getType(), GetElementPtr, AllocInfo),
1466 SourceElementType(GEPI.SourceElementType),
1467 ResultElementType(GEPI.ResultElementType) {
1468 assert(getNumOperands() == GEPI.getNumOperands() &&
1469 "Wrong number of operands allocated");
1470 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
1472}
1473
1475 if (auto *Struct = dyn_cast<StructType>(Ty)) {
1476 if (!Struct->indexValid(Idx))
1477 return nullptr;
1478 return Struct->getTypeAtIndex(Idx);
1479 }
1480 if (!Idx->getType()->isIntOrIntVectorTy())
1481 return nullptr;
1482 if (auto *Array = dyn_cast<ArrayType>(Ty))
1483 return Array->getElementType();
1484 if (auto *Vector = dyn_cast<VectorType>(Ty))
1485 return Vector->getElementType();
1486 return nullptr;
1487}
1488
1490 if (auto *Struct = dyn_cast<StructType>(Ty)) {
1491 if (Idx >= Struct->getNumElements())
1492 return nullptr;
1493 return Struct->getElementType(Idx);
1494 }
1495 if (auto *Array = dyn_cast<ArrayType>(Ty))
1496 return Array->getElementType();
1497 if (auto *Vector = dyn_cast<VectorType>(Ty))
1498 return Vector->getElementType();
1499 return nullptr;
1500}
1501
1502template <typename IndexTy>
1504 if (IdxList.empty())
1505 return Ty;
1506 for (IndexTy V : IdxList.slice(1)) {
1508 if (!Ty)
1509 return Ty;
1510 }
1511 return Ty;
1512}
1513
1515 return getIndexedTypeInternal(Ty, IdxList);
1516}
1517
1519 ArrayRef<Constant *> IdxList) {
1520 return getIndexedTypeInternal(Ty, IdxList);
1521}
1522
1524 return getIndexedTypeInternal(Ty, IdxList);
1525}
1526
1527/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1528/// zeros. If so, the result pointer and the first operand have the same
1529/// value, just potentially different types.
1531 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1532 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1533 if (!CI->isZero()) return false;
1534 } else {
1535 return false;
1536 }
1537 }
1538 return true;
1539}
1540
1541/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1542/// constant integers. If so, the result pointer and the first operand have
1543/// a constant offset between them.
1545 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1546 if (!isa<ConstantInt>(getOperand(i)))
1547 return false;
1548 }
1549 return true;
1550}
1551
1554}
1555
1557 GEPNoWrapFlags NW = cast<GEPOperator>(this)->getNoWrapFlags();
1558 if (B)
1560 else
1561 NW = NW.withoutInBounds();
1562 setNoWrapFlags(NW);
1563}
1564
1566 return cast<GEPOperator>(this)->getNoWrapFlags();
1567}
1568
1570 return cast<GEPOperator>(this)->isInBounds();
1571}
1572
1574 return cast<GEPOperator>(this)->hasNoUnsignedSignedWrap();
1575}
1576
1578 return cast<GEPOperator>(this)->hasNoUnsignedWrap();
1579}
1580
1582 APInt &Offset) const {
1583 // Delegate to the generic GEPOperator implementation.
1584 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
1585}
1586
1588 const DataLayout &DL, unsigned BitWidth,
1589 SmallMapVector<Value *, APInt, 4> &VariableOffsets,
1590 APInt &ConstantOffset) const {
1591 // Delegate to the generic GEPOperator implementation.
1592 return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets,
1593 ConstantOffset);
1594}
1595
1596//===----------------------------------------------------------------------===//
1597// ExtractElementInst Implementation
1598//===----------------------------------------------------------------------===//
1599
1600ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1601 const Twine &Name,
1602 InsertPosition InsertBef)
1603 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1604 ExtractElement, AllocMarker, InsertBef) {
1605 assert(isValidOperands(Val, Index) &&
1606 "Invalid extractelement instruction operands!");
1607 Op<0>() = Val;
1608 Op<1>() = Index;
1609 setName(Name);
1610}
1611
1612bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1613 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
1614 return false;
1615 return true;
1616}
1617
1618//===----------------------------------------------------------------------===//
1619// InsertElementInst Implementation
1620//===----------------------------------------------------------------------===//
1621
1622InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1623 const Twine &Name,
1624 InsertPosition InsertBef)
1625 : Instruction(Vec->getType(), InsertElement, AllocMarker, InsertBef) {
1626 assert(isValidOperands(Vec, Elt, Index) &&
1627 "Invalid insertelement instruction operands!");
1628 Op<0>() = Vec;
1629 Op<1>() = Elt;
1630 Op<2>() = Index;
1631 setName(Name);
1632}
1633
1635 const Value *Index) {
1636 if (!Vec->getType()->isVectorTy())
1637 return false; // First operand of insertelement must be vector type.
1638
1639 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1640 return false;// Second operand of insertelement must be vector element type.
1641
1642 if (!Index->getType()->isIntegerTy())
1643 return false; // Third operand of insertelement must be i32.
1644 return true;
1645}
1646
1647//===----------------------------------------------------------------------===//
1648// ShuffleVectorInst Implementation
1649//===----------------------------------------------------------------------===//
1650
1652 assert(V && "Cannot create placeholder of nullptr V");
1653 return PoisonValue::get(V->getType());
1654}
1655
1657 InsertPosition InsertBefore)
1659 InsertBefore) {}
1660
1662 const Twine &Name,
1663 InsertPosition InsertBefore)
1665 InsertBefore) {}
1666
1668 const Twine &Name,
1669 InsertPosition InsertBefore)
1670 : Instruction(
1671 VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1672 cast<VectorType>(Mask->getType())->getElementCount()),
1673 ShuffleVector, AllocMarker, InsertBefore) {
1674 assert(isValidOperands(V1, V2, Mask) &&
1675 "Invalid shuffle vector instruction operands!");
1676
1677 Op<0>() = V1;
1678 Op<1>() = V2;
1679 SmallVector<int, 16> MaskArr;
1680 getShuffleMask(cast<Constant>(Mask), MaskArr);
1681 setShuffleMask(MaskArr);
1682 setName(Name);
1683}
1684
1686 const Twine &Name,
1687 InsertPosition InsertBefore)
1688 : Instruction(
1689 VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1690 Mask.size(), isa<ScalableVectorType>(V1->getType())),
1691 ShuffleVector, AllocMarker, InsertBefore) {
1692 assert(isValidOperands(V1, V2, Mask) &&
1693 "Invalid shuffle vector instruction operands!");
1694 Op<0>() = V1;
1695 Op<1>() = V2;
1696 setShuffleMask(Mask);
1697 setName(Name);
1698}
1699
1701 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
1702 int NumMaskElts = ShuffleMask.size();
1703 SmallVector<int, 16> NewMask(NumMaskElts);
1704 for (int i = 0; i != NumMaskElts; ++i) {
1705 int MaskElt = getMaskValue(i);
1706 if (MaskElt == PoisonMaskElem) {
1707 NewMask[i] = PoisonMaskElem;
1708 continue;
1709 }
1710 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask");
1711 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts;
1712 NewMask[i] = MaskElt;
1713 }
1714 setShuffleMask(NewMask);
1715 Op<0>().swap(Op<1>());
1716}
1717
1719 ArrayRef<int> Mask) {
1720 // V1 and V2 must be vectors of the same type.
1721 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
1722 return false;
1723
1724 // Make sure the mask elements make sense.
1725 int V1Size =
1726 cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue();
1727 for (int Elem : Mask)
1728 if (Elem != PoisonMaskElem && Elem >= V1Size * 2)
1729 return false;
1730
1731 if (isa<ScalableVectorType>(V1->getType()))
1732 if ((Mask[0] != 0 && Mask[0] != PoisonMaskElem) || !all_equal(Mask))
1733 return false;
1734
1735 return true;
1736}
1737
1739 const Value *Mask) {
1740 // V1 and V2 must be vectors of the same type.
1741 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1742 return false;
1743
1744 // Mask must be vector of i32, and must be the same kind of vector as the
1745 // input vectors
1746 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
1747 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) ||
1748 isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType()))
1749 return false;
1750
1751 // Check to see if Mask is valid.
1752 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1753 return true;
1754
1755 // NOTE: Through vector ConstantInt we have the potential to support more
1756 // than just zero splat masks but that requires a LangRef change.
1757 if (isa<ScalableVectorType>(MaskTy))
1758 return false;
1759
1760 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
1761
1762 if (const auto *CI = dyn_cast<ConstantInt>(Mask))
1763 return !CI->uge(V1Size * 2);
1764
1765 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
1766 for (Value *Op : MV->operands()) {
1767 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
1768 if (CI->uge(V1Size*2))
1769 return false;
1770 } else if (!isa<UndefValue>(Op)) {
1771 return false;
1772 }
1773 }
1774 return true;
1775 }
1776
1777 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
1778 for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements();
1779 i != e; ++i)
1780 if (CDS->getElementAsInteger(i) >= V1Size*2)
1781 return false;
1782 return true;
1783 }
1784
1785 return false;
1786}
1787
1789 SmallVectorImpl<int> &Result) {
1790 ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount();
1791
1792 if (isa<ConstantAggregateZero>(Mask)) {
1793 Result.resize(EC.getKnownMinValue(), 0);
1794 return;
1795 }
1796
1797 Result.reserve(EC.getKnownMinValue());
1798
1799 if (EC.isScalable()) {
1800 assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) &&
1801 "Scalable vector shuffle mask must be undef or zeroinitializer");
1802 int MaskVal = isa<UndefValue>(Mask) ? -1 : 0;
1803 for (unsigned I = 0; I < EC.getKnownMinValue(); ++I)
1804 Result.emplace_back(MaskVal);
1805 return;
1806 }
1807
1808 unsigned NumElts = EC.getKnownMinValue();
1809
1810 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
1811 for (unsigned i = 0; i != NumElts; ++i)
1812 Result.push_back(CDS->getElementAsInteger(i));
1813 return;
1814 }
1815 for (unsigned i = 0; i != NumElts; ++i) {
1816 Constant *C = Mask->getAggregateElement(i);
1817 Result.push_back(isa<UndefValue>(C) ? -1 :
1818 cast<ConstantInt>(C)->getZExtValue());
1819 }
1820}
1821
1823 ShuffleMask.assign(Mask.begin(), Mask.end());
1824 ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType());
1825}
1826
1828 Type *ResultTy) {
1829 Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext());
1830 if (isa<ScalableVectorType>(ResultTy)) {
1831 assert(all_equal(Mask) && "Unexpected shuffle");
1832 Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true);
1833 if (Mask[0] == 0)
1834 return Constant::getNullValue(VecTy);
1835 return PoisonValue::get(VecTy);
1836 }
1838 for (int Elem : Mask) {
1839 if (Elem == PoisonMaskElem)
1840 MaskConst.push_back(PoisonValue::get(Int32Ty));
1841 else
1842 MaskConst.push_back(ConstantInt::get(Int32Ty, Elem));
1843 }
1844 return ConstantVector::get(MaskConst);
1845}
1846
1847static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1848 assert(!Mask.empty() && "Shuffle mask must contain elements");
1849 bool UsesLHS = false;
1850 bool UsesRHS = false;
1851 for (int I : Mask) {
1852 if (I == -1)
1853 continue;
1854 assert(I >= 0 && I < (NumOpElts * 2) &&
1855 "Out-of-bounds shuffle mask element");
1856 UsesLHS |= (I < NumOpElts);
1857 UsesRHS |= (I >= NumOpElts);
1858 if (UsesLHS && UsesRHS)
1859 return false;
1860 }
1861 // Allow for degenerate case: completely undef mask means neither source is used.
1862 return UsesLHS || UsesRHS;
1863}
1864
1866 // We don't have vector operand size information, so assume operands are the
1867 // same size as the mask.
1868 return isSingleSourceMaskImpl(Mask, NumSrcElts);
1869}
1870
1871static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1872 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
1873 return false;
1874 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
1875 if (Mask[i] == -1)
1876 continue;
1877 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
1878 return false;
1879 }
1880 return true;
1881}
1882
1884 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1885 return false;
1886 // We don't have vector operand size information, so assume operands are the
1887 // same size as the mask.
1888 return isIdentityMaskImpl(Mask, NumSrcElts);
1889}
1890
1892 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1893 return false;
1894 if (!isSingleSourceMask(Mask, NumSrcElts))
1895 return false;
1896
1897 // The number of elements in the mask must be at least 2.
1898 if (NumSrcElts < 2)
1899 return false;
1900
1901 for (int I = 0, E = Mask.size(); I < E; ++I) {
1902 if (Mask[I] == -1)
1903 continue;
1904 if (Mask[I] != (NumSrcElts - 1 - I) &&
1905 Mask[I] != (NumSrcElts + NumSrcElts - 1 - I))
1906 return false;
1907 }
1908 return true;
1909}
1910
1912 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1913 return false;
1914 if (!isSingleSourceMask(Mask, NumSrcElts))
1915 return false;
1916 for (int I = 0, E = Mask.size(); I < E; ++I) {
1917 if (Mask[I] == -1)
1918 continue;
1919 if (Mask[I] != 0 && Mask[I] != NumSrcElts)
1920 return false;
1921 }
1922 return true;
1923}
1924
1926 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1927 return false;
1928 // Select is differentiated from identity. It requires using both sources.
1929 if (isSingleSourceMask(Mask, NumSrcElts))
1930 return false;
1931 for (int I = 0, E = Mask.size(); I < E; ++I) {
1932 if (Mask[I] == -1)
1933 continue;
1934 if (Mask[I] != I && Mask[I] != (NumSrcElts + I))
1935 return false;
1936 }
1937 return true;
1938}
1939
1941 // Example masks that will return true:
1942 // v1 = <a, b, c, d>
1943 // v2 = <e, f, g, h>
1944 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1945 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1946
1947 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1948 return false;
1949 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1950 int Sz = Mask.size();
1951 if (Sz < 2 || !isPowerOf2_32(Sz))
1952 return false;
1953
1954 // 2. The first element of the mask must be either a 0 or a 1.
1955 if (Mask[0] != 0 && Mask[0] != 1)
1956 return false;
1957
1958 // 3. The difference between the first 2 elements must be equal to the
1959 // number of elements in the mask.
1960 if ((Mask[1] - Mask[0]) != NumSrcElts)
1961 return false;
1962
1963 // 4. The difference between consecutive even-numbered and odd-numbered
1964 // elements must be equal to 2.
1965 for (int I = 2; I < Sz; ++I) {
1966 int MaskEltVal = Mask[I];
1967 if (MaskEltVal == -1)
1968 return false;
1969 int MaskEltPrevVal = Mask[I - 2];
1970 if (MaskEltVal - MaskEltPrevVal != 2)
1971 return false;
1972 }
1973 return true;
1974}
1975
1977 int &Index) {
1978 if (Mask.size() != static_cast<unsigned>(NumSrcElts))
1979 return false;
1980 // Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
1981 int StartIndex = -1;
1982 for (int I = 0, E = Mask.size(); I != E; ++I) {
1983 int MaskEltVal = Mask[I];
1984 if (MaskEltVal == -1)
1985 continue;
1986
1987 if (StartIndex == -1) {
1988 // Don't support a StartIndex that begins in the second input, or if the
1989 // first non-undef index would access below the StartIndex.
1990 if (MaskEltVal < I || NumSrcElts <= (MaskEltVal - I))
1991 return false;
1992
1993 StartIndex = MaskEltVal - I;
1994 continue;
1995 }
1996
1997 // Splice is sequential starting from StartIndex.
1998 if (MaskEltVal != (StartIndex + I))
1999 return false;
2000 }
2001
2002 if (StartIndex == -1)
2003 return false;
2004
2005 // NOTE: This accepts StartIndex == 0 (COPY).
2006 Index = StartIndex;
2007 return true;
2008}
2009
2011 int NumSrcElts, int &Index) {
2012 // Must extract from a single source.
2013 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
2014 return false;
2015
2016 // Must be smaller (else this is an Identity shuffle).
2017 if (NumSrcElts <= (int)Mask.size())
2018 return false;
2019
2020 // Find start of extraction, accounting that we may start with an UNDEF.
2021 int SubIndex = -1;
2022 for (int i = 0, e = Mask.size(); i != e; ++i) {
2023 int M = Mask[i];
2024 if (M < 0)
2025 continue;
2026 int Offset = (M % NumSrcElts) - i;
2027 if (0 <= SubIndex && SubIndex != Offset)
2028 return false;
2029 SubIndex = Offset;
2030 }
2031
2032 if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) {
2033 Index = SubIndex;
2034 return true;
2035 }
2036 return false;
2037}
2038
2040 int NumSrcElts, int &NumSubElts,
2041 int &Index) {
2042 int NumMaskElts = Mask.size();
2043
2044 // Don't try to match if we're shuffling to a smaller size.
2045 if (NumMaskElts < NumSrcElts)
2046 return false;
2047
2048 // TODO: We don't recognize self-insertion/widening.
2049 if (isSingleSourceMaskImpl(Mask, NumSrcElts))
2050 return false;
2051
2052 // Determine which mask elements are attributed to which source.
2053 APInt UndefElts = APInt::getZero(NumMaskElts);
2054 APInt Src0Elts = APInt::getZero(NumMaskElts);
2055 APInt Src1Elts = APInt::getZero(NumMaskElts);
2056 bool Src0Identity = true;
2057 bool Src1Identity = true;
2058
2059 for (int i = 0; i != NumMaskElts; ++i) {
2060 int M = Mask[i];
2061 if (M < 0) {
2062 UndefElts.setBit(i);
2063 continue;
2064 }
2065 if (M < NumSrcElts) {
2066 Src0Elts.setBit(i);
2067 Src0Identity &= (M == i);
2068 continue;
2069 }
2070 Src1Elts.setBit(i);
2071 Src1Identity &= (M == (i + NumSrcElts));
2072 }
2073 assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() &&
2074 "unknown shuffle elements");
2075 assert(!Src0Elts.isZero() && !Src1Elts.isZero() &&
2076 "2-source shuffle not found");
2077
2078 // Determine lo/hi span ranges.
2079 // TODO: How should we handle undefs at the start of subvector insertions?
2080 int Src0Lo = Src0Elts.countr_zero();
2081 int Src1Lo = Src1Elts.countr_zero();
2082 int Src0Hi = NumMaskElts - Src0Elts.countl_zero();
2083 int Src1Hi = NumMaskElts - Src1Elts.countl_zero();
2084
2085 // If src0 is in place, see if the src1 elements is inplace within its own
2086 // span.
2087 if (Src0Identity) {
2088 int NumSub1Elts = Src1Hi - Src1Lo;
2089 ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts);
2090 if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) {
2091 NumSubElts = NumSub1Elts;
2092 Index = Src1Lo;
2093 return true;
2094 }
2095 }
2096
2097 // If src1 is in place, see if the src0 elements is inplace within its own
2098 // span.
2099 if (Src1Identity) {
2100 int NumSub0Elts = Src0Hi - Src0Lo;
2101 ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts);
2102 if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) {
2103 NumSubElts = NumSub0Elts;
2104 Index = Src0Lo;
2105 return true;
2106 }
2107 }
2108
2109 return false;
2110}
2111
2113 // FIXME: Not currently possible to express a shuffle mask for a scalable
2114 // vector for this case.
2115 if (isa<ScalableVectorType>(getType()))
2116 return false;
2117
2118 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2119 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2120 if (NumMaskElts <= NumOpElts)
2121 return false;
2122
2123 // The first part of the mask must choose elements from exactly 1 source op.
2125 if (!isIdentityMaskImpl(Mask, NumOpElts))
2126 return false;
2127
2128 // All extending must be with undef elements.
2129 for (int i = NumOpElts; i < NumMaskElts; ++i)
2130 if (Mask[i] != -1)
2131 return false;
2132
2133 return true;
2134}
2135
2137 // FIXME: Not currently possible to express a shuffle mask for a scalable
2138 // vector for this case.
2139 if (isa<ScalableVectorType>(getType()))
2140 return false;
2141
2142 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2143 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2144 if (NumMaskElts >= NumOpElts)
2145 return false;
2146
2147 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
2148}
2149
2151 // Vector concatenation is differentiated from identity with padding.
2152 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
2153 return false;
2154
2155 // FIXME: Not currently possible to express a shuffle mask for a scalable
2156 // vector for this case.
2157 if (isa<ScalableVectorType>(getType()))
2158 return false;
2159
2160 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2161 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2162 if (NumMaskElts != NumOpElts * 2)
2163 return false;
2164
2165 // Use the mask length rather than the operands' vector lengths here. We
2166 // already know that the shuffle returns a vector twice as long as the inputs,
2167 // and neither of the inputs are undef vectors. If the mask picks consecutive
2168 // elements from both inputs, then this is a concatenation of the inputs.
2169 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
2170}
2171
2173 int ReplicationFactor, int VF) {
2174 assert(Mask.size() == (unsigned)ReplicationFactor * VF &&
2175 "Unexpected mask size.");
2176
2177 for (int CurrElt : seq(VF)) {
2178 ArrayRef<int> CurrSubMask = Mask.take_front(ReplicationFactor);
2179 assert(CurrSubMask.size() == (unsigned)ReplicationFactor &&
2180 "Run out of mask?");
2181 Mask = Mask.drop_front(ReplicationFactor);
2182 if (!all_of(CurrSubMask, [CurrElt](int MaskElt) {
2183 return MaskElt == PoisonMaskElem || MaskElt == CurrElt;
2184 }))
2185 return false;
2186 }
2187 assert(Mask.empty() && "Did not consume the whole mask?");
2188
2189 return true;
2190}
2191
2193 int &ReplicationFactor, int &VF) {
2194 // undef-less case is trivial.
2195 if (!llvm::is_contained(Mask, PoisonMaskElem)) {
2196 ReplicationFactor =
2197 Mask.take_while([](int MaskElt) { return MaskElt == 0; }).size();
2198 if (ReplicationFactor == 0 || Mask.size() % ReplicationFactor != 0)
2199 return false;
2200 VF = Mask.size() / ReplicationFactor;
2201 return isReplicationMaskWithParams(Mask, ReplicationFactor, VF);
2202 }
2203
2204 // However, if the mask contains undef's, we have to enumerate possible tuples
2205 // and pick one. There are bounds on replication factor: [1, mask size]
2206 // (where RF=1 is an identity shuffle, RF=mask size is a broadcast shuffle)
2207 // Additionally, mask size is a replication factor multiplied by vector size,
2208 // which further significantly reduces the search space.
2209
2210 // Before doing that, let's perform basic correctness checking first.
2211 int Largest = -1;
2212 for (int MaskElt : Mask) {
2213 if (MaskElt == PoisonMaskElem)
2214 continue;
2215 // Elements must be in non-decreasing order.
2216 if (MaskElt < Largest)
2217 return false;
2218 Largest = std::max(Largest, MaskElt);
2219 }
2220
2221 // Prefer larger replication factor if all else equal.
2222 for (int PossibleReplicationFactor :
2223 reverse(seq_inclusive<unsigned>(1, Mask.size()))) {
2224 if (Mask.size() % PossibleReplicationFactor != 0)
2225 continue;
2226 int PossibleVF = Mask.size() / PossibleReplicationFactor;
2227 if (!isReplicationMaskWithParams(Mask, PossibleReplicationFactor,
2228 PossibleVF))
2229 continue;
2230 ReplicationFactor = PossibleReplicationFactor;
2231 VF = PossibleVF;
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
2238bool ShuffleVectorInst::isReplicationMask(int &ReplicationFactor,
2239 int &VF) const {
2240 // Not possible to express a shuffle mask for a scalable vector for this
2241 // case.
2242 if (isa<ScalableVectorType>(getType()))
2243 return false;
2244
2245 VF = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2246 if (ShuffleMask.size() % VF != 0)
2247 return false;
2248 ReplicationFactor = ShuffleMask.size() / VF;
2249
2250 return isReplicationMaskWithParams(ShuffleMask, ReplicationFactor, VF);
2251}
2252
2254 if (VF <= 0 || Mask.size() < static_cast<unsigned>(VF) ||
2255 Mask.size() % VF != 0)
2256 return false;
2257 for (unsigned K = 0, Sz = Mask.size(); K < Sz; K += VF) {
2258 ArrayRef<int> SubMask = Mask.slice(K, VF);
2259 if (all_of(SubMask, [](int Idx) { return Idx == PoisonMaskElem; }))
2260 continue;
2261 SmallBitVector Used(VF, false);
2262 for (int Idx : SubMask) {
2263 if (Idx != PoisonMaskElem && Idx < VF)
2264 Used.set(Idx);
2265 }
2266 if (!Used.all())
2267 return false;
2268 }
2269 return true;
2270}
2271
2272/// Return true if this shuffle mask is a replication mask.
2274 // Not possible to express a shuffle mask for a scalable vector for this
2275 // case.
2276 if (isa<ScalableVectorType>(getType()))
2277 return false;
2278 if (!isSingleSourceMask(ShuffleMask, VF))
2279 return false;
2280
2281 return isOneUseSingleSourceMask(ShuffleMask, VF);
2282}
2283
2284bool ShuffleVectorInst::isInterleave(unsigned Factor) {
2285 FixedVectorType *OpTy = dyn_cast<FixedVectorType>(getOperand(0)->getType());
2286 // shuffle_vector can only interleave fixed length vectors - for scalable
2287 // vectors, see the @llvm.vector.interleave2 intrinsic
2288 if (!OpTy)
2289 return false;
2290 unsigned OpNumElts = OpTy->getNumElements();
2291
2292 return isInterleaveMask(ShuffleMask, Factor, OpNumElts * 2);
2293}
2294
2296 ArrayRef<int> Mask, unsigned Factor, unsigned NumInputElts,
2297 SmallVectorImpl<unsigned> &StartIndexes) {
2298 unsigned NumElts = Mask.size();
2299 if (NumElts % Factor)
2300 return false;
2301
2302 unsigned LaneLen = NumElts / Factor;
2303 if (!isPowerOf2_32(LaneLen))
2304 return false;
2305
2306 StartIndexes.resize(Factor);
2307
2308 // Check whether each element matches the general interleaved rule.
2309 // Ignore undef elements, as long as the defined elements match the rule.
2310 // Outer loop processes all factors (x, y, z in the above example)
2311 unsigned I = 0, J;
2312 for (; I < Factor; I++) {
2313 unsigned SavedLaneValue;
2314 unsigned SavedNoUndefs = 0;
2315
2316 // Inner loop processes consecutive accesses (x, x+1... in the example)
2317 for (J = 0; J < LaneLen - 1; J++) {
2318 // Lane computes x's position in the Mask
2319 unsigned Lane = J * Factor + I;
2320 unsigned NextLane = Lane + Factor;
2321 int LaneValue = Mask[Lane];
2322 int NextLaneValue = Mask[NextLane];
2323
2324 // If both are defined, values must be sequential
2325 if (LaneValue >= 0 && NextLaneValue >= 0 &&
2326 LaneValue + 1 != NextLaneValue)
2327 break;
2328
2329 // If the next value is undef, save the current one as reference
2330 if (LaneValue >= 0 && NextLaneValue < 0) {
2331 SavedLaneValue = LaneValue;
2332 SavedNoUndefs = 1;
2333 }
2334
2335 // Undefs are allowed, but defined elements must still be consecutive:
2336 // i.e.: x,..., undef,..., x + 2,..., undef,..., undef,..., x + 5, ....
2337 // Verify this by storing the last non-undef followed by an undef
2338 // Check that following non-undef masks are incremented with the
2339 // corresponding distance.
2340 if (SavedNoUndefs > 0 && LaneValue < 0) {
2341 SavedNoUndefs++;
2342 if (NextLaneValue >= 0 &&
2343 SavedLaneValue + SavedNoUndefs != (unsigned)NextLaneValue)
2344 break;
2345 }
2346 }
2347
2348 if (J < LaneLen - 1)
2349 return false;
2350
2351 int StartMask = 0;
2352 if (Mask[I] >= 0) {
2353 // Check that the start of the I range (J=0) is greater than 0
2354 StartMask = Mask[I];
2355 } else if (Mask[(LaneLen - 1) * Factor + I] >= 0) {
2356 // StartMask defined by the last value in lane
2357 StartMask = Mask[(LaneLen - 1) * Factor + I] - J;
2358 } else if (SavedNoUndefs > 0) {
2359 // StartMask defined by some non-zero value in the j loop
2360 StartMask = SavedLaneValue - (LaneLen - 1 - SavedNoUndefs);
2361 }
2362 // else StartMask remains set to 0, i.e. all elements are undefs
2363
2364 if (StartMask < 0)
2365 return false;
2366 // We must stay within the vectors; This case can happen with undefs.
2367 if (StartMask + LaneLen > NumInputElts)
2368 return false;
2369
2370 StartIndexes[I] = StartMask;
2371 }
2372
2373 return true;
2374}
2375
2376/// Check if the mask is a DE-interleave mask of the given factor
2377/// \p Factor like:
2378/// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor>
2380 unsigned Factor,
2381 unsigned &Index) {
2382 // Check all potential start indices from 0 to (Factor - 1).
2383 for (unsigned Idx = 0; Idx < Factor; Idx++) {
2384 unsigned I = 0;
2385
2386 // Check that elements are in ascending order by Factor. Ignore undef
2387 // elements.
2388 for (; I < Mask.size(); I++)
2389 if (Mask[I] >= 0 && static_cast<unsigned>(Mask[I]) != Idx + I * Factor)
2390 break;
2391
2392 if (I == Mask.size()) {
2393 Index = Idx;
2394 return true;
2395 }
2396 }
2397
2398 return false;
2399}
2400
2401/// Try to lower a vector shuffle as a bit rotation.
2402///
2403/// Look for a repeated rotation pattern in each sub group.
2404/// Returns an element-wise left bit rotation amount or -1 if failed.
2405static int matchShuffleAsBitRotate(ArrayRef<int> Mask, int NumSubElts) {
2406 int NumElts = Mask.size();
2407 assert((NumElts % NumSubElts) == 0 && "Illegal shuffle mask");
2408
2409 int RotateAmt = -1;
2410 for (int i = 0; i != NumElts; i += NumSubElts) {
2411 for (int j = 0; j != NumSubElts; ++j) {
2412 int M = Mask[i + j];
2413 if (M < 0)
2414 continue;
2415 if (M < i || M >= i + NumSubElts)
2416 return -1;
2417 int Offset = (NumSubElts - (M - (i + j))) % NumSubElts;
2418 if (0 <= RotateAmt && Offset != RotateAmt)
2419 return -1;
2420 RotateAmt = Offset;
2421 }
2422 }
2423 return RotateAmt;
2424}
2425
2427 ArrayRef<int> Mask, unsigned EltSizeInBits, unsigned MinSubElts,
2428 unsigned MaxSubElts, unsigned &NumSubElts, unsigned &RotateAmt) {
2429 for (NumSubElts = MinSubElts; NumSubElts <= MaxSubElts; NumSubElts *= 2) {
2430 int EltRotateAmt = matchShuffleAsBitRotate(Mask, NumSubElts);
2431 if (EltRotateAmt < 0)
2432 continue;
2433 RotateAmt = EltRotateAmt * EltSizeInBits;
2434 return true;
2435 }
2436
2437 return false;
2438}
2439
2440//===----------------------------------------------------------------------===//
2441// InsertValueInst Class
2442//===----------------------------------------------------------------------===//
2443
2444void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2445 const Twine &Name) {
2446 assert(getNumOperands() == 2 && "NumOperands not initialized?");
2447
2448 // There's no fundamental reason why we require at least one index
2449 // (other than weirdness with &*IdxBegin being invalid; see
2450 // getelementptr's init routine for example). But there's no
2451 // present need to support it.
2452 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
2453
2455 Val->getType() && "Inserted value must match indexed type!");
2456 Op<0>() = Agg;
2457 Op<1>() = Val;
2458
2459 Indices.append(Idxs.begin(), Idxs.end());
2460 setName(Name);
2461}
2462
2463InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
2464 : Instruction(IVI.getType(), InsertValue, AllocMarker),
2465 Indices(IVI.Indices) {
2466 Op<0>() = IVI.getOperand(0);
2467 Op<1>() = IVI.getOperand(1);
2469}
2470
2471//===----------------------------------------------------------------------===//
2472// ExtractValueInst Class
2473//===----------------------------------------------------------------------===//
2474
2475void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
2476 assert(getNumOperands() == 1 && "NumOperands not initialized?");
2477
2478 // There's no fundamental reason why we require at least one index.
2479 // But there's no present need to support it.
2480 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
2481
2482 Indices.append(Idxs.begin(), Idxs.end());
2483 setName(Name);
2484}
2485
2486ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
2487 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0),
2488 (BasicBlock *)nullptr),
2489 Indices(EVI.Indices) {
2491}
2492
2493// getIndexedType - Returns the type of the element that would be extracted
2494// with an extractvalue instruction with the specified parameters.
2495//
2496// A null type is returned if the indices are invalid for the specified
2497// pointer type.
2498//
2500 ArrayRef<unsigned> Idxs) {
2501 for (unsigned Index : Idxs) {
2502 // We can't use CompositeType::indexValid(Index) here.
2503 // indexValid() always returns true for arrays because getelementptr allows
2504 // out-of-bounds indices. Since we don't allow those for extractvalue and
2505 // insertvalue we need to check array indexing manually.
2506 // Since the only other types we can index into are struct types it's just
2507 // as easy to check those manually as well.
2508 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
2509 if (Index >= AT->getNumElements())
2510 return nullptr;
2511 Agg = AT->getElementType();
2512 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
2513 if (Index >= ST->getNumElements())
2514 return nullptr;
2515 Agg = ST->getElementType(Index);
2516 } else {
2517 // Not a valid type to index into.
2518 return nullptr;
2519 }
2520 }
2521 return const_cast<Type*>(Agg);
2522}
2523
2524//===----------------------------------------------------------------------===//
2525// UnaryOperator Class
2526//===----------------------------------------------------------------------===//
2527
2529 const Twine &Name, InsertPosition InsertBefore)
2530 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2531 Op<0>() = S;
2532 setName(Name);
2533 AssertOK();
2534}
2535
2537 InsertPosition InsertBefore) {
2538 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2539}
2540
2541void UnaryOperator::AssertOK() {
2542 Value *LHS = getOperand(0);
2543 (void)LHS; // Silence warnings.
2544#ifndef NDEBUG
2545 switch (getOpcode()) {
2546 case FNeg:
2547 assert(getType() == LHS->getType() &&
2548 "Unary operation should return same type as operand!");
2549 assert(getType()->isFPOrFPVectorTy() &&
2550 "Tried to create a floating-point operation on a "
2551 "non-floating-point type!");
2552 break;
2553 default: llvm_unreachable("Invalid opcode provided");
2554 }
2555#endif
2556}
2557
2558//===----------------------------------------------------------------------===//
2559// BinaryOperator Class
2560//===----------------------------------------------------------------------===//
2561
2563 const Twine &Name, InsertPosition InsertBefore)
2564 : Instruction(Ty, iType, AllocMarker, InsertBefore) {
2565 Op<0>() = S1;
2566 Op<1>() = S2;
2567 setName(Name);
2568 AssertOK();
2569}
2570
2571void BinaryOperator::AssertOK() {
2572 Value *LHS = getOperand(0), *RHS = getOperand(1);
2573 (void)LHS; (void)RHS; // Silence warnings.
2574 assert(LHS->getType() == RHS->getType() &&
2575 "Binary operator operand types must match!");
2576#ifndef NDEBUG
2577 switch (getOpcode()) {
2578 case Add: case Sub:
2579 case Mul:
2580 assert(getType() == LHS->getType() &&
2581 "Arithmetic operation should return same type as operands!");
2582 assert(getType()->isIntOrIntVectorTy() &&
2583 "Tried to create an integer operation on a non-integer type!");
2584 break;
2585 case FAdd: case FSub:
2586 case FMul:
2587 assert(getType() == LHS->getType() &&
2588 "Arithmetic operation should return same type as operands!");
2589 assert(getType()->isFPOrFPVectorTy() &&
2590 "Tried to create a floating-point operation on a "
2591 "non-floating-point type!");
2592 break;
2593 case UDiv:
2594 case SDiv:
2595 assert(getType() == LHS->getType() &&
2596 "Arithmetic operation should return same type as operands!");
2597 assert(getType()->isIntOrIntVectorTy() &&
2598 "Incorrect operand type (not integer) for S/UDIV");
2599 break;
2600 case FDiv:
2601 assert(getType() == LHS->getType() &&
2602 "Arithmetic operation should return same type as operands!");
2603 assert(getType()->isFPOrFPVectorTy() &&
2604 "Incorrect operand type (not floating point) for FDIV");
2605 break;
2606 case URem:
2607 case SRem:
2608 assert(getType() == LHS->getType() &&
2609 "Arithmetic operation should return same type as operands!");
2610 assert(getType()->isIntOrIntVectorTy() &&
2611 "Incorrect operand type (not integer) for S/UREM");
2612 break;
2613 case FRem:
2614 assert(getType() == LHS->getType() &&
2615 "Arithmetic operation should return same type as operands!");
2616 assert(getType()->isFPOrFPVectorTy() &&
2617 "Incorrect operand type (not floating point) for FREM");
2618 break;
2619 case Shl:
2620 case LShr:
2621 case AShr:
2622 assert(getType() == LHS->getType() &&
2623 "Shift operation should return same type as operands!");
2624 assert(getType()->isIntOrIntVectorTy() &&
2625 "Tried to create a shift operation on a non-integral type!");
2626 break;
2627 case And: case Or:
2628 case Xor:
2629 assert(getType() == LHS->getType() &&
2630 "Logical operation should return same type as operands!");
2631 assert(getType()->isIntOrIntVectorTy() &&
2632 "Tried to create a logical operation on a non-integral type!");
2633 break;
2634 default: llvm_unreachable("Invalid opcode provided");
2635 }
2636#endif
2637}
2638
2640 const Twine &Name,
2641 InsertPosition InsertBefore) {
2642 assert(S1->getType() == S2->getType() &&
2643 "Cannot create binary operator with two operands of differing type!");
2644 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
2645}
2646
2648 InsertPosition InsertBefore) {
2649 Value *Zero = ConstantInt::get(Op->getType(), 0);
2650 return new BinaryOperator(Instruction::Sub, Zero, Op, Op->getType(), Name,
2651 InsertBefore);
2652}
2653
2655 InsertPosition InsertBefore) {
2656 Value *Zero = ConstantInt::get(Op->getType(), 0);
2657 return BinaryOperator::CreateNSWSub(Zero, Op, Name, InsertBefore);
2658}
2659
2661 InsertPosition InsertBefore) {
2662 Constant *C = Constant::getAllOnesValue(Op->getType());
2663 return new BinaryOperator(Instruction::Xor, Op, C,
2664 Op->getType(), Name, InsertBefore);
2665}
2666
2667// Exchange the two operands to this instruction. This instruction is safe to
2668// use on any binary instruction and does not modify the semantics of the
2669// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2670// is changed.
2672 if (!isCommutative())
2673 return true; // Can't commute operands
2674 Op<0>().swap(Op<1>());
2675 return false;
2676}
2677
2678//===----------------------------------------------------------------------===//
2679// FPMathOperator Class
2680//===----------------------------------------------------------------------===//
2681
2683 const MDNode *MD =
2684 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2685 if (!MD)
2686 return 0.0;
2687 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
2688 return Accuracy->getValueAPF().convertToFloat();
2689}
2690
2691//===----------------------------------------------------------------------===//
2692// CastInst Class
2693//===----------------------------------------------------------------------===//
2694
2695// Just determine if this cast only deals with integral->integral conversion.
2697 switch (getOpcode()) {
2698 default: return false;
2699 case Instruction::ZExt:
2700 case Instruction::SExt:
2701 case Instruction::Trunc:
2702 return true;
2703 case Instruction::BitCast:
2704 return getOperand(0)->getType()->isIntegerTy() &&
2705 getType()->isIntegerTy();
2706 }
2707}
2708
2709/// This function determines if the CastInst does not require any bits to be
2710/// changed in order to effect the cast. Essentially, it identifies cases where
2711/// no code gen is necessary for the cast, hence the name no-op cast. For
2712/// example, the following are all no-op casts:
2713/// # bitcast i32* %x to i8*
2714/// # bitcast <2 x i32> %x to <4 x i16>
2715/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
2716/// Determine if the described cast is a no-op.
2718 Type *SrcTy,
2719 Type *DestTy,
2720 const DataLayout &DL) {
2721 assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition");
2722 switch (Opcode) {
2723 default: llvm_unreachable("Invalid CastOp");
2724 case Instruction::Trunc:
2725 case Instruction::ZExt:
2726 case Instruction::SExt:
2727 case Instruction::FPTrunc:
2728 case Instruction::FPExt:
2729 case Instruction::UIToFP:
2730 case Instruction::SIToFP:
2731 case Instruction::FPToUI:
2732 case Instruction::FPToSI:
2733 case Instruction::AddrSpaceCast:
2734 // TODO: Target informations may give a more accurate answer here.
2735 return false;
2736 case Instruction::BitCast:
2737 return true; // BitCast never modifies bits.
2738 case Instruction::PtrToInt:
2739 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
2740 DestTy->getScalarSizeInBits();
2741 case Instruction::IntToPtr:
2742 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
2743 SrcTy->getScalarSizeInBits();
2744 }
2745}
2746
2748 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
2749}
2750
2751/// This function determines if a pair of casts can be eliminated and what
2752/// opcode should be used in the elimination. This assumes that there are two
2753/// instructions like this:
2754/// * %F = firstOpcode SrcTy %x to MidTy
2755/// * %S = secondOpcode MidTy %F to DstTy
2756/// The function returns a resultOpcode so these two casts can be replaced with:
2757/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2758/// If no such cast is permitted, the function returns 0.
2761 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2762 Type *DstIntPtrTy) {
2763 // Define the 144 possibilities for these two cast instructions. The values
2764 // in this matrix determine what to do in a given situation and select the
2765 // case in the switch below. The rows correspond to firstOp, the columns
2766 // correspond to secondOp. In looking at the table below, keep in mind
2767 // the following cast properties:
2768 //
2769 // Size Compare Source Destination
2770 // Operator Src ? Size Type Sign Type Sign
2771 // -------- ------------ ------------------- ---------------------
2772 // TRUNC > Integer Any Integral Any
2773 // ZEXT < Integral Unsigned Integer Any
2774 // SEXT < Integral Signed Integer Any
2775 // FPTOUI n/a FloatPt n/a Integral Unsigned
2776 // FPTOSI n/a FloatPt n/a Integral Signed
2777 // UITOFP n/a Integral Unsigned FloatPt n/a
2778 // SITOFP n/a Integral Signed FloatPt n/a
2779 // FPTRUNC > FloatPt n/a FloatPt n/a
2780 // FPEXT < FloatPt n/a FloatPt n/a
2781 // PTRTOINT n/a Pointer n/a Integral Unsigned
2782 // INTTOPTR n/a Integral Unsigned Pointer n/a
2783 // BITCAST = FirstClass n/a FirstClass n/a
2784 // ADDRSPCST n/a Pointer n/a Pointer n/a
2785 //
2786 // NOTE: some transforms are safe, but we consider them to be non-profitable.
2787 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2788 // into "fptoui double to i64", but this loses information about the range
2789 // of the produced value (we no longer know the top-part is all zeros).
2790 // Further this conversion is often much more expensive for typical hardware,
2791 // and causes issues when building libgcc. We disallow fptosi+sext for the
2792 // same reason.
2793 const unsigned numCastOps =
2794 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2795 static const uint8_t CastResults[numCastOps][numCastOps] = {
2796 // T F F U S F F P I B A -+
2797 // R Z S P P I I T P 2 N T S |
2798 // U E E 2 2 2 2 R E I T C C +- secondOp
2799 // N X X U S F F N X N 2 V V |
2800 // C T T I I P P C T T P T T -+
2801 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
2802 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
2803 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2804 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2805 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2806 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2807 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
2808 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
2809 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
2810 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2811 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2812 { 5, 5, 5, 0, 0, 5, 5, 0, 0,16, 5, 1,14}, // BitCast |
2813 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
2814 };
2815
2816 // TODO: This logic could be encoded into the table above and handled in the
2817 // switch below.
2818 // If either of the casts are a bitcast from scalar to vector, disallow the
2819 // merging. However, any pair of bitcasts are allowed.
2820 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2821 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2822 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
2823
2824 // Check if any of the casts convert scalars <-> vectors.
2825 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2826 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2827 if (!AreBothBitcasts)
2828 return 0;
2829
2830 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2831 [secondOp-Instruction::CastOpsBegin];
2832 switch (ElimCase) {
2833 case 0:
2834 // Categorically disallowed.
2835 return 0;
2836 case 1:
2837 // Allowed, use first cast's opcode.
2838 return firstOp;
2839 case 2:
2840 // Allowed, use second cast's opcode.
2841 return secondOp;
2842 case 3:
2843 // No-op cast in second op implies firstOp as long as the DestTy
2844 // is integer and we are not converting between a vector and a
2845 // non-vector type.
2846 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2847 return firstOp;
2848 return 0;
2849 case 4:
2850 // No-op cast in second op implies firstOp as long as the DestTy
2851 // matches MidTy.
2852 if (DstTy == MidTy)
2853 return firstOp;
2854 return 0;
2855 case 5:
2856 // No-op cast in first op implies secondOp as long as the SrcTy
2857 // is an integer.
2858 if (SrcTy->isIntegerTy())
2859 return secondOp;
2860 return 0;
2861 case 7: {
2862 // Disable inttoptr/ptrtoint optimization if enabled.
2863 if (DisableI2pP2iOpt)
2864 return 0;
2865
2866 // Cannot simplify if address spaces are different!
2867 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2868 return 0;
2869
2870 unsigned MidSize = MidTy->getScalarSizeInBits();
2871 // We can still fold this without knowing the actual sizes as long we
2872 // know that the intermediate pointer is the largest possible
2873 // pointer size.
2874 // FIXME: Is this always true?
2875 if (MidSize == 64)
2876 return Instruction::BitCast;
2877
2878 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
2879 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
2880 return 0;
2881 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
2882 if (MidSize >= PtrSize)
2883 return Instruction::BitCast;
2884 return 0;
2885 }
2886 case 8: {
2887 // ext, trunc -> bitcast, if the SrcTy and DstTy are the same
2888 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2889 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2890 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2891 unsigned DstSize = DstTy->getScalarSizeInBits();
2892 if (SrcTy == DstTy)
2893 return Instruction::BitCast;
2894 if (SrcSize < DstSize)
2895 return firstOp;
2896 if (SrcSize > DstSize)
2897 return secondOp;
2898 return 0;
2899 }
2900 case 9:
2901 // zext, sext -> zext, because sext can't sign extend after zext
2902 return Instruction::ZExt;
2903 case 11: {
2904 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2905 if (!MidIntPtrTy)
2906 return 0;
2907 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
2908 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2909 unsigned DstSize = DstTy->getScalarSizeInBits();
2910 if (SrcSize <= PtrSize && SrcSize == DstSize)
2911 return Instruction::BitCast;
2912 return 0;
2913 }
2914 case 12:
2915 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2916 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2917 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2918 return Instruction::AddrSpaceCast;
2919 return Instruction::BitCast;
2920 case 13:
2921 // FIXME: this state can be merged with (1), but the following assert
2922 // is useful to check the correcteness of the sequence due to semantic
2923 // change of bitcast.
2924 assert(
2925 SrcTy->isPtrOrPtrVectorTy() &&
2926 MidTy->isPtrOrPtrVectorTy() &&
2927 DstTy->isPtrOrPtrVectorTy() &&
2928 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2929 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2930 "Illegal addrspacecast, bitcast sequence!");
2931 // Allowed, use first cast's opcode
2932 return firstOp;
2933 case 14:
2934 // bitcast, addrspacecast -> addrspacecast
2935 return Instruction::AddrSpaceCast;
2936 case 15:
2937 // FIXME: this state can be merged with (1), but the following assert
2938 // is useful to check the correcteness of the sequence due to semantic
2939 // change of bitcast.
2940 assert(
2941 SrcTy->isIntOrIntVectorTy() &&
2942 MidTy->isPtrOrPtrVectorTy() &&
2943 DstTy->isPtrOrPtrVectorTy() &&
2944 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2945 "Illegal inttoptr, bitcast sequence!");
2946 // Allowed, use first cast's opcode
2947 return firstOp;
2948 case 16:
2949 // FIXME: this state can be merged with (2), but the following assert
2950 // is useful to check the correcteness of the sequence due to semantic
2951 // change of bitcast.
2952 assert(
2953 SrcTy->isPtrOrPtrVectorTy() &&
2954 MidTy->isPtrOrPtrVectorTy() &&
2955 DstTy->isIntOrIntVectorTy() &&
2956 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2957 "Illegal bitcast, ptrtoint sequence!");
2958 // Allowed, use second cast's opcode
2959 return secondOp;
2960 case 17:
2961 // (sitofp (zext x)) -> (uitofp x)
2962 return Instruction::UIToFP;
2963 case 99:
2964 // Cast combination can't happen (error in input). This is for all cases
2965 // where the MidTy is not the same for the two cast instructions.
2966 llvm_unreachable("Invalid Cast Combination");
2967 default:
2968 llvm_unreachable("Error in CastResults table!!!");
2969 }
2970}
2971
2973 const Twine &Name, InsertPosition InsertBefore) {
2974 assert(castIsValid(op, S, Ty) && "Invalid cast!");
2975 // Construct and return the appropriate CastInst subclass
2976 switch (op) {
2977 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2978 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2979 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2980 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2981 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2982 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2983 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2984 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2985 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2986 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2987 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2988 case BitCast:
2989 return new BitCastInst(S, Ty, Name, InsertBefore);
2990 case AddrSpaceCast:
2991 return new AddrSpaceCastInst(S, Ty, Name, InsertBefore);
2992 default:
2993 llvm_unreachable("Invalid opcode provided");
2994 }
2995}
2996
2998 InsertPosition InsertBefore) {
2999 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3000 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3001 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
3002}
3003
3005 InsertPosition InsertBefore) {
3006 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3007 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3008 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
3009}
3010
3012 InsertPosition InsertBefore) {
3013 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3014 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3015 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
3016}
3017
3018/// Create a BitCast or a PtrToInt cast instruction
3020 InsertPosition InsertBefore) {
3021 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3022 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
3023 "Invalid cast");
3024 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
3025 assert((!Ty->isVectorTy() ||
3026 cast<VectorType>(Ty)->getElementCount() ==
3027 cast<VectorType>(S->getType())->getElementCount()) &&
3028 "Invalid cast");
3029
3030 if (Ty->isIntOrIntVectorTy())
3031 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
3032
3033 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
3034}
3035
3037 Value *S, Type *Ty, const Twine &Name, InsertPosition InsertBefore) {
3038 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3039 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
3040
3042 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
3043
3044 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3045}
3046
3048 const Twine &Name,
3049 InsertPosition InsertBefore) {
3050 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
3051 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
3052 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
3053 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
3054
3055 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3056}
3057
3059 const Twine &Name,
3060 InsertPosition InsertBefore) {
3061 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
3062 "Invalid integer cast");
3063 unsigned SrcBits = C->getType()->getScalarSizeInBits();
3064 unsigned DstBits = Ty->getScalarSizeInBits();
3065 Instruction::CastOps opcode =
3066 (SrcBits == DstBits ? Instruction::BitCast :
3067 (SrcBits > DstBits ? Instruction::Trunc :
3068 (isSigned ? Instruction::SExt : Instruction::ZExt)));
3069 return Create(opcode, C, Ty, Name, InsertBefore);
3070}
3071
3073 InsertPosition InsertBefore) {
3074 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
3075 "Invalid cast");
3076 unsigned SrcBits = C->getType()->getScalarSizeInBits();
3077 unsigned DstBits = Ty->getScalarSizeInBits();
3078 assert((C->getType() == Ty || SrcBits != DstBits) && "Invalid cast");
3079 Instruction::CastOps opcode =
3080 (SrcBits == DstBits ? Instruction::BitCast :
3081 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
3082 return Create(opcode, C, Ty, Name, InsertBefore);
3083}
3084
3085bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
3086 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
3087 return false;
3088
3089 if (SrcTy == DestTy)
3090 return true;
3091
3092 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3093 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
3094 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3095 // An element by element cast. Valid if casting the elements is valid.
3096 SrcTy = SrcVecTy->getElementType();
3097 DestTy = DestVecTy->getElementType();
3098 }
3099 }
3100 }
3101
3102 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
3103 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
3104 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
3105 }
3106 }
3107
3108 TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3109 TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
3110
3111 // Could still have vectors of pointers if the number of elements doesn't
3112 // match
3113 if (SrcBits.getKnownMinValue() == 0 || DestBits.getKnownMinValue() == 0)
3114 return false;
3115
3116 if (SrcBits != DestBits)
3117 return false;
3118
3119 return true;
3120}
3121
3123 const DataLayout &DL) {
3124 // ptrtoint and inttoptr are not allowed on non-integral pointers
3125 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3126 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
3127 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3128 !DL.isNonIntegralPointerType(PtrTy));
3129 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3130 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
3131 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3132 !DL.isNonIntegralPointerType(PtrTy));
3133
3134 return isBitCastable(SrcTy, DestTy);
3135}
3136
3137// Provide a way to get a "cast" where the cast opcode is inferred from the
3138// types and size of the operand. This, basically, is a parallel of the
3139// logic in the castIsValid function below. This axiom should hold:
3140// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3141// should not assert in castIsValid. In other words, this produces a "correct"
3142// casting opcode for the arguments passed to it.
3145 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3146 Type *SrcTy = Src->getType();
3147
3148 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3149 "Only first class types are castable!");
3150
3151 if (SrcTy == DestTy)
3152 return BitCast;
3153
3154 // FIXME: Check address space sizes here
3155 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3156 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
3157 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3158 // An element by element cast. Find the appropriate opcode based on the
3159 // element types.
3160 SrcTy = SrcVecTy->getElementType();
3161 DestTy = DestVecTy->getElementType();
3162 }
3163
3164 // Get the bit sizes, we'll need these
3165 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3166 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
3167
3168 // Run through the possibilities ...
3169 if (DestTy->isIntegerTy()) { // Casting to integral
3170 if (SrcTy->isIntegerTy()) { // Casting from integral
3171 if (DestBits < SrcBits)
3172 return Trunc; // int -> smaller int
3173 else if (DestBits > SrcBits) { // its an extension
3174 if (SrcIsSigned)
3175 return SExt; // signed -> SEXT
3176 else
3177 return ZExt; // unsigned -> ZEXT
3178 } else {
3179 return BitCast; // Same size, No-op cast
3180 }
3181 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
3182 if (DestIsSigned)
3183 return FPToSI; // FP -> sint
3184 else
3185 return FPToUI; // FP -> uint
3186 } else if (SrcTy->isVectorTy()) {
3187 assert(DestBits == SrcBits &&
3188 "Casting vector to integer of different width");
3189 return BitCast; // Same size, no-op cast
3190 } else {
3191 assert(SrcTy->isPointerTy() &&
3192 "Casting from a value that is not first-class type");
3193 return PtrToInt; // ptr -> int
3194 }
3195 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3196 if (SrcTy->isIntegerTy()) { // Casting from integral
3197 if (SrcIsSigned)
3198 return SIToFP; // sint -> FP
3199 else
3200 return UIToFP; // uint -> FP
3201 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
3202 if (DestBits < SrcBits) {
3203 return FPTrunc; // FP -> smaller FP
3204 } else if (DestBits > SrcBits) {
3205 return FPExt; // FP -> larger FP
3206 } else {
3207 return BitCast; // same size, no-op cast
3208 }
3209 } else if (SrcTy->isVectorTy()) {
3210 assert(DestBits == SrcBits &&
3211 "Casting vector to floating point of different width");
3212 return BitCast; // same size, no-op cast
3213 }
3214 llvm_unreachable("Casting pointer or non-first class to float");
3215 } else if (DestTy->isVectorTy()) {
3216 assert(DestBits == SrcBits &&
3217 "Illegal cast to vector (wrong type or size)");
3218 return BitCast;
3219 } else if (DestTy->isPointerTy()) {
3220 if (SrcTy->isPointerTy()) {
3221 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3222 return AddrSpaceCast;
3223 return BitCast; // ptr -> ptr
3224 } else if (SrcTy->isIntegerTy()) {
3225 return IntToPtr; // int -> ptr
3226 }
3227 llvm_unreachable("Casting pointer to other than pointer or int");
3228 }
3229 llvm_unreachable("Casting to type that is not first-class");
3230}
3231
3232//===----------------------------------------------------------------------===//
3233// CastInst SubClass Constructors
3234//===----------------------------------------------------------------------===//
3235
3236/// Check that the construction parameters for a CastInst are correct. This
3237/// could be broken out into the separate constructors but it is useful to have
3238/// it in one place and to eliminate the redundant code for getting the sizes
3239/// of the types involved.
3240bool
3242 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3243 SrcTy->isAggregateType() || DstTy->isAggregateType())
3244 return false;
3245
3246 // Get the size of the types in bits, and whether we are dealing
3247 // with vector types, we'll need this later.
3248 bool SrcIsVec = isa<VectorType>(SrcTy);
3249 bool DstIsVec = isa<VectorType>(DstTy);
3250 unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits();
3251 unsigned DstScalarBitSize = DstTy->getScalarSizeInBits();
3252
3253 // If these are vector types, get the lengths of the vectors (using zero for
3254 // scalar types means that checking that vector lengths match also checks that
3255 // scalars are not being converted to vectors or vectors to scalars).
3256 ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount()
3258 ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount()
3260
3261 // Switch on the opcode provided
3262 switch (op) {
3263 default: return false; // This is an input error
3264 case Instruction::Trunc:
3265 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3266 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3267 case Instruction::ZExt:
3268 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3269 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3270 case Instruction::SExt:
3271 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3272 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3273 case Instruction::FPTrunc:
3274 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3275 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3276 case Instruction::FPExt:
3277 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3278 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3279 case Instruction::UIToFP:
3280 case Instruction::SIToFP:
3281 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3282 SrcEC == DstEC;
3283 case Instruction::FPToUI:
3284 case Instruction::FPToSI:
3285 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3286 SrcEC == DstEC;
3287 case Instruction::PtrToInt:
3288 if (SrcEC != DstEC)
3289 return false;
3290 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
3291 case Instruction::IntToPtr:
3292 if (SrcEC != DstEC)
3293 return false;
3294 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
3295 case Instruction::BitCast: {
3296 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3297 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3298
3299 // BitCast implies a no-op cast of type only. No bits change.
3300 // However, you can't cast pointers to anything but pointers.
3301 if (!SrcPtrTy != !DstPtrTy)
3302 return false;
3303
3304 // For non-pointer cases, the cast is okay if the source and destination bit
3305 // widths are identical.
3306 if (!SrcPtrTy)
3307 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3308
3309 // If both are pointers then the address spaces must match.
3310 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3311 return false;
3312
3313 // A vector of pointers must have the same number of elements.
3314 if (SrcIsVec && DstIsVec)
3315 return SrcEC == DstEC;
3316 if (SrcIsVec)
3317 return SrcEC == ElementCount::getFixed(1);
3318 if (DstIsVec)
3319 return DstEC == ElementCount::getFixed(1);
3320
3321 return true;
3322 }
3323 case Instruction::AddrSpaceCast: {
3324 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3325 if (!SrcPtrTy)
3326 return false;
3327
3328 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3329 if (!DstPtrTy)
3330 return false;
3331
3332 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3333 return false;
3334
3335 return SrcEC == DstEC;
3336 }
3337 }
3338}
3339
3341 InsertPosition InsertBefore)
3342 : CastInst(Ty, Trunc, S, Name, InsertBefore) {
3343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
3344}
3345
3347 InsertPosition InsertBefore)
3348 : CastInst(Ty, ZExt, S, Name, InsertBefore) {
3349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
3350}
3351
3353 InsertPosition InsertBefore)
3354 : CastInst(Ty, SExt, S, Name, InsertBefore) {
3355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
3356}
3357
3359 InsertPosition InsertBefore)
3360 : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
3361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
3362}
3363
3365 InsertPosition InsertBefore)
3366 : CastInst(Ty, FPExt, S, Name, InsertBefore) {
3367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
3368}
3369
3371 InsertPosition InsertBefore)
3372 : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
3373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
3374}
3375
3377 InsertPosition InsertBefore)
3378 : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
3379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
3380}
3381
3383 InsertPosition InsertBefore)
3384 : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
3385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
3386}
3387
3389 InsertPosition InsertBefore)
3390 : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
3391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
3392}
3393
3395 InsertPosition InsertBefore)
3396 : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
3397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
3398}
3399
3401 InsertPosition InsertBefore)
3402 : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
3403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
3404}
3405
3407 InsertPosition InsertBefore)
3408 : CastInst(Ty, BitCast, S, Name, InsertBefore) {
3409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
3410}
3411
3413 InsertPosition InsertBefore)
3414 : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3416}
3417
3418//===----------------------------------------------------------------------===//
3419// CmpInst Classes
3420//===----------------------------------------------------------------------===//
3421
3423 Value *RHS, const Twine &Name, InsertPosition InsertBefore,
3424 Instruction *FlagsSource)
3425 : Instruction(ty, op, AllocMarker, InsertBefore) {
3426 Op<0>() = LHS;
3427 Op<1>() = RHS;
3428 setPredicate((Predicate)predicate);
3429 setName(Name);
3430 if (FlagsSource)
3431 copyIRFlags(FlagsSource);
3432}
3433
3435 const Twine &Name, InsertPosition InsertBefore) {
3436 if (Op == Instruction::ICmp) {
3437 if (InsertBefore.isValid())
3438 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3439 S1, S2, Name);
3440 else
3441 return new ICmpInst(CmpInst::Predicate(predicate),
3442 S1, S2, Name);
3443 }
3444
3445 if (InsertBefore.isValid())
3446 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3447 S1, S2, Name);
3448 else
3449 return new FCmpInst(CmpInst::Predicate(predicate),
3450 S1, S2, Name);
3451}
3452
3454 Value *S2,
3455 const Instruction *FlagsSource,
3456 const Twine &Name,
3457 InsertPosition InsertBefore) {
3458 CmpInst *Inst = Create(Op, Pred, S1, S2, Name, InsertBefore);
3459 Inst->copyIRFlags(FlagsSource);
3460 return Inst;
3461}
3462
3464 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3465 IC->swapOperands();
3466 else
3467 cast<FCmpInst>(this)->swapOperands();
3468}
3469
3471 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
3472 return IC->isCommutative();
3473 return cast<FCmpInst>(this)->isCommutative();
3474}
3475
3478 return ICmpInst::isEquality(P);
3480 return FCmpInst::isEquality(P);
3481 llvm_unreachable("Unsupported predicate kind");
3482}
3483
3484// Returns true if either operand of CmpInst is a provably non-zero
3485// floating-point constant.
3486static bool hasNonZeroFPOperands(const CmpInst *Cmp) {
3487 auto *LHS = dyn_cast<Constant>(Cmp->getOperand(0));
3488 auto *RHS = dyn_cast<Constant>(Cmp->getOperand(1));
3489 if (auto *Const = LHS ? LHS : RHS) {
3490 using namespace llvm::PatternMatch;
3491 return match(Const, m_NonZeroNotDenormalFP());
3492 }
3493 return false;
3494}
3495
3496// Floating-point equality is not an equivalence when comparing +0.0 with
3497// -0.0, when comparing NaN with another value, or when flushing
3498// denormals-to-zero.
3499bool CmpInst::isEquivalence(bool Invert) const {
3500 switch (Invert ? getInversePredicate() : getPredicate()) {
3502 return true;
3504 if (!hasNoNaNs())
3505 return false;
3506 [[fallthrough]];
3508 return hasNonZeroFPOperands(this);
3509 default:
3510 return false;
3511 }
3512}
3513
3515 switch (pred) {
3516 default: llvm_unreachable("Unknown cmp predicate!");
3517 case ICMP_EQ: return ICMP_NE;
3518 case ICMP_NE: return ICMP_EQ;
3519 case ICMP_UGT: return ICMP_ULE;
3520 case ICMP_ULT: return ICMP_UGE;
3521 case ICMP_UGE: return ICMP_ULT;
3522 case ICMP_ULE: return ICMP_UGT;
3523 case ICMP_SGT: return ICMP_SLE;
3524 case ICMP_SLT: return ICMP_SGE;
3525 case ICMP_SGE: return ICMP_SLT;
3526 case ICMP_SLE: return ICMP_SGT;
3527
3528 case FCMP_OEQ: return FCMP_UNE;
3529 case FCMP_ONE: return FCMP_UEQ;
3530 case FCMP_OGT: return FCMP_ULE;
3531 case FCMP_OLT: return FCMP_UGE;
3532 case FCMP_OGE: return FCMP_ULT;
3533 case FCMP_OLE: return FCMP_UGT;
3534 case FCMP_UEQ: return FCMP_ONE;
3535 case FCMP_UNE: return FCMP_OEQ;
3536 case FCMP_UGT: return FCMP_OLE;
3537 case FCMP_ULT: return FCMP_OGE;
3538 case FCMP_UGE: return FCMP_OLT;
3539 case FCMP_ULE: return FCMP_OGT;
3540 case FCMP_ORD: return FCMP_UNO;
3541 case FCMP_UNO: return FCMP_ORD;
3542 case FCMP_TRUE: return FCMP_FALSE;
3543 case FCMP_FALSE: return FCMP_TRUE;
3544 }
3545}
3546
3548 switch (Pred) {
3549 default: return "unknown";
3550 case FCmpInst::FCMP_FALSE: return "false";
3551 case FCmpInst::FCMP_OEQ: return "oeq";
3552 case FCmpInst::FCMP_OGT: return "ogt";
3553 case FCmpInst::FCMP_OGE: return "oge";
3554 case FCmpInst::FCMP_OLT: return "olt";
3555 case FCmpInst::FCMP_OLE: return "ole";
3556 case FCmpInst::FCMP_ONE: return "one";
3557 case FCmpInst::FCMP_ORD: return "ord";
3558 case FCmpInst::FCMP_UNO: return "uno";
3559 case FCmpInst::FCMP_UEQ: return "ueq";
3560 case FCmpInst::FCMP_UGT: return "ugt";
3561 case FCmpInst::FCMP_UGE: return "uge";
3562 case FCmpInst::FCMP_ULT: return "ult";
3563 case FCmpInst::FCMP_ULE: return "ule";
3564 case FCmpInst::FCMP_UNE: return "une";
3565 case FCmpInst::FCMP_TRUE: return "true";
3566 case ICmpInst::ICMP_EQ: return "eq";
3567 case ICmpInst::ICMP_NE: return "ne";
3568 case ICmpInst::ICMP_SGT: return "sgt";
3569 case ICmpInst::ICMP_SGE: return "sge";
3570 case ICmpInst::ICMP_SLT: return "slt";
3571 case ICmpInst::ICMP_SLE: return "sle";
3572 case ICmpInst::ICMP_UGT: return "ugt";
3573 case ICmpInst::ICMP_UGE: return "uge";
3574 case ICmpInst::ICMP_ULT: return "ult";
3575 case ICmpInst::ICMP_ULE: return "ule";
3576 }
3577}
3578
3581 return OS;
3582}
3583
3585 switch (pred) {
3586 default: llvm_unreachable("Unknown icmp predicate!");
3587 case ICMP_EQ: case ICMP_NE:
3588 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3589 return pred;
3590 case ICMP_UGT: return ICMP_SGT;
3591 case ICMP_ULT: return ICMP_SLT;
3592 case ICMP_UGE: return ICMP_SGE;
3593 case ICMP_ULE: return ICMP_SLE;
3594 }
3595}
3596
3598 switch (pred) {
3599 default: llvm_unreachable("Unknown icmp predicate!");
3600 case ICMP_EQ: case ICMP_NE:
3601 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3602 return pred;
3603 case ICMP_SGT: return ICMP_UGT;
3604 case ICMP_SLT: return ICMP_ULT;
3605 case ICMP_SGE: return ICMP_UGE;
3606 case ICMP_SLE: return ICMP_ULE;
3607 }
3608}
3609
3611 switch (pred) {
3612 default: llvm_unreachable("Unknown cmp predicate!");
3613 case ICMP_EQ: case ICMP_NE:
3614 return pred;
3615 case ICMP_SGT: return ICMP_SLT;
3616 case ICMP_SLT: return ICMP_SGT;
3617 case ICMP_SGE: return ICMP_SLE;
3618 case ICMP_SLE: return ICMP_SGE;
3619 case ICMP_UGT: return ICMP_ULT;
3620 case ICMP_ULT: return ICMP_UGT;
3621 case ICMP_UGE: return ICMP_ULE;
3622 case ICMP_ULE: return ICMP_UGE;
3623
3624 case FCMP_FALSE: case FCMP_TRUE:
3625 case FCMP_OEQ: case FCMP_ONE:
3626 case FCMP_UEQ: case FCMP_UNE:
3627 case FCMP_ORD: case FCMP_UNO:
3628 return pred;
3629 case FCMP_OGT: return FCMP_OLT;
3630 case FCMP_OLT: return FCMP_OGT;
3631 case FCMP_OGE: return FCMP_OLE;
3632 case FCMP_OLE: return FCMP_OGE;
3633 case FCMP_UGT: return FCMP_ULT;
3634 case FCMP_ULT: return FCMP_UGT;
3635 case FCMP_UGE: return FCMP_ULE;
3636 case FCMP_ULE: return FCMP_UGE;
3637 }
3638}
3639
3641 switch (pred) {
3642 case ICMP_SGE:
3643 case ICMP_SLE:
3644 case ICMP_UGE:
3645 case ICMP_ULE:
3646 case FCMP_OGE:
3647 case FCMP_OLE:
3648 case FCMP_UGE:
3649 case FCMP_ULE:
3650 return true;
3651 default:
3652 return false;
3653 }
3654}
3655
3657 switch (pred) {
3658 case ICMP_SGT:
3659 case ICMP_SLT:
3660 case ICMP_UGT:
3661 case ICMP_ULT:
3662 case FCMP_OGT:
3663 case FCMP_OLT:
3664 case FCMP_UGT:
3665 case FCMP_ULT:
3666 return true;
3667 default:
3668 return false;
3669 }
3670}
3671
3673 switch (pred) {
3674 case ICMP_SGE:
3675 return ICMP_SGT;
3676 case ICMP_SLE:
3677 return ICMP_SLT;
3678 case ICMP_UGE:
3679 return ICMP_UGT;
3680 case ICMP_ULE:
3681 return ICMP_ULT;
3682 case FCMP_OGE:
3683 return FCMP_OGT;
3684 case FCMP_OLE:
3685 return FCMP_OLT;
3686 case FCMP_UGE:
3687 return FCMP_UGT;
3688 case FCMP_ULE:
3689 return FCMP_ULT;
3690 default:
3691 return pred;
3692 }
3693}
3694
3696 switch (pred) {
3697 case ICMP_SGT:
3698 return ICMP_SGE;
3699 case ICMP_SLT:
3700 return ICMP_SLE;
3701 case ICMP_UGT:
3702 return ICMP_UGE;
3703 case ICMP_ULT:
3704 return ICMP_ULE;
3705 case FCMP_OGT:
3706 return FCMP_OGE;
3707 case FCMP_OLT:
3708 return FCMP_OLE;
3709 case FCMP_UGT:
3710 return FCMP_UGE;
3711 case FCMP_ULT:
3712 return FCMP_ULE;
3713 default:
3714 return pred;
3715 }
3716}
3717
3719 assert(CmpInst::isRelational(pred) && "Call only with relational predicate!");
3720
3724 return getStrictPredicate(pred);
3725
3726 llvm_unreachable("Unknown predicate!");
3727}
3728
3730 switch (predicate) {
3731 default: return false;
3733 case ICmpInst::ICMP_UGE: return true;
3734 }
3735}
3736
3738 switch (predicate) {
3739 default: return false;
3741 case ICmpInst::ICMP_SGE: return true;
3742 }
3743}
3744
3745bool ICmpInst::compare(const APInt &LHS, const APInt &RHS,
3746 ICmpInst::Predicate Pred) {
3747 assert(ICmpInst::isIntPredicate(Pred) && "Only for integer predicates!");
3748 switch (Pred) {
3750 return LHS.eq(RHS);
3752 return LHS.ne(RHS);
3754 return LHS.ugt(RHS);
3756 return LHS.uge(RHS);
3758 return LHS.ult(RHS);
3760 return LHS.ule(RHS);
3762 return LHS.sgt(RHS);
3764 return LHS.sge(RHS);
3766 return LHS.slt(RHS);
3768 return LHS.sle(RHS);
3769 default:
3770 llvm_unreachable("Unexpected non-integer predicate.");
3771 };
3772}
3773
3774bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS,
3775 FCmpInst::Predicate Pred) {
3776 APFloat::cmpResult R = LHS.compare(RHS);
3777 switch (Pred) {
3778 default:
3779 llvm_unreachable("Invalid FCmp Predicate");
3781 return false;
3783 return true;
3784 case FCmpInst::FCMP_UNO:
3785 return R == APFloat::cmpUnordered;
3786 case FCmpInst::FCMP_ORD:
3787 return R != APFloat::cmpUnordered;
3788 case FCmpInst::FCMP_UEQ:
3789 return R == APFloat::cmpUnordered || R == APFloat::cmpEqual;
3790 case FCmpInst::FCMP_OEQ:
3791 return R == APFloat::cmpEqual;
3792 case FCmpInst::FCMP_UNE:
3793 return R != APFloat::cmpEqual;
3794 case FCmpInst::FCMP_ONE:
3796 case FCmpInst::FCMP_ULT:
3797 return R == APFloat::cmpUnordered || R == APFloat::cmpLessThan;
3798 case FCmpInst::FCMP_OLT:
3799 return R == APFloat::cmpLessThan;
3800 case FCmpInst::FCMP_UGT:
3802 case FCmpInst::FCMP_OGT:
3803 return R == APFloat::cmpGreaterThan;
3804 case FCmpInst::FCMP_ULE:
3805 return R != APFloat::cmpGreaterThan;
3806 case FCmpInst::FCMP_OLE:
3807 return R == APFloat::cmpLessThan || R == APFloat::cmpEqual;
3808 case FCmpInst::FCMP_UGE:
3809 return R != APFloat::cmpLessThan;
3810 case FCmpInst::FCMP_OGE:
3811 return R == APFloat::cmpGreaterThan || R == APFloat::cmpEqual;
3812 }
3813}
3814
3815std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
3816 const KnownBits &RHS,
3817 ICmpInst::Predicate Pred) {
3818 switch (Pred) {
3819 case ICmpInst::ICMP_EQ:
3820 return KnownBits::eq(LHS, RHS);
3821 case ICmpInst::ICMP_NE:
3822 return KnownBits::ne(LHS, RHS);
3823 case ICmpInst::ICMP_UGE:
3824 return KnownBits::uge(LHS, RHS);
3825 case ICmpInst::ICMP_UGT:
3826 return KnownBits::ugt(LHS, RHS);
3827 case ICmpInst::ICMP_ULE:
3828 return KnownBits::ule(LHS, RHS);
3829 case ICmpInst::ICMP_ULT:
3830 return KnownBits::ult(LHS, RHS);
3831 case ICmpInst::ICMP_SGE:
3832 return KnownBits::sge(LHS, RHS);
3833 case ICmpInst::ICMP_SGT:
3834 return KnownBits::sgt(LHS, RHS);
3835 case ICmpInst::ICMP_SLE:
3836 return KnownBits::sle(LHS, RHS);
3837 case ICmpInst::ICMP_SLT:
3838 return KnownBits::slt(LHS, RHS);
3839 default:
3840 llvm_unreachable("Unexpected non-integer predicate.");
3841 }
3842}
3843
3846 return pred;
3847 if (isSigned(pred))
3848 return getUnsignedPredicate(pred);
3849 if (isUnsigned(pred))
3850 return getSignedPredicate(pred);
3851
3852 llvm_unreachable("Unknown predicate!");
3853}
3854
3856 switch (predicate) {
3857 default: return false;
3860 case FCmpInst::FCMP_ORD: return true;
3861 }
3862}
3863
3865 switch (predicate) {
3866 default: return false;
3869 case FCmpInst::FCMP_UNO: return true;
3870 }
3871}
3872
3874 switch(predicate) {
3875 default: return false;
3876 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3877 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3878 }
3879}
3880
3882 switch(predicate) {
3883 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3884 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3885 default: return false;
3886 }
3887}
3888
3890 // If the predicates match, then we know the first condition implies the
3891 // second is true.
3892 if (Pred1 == Pred2)
3893 return true;
3894
3895 switch (Pred1) {
3896 default:
3897 break;
3898 case ICMP_EQ:
3899 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
3900 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3901 Pred2 == ICMP_SLE;
3902 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3903 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3904 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3905 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3906 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3907 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3908 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3909 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
3910 }
3911 return false;
3912}
3913
3915 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
3916}
3917
3918//===----------------------------------------------------------------------===//
3919// CmpPredicate Implementation
3920//===----------------------------------------------------------------------===//
3921
3922std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
3923 CmpPredicate B) {
3924 if (A.Pred == B.Pred)
3925 return A.HasSameSign == B.HasSameSign ? A : CmpPredicate(A.Pred);
3926 if (A.HasSameSign &&
3928 return B.Pred;
3929 if (B.HasSameSign &&
3931 return A.Pred;
3932 return {};
3933}
3934
3936 if (auto *ICI = dyn_cast<ICmpInst>(Cmp))
3937 return ICI->getCmpPredicate();
3938 return Cmp->getPredicate();
3939}
3940
3942 return {CmpInst::getSwappedPredicate(P), P.hasSameSign()};
3943}
3944
3946 return getSwapped(get(Cmp));
3947}
3948
3949//===----------------------------------------------------------------------===//
3950// SwitchInst Implementation
3951//===----------------------------------------------------------------------===//
3952
3953void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3954 assert(Value && Default && NumReserved);
3955 ReservedSpace = NumReserved;
3957 allocHungoffUses(ReservedSpace);
3958
3959 Op<0>() = Value;
3960 Op<1>() = Default;
3961}
3962
3963/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3964/// switch on and a default destination. The number of additional cases can
3965/// be specified here to make memory allocation more efficient. This
3966/// constructor can also autoinsert before another instruction.
3967SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3968 InsertPosition InsertBefore)
3969 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3970 AllocMarker, InsertBefore) {
3971 init(Value, Default, 2+NumCases*2);
3972}
3973
3974SwitchInst::SwitchInst(const SwitchInst &SI)
3975 : Instruction(SI.getType(), Instruction::Switch, AllocMarker) {
3976 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3977 setNumHungOffUseOperands(SI.getNumOperands());
3978 Use *OL = getOperandList();
3979 const Use *InOL = SI.getOperandList();
3980 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
3981 OL[i] = InOL[i];
3982 OL[i+1] = InOL[i+1];
3983 }
3984 SubclassOptionalData = SI.SubclassOptionalData;
3985}
3986
3987/// addCase - Add an entry to the switch instruction...
3988///
3990 unsigned NewCaseIdx = getNumCases();
3991 unsigned OpNo = getNumOperands();
3992 if (OpNo+2 > ReservedSpace)
3993 growOperands(); // Get more space!
3994 // Initialize some new operands.
3995 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3997 CaseHandle Case(this, NewCaseIdx);
3998 Case.setValue(OnVal);
3999 Case.setSuccessor(Dest);
4000}
4001
4002/// removeCase - This method removes the specified case and its successor
4003/// from the switch instruction.
4005 unsigned idx = I->getCaseIndex();
4006
4007 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
4008
4009 unsigned NumOps = getNumOperands();
4010 Use *OL = getOperandList();
4011
4012 // Overwrite this case with the end of the list.
4013 if (2 + (idx + 1) * 2 != NumOps) {
4014 OL[2 + idx * 2] = OL[NumOps - 2];
4015 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
4016 }
4017
4018 // Nuke the last value.
4019 OL[NumOps-2].set(nullptr);
4020 OL[NumOps-2+1].set(nullptr);
4021 setNumHungOffUseOperands(NumOps-2);
4022
4023 return CaseIt(this, idx);
4024}
4025
4026/// growOperands - grow operands - This grows the operand list in response
4027/// to a push_back style of operation. This grows the number of ops by 3 times.
4028///
4029void SwitchInst::growOperands() {
4030 unsigned e = getNumOperands();
4031 unsigned NumOps = e*3;
4032
4033 ReservedSpace = NumOps;
4034 growHungoffUses(ReservedSpace);
4035}
4036
4038 assert(Changed && "called only if metadata has changed");
4039
4040 if (!Weights)
4041 return nullptr;
4042
4043 assert(SI.getNumSuccessors() == Weights->size() &&
4044 "num of prof branch_weights must accord with num of successors");
4045
4046 bool AllZeroes = all_of(*Weights, [](uint32_t W) { return W == 0; });
4047
4048 if (AllZeroes || Weights->size() < 2)
4049 return nullptr;
4050
4051 return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights);
4052}
4053
4055 MDNode *ProfileData = getBranchWeightMDNode(SI);
4056 if (!ProfileData)
4057 return;
4058
4059 if (getNumBranchWeights(*ProfileData) != SI.getNumSuccessors()) {
4060 llvm_unreachable("number of prof branch_weights metadata operands does "
4061 "not correspond to number of succesors");
4062 }
4063
4065 if (!extractBranchWeights(ProfileData, Weights))
4066 return;
4067 this->Weights = std::move(Weights);
4068}
4069
4072 if (Weights) {
4073 assert(SI.getNumSuccessors() == Weights->size() &&
4074 "num of prof branch_weights must accord with num of successors");
4075 Changed = true;
4076 // Copy the last case to the place of the removed one and shrink.
4077 // This is tightly coupled with the way SwitchInst::removeCase() removes
4078 // the cases in SwitchInst::removeCase(CaseIt).
4079 (*Weights)[I->getCaseIndex() + 1] = Weights->back();
4080 Weights->pop_back();
4081 }
4082 return SI.removeCase(I);
4083}
4084
4086 ConstantInt *OnVal, BasicBlock *Dest,
4088 SI.addCase(OnVal, Dest);
4089
4090 if (!Weights && W && *W) {
4091 Changed = true;
4092 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0);
4093 (*Weights)[SI.getNumSuccessors() - 1] = *W;
4094 } else if (Weights) {
4095 Changed = true;
4096 Weights->push_back(W.value_or(0));
4097 }
4098 if (Weights)
4099 assert(SI.getNumSuccessors() == Weights->size() &&
4100 "num of prof branch_weights must accord with num of successors");
4101}
4102
4105 // Instruction is erased. Mark as unchanged to not touch it in the destructor.
4106 Changed = false;
4107 if (Weights)
4108 Weights->resize(0);
4109 return SI.eraseFromParent();
4110}
4111
4114 if (!Weights)
4115 return std::nullopt;
4116 return (*Weights)[idx];
4117}
4118
4121 if (!W)
4122 return;
4123
4124 if (!Weights && *W)
4125 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0);
4126
4127 if (Weights) {
4128 auto &OldW = (*Weights)[idx];
4129 if (*W != OldW) {
4130 Changed = true;
4131 OldW = *W;
4132 }
4133 }
4134}
4135
4138 unsigned idx) {
4139 if (MDNode *ProfileData = getBranchWeightMDNode(SI))
4140 if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1)
4141 return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1))
4142 ->getValue()
4143 .getZExtValue();
4144
4145 return std::nullopt;
4146}
4147
4148//===----------------------------------------------------------------------===//
4149// IndirectBrInst Implementation
4150//===----------------------------------------------------------------------===//
4151
4152void IndirectBrInst::init(Value *Address, unsigned NumDests) {
4153 assert(Address && Address->getType()->isPointerTy() &&
4154 "Address of indirectbr must be a pointer");
4155 ReservedSpace = 1+NumDests;
4157 allocHungoffUses(ReservedSpace);
4158
4159 Op<0>() = Address;
4160}
4161
4162
4163/// growOperands - grow operands - This grows the operand list in response
4164/// to a push_back style of operation. This grows the number of ops by 2 times.
4165///
4166void IndirectBrInst::growOperands() {
4167 unsigned e = getNumOperands();
4168 unsigned NumOps = e*2;
4169
4170 ReservedSpace = NumOps;
4171 growHungoffUses(ReservedSpace);
4172}
4173
4174IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
4175 InsertPosition InsertBefore)
4176 : Instruction(Type::getVoidTy(Address->getContext()),
4177 Instruction::IndirectBr, AllocMarker, InsertBefore) {
4178 init(Address, NumCases);
4179}
4180
4181IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
4182 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
4183 AllocMarker) {
4184 NumUserOperands = IBI.NumUserOperands;
4185 allocHungoffUses(IBI.getNumOperands());
4186 Use *OL = getOperandList();
4187 const Use *InOL = IBI.getOperandList();
4188 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
4189 OL[i] = InOL[i];
4190 SubclassOptionalData = IBI.SubclassOptionalData;
4191}
4192
4193/// addDestination - Add a destination.
4194///
4196 unsigned OpNo = getNumOperands();
4197 if (OpNo+1 > ReservedSpace)
4198 growOperands(); // Get more space!
4199 // Initialize some new operands.
4200 assert(OpNo < ReservedSpace && "Growing didn't work!");
4202 getOperandList()[OpNo] = DestBB;
4203}
4204
4205/// removeDestination - This method removes the specified successor from the
4206/// indirectbr instruction.
4208 assert(idx < getNumOperands()-1 && "Successor index out of range!");
4209
4210 unsigned NumOps = getNumOperands();
4211 Use *OL = getOperandList();
4212
4213 // Replace this value with the last one.
4214 OL[idx+1] = OL[NumOps-1];
4215
4216 // Nuke the last value.
4217 OL[NumOps-1].set(nullptr);
4218 setNumHungOffUseOperands(NumOps-1);
4219}
4220
4221//===----------------------------------------------------------------------===//
4222// FreezeInst Implementation
4223//===----------------------------------------------------------------------===//
4224
4226 : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) {
4227 setName(Name);
4228}
4229
4230//===----------------------------------------------------------------------===//
4231// cloneImpl() implementations
4232//===----------------------------------------------------------------------===//
4233
4234// Define these methods here so vtables don't get emitted into every translation
4235// unit that uses these classes.
4236
4239 return new (AllocMarker) GetElementPtrInst(*this, AllocMarker);
4240}
4241
4243 return Create(getOpcode(), Op<0>());
4244}
4245
4247 return Create(getOpcode(), Op<0>(), Op<1>());
4248}
4249
4251 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
4252}
4253
4255 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
4256}
4257
4259 return new ExtractValueInst(*this);
4260}
4261
4263 return new InsertValueInst(*this);
4264}
4265
4268 getOperand(0), getAlign());
4269 Result->setUsedWithInAlloca(isUsedWithInAlloca());
4270 Result->setSwiftError(isSwiftError());
4271 return Result;
4272}
4273
4275 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
4277}
4278
4280 return new StoreInst(getOperand(0), getOperand(1), isVolatile(), getAlign(),
4282}
4283
4288 Result->setVolatile(isVolatile());
4289 Result->setWeak(isWeak());
4290 return Result;
4291}
4292
4294 AtomicRMWInst *Result =
4297 Result->setVolatile(isVolatile());
4298 return Result;
4299}
4300
4302 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
4303}
4304
4306 return new TruncInst(getOperand(0), getType());
4307}
4308
4310 return new ZExtInst(getOperand(0), getType());
4311}
4312
4314 return new SExtInst(getOperand(0), getType());
4315}
4316
4318 return new FPTruncInst(getOperand(0), getType());
4319}
4320
4322 return new FPExtInst(getOperand(0), getType());
4323}
4324
4326 return new UIToFPInst(getOperand(0), getType());
4327}
4328
4330 return new SIToFPInst(getOperand(0), getType());
4331}
4332
4334 return new FPToUIInst(getOperand(0), getType());
4335}
4336
4338 return new FPToSIInst(getOperand(0), getType());
4339}
4340
4342 return new PtrToIntInst(getOperand(0), getType());
4343}
4344
4346 return new IntToPtrInst(getOperand(0), getType());
4347}
4348
4350 return new BitCastInst(getOperand(0), getType());
4351}
4352
4354 return new AddrSpaceCastInst(getOperand(0), getType());
4355}
4356
4358 if (hasOperandBundles()) {
4362 return new (AllocMarker) CallInst(*this, AllocMarker);
4363 }
4365 return new (AllocMarker) CallInst(*this, AllocMarker);
4366}
4367
4370}
4371
4373 return new VAArgInst(getOperand(0), getType());
4374}
4375
4378}
4379
4382}
4383
4386}
4387
4388PHINode *PHINode::cloneImpl() const { return new (AllocMarker) PHINode(*this); }
4389
4391 return new LandingPadInst(*this);
4392}
4393
4396 return new (AllocMarker) ReturnInst(*this, AllocMarker);
4397}
4398
4401 return new (AllocMarker) BranchInst(*this, AllocMarker);
4402}
4403
4404SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
4405
4407 return new IndirectBrInst(*this);
4408}
4409
4411 if (hasOperandBundles()) {
4415 return new (AllocMarker) InvokeInst(*this, AllocMarker);
4416 }
4418 return new (AllocMarker) InvokeInst(*this, AllocMarker);
4419}
4420
4422 if (hasOperandBundles()) {
4426 return new (AllocMarker) CallBrInst(*this, AllocMarker);
4427 }
4429 return new (AllocMarker) CallBrInst(*this, AllocMarker);
4430}
4431
4433 return new (AllocMarker) ResumeInst(*this);
4434}
4435
4438 return new (AllocMarker) CleanupReturnInst(*this, AllocMarker);
4439}
4440
4442 return new (AllocMarker) CatchReturnInst(*this);
4443}
4444
4446 return new CatchSwitchInst(*this);
4447}
4448
4451 return new (AllocMarker) FuncletPadInst(*this, AllocMarker);
4452}
4453
4455 LLVMContext &Context = getContext();
4456 return new UnreachableInst(Context);
4457}
4458
4460 return new FreezeInst(getOperand(0));
4461}
static const LLT S1
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
return RetTy
Definition: DeadArgumentElimi