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