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