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