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