blob: 06b46724a87f80d92ac0a21730d20fc9e36455b5 [file] [log] [blame]
Eugene Zelenko46795222017-05-15 21:57:41 +00001//===- Instructions.cpp - Implement the LLVM instructions -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerb12261a2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos91366a82004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Patel6c6c0162009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000017#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth4bbfbdf2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Chandler Carruth6bef5bc2018-12-27 23:40:17 +000030#include "llvm/IR/Intrinsics.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000031#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Metadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000035#include "llvm/IR/Type.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/AtomicOrdering.h"
38#include "llvm/Support/Casting.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000039#include "llvm/Support/ErrorHandling.h"
Christopher Lamb43c7f372007-04-22 19:24:39 +000040#include "llvm/Support/MathExtras.h"
Eugene Zelenko46795222017-05-15 21:57:41 +000041#include <algorithm>
42#include <cassert>
43#include <cstdint>
44#include <vector>
45
Alkis Evlogimenos91366a82004-07-29 12:33:25 +000046using namespace llvm;
47
Chris Lattner50ee9dd2008-01-02 23:42:30 +000048//===----------------------------------------------------------------------===//
Bjorn Pettersson2079dc32018-06-26 06:17:00 +000049// AllocaInst Class
50//===----------------------------------------------------------------------===//
51
52Optional<uint64_t>
53AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
54 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
55 if (isArrayAllocation()) {
56 auto C = dyn_cast<ConstantInt>(getArraySize());
57 if (!C)
58 return None;
59 Size *= C->getZExtValue();
60 }
61 return Size;
62}
63
64//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +000065// CallSite Class
66//===----------------------------------------------------------------------===//
67
Gabor Greifc9f75002010-03-24 13:21:49 +000068User::op_iterator CallSite::getCallee() const {
Chandler Carruth9fa222d2018-11-22 10:31:35 +000069 return cast<CallBase>(getInstruction())->op_end() - 1;
Gabor Greifc9f75002010-03-24 13:21:49 +000070}
71
Gordon Henriksenafba8fe662007-12-10 02:14:30 +000072//===----------------------------------------------------------------------===//
Chris Lattnerb76ec322008-12-29 00:12:50 +000073// SelectInst Class
74//===----------------------------------------------------------------------===//
75
76/// areInvalidOperands - Return a string if the specified operands are invalid
77/// for a select operation, otherwise return null.
78const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
79 if (Op1->getType() != Op2->getType())
80 return "both values to select must have same type";
David Majnemer2dacece2015-08-14 05:09:07 +000081
82 if (Op1->getType()->isTokenTy())
83 return "select values cannot have token type";
84
Chris Lattnerdb125cf2011-07-18 04:54:35 +000085 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattnerb76ec322008-12-29 00:12:50 +000086 // Vector select.
Owen Anderson1d0be152009-08-13 21:58:54 +000087 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattnerb76ec322008-12-29 00:12:50 +000088 return "vector select condition element type must be i1";
Chris Lattnerdb125cf2011-07-18 04:54:35 +000089 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperec0f0bc2014-04-09 06:08:46 +000090 if (!ET)
Chris Lattnerb76ec322008-12-29 00:12:50 +000091 return "selected values for vector select must be vectors";
92 if (ET->getNumElements() != VT->getNumElements())
93 return "vector select requires selected vectors to have "
94 "the same vector length as select condition";
Owen Anderson1d0be152009-08-13 21:58:54 +000095 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattnerb76ec322008-12-29 00:12:50 +000096 return "select condition must be i1 or <n x i1>";
97 }
Craig Topperec0f0bc2014-04-09 06:08:46 +000098 return nullptr;
Chris Lattnerb76ec322008-12-29 00:12:50 +000099}
100
Chris Lattnerb76ec322008-12-29 00:12:50 +0000101//===----------------------------------------------------------------------===//
Chris Lattnerb12261a2005-01-29 00:35:16 +0000102// PHINode Class
103//===----------------------------------------------------------------------===//
104
105PHINode::PHINode(const PHINode &PN)
Pete Cooperea423672015-06-10 22:38:46 +0000106 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
107 ReservedSpace(PN.getNumOperands()) {
108 allocHungoffUses(PN.getNumOperands());
Jay Foad95c3e482011-06-23 09:09:15 +0000109 std::copy(PN.op_begin(), PN.op_end(), op_begin());
110 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000111 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerb12261a2005-01-29 00:35:16 +0000112}
113
Chris Lattnerb12261a2005-01-29 00:35:16 +0000114// removeIncomingValue - Remove an incoming value. This is useful if a
115// predecessor basic block is deleted.
116Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad95c3e482011-06-23 09:09:15 +0000117 Value *Removed = getIncomingValue(Idx);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000118
119 // Move everything after this operand down.
120 //
121 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad95c3e482011-06-23 09:09:15 +0000122 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerb12261a2005-01-29 00:35:16 +0000123 // use/def lists, which is kinda lame.
Jay Foad95c3e482011-06-23 09:09:15 +0000124 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
125 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000126
127 // Nuke the last value.
Craig Topperec0f0bc2014-04-09 06:08:46 +0000128 Op<-1>().set(nullptr);
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000129 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000130
131 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad95c3e482011-06-23 09:09:15 +0000132 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerb12261a2005-01-29 00:35:16 +0000133 // If anyone is using this PHI, make them use a dummy value instead...
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000134 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerb12261a2005-01-29 00:35:16 +0000135 eraseFromParent();
136 }
137 return Removed;
138}
139
Jay Foad8891ed72011-04-01 08:00:58 +0000140/// growOperands - grow operands - This grows the operand list in response
141/// to a push_back style of operation. This grows the number of ops by 1.5
142/// times.
Chris Lattnerb12261a2005-01-29 00:35:16 +0000143///
Jay Foad8891ed72011-04-01 08:00:58 +0000144void PHINode::growOperands() {
Gabor Greifefe65362008-05-10 08:32:32 +0000145 unsigned e = getNumOperands();
Jay Foad95c3e482011-06-23 09:09:15 +0000146 unsigned NumOps = e + e / 2;
147 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
148
Chris Lattnerb12261a2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Pete Cooper33102d22015-06-10 22:38:41 +0000150 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000151}
152
Nate Begemana83ba0f2005-08-04 23:24:19 +0000153/// hasConstantValue - If the specified PHI node always merges together the same
154/// value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +0000155Value *PHINode::hasConstantValue() const {
156 // Exploit the fact that phi nodes always have at least one entry.
157 Value *ConstantValue = getIncomingValue(0);
158 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes44d5c062012-07-03 17:10:28 +0000159 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
160 if (ConstantValue != this)
Craig Topperec0f0bc2014-04-09 06:08:46 +0000161 return nullptr; // Incoming values not all the same.
Nuno Lopes44d5c062012-07-03 17:10:28 +0000162 // The case where the first value is this PHI.
163 ConstantValue = getIncomingValue(i);
164 }
Nuno Lopes0fd518b2012-07-03 21:15:40 +0000165 if (ConstantValue == this)
166 return UndefValue::get(getType());
Duncan Sandsff103412010-11-17 04:30:22 +0000167 return ConstantValue;
Nate Begemana83ba0f2005-08-04 23:24:19 +0000168}
169
Nicolai Haehnlea16ecd432016-04-14 17:42:47 +0000170/// hasConstantOrUndefValue - Whether the specified PHI node always merges
171/// together the same value, assuming that undefs result in the same value as
172/// non-undefs.
173/// Unlike \ref hasConstantValue, this does not return a value because the
174/// unique non-undef incoming value need not dominate the PHI node.
175bool PHINode::hasConstantOrUndefValue() const {
176 Value *ConstantValue = nullptr;
177 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
178 Value *Incoming = getIncomingValue(i);
179 if (Incoming != this && !isa<UndefValue>(Incoming)) {
180 if (ConstantValue && ConstantValue != Incoming)
181 return false;
182 ConstantValue = Incoming;
183 }
184 }
185 return true;
186}
187
Bill Wendlinge6e88262011-08-12 20:24:12 +0000188//===----------------------------------------------------------------------===//
189// LandingPadInst Implementation
190//===----------------------------------------------------------------------===//
191
David Majnemercc714e22015-06-17 20:52:32 +0000192LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
193 const Twine &NameStr, Instruction *InsertBefore)
194 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
195 init(NumReservedValues, NameStr);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000196}
197
David Majnemercc714e22015-06-17 20:52:32 +0000198LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
199 const Twine &NameStr, BasicBlock *InsertAtEnd)
200 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
201 init(NumReservedValues, NameStr);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000202}
203
204LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooperea423672015-06-10 22:38:46 +0000205 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
206 LP.getNumOperands()),
207 ReservedSpace(LP.getNumOperands()) {
208 allocHungoffUses(LP.getNumOperands());
Pete Coopercff40fc2015-06-12 17:48:05 +0000209 Use *OL = getOperandList();
210 const Use *InOL = LP.getOperandList();
Bill Wendlinge6e88262011-08-12 20:24:12 +0000211 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
212 OL[I] = InOL[I];
213
214 setCleanup(LP.isCleanup());
215}
216
David Majnemercc714e22015-06-17 20:52:32 +0000217LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlinge6e88262011-08-12 20:24:12 +0000218 const Twine &NameStr,
219 Instruction *InsertBefore) {
David Majnemercc714e22015-06-17 20:52:32 +0000220 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000221}
222
David Majnemercc714e22015-06-17 20:52:32 +0000223LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlinge6e88262011-08-12 20:24:12 +0000224 const Twine &NameStr,
225 BasicBlock *InsertAtEnd) {
David Majnemercc714e22015-06-17 20:52:32 +0000226 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000227}
228
David Majnemercc714e22015-06-17 20:52:32 +0000229void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlinge6e88262011-08-12 20:24:12 +0000230 ReservedSpace = NumReservedValues;
David Majnemercc714e22015-06-17 20:52:32 +0000231 setNumHungOffUseOperands(0);
Pete Cooperea423672015-06-10 22:38:46 +0000232 allocHungoffUses(ReservedSpace);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000233 setName(NameStr);
234 setCleanup(false);
235}
236
237/// growOperands - grow operands - This grows the operand list in response to a
238/// push_back style of operation. This grows the number of ops by 2 times.
239void LandingPadInst::growOperands(unsigned Size) {
240 unsigned e = getNumOperands();
241 if (ReservedSpace >= e + Size) return;
David Majnemercc714e22015-06-17 20:52:32 +0000242 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper33102d22015-06-10 22:38:41 +0000243 growHungoffUses(ReservedSpace);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000244}
245
Rafael Espindoladcac1522014-06-04 18:51:31 +0000246void LandingPadInst::addClause(Constant *Val) {
Bill Wendlinge6e88262011-08-12 20:24:12 +0000247 unsigned OpNo = getNumOperands();
248 growOperands(1);
249 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000250 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Coopercff40fc2015-06-12 17:48:05 +0000251 getOperandList()[OpNo] = Val;
Bill Wendlinge6e88262011-08-12 20:24:12 +0000252}
Chris Lattnerb12261a2005-01-29 00:35:16 +0000253
254//===----------------------------------------------------------------------===//
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000255// CallBase Implementation
256//===----------------------------------------------------------------------===//
257
Chandler Carruth6bef5bc2018-12-27 23:40:17 +0000258Function *CallBase::getCaller() { return getParent()->getParent(); }
259
Chandler Carruth9d8b7a02019-01-07 07:15:51 +0000260bool CallBase::isIndirectCall() const {
261 const Value *V = getCalledValue();
262 if (isa<Function>(V) || isa<Constant>(V))
263 return false;
264 if (const CallInst *CI = dyn_cast<CallInst>(this))
265 if (CI->isInlineAsm())
266 return false;
267 return true;
268}
269
Chandler Carruth6bef5bc2018-12-27 23:40:17 +0000270Intrinsic::ID CallBase::getIntrinsicID() const {
271 if (auto *F = getCalledFunction())
272 return F->getIntrinsicID();
273 return Intrinsic::not_intrinsic;
274}
275
276bool CallBase::isReturnNonNull() const {
277 if (hasRetAttr(Attribute::NonNull))
278 return true;
279
280 if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
281 !NullPointerIsDefined(getCaller(),
282 getType()->getPointerAddressSpace()))
283 return true;
284
285 return false;
286}
287
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000288Value *CallBase::getReturnedArgOperand() const {
289 unsigned Index;
290
291 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
292 return getArgOperand(Index - AttributeList::FirstArgIndex);
293 if (const Function *F = getCalledFunction())
294 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
295 Index)
296 return getArgOperand(Index - AttributeList::FirstArgIndex);
297
298 return nullptr;
299}
300
301bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
302 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
303 return true;
304
305 // Look at the callee, if available.
306 if (const Function *F = getCalledFunction())
307 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
308 return false;
309}
310
311/// Determine whether the argument or parameter has the given attribute.
312bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
313 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
314
315 if (Attrs.hasParamAttribute(ArgNo, Kind))
316 return true;
317 if (const Function *F = getCalledFunction())
318 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
319 return false;
320}
321
322bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
323 if (const Function *F = getCalledFunction())
324 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
325 return false;
326}
327
328bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
329 if (const Function *F = getCalledFunction())
330 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
331 return false;
332}
333
334CallBase::op_iterator
335CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
336 const unsigned BeginIndex) {
337 auto It = op_begin() + BeginIndex;
338 for (auto &B : Bundles)
339 It = std::copy(B.input_begin(), B.input_end(), It);
340
341 auto *ContextImpl = getContext().pImpl;
342 auto BI = Bundles.begin();
343 unsigned CurrentIndex = BeginIndex;
344
345 for (auto &BOI : bundle_op_infos()) {
346 assert(BI != Bundles.end() && "Incorrect allocation?");
347
348 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
349 BOI.Begin = CurrentIndex;
350 BOI.End = CurrentIndex + BI->input_size();
351 CurrentIndex = BOI.End;
352 BI++;
353 }
354
355 assert(BI == Bundles.end() && "Incorrect allocation?");
356
357 return It;
358}
359
360//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000361// CallInst Implementation
362//===----------------------------------------------------------------------===//
363
David Blaikie93a23a32015-04-23 21:36:23 +0000364void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das5b674c02015-09-24 19:14:18 +0000365 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie93a23a32015-04-23 21:36:23 +0000366 this->FTy = FTy;
Sanjoy Das5b674c02015-09-24 19:14:18 +0000367 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
368 "NumOperands not set up?");
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000369 setCalledOperand(Func);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000370
Jay Foada3efbb12011-07-15 08:37:34 +0000371#ifndef NDEBUG
Jay Foada3efbb12011-07-15 08:37:34 +0000372 assert((Args.size() == FTy->getNumParams() ||
373 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000374 "Calling a function with bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000375
376 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songaf7b1832018-07-30 19:41:25 +0000377 assert((i >= FTy->getNumParams() ||
Jay Foada3efbb12011-07-15 08:37:34 +0000378 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000379 "Calling a function with a bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000380#endif
381
Fangrui Song53a62242018-11-17 01:44:25 +0000382 llvm::copy(Args, op_begin());
Sanjoy Das5b674c02015-09-24 19:14:18 +0000383
384 auto It = populateBundleOperandInfos(Bundles, Args.size());
385 (void)It;
386 assert(It + 1 == op_end() && "Should add up!");
387
Jay Foada3efbb12011-07-15 08:37:34 +0000388 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000389}
390
James Y Knightf806f422019-01-14 21:37:42 +0000391void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
392 this->FTy = FTy;
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000393 assert(getNumOperands() == 1 && "NumOperands not set up?");
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000394 setCalledOperand(Func);
Misha Brukmanfd939082005-04-21 23:48:37 +0000395
Chris Lattner2e21fce2007-02-01 04:59:37 +0000396 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foada3efbb12011-07-15 08:37:34 +0000397
398 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000399}
400
James Y Knightf806f422019-01-14 21:37:42 +0000401CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
402 Instruction *InsertBefore)
403 : CallBase(Ty->getReturnType(), Instruction::Call,
404 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) {
405 init(Ty, Func, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000406}
407
James Y Knightf806f422019-01-14 21:37:42 +0000408CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
409 BasicBlock *InsertAtEnd)
410 : CallBase(Ty->getReturnType(), Instruction::Call,
411 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) {
412 init(Ty, Func, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000413}
414
Misha Brukmanfd939082005-04-21 23:48:37 +0000415CallInst::CallInst(const CallInst &CI)
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000416 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
417 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(),
418 CI.getNumOperands()) {
Reid Kleckner39497492014-05-06 20:08:20 +0000419 setTailCallKind(CI.getTailCallKind());
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000420 setCallingConv(CI.getCallingConv());
Sanjoy Das5b674c02015-09-24 19:14:18 +0000421
Jay Foada3efbb12011-07-15 08:37:34 +0000422 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das5b674c02015-09-24 19:14:18 +0000423 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
424 bundle_op_info_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000425 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000426}
427
Sanjoy Dasaca81122015-11-18 06:23:38 +0000428CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
429 Instruction *InsertPt) {
Sanjoy Dasfd947f32015-12-10 06:39:02 +0000430 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Dasaca81122015-11-18 06:23:38 +0000431
432 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
433 InsertPt);
434 NewCI->setTailCallKind(CI->getTailCallKind());
435 NewCI->setCallingConv(CI->getCallingConv());
436 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasf001e6b2015-12-09 01:01:28 +0000437 NewCI->setAttributes(CI->getAttributes());
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000438 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Dasaca81122015-11-18 06:23:38 +0000439 return NewCI;
440}
441
Hal Finkel9a51ef12016-07-10 23:01:32 +0000442
Reid Klecknera82b3762017-05-03 18:17:31 +0000443
Hal Finkel9a51ef12016-07-10 23:01:32 +0000444
Eric Christopher0bf7b412008-05-16 20:39:43 +0000445
Amaury Sechet4e14c832016-06-15 05:14:29 +0000446
Reid Kleckner1e9afac2017-05-31 19:23:09 +0000447
Reid Klecknera82b3762017-05-03 18:17:31 +0000448
Amaury Sechetb5052752016-04-21 21:29:10 +0000449
Sanjoy Dasa6abdeb2015-11-04 21:05:24 +0000450
Evan Chengfabcb912009-09-10 04:36:43 +0000451/// IsConstantOne - Return true only if val is constant int 1
452static bool IsConstantOne(Value *val) {
Reid Klecknere094cca2014-11-13 22:55:19 +0000453 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault74474262014-09-15 17:56:51 +0000454 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
455 return CVal && CVal->isOne();
Evan Chengfabcb912009-09-10 04:36:43 +0000456}
457
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000458static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000459 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemer30471832016-04-29 08:07:22 +0000460 Type *AllocTy, Value *AllocSize,
461 Value *ArraySize,
462 ArrayRef<OperandBundleDef> OpB,
463 Function *MallocF, const Twine &Name) {
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000464 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez88d98392009-09-18 19:20:02 +0000465 "createMalloc needs either InsertBefore or InsertAtEnd");
466
Fangrui Songaf7b1832018-07-30 19:41:25 +0000467 // malloc(type) becomes:
Victor Hernandez88d98392009-09-18 19:20:02 +0000468 // bitcast (i8* malloc(typeSize)) to type*
469 // malloc(type, arraySize) becomes:
Ana Pazose6463c92016-02-03 21:34:39 +0000470 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000471 if (!ArraySize)
472 ArraySize = ConstantInt::get(IntPtrTy, 1);
473 else if (ArraySize->getType() != IntPtrTy) {
474 if (InsertBefore)
Victor Hernandez4d81d972009-11-07 00:36:50 +0000475 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
476 "", InsertBefore);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000477 else
Victor Hernandez4d81d972009-11-07 00:36:50 +0000478 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
479 "", InsertAtEnd);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000480 }
Evan Chengfabcb912009-09-10 04:36:43 +0000481
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000482 if (!IsConstantOne(ArraySize)) {
Evan Chengfabcb912009-09-10 04:36:43 +0000483 if (IsConstantOne(AllocSize)) {
484 AllocSize = ArraySize; // Operand * 1 = Operand
485 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
486 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
487 false /*ZExt*/);
488 // Malloc arg is constant product of type size and array size
489 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
490 } else {
Evan Chengfabcb912009-09-10 04:36:43 +0000491 // Multiply type size by the array size...
492 if (InsertBefore)
Victor Hernandez88d98392009-09-18 19:20:02 +0000493 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
494 "mallocsize", InsertBefore);
Evan Chengfabcb912009-09-10 04:36:43 +0000495 else
Victor Hernandez88d98392009-09-18 19:20:02 +0000496 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
497 "mallocsize", InsertAtEnd);
Evan Chengfabcb912009-09-10 04:36:43 +0000498 }
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000499 }
Evan Chengfabcb912009-09-10 04:36:43 +0000500
Victor Hernandez88d98392009-09-18 19:20:02 +0000501 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Chengfabcb912009-09-10 04:36:43 +0000502 // Create the call to Malloc.
Ana Pazose6463c92016-02-03 21:34:39 +0000503 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
504 Module *M = BB->getParent()->getParent();
Chris Lattner1afcace2011-07-09 17:41:24 +0000505 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezf06dca22009-11-10 19:53:28 +0000506 Value *MallocFunc = MallocF;
507 if (!MallocFunc)
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000508 // prototype malloc as "void *malloc(size_t)"
Serge Guelton9d544002017-04-11 15:01:18 +0000509 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000510 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperec0f0bc2014-04-09 06:08:46 +0000511 CallInst *MCall = nullptr;
512 Instruction *Result = nullptr;
Victor Hernandez88d98392009-09-18 19:20:02 +0000513 if (InsertBefore) {
David Majnemer30471832016-04-29 08:07:22 +0000514 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
515 InsertBefore);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000516 Result = MCall;
517 if (Result->getType() != AllocPtrType)
518 // Create a cast instruction to convert to the right type...
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000519 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez88d98392009-09-18 19:20:02 +0000520 } else {
David Majnemer30471832016-04-29 08:07:22 +0000521 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000522 Result = MCall;
523 if (Result->getType() != AllocPtrType) {
524 InsertAtEnd->getInstList().push_back(MCall);
525 // Create a cast instruction to convert to the right type...
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000526 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000527 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000528 }
Evan Chengfabcb912009-09-10 04:36:43 +0000529 MCall->setTailCall();
Victor Hernandezf06dca22009-11-10 19:53:28 +0000530 if (Function *F = dyn_cast<Function>(MallocFunc)) {
531 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera82b3762017-05-03 18:17:31 +0000532 if (!F->returnDoesNotAlias())
533 F->setReturnDoesNotAlias();
Victor Hernandezf06dca22009-11-10 19:53:28 +0000534 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000535 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez88d98392009-09-18 19:20:02 +0000536
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000537 return Result;
Evan Chengfabcb912009-09-10 04:36:43 +0000538}
539
540/// CreateMalloc - Generate the IR for a call to malloc:
541/// 1. Compute the malloc call's argument as the specified type's size,
542/// possibly multiplied by the array size if the array size is not
543/// constant 1.
544/// 2. Call malloc with that argument.
545/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000546Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000547 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000548 Value *AllocSize, Value *ArraySize,
Ana Pazose6463c92016-02-03 21:34:39 +0000549 Function *MallocF,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000550 const Twine &Name) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000551 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemer30471832016-04-29 08:07:22 +0000552 ArraySize, None, MallocF, Name);
Evan Chengfabcb912009-09-10 04:36:43 +0000553}
David Majnemer30471832016-04-29 08:07:22 +0000554Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
555 Type *IntPtrTy, Type *AllocTy,
556 Value *AllocSize, Value *ArraySize,
557 ArrayRef<OperandBundleDef> OpB,
558 Function *MallocF,
559 const Twine &Name) {
560 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
561 ArraySize, OpB, MallocF, Name);
562}
563
Evan Chengfabcb912009-09-10 04:36:43 +0000564/// CreateMalloc - Generate the IR for a call to malloc:
565/// 1. Compute the malloc call's argument as the specified type's size,
566/// possibly multiplied by the array size if the array size is not
567/// constant 1.
568/// 2. Call malloc with that argument.
569/// 3. Bitcast the result of the malloc call to the specified type.
570/// Note: This function does not add the bitcast to the basic block, that is the
571/// responsibility of the caller.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000572Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000573 Type *IntPtrTy, Type *AllocTy,
Fangrui Songaf7b1832018-07-30 19:41:25 +0000574 Value *AllocSize, Value *ArraySize,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000575 Function *MallocF, const Twine &Name) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000576 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemer30471832016-04-29 08:07:22 +0000577 ArraySize, None, MallocF, Name);
578}
579Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
580 Type *IntPtrTy, Type *AllocTy,
581 Value *AllocSize, Value *ArraySize,
582 ArrayRef<OperandBundleDef> OpB,
583 Function *MallocF, const Twine &Name) {
584 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
585 ArraySize, OpB, MallocF, Name);
Evan Chengfabcb912009-09-10 04:36:43 +0000586}
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000587
David Majnemer30471832016-04-29 08:07:22 +0000588static Instruction *createFree(Value *Source,
589 ArrayRef<OperandBundleDef> Bundles,
590 Instruction *InsertBefore,
Victor Hernandez66284e02009-10-24 04:23:03 +0000591 BasicBlock *InsertAtEnd) {
592 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
593 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands1df98592010-02-16 11:11:14 +0000594 assert(Source->getType()->isPointerTy() &&
Victor Hernandez66284e02009-10-24 04:23:03 +0000595 "Can not free something of nonpointer type!");
596
Ana Pazose6463c92016-02-03 21:34:39 +0000597 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
598 Module *M = BB->getParent()->getParent();
Victor Hernandez66284e02009-10-24 04:23:03 +0000599
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000600 Type *VoidTy = Type::getVoidTy(M->getContext());
601 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandez66284e02009-10-24 04:23:03 +0000602 // prototype free as "void free(void*)"
Serge Guelton9d544002017-04-11 15:01:18 +0000603 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazose6463c92016-02-03 21:34:39 +0000604 CallInst *Result = nullptr;
Victor Hernandez66284e02009-10-24 04:23:03 +0000605 Value *PtrCast = Source;
606 if (InsertBefore) {
607 if (Source->getType() != IntPtrTy)
608 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemer30471832016-04-29 08:07:22 +0000609 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +0000610 } else {
611 if (Source->getType() != IntPtrTy)
612 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemer30471832016-04-29 08:07:22 +0000613 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandez66284e02009-10-24 04:23:03 +0000614 }
615 Result->setTailCall();
Chris Lattner47fc9d32009-11-09 07:12:01 +0000616 if (Function *F = dyn_cast<Function>(FreeFunc))
617 Result->setCallingConv(F->getCallingConv());
Victor Hernandez66284e02009-10-24 04:23:03 +0000618
619 return Result;
620}
621
622/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazose6463c92016-02-03 21:34:39 +0000623Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemer30471832016-04-29 08:07:22 +0000624 return createFree(Source, None, InsertBefore, nullptr);
625}
626Instruction *CallInst::CreateFree(Value *Source,
627 ArrayRef<OperandBundleDef> Bundles,
628 Instruction *InsertBefore) {
629 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandez66284e02009-10-24 04:23:03 +0000630}
631
632/// CreateFree - Generate the IR for a call to the builtin free function.
633/// Note: This function does not add the call to the basic block, that is the
634/// responsibility of the caller.
Ana Pazose6463c92016-02-03 21:34:39 +0000635Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemer30471832016-04-29 08:07:22 +0000636 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
637 assert(FreeCall && "CreateFree did not create a CallInst");
638 return FreeCall;
639}
640Instruction *CallInst::CreateFree(Value *Source,
641 ArrayRef<OperandBundleDef> Bundles,
642 BasicBlock *InsertAtEnd) {
643 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandez66284e02009-10-24 04:23:03 +0000644 assert(FreeCall && "CreateFree did not create a CallInst");
645 return FreeCall;
646}
647
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000648//===----------------------------------------------------------------------===//
649// InvokeInst Implementation
650//===----------------------------------------------------------------------===//
651
David Blaikie115494b2015-05-13 18:35:26 +0000652void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
653 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das5b674c02015-09-24 19:14:18 +0000654 ArrayRef<OperandBundleDef> Bundles,
David Blaikie115494b2015-05-13 18:35:26 +0000655 const Twine &NameStr) {
656 this->FTy = FTy;
David Blaikie93a23a32015-04-23 21:36:23 +0000657
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000658 assert((int)getNumOperands() ==
659 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
Sanjoy Das5b674c02015-09-24 19:14:18 +0000660 "NumOperands not set up?");
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000661 setNormalDest(IfNormal);
662 setUnwindDest(IfException);
663 setCalledOperand(Fn);
Jay Foada3efbb12011-07-15 08:37:34 +0000664
665#ifndef NDEBUG
Jay Foada3efbb12011-07-15 08:37:34 +0000666 assert(((Args.size() == FTy->getNumParams()) ||
667 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif050560e2010-03-23 13:45:54 +0000668 "Invoking a function with bad signature");
Misha Brukmanfd939082005-04-21 23:48:37 +0000669
Jay Foada3efbb12011-07-15 08:37:34 +0000670 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songaf7b1832018-07-30 19:41:25 +0000671 assert((i >= FTy->getNumParams() ||
Chris Lattnerd2dd1502007-02-13 01:04:01 +0000672 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000673 "Invoking a function with a bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000674#endif
675
Fangrui Song53a62242018-11-17 01:44:25 +0000676 llvm::copy(Args, op_begin());
Sanjoy Das5b674c02015-09-24 19:14:18 +0000677
678 auto It = populateBundleOperandInfos(Bundles, Args.size());
679 (void)It;
680 assert(It + 3 == op_end() && "Should add up!");
681
Jay Foada3efbb12011-07-15 08:37:34 +0000682 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000683}
684
Misha Brukmanfd939082005-04-21 23:48:37 +0000685InvokeInst::InvokeInst(const InvokeInst &II)
Chandler Carruth9fa222d2018-11-22 10:31:35 +0000686 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
687 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(),
688 II.getNumOperands()) {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000689 setCallingConv(II.getCallingConv());
Jay Foada3efbb12011-07-15 08:37:34 +0000690 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das5b674c02015-09-24 19:14:18 +0000691 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
692 bundle_op_info_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000693 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000694}
695
Sanjoy Dasaca81122015-11-18 06:23:38 +0000696InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
697 Instruction *InsertPt) {
Sanjoy Dasfd947f32015-12-10 06:39:02 +0000698 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Dasaca81122015-11-18 06:23:38 +0000699
700 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
701 II->getUnwindDest(), Args, OpB,
702 II->getName(), InsertPt);
703 NewII->setCallingConv(II->getCallingConv());
704 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasf001e6b2015-12-09 01:01:28 +0000705 NewII->setAttributes(II->getAttributes());
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000706 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Dasaca81122015-11-18 06:23:38 +0000707 return NewII;
708}
709
Sanjoy Das5ff59072015-04-16 20:29:50 +0000710
Bill Wendlinge6e88262011-08-12 20:24:12 +0000711LandingPadInst *InvokeInst::getLandingPadInst() const {
712 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
713}
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000714
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000715//===----------------------------------------------------------------------===//
716// ReturnInst Implementation
717//===----------------------------------------------------------------------===//
718
Chris Lattner910c80a2007-02-24 00:55:48 +0000719ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000720 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
721 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
722 RI.getNumOperands()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000723 if (RI.getNumOperands())
Gabor Greif6c80c382008-05-26 21:33:52 +0000724 Op<0>() = RI.Op<0>();
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000725 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner910c80a2007-02-24 00:55:48 +0000726}
727
Owen Anderson1d0be152009-08-13 21:58:54 +0000728ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000729 : Instruction(Type::getVoidTy(C), Instruction::Ret,
730 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
731 InsertBefore) {
Devang Patel814ebd72008-02-26 18:49:29 +0000732 if (retVal)
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000733 Op<0>() = retVal;
Chris Lattner910c80a2007-02-24 00:55:48 +0000734}
Eugene Zelenko46795222017-05-15 21:57:41 +0000735
Owen Anderson1d0be152009-08-13 21:58:54 +0000736ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000737 : Instruction(Type::getVoidTy(C), Instruction::Ret,
738 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
739 InsertAtEnd) {
Devang Patel814ebd72008-02-26 18:49:29 +0000740 if (retVal)
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000741 Op<0>() = retVal;
Chris Lattner910c80a2007-02-24 00:55:48 +0000742}
Eugene Zelenko46795222017-05-15 21:57:41 +0000743
Owen Anderson1d0be152009-08-13 21:58:54 +0000744ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000745 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
746 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel57ef4f42008-02-23 00:35:18 +0000747
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000748//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000749// ResumeInst Implementation
750//===----------------------------------------------------------------------===//
751
752ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000753 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
754 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000755 Op<0>() = RI.Op<0>();
756}
757
758ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000759 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
760 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000761 Op<0>() = Exn;
762}
763
764ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000765 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
766 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000767 Op<0>() = Exn;
768}
769
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000770//===----------------------------------------------------------------------===//
David Majnemer4a45f082015-07-31 17:58:14 +0000771// CleanupReturnInst Implementation
772//===----------------------------------------------------------------------===//
773
774CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000775 : Instruction(CRI.getType(), Instruction::CleanupRet,
776 OperandTraits<CleanupReturnInst>::op_end(this) -
777 CRI.getNumOperands(),
778 CRI.getNumOperands()) {
David Majnemerb8924652015-08-04 08:21:40 +0000779 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8cec2f22015-12-12 05:38:55 +0000780 Op<0>() = CRI.Op<0>();
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000781 if (CRI.hasUnwindDest())
David Majnemer8cec2f22015-12-12 05:38:55 +0000782 Op<1>() = CRI.Op<1>();
David Majnemer4a45f082015-07-31 17:58:14 +0000783}
784
David Majnemer8cec2f22015-12-12 05:38:55 +0000785void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer4a45f082015-07-31 17:58:14 +0000786 if (UnwindBB)
787 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer4a45f082015-07-31 17:58:14 +0000788
David Majnemer8cec2f22015-12-12 05:38:55 +0000789 Op<0>() = CleanupPad;
David Majnemer4a45f082015-07-31 17:58:14 +0000790 if (UnwindBB)
David Majnemer8cec2f22015-12-12 05:38:55 +0000791 Op<1>() = UnwindBB;
David Majnemer4a45f082015-07-31 17:58:14 +0000792}
793
David Majnemer8cec2f22015-12-12 05:38:55 +0000794CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
795 unsigned Values, Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000796 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
797 Instruction::CleanupRet,
798 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
799 Values, InsertBefore) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000800 init(CleanupPad, UnwindBB);
David Majnemer4a45f082015-07-31 17:58:14 +0000801}
802
David Majnemer8cec2f22015-12-12 05:38:55 +0000803CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
804 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000805 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
806 Instruction::CleanupRet,
807 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
808 Values, InsertAtEnd) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000809 init(CleanupPad, UnwindBB);
David Majnemer4a45f082015-07-31 17:58:14 +0000810}
811
David Majnemer4a45f082015-07-31 17:58:14 +0000812//===----------------------------------------------------------------------===//
David Majnemer4a45f082015-07-31 17:58:14 +0000813// CatchReturnInst Implementation
814//===----------------------------------------------------------------------===//
David Majnemer8cec2f22015-12-12 05:38:55 +0000815void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000816 Op<0>() = CatchPad;
817 Op<1>() = BB;
David Majnemerde17e772015-08-15 02:46:08 +0000818}
David Majnemer4a45f082015-07-31 17:58:14 +0000819
820CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000821 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
822 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000823 Op<0>() = CRI.Op<0>();
824 Op<1>() = CRI.Op<1>();
David Majnemer4a45f082015-07-31 17:58:14 +0000825}
826
David Majnemer8cec2f22015-12-12 05:38:55 +0000827CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemerde17e772015-08-15 02:46:08 +0000828 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000829 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
830 OperandTraits<CatchReturnInst>::op_begin(this), 2,
831 InsertBefore) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000832 init(CatchPad, BB);
David Majnemer4a45f082015-07-31 17:58:14 +0000833}
834
David Majnemer8cec2f22015-12-12 05:38:55 +0000835CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemerde17e772015-08-15 02:46:08 +0000836 BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000837 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
838 OperandTraits<CatchReturnInst>::op_begin(this), 2,
839 InsertAtEnd) {
Joseph Tremouletd4a765f2015-08-23 00:26:33 +0000840 init(CatchPad, BB);
David Majnemer4a45f082015-07-31 17:58:14 +0000841}
842
David Majnemer4a45f082015-07-31 17:58:14 +0000843//===----------------------------------------------------------------------===//
David Majnemer8cec2f22015-12-12 05:38:55 +0000844// CatchSwitchInst Implementation
David Majnemer4a45f082015-07-31 17:58:14 +0000845//===----------------------------------------------------------------------===//
David Majnemer8cec2f22015-12-12 05:38:55 +0000846
847CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
848 unsigned NumReservedValues,
849 const Twine &NameStr,
850 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000851 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
852 InsertBefore) {
David Majnemer8cec2f22015-12-12 05:38:55 +0000853 if (UnwindDest)
854 ++NumReservedValues;
855 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer4a45f082015-07-31 17:58:14 +0000856 setName(NameStr);
857}
858
David Majnemer8cec2f22015-12-12 05:38:55 +0000859CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
860 unsigned NumReservedValues,
861 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000862 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
863 InsertAtEnd) {
David Majnemer8cec2f22015-12-12 05:38:55 +0000864 if (UnwindDest)
865 ++NumReservedValues;
866 init(ParentPad, UnwindDest, NumReservedValues + 1);
867 setName(NameStr);
David Majnemer4a45f082015-07-31 17:58:14 +0000868}
869
David Majnemer8cec2f22015-12-12 05:38:55 +0000870CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000871 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
872 CSI.getNumOperands()) {
David Majnemer8cec2f22015-12-12 05:38:55 +0000873 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
874 setNumHungOffUseOperands(ReservedSpace);
875 Use *OL = getOperandList();
876 const Use *InOL = CSI.getOperandList();
877 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
878 OL[I] = InOL[I];
David Majnemer4a45f082015-07-31 17:58:14 +0000879}
David Majnemer8cec2f22015-12-12 05:38:55 +0000880
881void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
882 unsigned NumReservedValues) {
883 assert(ParentPad && NumReservedValues);
884
885 ReservedSpace = NumReservedValues;
886 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
887 allocHungoffUses(ReservedSpace);
888
889 Op<0>() = ParentPad;
890 if (UnwindDest) {
891 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
892 setUnwindDest(UnwindDest);
893 }
894}
895
896/// growOperands - grow operands - This grows the operand list in response to a
897/// push_back style of operation. This grows the number of ops by 2 times.
898void CatchSwitchInst::growOperands(unsigned Size) {
899 unsigned NumOperands = getNumOperands();
900 assert(NumOperands >= 1);
901 if (ReservedSpace >= NumOperands + Size)
902 return;
903 ReservedSpace = (NumOperands + Size / 2) * 2;
904 growHungoffUses(ReservedSpace);
905}
906
907void CatchSwitchInst::addHandler(BasicBlock *Handler) {
908 unsigned OpNo = getNumOperands();
909 growOperands(1);
910 assert(OpNo < ReservedSpace && "Growing didn't work!");
911 setNumHungOffUseOperands(getNumOperands() + 1);
912 getOperandList()[OpNo] = Handler;
913}
914
Joseph Tremouletd28c1bc2016-01-05 02:37:41 +0000915void CatchSwitchInst::removeHandler(handler_iterator HI) {
916 // Move all subsequent handlers up one.
917 Use *EndDst = op_end() - 1;
918 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
919 *CurDst = *(CurDst + 1);
920 // Null out the last handler use.
921 *EndDst = nullptr;
922
923 setNumHungOffUseOperands(getNumOperands() - 1);
924}
925
David Majnemer8cec2f22015-12-12 05:38:55 +0000926//===----------------------------------------------------------------------===//
927// FuncletPadInst Implementation
928//===----------------------------------------------------------------------===//
929void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
930 const Twine &NameStr) {
931 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
Fangrui Song53a62242018-11-17 01:44:25 +0000932 llvm::copy(Args, op_begin());
David Majnemer8cec2f22015-12-12 05:38:55 +0000933 setParentPad(ParentPad);
934 setName(NameStr);
935}
936
937FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
938 : Instruction(FPI.getType(), FPI.getOpcode(),
939 OperandTraits<FuncletPadInst>::op_end(this) -
940 FPI.getNumOperands(),
941 FPI.getNumOperands()) {
942 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
943 setParentPad(FPI.getParentPad());
944}
945
946FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
947 ArrayRef<Value *> Args, unsigned Values,
948 const Twine &NameStr, Instruction *InsertBefore)
949 : Instruction(ParentPad->getType(), Op,
950 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
951 InsertBefore) {
952 init(ParentPad, Args, NameStr);
953}
954
955FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
956 ArrayRef<Value *> Args, unsigned Values,
957 const Twine &NameStr, BasicBlock *InsertAtEnd)
958 : Instruction(ParentPad->getType(), Op,
959 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
960 InsertAtEnd) {
961 init(ParentPad, Args, NameStr);
David Majnemer4a45f082015-07-31 17:58:14 +0000962}
963
964//===----------------------------------------------------------------------===//
Chris Lattnerb976e662004-10-16 18:08:06 +0000965// UnreachableInst Implementation
966//===----------------------------------------------------------------------===//
967
Fangrui Songaf7b1832018-07-30 19:41:25 +0000968UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +0000969 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000970 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
971 0, InsertBefore) {}
Owen Anderson1d0be152009-08-13 21:58:54 +0000972UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000973 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
974 0, InsertAtEnd) {}
Chris Lattner910c80a2007-02-24 00:55:48 +0000975
Chris Lattnerb976e662004-10-16 18:08:06 +0000976//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000977// BranchInst Implementation
978//===----------------------------------------------------------------------===//
979
Chris Lattnerb12261a2005-01-29 00:35:16 +0000980void BranchInst::AssertOK() {
981 if (isConditional())
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000982 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerb12261a2005-01-29 00:35:16 +0000983 "May only branch on boolean predicates!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000984}
985
Chris Lattner910c80a2007-02-24 00:55:48 +0000986BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000987 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
988 OperandTraits<BranchInst>::op_end(this) - 1, 1,
989 InsertBefore) {
Craig Topper0b6cb712014-04-15 06:32:26 +0000990 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifae5a20a2009-03-12 18:34:49 +0000991 Op<-1>() = IfTrue;
Chris Lattner910c80a2007-02-24 00:55:48 +0000992}
Eugene Zelenko46795222017-05-15 21:57:41 +0000993
Chris Lattner910c80a2007-02-24 00:55:48 +0000994BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
995 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +0000996 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
997 OperandTraits<BranchInst>::op_end(this) - 3, 3,
998 InsertBefore) {
Gabor Greifae5a20a2009-03-12 18:34:49 +0000999 Op<-1>() = IfTrue;
1000 Op<-2>() = IfFalse;
1001 Op<-3>() = Cond;
Chris Lattner910c80a2007-02-24 00:55:48 +00001002#ifndef NDEBUG
1003 AssertOK();
1004#endif
1005}
1006
1007BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00001008 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1009 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper0b6cb712014-04-15 06:32:26 +00001010 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00001011 Op<-1>() = IfTrue;
Chris Lattner910c80a2007-02-24 00:55:48 +00001012}
1013
1014BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00001015 BasicBlock *InsertAtEnd)
1016 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1017 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00001018 Op<-1>() = IfTrue;
1019 Op<-2>() = IfFalse;
1020 Op<-3>() = Cond;
Chris Lattner910c80a2007-02-24 00:55:48 +00001021#ifndef NDEBUG
1022 AssertOK();
1023#endif
1024}
1025
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00001026BranchInst::BranchInst(const BranchInst &BI)
1027 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
1028 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1029 BI.getNumOperands()) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00001030 Op<-1>() = BI.Op<-1>();
Chris Lattnerb12261a2005-01-29 00:35:16 +00001031 if (BI.getNumOperands() != 1) {
1032 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00001033 Op<-3>() = BI.Op<-3>();
1034 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001035 }
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001036 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001037}
1038
Chandler Carruth602650c2011-10-17 01:11:57 +00001039void BranchInst::swapSuccessors() {
1040 assert(isConditional() &&
1041 "Cannot swap successors of an unconditional branch");
1042 Op<-1>().swap(Op<-2>());
1043
1044 // Update profile metadata if present and it matches our structural
1045 // expectations.
Xinliang David Liee836b92016-08-23 15:39:03 +00001046 swapProfMetadata();
Chandler Carruth602650c2011-10-17 01:11:57 +00001047}
1048
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001049//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +00001050// AllocaInst Implementation
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001051//===----------------------------------------------------------------------===//
1052
Owen Anderson9adc0ab2009-07-14 23:09:55 +00001053static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerb12261a2005-01-29 00:35:16 +00001054 if (!Amt)
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnere46749c2006-05-10 04:32:43 +00001056 else {
1057 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner53336cb2007-10-18 16:10:48 +00001058 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohmanf75a7d32010-05-28 01:14:11 +00001059 assert(Amt->getType()->isIntegerTy() &&
1060 "Allocation array size is not an integer!");
Chris Lattnere46749c2006-05-10 04:32:43 +00001061 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001062 return Amt;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001063}
1064
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001065AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez7b929da2009-10-23 21:09:37 +00001066 Instruction *InsertBefore)
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001067 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez7b929da2009-10-23 21:09:37 +00001068
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001069AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez7b929da2009-10-23 21:09:37 +00001070 BasicBlock *InsertAtEnd)
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001071 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez7b929da2009-10-23 21:09:37 +00001072
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001073AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +00001074 const Twine &Name, Instruction *InsertBefore)
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001075 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1076
1077AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1078 const Twine &Name, BasicBlock *InsertAtEnd)
1079 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1080
1081AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1082 unsigned Align, const Twine &Name,
1083 Instruction *InsertBefore)
1084 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1085 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1086 AllocatedType(Ty) {
Dan Gohman52837072008-03-24 16:55:58 +00001087 setAlignment(Align);
Benjamin Kramerf0127052010-01-05 13:12:22 +00001088 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattnerf00042a2007-02-13 07:54:42 +00001089 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001090}
1091
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00001092AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1093 unsigned Align, const Twine &Name,
1094 BasicBlock *InsertAtEnd)
1095 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1096 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikie3453e8b2015-04-29 23:00:35 +00001097 AllocatedType(Ty) {
Dan Gohman52837072008-03-24 16:55:58 +00001098 setAlignment(Align);
Benjamin Kramerf0127052010-01-05 13:12:22 +00001099 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattnerf00042a2007-02-13 07:54:42 +00001100 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001101}
1102
Victor Hernandez7b929da2009-10-23 21:09:37 +00001103void AllocaInst::setAlignment(unsigned Align) {
Dan Gohman52837072008-03-24 16:55:58 +00001104 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +00001105 assert(Align <= MaximumAlignment &&
1106 "Alignment is greater than MaximumAlignment!");
Reid Kleckner3cbfa162014-01-17 23:58:17 +00001107 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1108 (Log2_32(Align) + 1));
Dan Gohman52837072008-03-24 16:55:58 +00001109 assert(getAlignment() == Align && "Alignment representation error!");
1110}
1111
Victor Hernandez7b929da2009-10-23 21:09:37 +00001112bool AllocaInst::isArrayAllocation() const {
Reid Spencerbb9723b2007-03-01 20:27:41 +00001113 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohmane0214d52010-09-27 15:15:44 +00001114 return !CI->isOne();
Chris Lattnerb12261a2005-01-29 00:35:16 +00001115 return true;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001116}
1117
Chris Lattnerc5dd22a2008-11-26 02:54:17 +00001118/// isStaticAlloca - Return true if this alloca is in the entry block of the
1119/// function and is a constant size. If so, the code generator will fold it
1120/// into the prolog/epilog code, so it is basically free.
1121bool AllocaInst::isStaticAlloca() const {
1122 // Must be constant size.
1123 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00001124
Chris Lattnerc5dd22a2008-11-26 02:54:17 +00001125 // Must be in the entry block.
1126 const BasicBlock *Parent = getParent();
Reid Kleckner3cbfa162014-01-17 23:58:17 +00001127 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattnerc5dd22a2008-11-26 02:54:17 +00001128}
1129
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001130//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001131// LoadInst Implementation
1132//===----------------------------------------------------------------------===//
1133
Chris Lattnerb12261a2005-01-29 00:35:16 +00001134void LoadInst::AssertOK() {
Duncan Sands1df98592010-02-16 11:11:14 +00001135 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001136 "Ptr must have pointer type.");
Eli Friedman21006d42011-08-09 23:02:53 +00001137 assert(!(isAtomic() && getAlignment() == 0) &&
1138 "Alignment required for atomic load");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001139}
1140
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001141LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1142 Instruction *InsertBef)
1143 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001144
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001145LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1146 BasicBlock *InsertAE)
1147 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001148
David Blaikie14a714f2015-05-20 21:46:30 +00001149LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001150 Instruction *InsertBef)
David Blaikie14a714f2015-05-20 21:46:30 +00001151 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman21006d42011-08-09 23:02:53 +00001152
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001153LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +00001154 BasicBlock *InsertAE)
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001155 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb43c7f372007-04-22 19:24:39 +00001156
David Blaikie0f0d21e2015-04-17 19:56:21 +00001157LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb43c7f372007-04-22 19:24:39 +00001158 unsigned Align, Instruction *InsertBef)
JF Bastienb36d1a82016-04-06 21:19:33 +00001159 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001160 SyncScope::System, InsertBef) {}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001161
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001162LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman6ab2d182007-07-18 20:51:11 +00001163 unsigned Align, BasicBlock *InsertAE)
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001164 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001165 SyncScope::System, InsertAE) {}
Dan Gohman6ab2d182007-07-18 20:51:11 +00001166
David Blaikieaf102352015-04-06 20:59:48 +00001167LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +00001168 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001169 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikieaf102352015-04-06 20:59:48 +00001170 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikiee86ba4b2015-05-20 20:22:31 +00001171 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman21006d42011-08-09 23:02:53 +00001172 setVolatile(isVolatile);
1173 setAlignment(Align);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001174 setAtomic(Order, SSID);
Eli Friedman21006d42011-08-09 23:02:53 +00001175 AssertOK();
1176 setName(Name);
1177}
1178
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001179LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1180 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001181 BasicBlock *InsertAE)
James Y Knight4ed9d9e2019-01-14 21:37:53 +00001182 : UnaryInstruction(Ty, Load, Ptr, InsertAE) {
1183 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Chris Lattnerf00042a2007-02-13 07:54:42 +00001184 setVolatile(isVolatile);
Eli Friedman21006d42011-08-09 23:02:53 +00001185 setAlignment(Align);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001186 setAtomic(Order, SSID);
Chris Lattnerf00042a2007-02-13 07:54:42 +00001187 AssertOK();
1188 setName(Name);
1189}
1190
Christopher Lamb43c7f372007-04-22 19:24:39 +00001191void LoadInst::setAlignment(unsigned Align) {
1192 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +00001193 assert(Align <= MaximumAlignment &&
1194 "Alignment is greater than MaximumAlignment!");
Eli Friedman21006d42011-08-09 23:02:53 +00001195 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerb2406d92009-12-29 02:46:09 +00001196 ((Log2_32(Align)+1)<<1));
Dan Gohman138aa2a2010-07-28 20:12:04 +00001197 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb43c7f372007-04-22 19:24:39 +00001198}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001199
1200//===----------------------------------------------------------------------===//
1201// StoreInst Implementation
1202//===----------------------------------------------------------------------===//
1203
Chris Lattnerb12261a2005-01-29 00:35:16 +00001204void StoreInst::AssertOK() {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001205 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands1df98592010-02-16 11:11:14 +00001206 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerb12261a2005-01-29 00:35:16 +00001207 "Ptr must have pointer type!");
1208 assert(getOperand(0)->getType() ==
1209 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos8fabb622004-08-06 14:33:37 +00001210 && "Ptr must be a pointer to Val type!");
Eli Friedman21006d42011-08-09 23:02:53 +00001211 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Laceyefe6aa12013-12-21 00:00:49 +00001212 "Alignment required for atomic store");
Chris Lattnerb12261a2005-01-29 00:35:16 +00001213}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001214
Chris Lattnerb12261a2005-01-29 00:35:16 +00001215StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfdef53e2015-03-05 22:05:26 +00001216 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerb12261a2005-01-29 00:35:16 +00001217
1218StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfdef53e2015-03-05 22:05:26 +00001219 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerb12261a2005-01-29 00:35:16 +00001220
Misha Brukmanfd939082005-04-21 23:48:37 +00001221StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerb12261a2005-01-29 00:35:16 +00001222 Instruction *InsertBefore)
Benjamin Kramerfdef53e2015-03-05 22:05:26 +00001223 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb43c7f372007-04-22 19:24:39 +00001224
1225StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfdef53e2015-03-05 22:05:26 +00001226 BasicBlock *InsertAtEnd)
1227 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1228
1229StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1230 Instruction *InsertBefore)
JF Bastienb36d1a82016-04-06 21:19:33 +00001231 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001232 SyncScope::System, InsertBefore) {}
Benjamin Kramerfdef53e2015-03-05 22:05:26 +00001233
1234StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1235 BasicBlock *InsertAtEnd)
JF Bastienb36d1a82016-04-06 21:19:33 +00001236 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001237 SyncScope::System, InsertAtEnd) {}
Chris Lattnerb12261a2005-01-29 00:35:16 +00001238
Misha Brukmanfd939082005-04-21 23:48:37 +00001239StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +00001240 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001241 SyncScope::ID SSID,
Eli Friedman21006d42011-08-09 23:02:53 +00001242 Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00001243 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001244 OperandTraits<StoreInst>::op_begin(this),
1245 OperandTraits<StoreInst>::operands(this),
Eli Friedman21006d42011-08-09 23:02:53 +00001246 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001247 Op<0>() = val;
1248 Op<1>() = addr;
Dan Gohman6ab2d182007-07-18 20:51:11 +00001249 setVolatile(isVolatile);
1250 setAlignment(Align);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001251 setAtomic(Order, SSID);
Dan Gohman6ab2d182007-07-18 20:51:11 +00001252 AssertOK();
1253}
1254
1255StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +00001256 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001257 SyncScope::ID SSID,
Eli Friedman21006d42011-08-09 23:02:53 +00001258 BasicBlock *InsertAtEnd)
1259 : Instruction(Type::getVoidTy(val->getContext()), Store,
1260 OperandTraits<StoreInst>::op_begin(this),
1261 OperandTraits<StoreInst>::operands(this),
1262 InsertAtEnd) {
1263 Op<0>() = val;
1264 Op<1>() = addr;
1265 setVolatile(isVolatile);
1266 setAlignment(Align);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001267 setAtomic(Order, SSID);
Chris Lattnerb12261a2005-01-29 00:35:16 +00001268 AssertOK();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001269}
1270
Christopher Lamb43c7f372007-04-22 19:24:39 +00001271void StoreInst::setAlignment(unsigned Align) {
1272 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +00001273 assert(Align <= MaximumAlignment &&
1274 "Alignment is greater than MaximumAlignment!");
Eli Friedman21006d42011-08-09 23:02:53 +00001275 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerb2406d92009-12-29 02:46:09 +00001276 ((Log2_32(Align)+1) << 1));
Dan Gohman138aa2a2010-07-28 20:12:04 +00001277 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb43c7f372007-04-22 19:24:39 +00001278}
1279
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001280//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +00001281// AtomicCmpXchgInst Implementation
1282//===----------------------------------------------------------------------===//
1283
1284void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northoverca396e32014-03-11 10:48:52 +00001285 AtomicOrdering SuccessOrdering,
1286 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001287 SyncScope::ID SSID) {
Eli Friedmanff030482011-07-28 21:48:00 +00001288 Op<0>() = Ptr;
1289 Op<1>() = Cmp;
1290 Op<2>() = NewVal;
Tim Northoverca396e32014-03-11 10:48:52 +00001291 setSuccessOrdering(SuccessOrdering);
1292 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001293 setSyncScopeID(SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001294
1295 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1296 "All operands must be non-null!");
1297 assert(getOperand(0)->getType()->isPointerTy() &&
1298 "Ptr must have pointer type!");
1299 assert(getOperand(1)->getType() ==
1300 cast<PointerType>(getOperand(0)->getType())->getElementType()
1301 && "Ptr must be a pointer to Cmp type!");
1302 assert(getOperand(2)->getType() ==
1303 cast<PointerType>(getOperand(0)->getType())->getElementType()
1304 && "Ptr must be a pointer to NewVal type!");
JF Bastienb36d1a82016-04-06 21:19:33 +00001305 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanff030482011-07-28 21:48:00 +00001306 "AtomicCmpXchg instructions must be atomic!");
JF Bastienb36d1a82016-04-06 21:19:33 +00001307 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northoverca396e32014-03-11 10:48:52 +00001308 "AtomicCmpXchg instructions must be atomic!");
JF Bastienb36d1a82016-04-06 21:19:33 +00001309 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1310 "AtomicCmpXchg failure argument shall be no stronger than the success "
1311 "argument");
1312 assert(FailureOrdering != AtomicOrdering::Release &&
1313 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northoverca396e32014-03-11 10:48:52 +00001314 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanff030482011-07-28 21:48:00 +00001315}
1316
1317AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northoverca396e32014-03-11 10:48:52 +00001318 AtomicOrdering SuccessOrdering,
1319 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001320 SyncScope::ID SSID,
Eli Friedmanff030482011-07-28 21:48:00 +00001321 Instruction *InsertBefore)
Tim Northover8f2a85e2014-06-13 14:24:07 +00001322 : Instruction(
Serge Gueltond35f86e2017-05-09 19:31:13 +00001323 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover8f2a85e2014-06-13 14:24:07 +00001324 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1325 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001326 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001327}
1328
1329AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northoverca396e32014-03-11 10:48:52 +00001330 AtomicOrdering SuccessOrdering,
1331 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001332 SyncScope::ID SSID,
Eli Friedmanff030482011-07-28 21:48:00 +00001333 BasicBlock *InsertAtEnd)
Tim Northover8f2a85e2014-06-13 14:24:07 +00001334 : Instruction(
Serge Gueltond35f86e2017-05-09 19:31:13 +00001335 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover8f2a85e2014-06-13 14:24:07 +00001336 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1337 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001338 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001339}
Tim Northover8f2a85e2014-06-13 14:24:07 +00001340
Eli Friedmanff030482011-07-28 21:48:00 +00001341//===----------------------------------------------------------------------===//
1342// AtomicRMWInst Implementation
1343//===----------------------------------------------------------------------===//
1344
1345void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1346 AtomicOrdering Ordering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001347 SyncScope::ID SSID) {
Eli Friedmanff030482011-07-28 21:48:00 +00001348 Op<0>() = Ptr;
1349 Op<1>() = Val;
1350 setOperation(Operation);
1351 setOrdering(Ordering);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001352 setSyncScopeID(SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001353
1354 assert(getOperand(0) && getOperand(1) &&
1355 "All operands must be non-null!");
1356 assert(getOperand(0)->getType()->isPointerTy() &&
1357 "Ptr must have pointer type!");
1358 assert(getOperand(1)->getType() ==
1359 cast<PointerType>(getOperand(0)->getType())->getElementType()
1360 && "Ptr must be a pointer to Val type!");
JF Bastienb36d1a82016-04-06 21:19:33 +00001361 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanff030482011-07-28 21:48:00 +00001362 "AtomicRMW instructions must be atomic!");
1363}
1364
1365AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1366 AtomicOrdering Ordering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001367 SyncScope::ID SSID,
Eli Friedmanff030482011-07-28 21:48:00 +00001368 Instruction *InsertBefore)
1369 : Instruction(Val->getType(), AtomicRMW,
1370 OperandTraits<AtomicRMWInst>::op_begin(this),
1371 OperandTraits<AtomicRMWInst>::operands(this),
1372 InsertBefore) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001373 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001374}
1375
1376AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1377 AtomicOrdering Ordering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001378 SyncScope::ID SSID,
Eli Friedmanff030482011-07-28 21:48:00 +00001379 BasicBlock *InsertAtEnd)
1380 : Instruction(Val->getType(), AtomicRMW,
1381 OperandTraits<AtomicRMWInst>::op_begin(this),
1382 OperandTraits<AtomicRMWInst>::operands(this),
1383 InsertAtEnd) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001384 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanff030482011-07-28 21:48:00 +00001385}
1386
Matt Arsenault2c2c51b2018-10-02 23:44:11 +00001387StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1388 switch (Op) {
1389 case AtomicRMWInst::Xchg:
1390 return "xchg";
1391 case AtomicRMWInst::Add:
1392 return "add";
1393 case AtomicRMWInst::Sub:
1394 return "sub";
1395 case AtomicRMWInst::And:
1396 return "and";
1397 case AtomicRMWInst::Nand:
1398 return "nand";
1399 case AtomicRMWInst::Or:
1400 return "or";
1401 case AtomicRMWInst::Xor:
1402 return "xor";
1403 case AtomicRMWInst::Max:
1404 return "max";
1405 case AtomicRMWInst::Min:
1406 return "min";
1407 case AtomicRMWInst::UMax:
1408 return "umax";
1409 case AtomicRMWInst::UMin:
1410 return "umin";
1411 case AtomicRMWInst::BAD_BINOP:
1412 return "<invalid operation>";
1413 }
1414
1415 llvm_unreachable("invalid atomicrmw operation");
1416}
1417
Eli Friedmanff030482011-07-28 21:48:00 +00001418//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +00001419// FenceInst Implementation
1420//===----------------------------------------------------------------------===//
1421
Fangrui Songaf7b1832018-07-30 19:41:25 +00001422FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001423 SyncScope::ID SSID,
Eli Friedman47f35132011-07-25 23:16:38 +00001424 Instruction *InsertBefore)
Craig Topperec0f0bc2014-04-09 06:08:46 +00001425 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedman47f35132011-07-25 23:16:38 +00001426 setOrdering(Ordering);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001427 setSyncScopeID(SSID);
Eli Friedman47f35132011-07-25 23:16:38 +00001428}
1429
Fangrui Songaf7b1832018-07-30 19:41:25 +00001430FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001431 SyncScope::ID SSID,
Eli Friedman47f35132011-07-25 23:16:38 +00001432 BasicBlock *InsertAtEnd)
Craig Topperec0f0bc2014-04-09 06:08:46 +00001433 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedman47f35132011-07-25 23:16:38 +00001434 setOrdering(Ordering);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00001435 setSyncScopeID(SSID);
Eli Friedman47f35132011-07-25 23:16:38 +00001436}
1437
1438//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001439// GetElementPtrInst Implementation
1440//===----------------------------------------------------------------------===//
1441
Jay Foada9203102011-07-25 09:48:08 +00001442void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001443 const Twine &Name) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +00001444 assert(getNumOperands() == 1 + IdxList.size() &&
1445 "NumOperands not initialized?");
Pete Cooper10520422015-05-21 22:48:54 +00001446 Op<0>() = Ptr;
Fangrui Song53a62242018-11-17 01:44:25 +00001447 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman338169d2008-06-04 16:14:12 +00001448 setName(Name);
Chris Lattner38bacf22005-05-03 05:43:30 +00001449}
1450
Gabor Greifefe65362008-05-10 08:32:32 +00001451GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikiea67d5ab2015-05-05 18:03:48 +00001452 : Instruction(GEPI.getType(), GetElementPtr,
1453 OperandTraits<GetElementPtrInst>::op_end(this) -
1454 GEPI.getNumOperands(),
1455 GEPI.getNumOperands()),
David Blaikied84c8ef2015-06-01 03:09:34 +00001456 SourceElementType(GEPI.SourceElementType),
1457 ResultElementType(GEPI.ResultElementType) {
Jay Foada9203102011-07-25 09:48:08 +00001458 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001459 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greifefe65362008-05-10 08:32:32 +00001460}
1461
Chris Lattnerc66996a2009-03-09 04:46:40 +00001462/// getIndexedType - Returns the type of the element that would be accessed with
1463/// a gep instruction with the specified parameters.
1464///
1465/// The Idxs pointer should point to a continuous piece of memory containing the
1466/// indices, either as Value* or uint64_t.
1467///
1468/// A null type is returned if the indices are invalid for the specified
1469/// pointer type.
1470///
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001471template <typename IndexTy>
David Blaikie25e3d2d2015-03-30 21:41:43 +00001472static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattnerc66996a2009-03-09 04:46:40 +00001473 // Handle the special case of the empty set index set, which is always valid.
Jay Foada9203102011-07-25 09:48:08 +00001474 if (IdxList.empty())
Dan Gohman041e2eb2008-05-15 19:50:34 +00001475 return Agg;
Nadav Rotem16087692011-12-05 06:29:09 +00001476
Chris Lattnerc66996a2009-03-09 04:46:40 +00001477 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner1afcace2011-07-09 17:41:24 +00001478 // it cannot be 'stepped over'.
1479 if (!Agg->isSized())
Craig Topperec0f0bc2014-04-09 06:08:46 +00001480 return nullptr;
Misha Brukmanfd939082005-04-21 23:48:37 +00001481
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001482 unsigned CurIdx = 1;
Jay Foada9203102011-07-25 09:48:08 +00001483 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001484 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperec0f0bc2014-04-09 06:08:46 +00001485 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foada9203102011-07-25 09:48:08 +00001486 IndexTy Index = IdxList[CurIdx];
Craig Topperec0f0bc2014-04-09 06:08:46 +00001487 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001488 Agg = CT->getTypeAtIndex(Index);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001489 }
Craig Topperec0f0bc2014-04-09 06:08:46 +00001490 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001491}
1492
David Blaikie25e3d2d2015-03-30 21:41:43 +00001493Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1494 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001495}
1496
David Blaikie25e3d2d2015-03-30 21:41:43 +00001497Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foada9203102011-07-25 09:48:08 +00001498 ArrayRef<Constant *> IdxList) {
David Blaikie25e3d2d2015-03-30 21:41:43 +00001499 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad25052d82011-01-14 08:07:43 +00001500}
1501
David Blaikie25e3d2d2015-03-30 21:41:43 +00001502Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1503 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001504}
1505
Chris Lattner6f771d42007-04-14 00:12:57 +00001506/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1507/// zeros. If so, the result pointer and the first operand have the same
1508/// value, just potentially different types.
1509bool GetElementPtrInst::hasAllZeroIndices() const {
1510 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1511 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1512 if (!CI->isZero()) return false;
1513 } else {
1514 return false;
1515 }
1516 }
1517 return true;
1518}
1519
Chris Lattner6b0974c2007-04-27 20:35:56 +00001520/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1521/// constant integers. If so, the result pointer and the first operand have
1522/// a constant offset between them.
1523bool GetElementPtrInst::hasAllConstantIndices() const {
1524 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1525 if (!isa<ConstantInt>(getOperand(i)))
1526 return false;
1527 }
1528 return true;
1529}
1530
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001531void GetElementPtrInst::setIsInBounds(bool B) {
1532 cast<GEPOperator>(this)->setIsInBounds(B);
1533}
Chris Lattner6f771d42007-04-14 00:12:57 +00001534
Nick Lewyckyae05e7d2009-09-27 21:33:04 +00001535bool GetElementPtrInst::isInBounds() const {
1536 return cast<GEPOperator>(this)->isInBounds();
1537}
1538
Chandler Carruth4ced4ee2012-12-11 10:29:10 +00001539bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1540 APInt &Offset) const {
Chandler Carruth7550f962012-12-11 11:05:15 +00001541 // Delegate to the generic GEPOperator implementation.
1542 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth4ced4ee2012-12-11 10:29:10 +00001543}
1544
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001545//===----------------------------------------------------------------------===//
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001546// ExtractElementInst Implementation
1547//===----------------------------------------------------------------------===//
1548
1549ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001550 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001551 Instruction *InsertBef)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001552 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +00001553 ExtractElement,
1554 OperandTraits<ExtractElementInst>::op_begin(this),
1555 2, InsertBef) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001556 assert(isValidOperands(Val, Index) &&
1557 "Invalid extractelement instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001558 Op<0>() = Val;
1559 Op<1>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001560 setName(Name);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001561}
1562
1563ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001564 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001565 BasicBlock *InsertAE)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001566 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +00001567 ExtractElement,
1568 OperandTraits<ExtractElementInst>::op_begin(this),
1569 2, InsertAE) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001570 assert(isValidOperands(Val, Index) &&
1571 "Invalid extractelement instruction operands!");
1572
Gabor Greif6c80c382008-05-26 21:33:52 +00001573 Op<0>() = Val;
1574 Op<1>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001575 setName(Name);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001576}
1577
Chris Lattnerd2325d02006-04-08 04:05:48 +00001578bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencerd4b4f2d2014-05-01 22:12:39 +00001579 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattnerd2325d02006-04-08 04:05:48 +00001580 return false;
1581 return true;
1582}
1583
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001584//===----------------------------------------------------------------------===//
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001585// InsertElementInst Implementation
1586//===----------------------------------------------------------------------===//
1587
Chris Lattnerd2325d02006-04-08 04:05:48 +00001588InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001589 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001590 Instruction *InsertBef)
Gabor Greifefe65362008-05-10 08:32:32 +00001591 : Instruction(Vec->getType(), InsertElement,
1592 OperandTraits<InsertElementInst>::op_begin(this),
1593 3, InsertBef) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001594 assert(isValidOperands(Vec, Elt, Index) &&
1595 "Invalid insertelement instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001596 Op<0>() = Vec;
1597 Op<1>() = Elt;
1598 Op<2>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001599 setName(Name);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001600}
1601
Chris Lattnerd2325d02006-04-08 04:05:48 +00001602InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001603 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001604 BasicBlock *InsertAE)
Gabor Greifefe65362008-05-10 08:32:32 +00001605 : Instruction(Vec->getType(), InsertElement,
1606 OperandTraits<InsertElementInst>::op_begin(this),
1607 3, InsertAE) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001608 assert(isValidOperands(Vec, Elt, Index) &&
1609 "Invalid insertelement instruction operands!");
1610
Gabor Greif6c80c382008-05-26 21:33:52 +00001611 Op<0>() = Vec;
1612 Op<1>() = Elt;
1613 Op<2>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001614 setName(Name);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001615}
1616
Fangrui Songaf7b1832018-07-30 19:41:25 +00001617bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattnerd2325d02006-04-08 04:05:48 +00001618 const Value *Index) {
Duncan Sands1df98592010-02-16 11:11:14 +00001619 if (!Vec->getType()->isVectorTy())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001620 return false; // First operand of insertelement must be vector type.
Fangrui Songaf7b1832018-07-30 19:41:25 +00001621
Reid Spencer9d6565a2007-02-15 02:26:10 +00001622 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohman86296cc2007-05-11 21:43:24 +00001623 return false;// Second operand of insertelement must be vector element type.
Fangrui Songaf7b1832018-07-30 19:41:25 +00001624
Michael J. Spencerd4b4f2d2014-05-01 22:12:39 +00001625 if (!Index->getType()->isIntegerTy())
Dan Gohmana119de82009-06-14 23:30:43 +00001626 return false; // Third operand of insertelement must be i32.
Chris Lattnerd2325d02006-04-08 04:05:48 +00001627 return true;
1628}
1629
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001630//===----------------------------------------------------------------------===//
Chris Lattner00f10232006-04-08 01:18:18 +00001631// ShuffleVectorInst Implementation
1632//===----------------------------------------------------------------------===//
1633
1634ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001635 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001636 Instruction *InsertBefore)
Owen Andersondebcb012009-07-29 22:17:13 +00001637: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wangaeb06d22008-11-10 04:46:22 +00001638 cast<VectorType>(Mask->getType())->getNumElements()),
1639 ShuffleVector,
1640 OperandTraits<ShuffleVectorInst>::op_begin(this),
1641 OperandTraits<ShuffleVectorInst>::operands(this),
1642 InsertBefore) {
Chris Lattner00f10232006-04-08 01:18:18 +00001643 assert(isValidOperands(V1, V2, Mask) &&
1644 "Invalid shuffle vector instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001645 Op<0>() = V1;
1646 Op<1>() = V2;
1647 Op<2>() = Mask;
Chris Lattner910c80a2007-02-24 00:55:48 +00001648 setName(Name);
Chris Lattner00f10232006-04-08 01:18:18 +00001649}
1650
1651ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001652 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001653 BasicBlock *InsertAtEnd)
Dan Gohman638d8532009-08-25 23:27:45 +00001654: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1655 cast<VectorType>(Mask->getType())->getNumElements()),
1656 ShuffleVector,
1657 OperandTraits<ShuffleVectorInst>::op_begin(this),
1658 OperandTraits<ShuffleVectorInst>::operands(this),
1659 InsertAtEnd) {
Chris Lattner00f10232006-04-08 01:18:18 +00001660 assert(isValidOperands(V1, V2, Mask) &&
1661 "Invalid shuffle vector instruction operands!");
1662
Gabor Greif6c80c382008-05-26 21:33:52 +00001663 Op<0>() = V1;
1664 Op<1>() = V2;
1665 Op<2>() = Mask;
Chris Lattner910c80a2007-02-24 00:55:48 +00001666 setName(Name);
Chris Lattner00f10232006-04-08 01:18:18 +00001667}
1668
Mon P Wangaeb06d22008-11-10 04:46:22 +00001669bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattner00f10232006-04-08 01:18:18 +00001670 const Value *Mask) {
Chris Lattner83694a92012-01-25 23:49:49 +00001671 // V1 and V2 must be vectors of the same type.
Duncan Sands1df98592010-02-16 11:11:14 +00001672 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattner8728f192008-03-02 05:28:33 +00001673 return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00001674
Chris Lattner83694a92012-01-25 23:49:49 +00001675 // Mask must be vector of i32.
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001676 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperec0f0bc2014-04-09 06:08:46 +00001677 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattner00f10232006-04-08 01:18:18 +00001678 return false;
Nate Begemana966af22010-08-13 00:16:46 +00001679
1680 // Check to see if Mask is valid.
Chris Lattner83694a92012-01-25 23:49:49 +00001681 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1682 return true;
1683
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001684 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001685 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +00001686 for (Value *Op : MV->operands()) {
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001687 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001688 if (CI->uge(V1Size*2))
Nate Begemana966af22010-08-13 00:16:46 +00001689 return false;
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +00001690 } else if (!isa<UndefValue>(Op)) {
Nate Begemana966af22010-08-13 00:16:46 +00001691 return false;
1692 }
1693 }
Chris Lattner83694a92012-01-25 23:49:49 +00001694 return true;
Mon P Wangcf62b372011-10-26 00:34:48 +00001695 }
Fangrui Songaf7b1832018-07-30 19:41:25 +00001696
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001697 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001698 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1699 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1700 if (CDS->getElementAsInteger(i) >= V1Size*2)
1701 return false;
1702 return true;
1703 }
Fangrui Songaf7b1832018-07-30 19:41:25 +00001704
Chris Lattner83694a92012-01-25 23:49:49 +00001705 // The bitcode reader can create a place holder for a forward reference
1706 // used as the shuffle mask. When this occurs, the shuffle mask will
1707 // fall into this case and fail. To avoid this error, do this bit of
1708 // ugliness to allow such a mask pass.
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001709 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner83694a92012-01-25 23:49:49 +00001710 if (CE->getOpcode() == Instruction::UserOp1)
1711 return true;
1712
1713 return false;
Chris Lattner00f10232006-04-08 01:18:18 +00001714}
1715
Sanjay Patel42f462a2018-06-19 18:44:00 +00001716int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattner56243b82012-01-26 02:51:13 +00001717 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001718 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner83694a92012-01-25 23:49:49 +00001719 return CDS->getElementAsInteger(i);
Chris Lattner56243b82012-01-26 02:51:13 +00001720 Constant *C = Mask->getAggregateElement(i);
Chris Lattner83694a92012-01-25 23:49:49 +00001721 if (isa<UndefValue>(C))
Chris Lattner8728f192008-03-02 05:28:33 +00001722 return -1;
Chris Lattner83694a92012-01-25 23:49:49 +00001723 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattner8728f192008-03-02 05:28:33 +00001724}
1725
Sanjay Patel42f462a2018-06-19 18:44:00 +00001726void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattner56243b82012-01-26 02:51:13 +00001727 SmallVectorImpl<int> &Result) {
1728 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songaf7b1832018-07-30 19:41:25 +00001729
Sanjay Patelaf30bb92017-04-19 16:22:19 +00001730 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001731 for (unsigned i = 0; i != NumElts; ++i)
1732 Result.push_back(CDS->getElementAsInteger(i));
1733 return;
Fangrui Songaf7b1832018-07-30 19:41:25 +00001734 }
Chris Lattner83694a92012-01-25 23:49:49 +00001735 for (unsigned i = 0; i != NumElts; ++i) {
1736 Constant *C = Mask->getAggregateElement(i);
1737 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner220dfa72012-01-26 00:41:50 +00001738 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner83694a92012-01-25 23:49:49 +00001739 }
1740}
1741
Sanjay Patel14ea0082018-08-30 15:05:38 +00001742static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel42f462a2018-06-19 18:44:00 +00001743 assert(!Mask.empty() && "Shuffle mask must contain elements");
1744 bool UsesLHS = false;
1745 bool UsesRHS = false;
Sanjay Patel14ea0082018-08-30 15:05:38 +00001746 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel42f462a2018-06-19 18:44:00 +00001747 if (Mask[i] == -1)
1748 continue;
Sanjay Patel14ea0082018-08-30 15:05:38 +00001749 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel42f462a2018-06-19 18:44:00 +00001750 "Out-of-bounds shuffle mask element");
Sanjay Patel14ea0082018-08-30 15:05:38 +00001751 UsesLHS |= (Mask[i] < NumOpElts);
1752 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel42f462a2018-06-19 18:44:00 +00001753 if (UsesLHS && UsesRHS)
1754 return false;
1755 }
1756 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1757 return true;
1758}
1759
Sanjay Patel14ea0082018-08-30 15:05:38 +00001760bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1761 // We don't have vector operand size information, so assume operands are the
1762 // same size as the mask.
1763 return isSingleSourceMaskImpl(Mask, Mask.size());
1764}
1765
1766static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1767 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel42f462a2018-06-19 18:44:00 +00001768 return false;
Sanjay Patel14ea0082018-08-30 15:05:38 +00001769 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel42f462a2018-06-19 18:44:00 +00001770 if (Mask[i] == -1)
1771 continue;
Sanjay Patel14ea0082018-08-30 15:05:38 +00001772 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel42f462a2018-06-19 18:44:00 +00001773 return false;
1774 }
1775 return true;
1776}
1777
Sanjay Patel14ea0082018-08-30 15:05:38 +00001778bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1779 // We don't have vector operand size information, so assume operands are the
1780 // same size as the mask.
1781 return isIdentityMaskImpl(Mask, Mask.size());
1782}
1783
Sanjay Patel42f462a2018-06-19 18:44:00 +00001784bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1785 if (!isSingleSourceMask(Mask))
1786 return false;
1787 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1788 if (Mask[i] == -1)
1789 continue;
1790 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1791 return false;
1792 }
1793 return true;
1794}
1795
1796bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1797 if (!isSingleSourceMask(Mask))
1798 return false;
1799 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1800 if (Mask[i] == -1)
1801 continue;
1802 if (Mask[i] != 0 && Mask[i] != NumElts)
1803 return false;
1804 }
1805 return true;
1806}
1807
1808bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1809 // Select is differentiated from identity. It requires using both sources.
1810 if (isSingleSourceMask(Mask))
1811 return false;
1812 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1813 if (Mask[i] == -1)
1814 continue;
1815 if (Mask[i] != i && Mask[i] != (NumElts + i))
1816 return false;
1817 }
1818 return true;
1819}
1820
1821bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1822 // Example masks that will return true:
1823 // v1 = <a, b, c, d>
1824 // v2 = <e, f, g, h>
1825 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1826 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1827
1828 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1829 int NumElts = Mask.size();
1830 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1831 return false;
1832
1833 // 2. The first element of the mask must be either a 0 or a 1.
1834 if (Mask[0] != 0 && Mask[0] != 1)
1835 return false;
1836
1837 // 3. The difference between the first 2 elements must be equal to the
1838 // number of elements in the mask.
1839 if ((Mask[1] - Mask[0]) != NumElts)
1840 return false;
1841
1842 // 4. The difference between consecutive even-numbered and odd-numbered
1843 // elements must be equal to 2.
1844 for (int i = 2; i < NumElts; ++i) {
1845 int MaskEltVal = Mask[i];
1846 if (MaskEltVal == -1)
1847 return false;
1848 int MaskEltPrevVal = Mask[i - 2];
1849 if (MaskEltVal - MaskEltPrevVal != 2)
1850 return false;
1851 }
1852 return true;
1853}
1854
Simon Pilgrim3ccdf222018-11-09 16:28:19 +00001855bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
1856 int NumSrcElts, int &Index) {
1857 // Must extract from a single source.
1858 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
1859 return false;
1860
1861 // Must be smaller (else this is an Identity shuffle).
Fangrui Songb9e11332018-11-09 16:45:37 +00001862 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrim3ccdf222018-11-09 16:28:19 +00001863 return false;
1864
1865 // Find start of extraction, accounting that we may start with an UNDEF.
1866 int SubIndex = -1;
1867 for (int i = 0, e = Mask.size(); i != e; ++i) {
1868 int M = Mask[i];
1869 if (M < 0)
1870 continue;
1871 int Offset = (M % NumSrcElts) - i;
1872 if (0 <= SubIndex && SubIndex != Offset)
1873 return false;
1874 SubIndex = Offset;
1875 }
1876
1877 if (0 <= SubIndex) {
1878 Index = SubIndex;
1879 return true;
1880 }
1881 return false;
1882}
1883
Sanjay Patel14ea0082018-08-30 15:05:38 +00001884bool ShuffleVectorInst::isIdentityWithPadding() const {
1885 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1886 int NumMaskElts = getType()->getVectorNumElements();
1887 if (NumMaskElts <= NumOpElts)
1888 return false;
1889
1890 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patele2e1cab2018-08-30 16:44:07 +00001891 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patel14ea0082018-08-30 15:05:38 +00001892 if (!isIdentityMaskImpl(Mask, NumOpElts))
1893 return false;
1894
1895 // All extending must be with undef elements.
1896 for (int i = NumOpElts; i < NumMaskElts; ++i)
1897 if (Mask[i] != -1)
1898 return false;
1899
1900 return true;
1901}
1902
1903bool ShuffleVectorInst::isIdentityWithExtract() const {
1904 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1905 int NumMaskElts = getType()->getVectorNumElements();
1906 if (NumMaskElts >= NumOpElts)
1907 return false;
1908
1909 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
1910}
Sanjay Patel42f462a2018-06-19 18:44:00 +00001911
Sanjay Patel1c86ce82018-09-20 15:21:52 +00001912bool ShuffleVectorInst::isConcat() const {
1913 // Vector concatenation is differentiated from identity with padding.
1914 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
1915 return false;
1916
1917 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1918 int NumMaskElts = getType()->getVectorNumElements();
1919 if (NumMaskElts != NumOpElts * 2)
1920 return false;
1921
1922 // Use the mask length rather than the operands' vector lengths here. We
1923 // already know that the shuffle returns a vector twice as long as the inputs,
1924 // and neither of the inputs are undef vectors. If the mask picks consecutive
1925 // elements from both inputs, then this is a concatenation of the inputs.
1926 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
1927}
1928
Dan Gohman041e2eb2008-05-15 19:50:34 +00001929//===----------------------------------------------------------------------===//
Dan Gohmane4569942008-05-23 00:36:11 +00001930// InsertValueInst Class
1931//===----------------------------------------------------------------------===//
1932
Fangrui Songaf7b1832018-07-30 19:41:25 +00001933void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001934 const Twine &Name) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +00001935 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foadfc6d3a42011-07-13 10:26:04 +00001936
1937 // There's no fundamental reason why we require at least one index
1938 // (other than weirdness with &*IdxBegin being invalid; see
1939 // getelementptr's init routine for example). But there's no
1940 // present need to support it.
Eugene Zelenko46795222017-05-15 21:57:41 +00001941 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foadfc6d3a42011-07-13 10:26:04 +00001942
1943 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommela4805cf2010-12-05 20:50:26 +00001944 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001945 Op<0>() = Agg;
1946 Op<1>() = Val;
Dan Gohmane4569942008-05-23 00:36:11 +00001947
Jay Foadfc6d3a42011-07-13 10:26:04 +00001948 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001949 setName(Name);
Dan Gohmane4569942008-05-23 00:36:11 +00001950}
1951
1952InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greifb9d9eba2008-05-27 11:03:29 +00001953 : Instruction(IVI.getType(), InsertValue,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001954 OperandTraits<InsertValueInst>::op_begin(this), 2),
1955 Indices(IVI.Indices) {
Dan Gohman80b96262008-06-17 23:25:49 +00001956 Op<0>() = IVI.getOperand(0);
1957 Op<1>() = IVI.getOperand(1);
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001958 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohmane4569942008-05-23 00:36:11 +00001959}
1960
1961//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001962// ExtractValueInst Class
1963//===----------------------------------------------------------------------===//
1964
Jay Foadfc6d3a42011-07-13 10:26:04 +00001965void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +00001966 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohmane4569942008-05-23 00:36:11 +00001967
Jay Foadfc6d3a42011-07-13 10:26:04 +00001968 // There's no fundamental reason why we require at least one index.
1969 // But there's no present need to support it.
Eugene Zelenko46795222017-05-15 21:57:41 +00001970 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohmane4569942008-05-23 00:36:11 +00001971
Jay Foadfc6d3a42011-07-13 10:26:04 +00001972 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001973 setName(Name);
Dan Gohmane4569942008-05-23 00:36:11 +00001974}
1975
1976ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001977 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001978 Indices(EVI.Indices) {
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001979 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohmane4569942008-05-23 00:36:11 +00001980}
1981
Dan Gohman041e2eb2008-05-15 19:50:34 +00001982// getIndexedType - Returns the type of the element that would be extracted
1983// with an extractvalue instruction with the specified parameters.
1984//
1985// A null type is returned if the indices are invalid for the specified
1986// pointer type.
1987//
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001988Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001989 ArrayRef<unsigned> Idxs) {
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +00001990 for (unsigned Index : Idxs) {
Frits van Bommela4805cf2010-12-05 20:50:26 +00001991 // We can't use CompositeType::indexValid(Index) here.
1992 // indexValid() always returns true for arrays because getelementptr allows
1993 // out-of-bounds indices. Since we don't allow those for extractvalue and
1994 // insertvalue we need to check array indexing manually.
1995 // Since the only other types we can index into are struct types it's just
1996 // as easy to check those manually as well.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001997 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommela4805cf2010-12-05 20:50:26 +00001998 if (Index >= AT->getNumElements())
Craig Topperec0f0bc2014-04-09 06:08:46 +00001999 return nullptr;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002000 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommela4805cf2010-12-05 20:50:26 +00002001 if (Index >= ST->getNumElements())
Craig Topperec0f0bc2014-04-09 06:08:46 +00002002 return nullptr;
Frits van Bommela4805cf2010-12-05 20:50:26 +00002003 } else {
2004 // Not a valid type to index into.
Craig Topperec0f0bc2014-04-09 06:08:46 +00002005 return nullptr;
Frits van Bommela4805cf2010-12-05 20:50:26 +00002006 }
2007
2008 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002009 }
Chris Lattner1afcace2011-07-09 17:41:24 +00002010 return const_cast<Type*>(Agg);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002011}
Chris Lattner00f10232006-04-08 01:18:18 +00002012
2013//===----------------------------------------------------------------------===//
Cameron McInallyca8cb682018-11-13 18:15:47 +00002014// UnaryOperator Class
2015//===----------------------------------------------------------------------===//
2016
2017UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2018 Type *Ty, const Twine &Name,
2019 Instruction *InsertBefore)
2020 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2021 Op<0>() = S;
2022 setName(Name);
2023 AssertOK();
2024}
2025
2026UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2027 Type *Ty, const Twine &Name,
2028 BasicBlock *InsertAtEnd)
2029 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2030 Op<0>() = S;
2031 setName(Name);
2032 AssertOK();
2033}
2034
2035UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2036 const Twine &Name,
2037 Instruction *InsertBefore) {
2038 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2039}
2040
2041UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2042 const Twine &Name,
2043 BasicBlock *InsertAtEnd) {
2044 UnaryOperator *Res = Create(Op, S, Name);
2045 InsertAtEnd->getInstList().push_back(Res);
2046 return Res;
2047}
2048
2049void UnaryOperator::AssertOK() {
2050 Value *LHS = getOperand(0);
2051 (void)LHS; // Silence warnings.
2052#ifndef NDEBUG
2053 switch (getOpcode()) {
2054 case FNeg:
2055 assert(getType() == LHS->getType() &&
2056 "Unary operation should return same type as operand!");
2057 assert(getType()->isFPOrFPVectorTy() &&
2058 "Tried to create a floating-point operation on a "
2059 "non-floating-point type!");
2060 break;
2061 default: llvm_unreachable("Invalid opcode provided");
2062 }
2063#endif
2064}
2065
2066//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002067// BinaryOperator Class
2068//===----------------------------------------------------------------------===//
2069
Chris Lattner910c80a2007-02-24 00:55:48 +00002070BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002071 Type *Ty, const Twine &Name,
Chris Lattner910c80a2007-02-24 00:55:48 +00002072 Instruction *InsertBefore)
Dan Gohman1eaac532010-05-03 22:44:19 +00002073 : Instruction(Ty, iType,
Gabor Greifefe65362008-05-10 08:32:32 +00002074 OperandTraits<BinaryOperator>::op_begin(this),
2075 OperandTraits<BinaryOperator>::operands(this),
2076 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00002077 Op<0>() = S1;
2078 Op<1>() = S2;
Chris Lattner910c80a2007-02-24 00:55:48 +00002079 setName(Name);
Craig Topper8edc5b12017-06-26 07:15:59 +00002080 AssertOK();
Chris Lattner910c80a2007-02-24 00:55:48 +00002081}
2082
Fangrui Songaf7b1832018-07-30 19:41:25 +00002083BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002084 Type *Ty, const Twine &Name,
Chris Lattner910c80a2007-02-24 00:55:48 +00002085 BasicBlock *InsertAtEnd)
Dan Gohman1eaac532010-05-03 22:44:19 +00002086 : Instruction(Ty, iType,
Gabor Greifefe65362008-05-10 08:32:32 +00002087 OperandTraits<BinaryOperator>::op_begin(this),
2088 OperandTraits<BinaryOperator>::operands(this),
2089 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00002090 Op<0>() = S1;
2091 Op<1>() = S2;
Chris Lattner910c80a2007-02-24 00:55:48 +00002092 setName(Name);
Craig Topper8edc5b12017-06-26 07:15:59 +00002093 AssertOK();
Chris Lattner910c80a2007-02-24 00:55:48 +00002094}
2095
Craig Topper8edc5b12017-06-26 07:15:59 +00002096void BinaryOperator::AssertOK() {
Chris Lattnerb12261a2005-01-29 00:35:16 +00002097 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +00002098 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerb12261a2005-01-29 00:35:16 +00002099 assert(LHS->getType() == RHS->getType() &&
2100 "Binary operator operand types must match!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002101#ifndef NDEBUG
Craig Topper8edc5b12017-06-26 07:15:59 +00002102 switch (getOpcode()) {
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002103 case Add: case Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002104 case Mul:
Chris Lattnerb12261a2005-01-29 00:35:16 +00002105 assert(getType() == LHS->getType() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002106 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002107 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002108 "Tried to create an integer operation on a non-integer type!");
2109 break;
2110 case FAdd: case FSub:
2111 case FMul:
2112 assert(getType() == LHS->getType() &&
2113 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002114 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002115 "Tried to create a floating-point operation on a "
2116 "non-floating-point type!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002117 break;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002118 case UDiv:
2119 case SDiv:
Reid Spencer1628cec2006-10-26 06:15:43 +00002120 assert(getType() == LHS->getType() &&
2121 "Arithmetic operation should return same type as operands!");
Craig Topper0b2cfb72017-06-25 17:33:48 +00002122 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00002123 "Incorrect operand type (not integer) for S/UDIV");
2124 break;
2125 case FDiv:
2126 assert(getType() == LHS->getType() &&
2127 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002128 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00002129 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer1628cec2006-10-26 06:15:43 +00002130 break;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002131 case URem:
2132 case SRem:
Reid Spencer0a783f72006-11-02 01:53:59 +00002133 assert(getType() == LHS->getType() &&
2134 "Arithmetic operation should return same type as operands!");
Craig Topper0b2cfb72017-06-25 17:33:48 +00002135 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00002136 "Incorrect operand type (not integer) for S/UREM");
2137 break;
2138 case FRem:
2139 assert(getType() == LHS->getType() &&
2140 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002141 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00002142 "Incorrect operand type (not floating point) for FREM");
Reid Spencer0a783f72006-11-02 01:53:59 +00002143 break;
Reid Spencer832254e2007-02-02 02:16:23 +00002144 case Shl:
2145 case LShr:
2146 case AShr:
2147 assert(getType() == LHS->getType() &&
2148 "Shift operation should return same type as operands!");
Craig Topper0b2cfb72017-06-25 17:33:48 +00002149 assert(getType()->isIntOrIntVectorTy() &&
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002150 "Tried to create a shift operation on a non-integral type!");
Reid Spencer832254e2007-02-02 02:16:23 +00002151 break;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002152 case And: case Or:
2153 case Xor:
Chris Lattnerb12261a2005-01-29 00:35:16 +00002154 assert(getType() == LHS->getType() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002155 "Logical operation should return same type as operands!");
Craig Topper0b2cfb72017-06-25 17:33:48 +00002156 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00002157 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002158 break;
Craig Topper8edc5b12017-06-26 07:15:59 +00002159 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002160 }
2161#endif
2162}
2163
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002164BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002165 const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002166 Instruction *InsertBefore) {
2167 assert(S1->getType() == S2->getType() &&
2168 "Cannot create binary operator with two operands of differing type!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00002169 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002170}
2171
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002172BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002173 const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002174 BasicBlock *InsertAtEnd) {
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002175 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002176 InsertAtEnd->getInstList().push_back(Res);
2177 return Res;
2178}
2179
Dan Gohman4ae51262009-08-12 16:23:25 +00002180BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002181 Instruction *InsertBefore) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002182 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer24d6da52007-01-21 00:29:26 +00002183 return new BinaryOperator(Instruction::Sub,
2184 zero, Op,
2185 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002186}
2187
Dan Gohman4ae51262009-08-12 16:23:25 +00002188BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002189 BasicBlock *InsertAtEnd) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002190 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer24d6da52007-01-21 00:29:26 +00002191 return new BinaryOperator(Instruction::Sub,
2192 zero, Op,
2193 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002194}
2195
Dan Gohmanbdc46c62009-12-18 02:58:50 +00002196BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2197 Instruction *InsertBefore) {
2198 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2199 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2200}
2201
2202BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2203 BasicBlock *InsertAtEnd) {
2204 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2205 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2206}
2207
Duncan Sands8991d512010-02-02 12:53:04 +00002208BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2209 Instruction *InsertBefore) {
2210 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2211 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2212}
2213
2214BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2215 BasicBlock *InsertAtEnd) {
2216 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2217 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2218}
2219
Dan Gohman4ae51262009-08-12 16:23:25 +00002220BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002221 Instruction *InsertBefore) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002222 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner4ca829e2012-01-25 06:02:56 +00002223 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002224 Op->getType(), Name, InsertBefore);
2225}
2226
Dan Gohman4ae51262009-08-12 16:23:25 +00002227BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002228 BasicBlock *InsertAtEnd) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002229 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner4ca829e2012-01-25 06:02:56 +00002230 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002231 Op->getType(), Name, InsertAtEnd);
2232}
2233
Dan Gohman4ae51262009-08-12 16:23:25 +00002234BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002235 Instruction *InsertBefore) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00002236 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnerfdbc82a2006-03-25 21:54:21 +00002237 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002238 Op->getType(), Name, InsertBefore);
2239}
2240
Dan Gohman4ae51262009-08-12 16:23:25 +00002241BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002242 BasicBlock *InsertAtEnd) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00002243 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerb9d41002005-12-21 18:22:19 +00002244 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002245 Op->getType(), Name, InsertAtEnd);
2246}
2247
Sanjay Patelb152eae2016-11-16 18:09:44 +00002248// Exchange the two operands to this instruction. This instruction is safe to
2249// use on any binary instruction and does not modify the semantics of the
2250// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2251// is changed.
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002252bool BinaryOperator::swapOperands() {
Reid Spencere4d87aa2006-12-23 06:05:41 +00002253 if (!isCommutative())
2254 return true; // Can't commute operands
Gabor Greif94fb68b2008-05-13 22:51:52 +00002255 Op<0>().swap(Op<1>());
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00002256 return false;
2257}
2258
Chris Lattner79bc3322006-09-18 04:54:57 +00002259//===----------------------------------------------------------------------===//
Duncan Sands8883c432012-04-16 16:28:59 +00002260// FPMathOperator Class
2261//===----------------------------------------------------------------------===//
2262
Duncan Sands8883c432012-04-16 16:28:59 +00002263float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +00002264 const MDNode *MD =
2265 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands8883c432012-04-16 16:28:59 +00002266 if (!MD)
2267 return 0.0;
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002268 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands2867c852012-04-16 19:39:33 +00002269 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands8883c432012-04-16 16:28:59 +00002270}
2271
Duncan Sands8883c432012-04-16 16:28:59 +00002272//===----------------------------------------------------------------------===//
Chris Lattner79bc3322006-09-18 04:54:57 +00002273// CastInst Class
2274//===----------------------------------------------------------------------===//
2275
Reid Spencer3da59db2006-11-27 01:05:10 +00002276// Just determine if this cast only deals with integral->integral conversion.
2277bool CastInst::isIntegerCast() const {
2278 switch (getOpcode()) {
2279 default: return false;
2280 case Instruction::ZExt:
2281 case Instruction::SExt:
2282 case Instruction::Trunc:
2283 return true;
2284 case Instruction::BitCast:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002285 return getOperand(0)->getType()->isIntegerTy() &&
2286 getType()->isIntegerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002287 }
Chris Lattner79bc3322006-09-18 04:54:57 +00002288}
2289
Reid Spencer3da59db2006-11-27 01:05:10 +00002290bool CastInst::isLosslessCast() const {
2291 // Only BitCast can be lossless, exit fast if we're not BitCast
2292 if (getOpcode() != Instruction::BitCast)
2293 return false;
2294
2295 // Identity cast is always lossless
Ana Pazose6463c92016-02-03 21:34:39 +00002296 Type *SrcTy = getOperand(0)->getType();
2297 Type *DstTy = getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00002298 if (SrcTy == DstTy)
2299 return true;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002300
Reid Spencer79e21d32006-12-31 05:26:44 +00002301 // Pointer to pointer is always lossless.
Duncan Sands1df98592010-02-16 11:11:14 +00002302 if (SrcTy->isPointerTy())
2303 return DstTy->isPointerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002304 return false; // Other types have no identity values
2305}
2306
2307/// This function determines if the CastInst does not require any bits to be
2308/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songaf7b1832018-07-30 19:41:25 +00002309/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer3da59db2006-11-27 01:05:10 +00002310/// example, the following are all no-op casts:
Dan Gohman9b38d7d2008-05-12 16:34:30 +00002311/// # bitcast i32* %x to i8*
Fangrui Songaf7b1832018-07-30 19:41:25 +00002312/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohman9b38d7d2008-05-12 16:34:30 +00002313/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl0b24b742018-05-01 16:10:38 +00002314/// Determine if the described cast is a no-op.
Dan Gohman2c048ea2010-05-28 21:41:37 +00002315bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002316 Type *SrcTy,
2317 Type *DestTy,
Mikael Holmene2da2632017-10-05 07:07:09 +00002318 const DataLayout &DL) {
Dan Gohman2c048ea2010-05-28 21:41:37 +00002319 switch (Opcode) {
Craig Topper50bee422012-02-05 22:14:15 +00002320 default: llvm_unreachable("Invalid CastOp");
Reid Spencer3da59db2006-11-27 01:05:10 +00002321 case Instruction::Trunc:
2322 case Instruction::ZExt:
Fangrui Songaf7b1832018-07-30 19:41:25 +00002323 case Instruction::SExt:
Reid Spencer3da59db2006-11-27 01:05:10 +00002324 case Instruction::FPTrunc:
2325 case Instruction::FPExt:
2326 case Instruction::UIToFP:
2327 case Instruction::SIToFP:
2328 case Instruction::FPToUI:
2329 case Instruction::FPToSI:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002330 case Instruction::AddrSpaceCast:
2331 // TODO: Target informations may give a more accurate answer here.
2332 return false;
Reid Spencer3da59db2006-11-27 01:05:10 +00002333 case Instruction::BitCast:
2334 return true; // BitCast never modifies bits.
2335 case Instruction::PtrToInt:
Mikael Holmene2da2632017-10-05 07:07:09 +00002336 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman2c048ea2010-05-28 21:41:37 +00002337 DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002338 case Instruction::IntToPtr:
Mikael Holmene2da2632017-10-05 07:07:09 +00002339 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman2c048ea2010-05-28 21:41:37 +00002340 SrcTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002341 }
2342}
2343
Mehdi Amini529919f2015-03-10 02:37:25 +00002344bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen42c9fd02017-10-03 06:03:49 +00002345 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenault38c18ef2014-03-06 17:33:55 +00002346}
2347
2348/// This function determines if a pair of casts can be eliminated and what
2349/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer3da59db2006-11-27 01:05:10 +00002350/// instructions like this:
2351/// * %F = firstOpcode SrcTy %x to MidTy
2352/// * %S = secondOpcode MidTy %F to DstTy
2353/// The function returns a resultOpcode so these two casts can be replaced with:
2354/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel46e81ea2015-12-11 18:12:01 +00002355/// If no such cast is permitted, the function returns 0.
Reid Spencer3da59db2006-11-27 01:05:10 +00002356unsigned CastInst::isEliminableCastPair(
2357 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sands446cf942012-10-30 16:03:32 +00002358 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2359 Type *DstIntPtrTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002360 // Define the 144 possibilities for these two cast instructions. The values
2361 // in this matrix determine what to do in a given situation and select the
Fangrui Songaf7b1832018-07-30 19:41:25 +00002362 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel46e81ea2015-12-11 18:12:01 +00002363 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer3da59db2006-11-27 01:05:10 +00002364 // the following cast properties:
2365 //
2366 // Size Compare Source Destination
2367 // Operator Src ? Size Type Sign Type Sign
2368 // -------- ------------ ------------------- ---------------------
2369 // TRUNC > Integer Any Integral Any
2370 // ZEXT < Integral Unsigned Integer Any
2371 // SEXT < Integral Signed Integer Any
2372 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002373 // FPTOSI n/a FloatPt n/a Integral Signed
2374 // UITOFP n/a Integral Unsigned FloatPt n/a
2375 // SITOFP n/a Integral Signed FloatPt n/a
2376 // FPTRUNC > FloatPt n/a FloatPt n/a
2377 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer3da59db2006-11-27 01:05:10 +00002378 // PTRTOINT n/a Pointer n/a Integral Unsigned
2379 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002380 // BITCAST = FirstClass n/a FirstClass n/a
2381 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner518f6fa2006-12-05 23:43:59 +00002382 //
2383 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohmana119de82009-06-14 23:30:43 +00002384 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2385 // into "fptoui double to i64", but this loses information about the range
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002386 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner518f6fa2006-12-05 23:43:59 +00002387 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002388 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner518f6fa2006-12-05 23:43:59 +00002389 // same reason.
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002390 const unsigned numCastOps =
Reid Spencer3da59db2006-11-27 01:05:10 +00002391 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2392 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002393 // T F F U S F F P I B A -+
2394 // R Z S P P I I T P 2 N T S |
2395 // U E E 2 2 2 2 R E I T C C +- secondOp
2396 // N X X U S F F N X N 2 V V |
2397 // C T T I I P P C T T P T T -+
2398 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaserb5750562015-04-21 00:05:41 +00002399 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002400 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2401 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2402 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2403 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2404 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougachac0d1ce52015-05-29 00:04:30 +00002405 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper5391b3c2018-03-02 18:16:51 +00002406 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002407 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2408 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2409 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2410 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer3da59db2006-11-27 01:05:10 +00002411 };
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002412
Sanjay Patel63b29822015-12-12 00:33:36 +00002413 // TODO: This logic could be encoded into the table above and handled in the
2414 // switch below.
Chris Lattnerdfd36262010-07-12 01:19:22 +00002415 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel63b29822015-12-12 00:33:36 +00002416 // merging. However, any pair of bitcasts are allowed.
2417 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2418 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2419 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem89879ec2011-08-29 19:58:36 +00002420
Sanjay Patel63b29822015-12-12 00:33:36 +00002421 // Check if any of the casts convert scalars <-> vectors.
2422 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2423 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2424 if (!AreBothBitcasts)
2425 return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +00002426
2427 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2428 [secondOp-Instruction::CastOpsBegin];
2429 switch (ElimCase) {
Fangrui Songaf7b1832018-07-30 19:41:25 +00002430 case 0:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002431 // Categorically disallowed.
Reid Spencer3da59db2006-11-27 01:05:10 +00002432 return 0;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002433 case 1:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002434 // Allowed, use first cast's opcode.
Reid Spencer3da59db2006-11-27 01:05:10 +00002435 return firstOp;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002436 case 2:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002437 // Allowed, use second cast's opcode.
Reid Spencer3da59db2006-11-27 01:05:10 +00002438 return secondOp;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002439 case 3:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002440 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange4a0a152010-01-23 04:35:57 +00002441 // is integer and we are not converting between a vector and a
Alp Toker087ab612013-12-05 05:44:44 +00002442 // non-vector type.
Duncan Sands1df98592010-02-16 11:11:14 +00002443 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002444 return firstOp;
2445 return 0;
2446 case 4:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002447 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner33b17582010-01-23 04:42:42 +00002448 // is floating point.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002449 if (DstTy->isFloatingPointTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002450 return firstOp;
2451 return 0;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002452 case 5:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002453 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner33b17582010-01-23 04:42:42 +00002454 // is an integer.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002455 if (SrcTy->isIntegerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002456 return secondOp;
2457 return 0;
2458 case 6:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002459 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner33b17582010-01-23 04:42:42 +00002460 // is a floating point.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002461 if (SrcTy->isFloatingPointTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002462 return secondOp;
2463 return 0;
Matt Arsenault3181f592013-07-30 22:27:10 +00002464 case 7: {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002465 // Cannot simplify if address spaces are different!
2466 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2467 return 0;
2468
Matt Arsenault3181f592013-07-30 22:27:10 +00002469 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002470 // We can still fold this without knowing the actual sizes as long we
2471 // know that the intermediate pointer is the largest possible
2472 // pointer size.
2473 // FIXME: Is this always true?
2474 if (MidSize == 64)
Matt Arsenault3181f592013-07-30 22:27:10 +00002475 return Instruction::BitCast;
2476
2477 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sands446cf942012-10-30 16:03:32 +00002478 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman295643b2009-07-21 23:19:40 +00002479 return 0;
Duncan Sands446cf942012-10-30 16:03:32 +00002480 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002481 if (MidSize >= PtrSize)
2482 return Instruction::BitCast;
2483 return 0;
2484 }
2485 case 8: {
2486 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2487 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2488 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman6de29f82009-06-15 22:12:54 +00002489 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2490 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002491 if (SrcSize == DstSize)
2492 return Instruction::BitCast;
2493 else if (SrcSize < DstSize)
2494 return firstOp;
2495 return secondOp;
2496 }
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002497 case 9:
2498 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer3da59db2006-11-27 01:05:10 +00002499 return Instruction::ZExt;
Matt Arsenault3181f592013-07-30 22:27:10 +00002500 case 11: {
Reid Spencer3da59db2006-11-27 01:05:10 +00002501 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sands446cf942012-10-30 16:03:32 +00002502 if (!MidIntPtrTy)
Dan Gohman295643b2009-07-21 23:19:40 +00002503 return 0;
Duncan Sands446cf942012-10-30 16:03:32 +00002504 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman6de29f82009-06-15 22:12:54 +00002505 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2506 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002507 if (SrcSize <= PtrSize && SrcSize == DstSize)
2508 return Instruction::BitCast;
2509 return 0;
2510 }
Eugene Zelenko46795222017-05-15 21:57:41 +00002511 case 12:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002512 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2513 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2514 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2515 return Instruction::AddrSpaceCast;
2516 return Instruction::BitCast;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002517 case 13:
2518 // FIXME: this state can be merged with (1), but the following assert
2519 // is useful to check the correcteness of the sequence due to semantic
2520 // change of bitcast.
2521 assert(
2522 SrcTy->isPtrOrPtrVectorTy() &&
2523 MidTy->isPtrOrPtrVectorTy() &&
2524 DstTy->isPtrOrPtrVectorTy() &&
2525 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2526 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2527 "Illegal addrspacecast, bitcast sequence!");
2528 // Allowed, use first cast's opcode
2529 return firstOp;
2530 case 14:
Jingyue Wuc77dec92014-06-06 21:52:55 +00002531 // bitcast, addrspacecast -> addrspacecast if the element type of
2532 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbournee1f5c812016-11-13 06:58:45 +00002533 if (SrcTy->getScalarType()->getPointerElementType() ==
2534 DstTy->getScalarType()->getPointerElementType())
Jingyue Wuc77dec92014-06-06 21:52:55 +00002535 return Instruction::AddrSpaceCast;
2536 return 0;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002537 case 15:
2538 // FIXME: this state can be merged with (1), but the following assert
2539 // is useful to check the correcteness of the sequence due to semantic
2540 // change of bitcast.
2541 assert(
2542 SrcTy->isIntOrIntVectorTy() &&
2543 MidTy->isPtrOrPtrVectorTy() &&
2544 DstTy->isPtrOrPtrVectorTy() &&
2545 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2546 "Illegal inttoptr, bitcast sequence!");
2547 // Allowed, use first cast's opcode
2548 return firstOp;
2549 case 16:
2550 // FIXME: this state can be merged with (2), but the following assert
2551 // is useful to check the correcteness of the sequence due to semantic
2552 // change of bitcast.
2553 assert(
2554 SrcTy->isPtrOrPtrVectorTy() &&
2555 MidTy->isPtrOrPtrVectorTy() &&
2556 DstTy->isIntOrIntVectorTy() &&
2557 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2558 "Illegal bitcast, ptrtoint sequence!");
2559 // Allowed, use second cast's opcode
2560 return secondOp;
Fiona Glaserb5750562015-04-21 00:05:41 +00002561 case 17:
2562 // (sitofp (zext x)) -> (uitofp x)
2563 return Instruction::UIToFP;
Fangrui Songaf7b1832018-07-30 19:41:25 +00002564 case 99:
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002565 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer3da59db2006-11-27 01:05:10 +00002566 // where the MidTy is not the same for the two cast instructions.
Craig Topper50bee422012-02-05 22:14:15 +00002567 llvm_unreachable("Invalid Cast Combination");
Reid Spencer3da59db2006-11-27 01:05:10 +00002568 default:
Craig Topper50bee422012-02-05 22:14:15 +00002569 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002570 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002571}
2572
Fangrui Songaf7b1832018-07-30 19:41:25 +00002573CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002574 const Twine &Name, Instruction *InsertBefore) {
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002575 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002576 // Construct and return the appropriate CastInst subclass
2577 switch (op) {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002578 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2579 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2580 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2581 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2582 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2583 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2584 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2585 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2586 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2587 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2588 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2589 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2590 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2591 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer3da59db2006-11-27 01:05:10 +00002592 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002593}
2594
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002595CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002596 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002597 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002598 // Construct and return the appropriate CastInst subclass
2599 switch (op) {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002600 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2601 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2602 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2603 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2604 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2605 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2606 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2607 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2608 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2609 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2610 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2611 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2612 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2613 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer3da59db2006-11-27 01:05:10 +00002614 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002615}
2616
Fangrui Songaf7b1832018-07-30 19:41:25 +00002617CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002618 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002619 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002620 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002621 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2622 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002623}
2624
Fangrui Songaf7b1832018-07-30 19:41:25 +00002625CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002626 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002627 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002628 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002629 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2630 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002631}
2632
Fangrui Songaf7b1832018-07-30 19:41:25 +00002633CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002634 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002635 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002636 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002637 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2638 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002639}
2640
Fangrui Songaf7b1832018-07-30 19:41:25 +00002641CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002642 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002643 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002644 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002645 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2646 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002647}
2648
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002649CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002650 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002651 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002652 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002653 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2654 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002655}
2656
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002657CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songaf7b1832018-07-30 19:41:25 +00002658 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002659 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002660 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002661 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2662 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002663}
2664
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002665CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002666 const Twine &Name,
Reid Spencer330d86d2006-12-05 03:28:26 +00002667 BasicBlock *InsertAtEnd) {
Matt Arsenault1bf0ec42013-07-31 00:17:33 +00002668 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2669 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2670 "Invalid cast");
2671 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu5b62bb02013-07-31 04:07:28 +00002672 assert((!Ty->isVectorTy() ||
2673 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencer330d86d2006-12-05 03:28:26 +00002674 "Invalid cast");
2675
Matt Arsenault1bf0ec42013-07-31 00:17:33 +00002676 if (Ty->isIntOrIntVectorTy())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002677 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002678
Matt Arsenault9f40cd72014-07-14 17:24:35 +00002679 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencer330d86d2006-12-05 03:28:26 +00002680}
2681
Adrian Prantl0b24b742018-05-01 16:10:38 +00002682/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault1bf0ec42013-07-31 00:17:33 +00002683CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2684 const Twine &Name,
Reid Spencer330d86d2006-12-05 03:28:26 +00002685 Instruction *InsertBefore) {
Evgeniy Stepanov344d3fb2013-01-15 16:43:00 +00002686 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2687 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencer330d86d2006-12-05 03:28:26 +00002688 "Invalid cast");
Matt Arsenault1bf0ec42013-07-31 00:17:33 +00002689 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu5b62bb02013-07-31 04:07:28 +00002690 assert((!Ty->isVectorTy() ||
2691 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault1bf0ec42013-07-31 00:17:33 +00002692 "Invalid cast");
Reid Spencer330d86d2006-12-05 03:28:26 +00002693
Evgeniy Stepanov344d3fb2013-01-15 16:43:00 +00002694 if (Ty->isIntOrIntVectorTy())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002695 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002696
Matt Arsenault9f40cd72014-07-14 17:24:35 +00002697 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2698}
2699
2700CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2701 Value *S, Type *Ty,
2702 const Twine &Name,
2703 BasicBlock *InsertAtEnd) {
2704 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2705 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2706
2707 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2708 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2709
2710 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2711}
2712
2713CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2714 Value *S, Type *Ty,
2715 const Twine &Name,
2716 Instruction *InsertBefore) {
2717 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2718 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2719
2720 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002721 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2722
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002723 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencer330d86d2006-12-05 03:28:26 +00002724}
2725
Chandler Carrutha87c3542014-11-25 08:20:27 +00002726CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2727 const Twine &Name,
2728 Instruction *InsertBefore) {
2729 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2730 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2731 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2732 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2733
2734 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2735}
2736
Matt Arsenault9f40cd72014-07-14 17:24:35 +00002737CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002738 bool isSigned, const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002739 Instruction *InsertBefore) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002740 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner859c3722010-01-10 20:21:42 +00002741 "Invalid integer cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002742 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2743 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002744 Instruction::CastOps opcode =
2745 (SrcBits == DstBits ? Instruction::BitCast :
2746 (SrcBits > DstBits ? Instruction::Trunc :
2747 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002748 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002749}
2750
Fangrui Songaf7b1832018-07-30 19:41:25 +00002751CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002752 bool isSigned, const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002753 BasicBlock *InsertAtEnd) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002754 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00002755 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002756 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2757 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002758 Instruction::CastOps opcode =
2759 (SrcBits == DstBits ? Instruction::BitCast :
2760 (SrcBits > DstBits ? Instruction::Trunc :
2761 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002762 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002763}
2764
Fangrui Songaf7b1832018-07-30 19:41:25 +00002765CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2766 const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002767 Instruction *InsertBefore) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002768 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002769 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002770 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2771 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002772 Instruction::CastOps opcode =
2773 (SrcBits == DstBits ? Instruction::BitCast :
2774 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002775 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002776}
2777
Fangrui Songaf7b1832018-07-30 19:41:25 +00002778CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2779 const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002780 BasicBlock *InsertAtEnd) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002781 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002782 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002783 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2784 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002785 Instruction::CastOps opcode =
2786 (SrcBits == DstBits ? Instruction::BitCast :
2787 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002788 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002789}
2790
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002791// Check whether it is valid to call getCastOpcode for these types.
2792// This routine must be kept in sync with getCastOpcode.
2793bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2794 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2795 return false;
2796
2797 if (SrcTy == DestTy)
2798 return true;
2799
2800 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2801 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2802 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2803 // An element by element cast. Valid if casting the elements is valid.
2804 SrcTy = SrcVecTy->getElementType();
2805 DestTy = DestVecTy->getElementType();
2806 }
2807
2808 // Get the bit sizes, we'll need these
2809 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2810 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2811
2812 // Run through the possibilities ...
2813 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie16ff2da2015-03-23 19:51:23 +00002814 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002815 return true;
David Blaikie16ff2da2015-03-23 19:51:23 +00002816 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002817 return true;
David Blaikie16ff2da2015-03-23 19:51:23 +00002818 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002819 return DestBits == SrcBits;
David Blaikie16ff2da2015-03-23 19:51:23 +00002820 // Casting from something else
2821 return SrcTy->isPointerTy();
Fangrui Songaf7b1832018-07-30 19:41:25 +00002822 }
David Blaikie16ff2da2015-03-23 19:51:23 +00002823 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2824 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002825 return true;
David Blaikie16ff2da2015-03-23 19:51:23 +00002826 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002827 return true;
David Blaikie16ff2da2015-03-23 19:51:23 +00002828 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002829 return DestBits == SrcBits;
David Blaikie16ff2da2015-03-23 19:51:23 +00002830 // Casting from something else
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002831 return false;
2832 }
David Blaikie16ff2da2015-03-23 19:51:23 +00002833 if (DestTy->isVectorTy()) // Casting to vector
2834 return DestBits == SrcBits;
2835 if (DestTy->isPointerTy()) { // Casting to pointer
2836 if (SrcTy->isPointerTy()) // Casting from pointer
2837 return true;
2838 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songaf7b1832018-07-30 19:41:25 +00002839 }
David Blaikie16ff2da2015-03-23 19:51:23 +00002840 if (DestTy->isX86_MMXTy()) {
2841 if (SrcTy->isVectorTy())
2842 return DestBits == SrcBits; // 64-bit vector to MMX
2843 return false;
2844 } // Casting to something else
2845 return false;
Matt Arsenault485c7fd2013-07-30 22:02:14 +00002846}
2847
Matt Arsenaultf34dc422013-07-30 20:45:05 +00002848bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2849 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2850 return false;
2851
2852 if (SrcTy == DestTy)
2853 return true;
2854
2855 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2856 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2857 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2858 // An element by element cast. Valid if casting the elements is valid.
2859 SrcTy = SrcVecTy->getElementType();
2860 DestTy = DestVecTy->getElementType();
2861 }
2862 }
2863 }
2864
2865 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2866 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2867 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2868 }
2869 }
2870
2871 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2872 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2873
2874 // Could still have vectors of pointers if the number of elements doesn't
2875 // match
2876 if (SrcBits == 0 || DestBits == 0)
2877 return false;
2878
2879 if (SrcBits != DestBits)
2880 return false;
2881
2882 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2883 return false;
2884
2885 return true;
2886}
2887
Chandler Carrutha87c3542014-11-25 08:20:27 +00002888bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Amini529919f2015-03-10 02:37:25 +00002889 const DataLayout &DL) {
Yichao Yu689fbe02017-10-22 20:28:17 +00002890 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carrutha87c3542014-11-25 08:20:27 +00002891 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2892 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu689fbe02017-10-22 20:28:17 +00002893 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2894 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carrutha87c3542014-11-25 08:20:27 +00002895 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2896 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu689fbe02017-10-22 20:28:17 +00002897 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2898 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carrutha87c3542014-11-25 08:20:27 +00002899
2900 return isBitCastable(SrcTy, DestTy);
2901}
2902
Matt Arsenaultf34dc422013-07-30 20:45:05 +00002903// Provide a way to get a "cast" where the cast opcode is inferred from the
2904// types and size of the operand. This, basically, is a parallel of the
Reid Spencer0a11af12007-01-17 02:46:11 +00002905// logic in the castIsValid function below. This axiom should hold:
2906// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2907// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer3da59db2006-11-27 01:05:10 +00002908// casting opcode for the arguments passed to it.
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002909// This routine must be kept in sync with isCastable.
Reid Spencer3da59db2006-11-27 01:05:10 +00002910Instruction::CastOps
Reid Spencer56667122006-12-04 02:43:42 +00002911CastInst::getCastOpcode(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002912 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2913 Type *SrcTy = Src->getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00002914
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002915 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2916 "Only first class types are castable!");
2917
Duncan Sands117feba2011-05-18 07:13:41 +00002918 if (SrcTy == DestTy)
2919 return BitCast;
2920
Matt Arsenaultf34dc422013-07-30 20:45:05 +00002921 // FIXME: Check address space sizes here
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002922 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2923 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sands117feba2011-05-18 07:13:41 +00002924 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2925 // An element by element cast. Find the appropriate opcode based on the
2926 // element types.
2927 SrcTy = SrcVecTy->getElementType();
2928 DestTy = DestVecTy->getElementType();
2929 }
2930
2931 // Get the bit sizes, we'll need these
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002932 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2933 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands117feba2011-05-18 07:13:41 +00002934
Reid Spencer3da59db2006-11-27 01:05:10 +00002935 // Run through the possibilities ...
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002936 if (DestTy->isIntegerTy()) { // Casting to integral
2937 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer3da59db2006-11-27 01:05:10 +00002938 if (DestBits < SrcBits)
2939 return Trunc; // int -> smaller int
2940 else if (DestBits > SrcBits) { // its an extension
Reid Spencer56667122006-12-04 02:43:42 +00002941 if (SrcIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002942 return SExt; // signed -> SEXT
2943 else
2944 return ZExt; // unsigned -> ZEXT
2945 } else {
2946 return BitCast; // Same size, No-op cast
2947 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002948 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songaf7b1832018-07-30 19:41:25 +00002949 if (DestIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002950 return FPToSI; // FP -> sint
2951 else
Fangrui Songaf7b1832018-07-30 19:41:25 +00002952 return FPToUI; // FP -> uint
Duncan Sands7016ec12011-05-18 10:59:25 +00002953 } else if (SrcTy->isVectorTy()) {
2954 assert(DestBits == SrcBits &&
2955 "Casting vector to integer of different width");
Reid Spencer3da59db2006-11-27 01:05:10 +00002956 return BitCast; // Same size, no-op cast
2957 } else {
Duncan Sands1df98592010-02-16 11:11:14 +00002958 assert(SrcTy->isPointerTy() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00002959 "Casting from a value that is not first-class type");
2960 return PtrToInt; // ptr -> int
2961 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002962 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2963 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer56667122006-12-04 02:43:42 +00002964 if (SrcIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002965 return SIToFP; // sint -> FP
2966 else
2967 return UIToFP; // uint -> FP
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002968 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer3da59db2006-11-27 01:05:10 +00002969 if (DestBits < SrcBits) {
2970 return FPTrunc; // FP -> smaller FP
2971 } else if (DestBits > SrcBits) {
2972 return FPExt; // FP -> larger FP
2973 } else {
2974 return BitCast; // same size, no-op cast
2975 }
Duncan Sands7016ec12011-05-18 10:59:25 +00002976 } else if (SrcTy->isVectorTy()) {
2977 assert(DestBits == SrcBits &&
Dan Gohman86296cc2007-05-11 21:43:24 +00002978 "Casting vector to floating point of different width");
Devang Patel8c3b47f2008-11-05 01:37:40 +00002979 return BitCast; // same size, no-op cast
Reid Spencer3da59db2006-11-27 01:05:10 +00002980 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002981 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands7016ec12011-05-18 10:59:25 +00002982 } else if (DestTy->isVectorTy()) {
2983 assert(DestBits == SrcBits &&
2984 "Illegal cast to vector (wrong type or size)");
2985 return BitCast;
Duncan Sands1df98592010-02-16 11:11:14 +00002986 } else if (DestTy->isPointerTy()) {
2987 if (SrcTy->isPointerTy()) {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002988 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2989 return AddrSpaceCast;
Reid Spencer3da59db2006-11-27 01:05:10 +00002990 return BitCast; // ptr -> ptr
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002991 } else if (SrcTy->isIntegerTy()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002992 return IntToPtr; // int -> ptr
Reid Spencer3da59db2006-11-27 01:05:10 +00002993 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002994 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesen0488fb62010-09-30 23:57:10 +00002995 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands7016ec12011-05-18 10:59:25 +00002996 if (SrcTy->isVectorTy()) {
2997 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesen0488fb62010-09-30 23:57:10 +00002998 return BitCast; // 64-bit vector to MMX
Dale Johannesen0488fb62010-09-30 23:57:10 +00002999 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00003000 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer3da59db2006-11-27 01:05:10 +00003001 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00003002 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer3da59db2006-11-27 01:05:10 +00003003}
3004
3005//===----------------------------------------------------------------------===//
3006// CastInst SubClass Constructors
3007//===----------------------------------------------------------------------===//
3008
3009/// Check that the construction parameters for a CastInst are correct. This
3010/// could be broken out into the separate constructors but it is useful to have
3011/// it in one place and to eliminate the redundant code for getting the sizes
3012/// of the types involved.
Fangrui Songaf7b1832018-07-30 19:41:25 +00003013bool
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003014CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00003015 // Check for type sanity on the arguments
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003016 Type *SrcTy = S->getType();
Evan Cheng582e4f22013-01-10 23:22:53 +00003017
Chris Lattner0b68a002010-01-26 21:51:43 +00003018 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3019 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer3da59db2006-11-27 01:05:10 +00003020 return false;
3021
3022 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00003023 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3024 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00003025
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003026 // If these are vector types, get the lengths of the vectors (using zero for
3027 // scalar types means that checking that vector lengths match also checks that
3028 // scalars are not being converted to vectors or vectors to scalars).
3029 unsigned SrcLength = SrcTy->isVectorTy() ?
3030 cast<VectorType>(SrcTy)->getNumElements() : 0;
3031 unsigned DstLength = DstTy->isVectorTy() ?
3032 cast<VectorType>(DstTy)->getNumElements() : 0;
3033
Reid Spencer3da59db2006-11-27 01:05:10 +00003034 // Switch on the opcode provided
3035 switch (op) {
3036 default: return false; // This is an input error
3037 case Instruction::Trunc:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003038 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3039 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00003040 case Instruction::ZExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003041 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3042 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003043 case Instruction::SExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003044 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3045 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00003046 case Instruction::FPTrunc:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003047 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3048 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00003049 case Instruction::FPExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003050 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3051 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00003052 case Instruction::UIToFP:
Reid Spencer3da59db2006-11-27 01:05:10 +00003053 case Instruction::SIToFP:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003054 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3055 SrcLength == DstLength;
Reid Spencer3da59db2006-11-27 01:05:10 +00003056 case Instruction::FPToUI:
Reid Spencer3da59db2006-11-27 01:05:10 +00003057 case Instruction::FPToSI:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00003058 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3059 SrcLength == DstLength;
Reid Spencer3da59db2006-11-27 01:05:10 +00003060 case Instruction::PtrToInt:
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00003061 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem16087692011-12-05 06:29:09 +00003062 return false;
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00003063 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3064 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3065 return false;
Craig Topper10600822017-07-09 07:04:00 +00003066 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00003067 case Instruction::IntToPtr:
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00003068 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem16087692011-12-05 06:29:09 +00003069 return false;
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00003070 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3071 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3072 return false;
Craig Topper10600822017-07-09 07:04:00 +00003073 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003074 case Instruction::BitCast: {
3075 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3076 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3077
Reid Spencer3da59db2006-11-27 01:05:10 +00003078 // BitCast implies a no-op cast of type only. No bits change.
3079 // However, you can't cast pointers to anything but pointers.
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003080 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer3da59db2006-11-27 01:05:10 +00003081 return false;
3082
Alp Toker087ab612013-12-05 05:44:44 +00003083 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenault59d3ae62013-11-15 01:34:59 +00003084 // widths are identical.
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003085 if (!SrcPtrTy)
Matt Arsenault59d3ae62013-11-15 01:34:59 +00003086 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3087
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003088 // If both are pointers then the address spaces must match.
3089 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3090 return false;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00003091
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003092 // A vector of pointers must have the same number of elements.
Serguei Katkov69344b02018-08-21 04:27:07 +00003093 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3094 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3095 if (SrcVecTy && DstVecTy)
3096 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3097 if (SrcVecTy)
3098 return SrcVecTy->getNumElements() == 1;
3099 if (DstVecTy)
3100 return DstVecTy->getNumElements() == 1;
Matt Arsenault79e3fb52014-01-22 19:21:33 +00003101
3102 return true;
3103 }
3104 case Instruction::AddrSpaceCast: {
3105 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3106 if (!SrcPtrTy)
3107 return false;
3108
3109 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3110 if (!DstPtrTy)
3111 return false;
3112
3113 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3114 return false;
3115
3116 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3117 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3118 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3119
3120 return false;
3121 }
3122
3123 return true;
3124 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003125 }
3126}
3127
3128TruncInst::TruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003129 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00003130) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003131 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00003132}
3133
3134TruncInst::TruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003135 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003136) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003137 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00003138}
3139
3140ZExtInst::ZExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003141 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003142) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003143 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003144}
3145
3146ZExtInst::ZExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003147 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003148) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003149 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003150}
3151SExtInst::SExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003152 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003153) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003154 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003155}
3156
Jeff Cohen97af7512006-12-02 02:22:01 +00003157SExtInst::SExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003158 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003159) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003160 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003161}
3162
3163FPTruncInst::FPTruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003164 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003165) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003166 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00003167}
3168
3169FPTruncInst::FPTruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003170 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003171) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003172 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00003173}
3174
3175FPExtInst::FPExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003176 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003177) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003178 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003179}
3180
3181FPExtInst::FPExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003182 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003183) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003184 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003185}
3186
3187UIToFPInst::UIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003188 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003189) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003190 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00003191}
3192
3193UIToFPInst::UIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003194 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003195) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003196 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00003197}
3198
3199SIToFPInst::SIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003200 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003201) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003202 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00003203}
3204
3205SIToFPInst::SIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003206 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003207) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003208 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00003209}
3210
3211FPToUIInst::FPToUIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003213) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer3da59db2006-11-27 01:05:10 +00003215}
3216
3217FPToUIInst::FPToUIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003218 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003219) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer3da59db2006-11-27 01:05:10 +00003221}
3222
3223FPToSIInst::FPToSIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003224 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003225) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer3da59db2006-11-27 01:05:10 +00003227}
3228
3229FPToSIInst::FPToSIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003230 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003231) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer3da59db2006-11-27 01:05:10 +00003233}
3234
3235PtrToIntInst::PtrToIntInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003236 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003237) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003238 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003239}
3240
3241PtrToIntInst::PtrToIntInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003242 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003243) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003244 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer3da59db2006-11-27 01:05:10 +00003245}
3246
3247IntToPtrInst::IntToPtrInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003248 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003249) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003250 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer3da59db2006-11-27 01:05:10 +00003251}
3252
3253IntToPtrInst::IntToPtrInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003254 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003255) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003256 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer3da59db2006-11-27 01:05:10 +00003257}
3258
3259BitCastInst::BitCastInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003260 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songaf7b1832018-07-30 19:41:25 +00003261) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003262 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer3da59db2006-11-27 01:05:10 +00003263}
3264
3265BitCastInst::BitCastInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003266 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songaf7b1832018-07-30 19:41:25 +00003267) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00003268 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer3da59db2006-11-27 01:05:10 +00003269}
Chris Lattner2f463862006-09-17 19:29:56 +00003270
Matt Arsenault59d3ae62013-11-15 01:34:59 +00003271AddrSpaceCastInst::AddrSpaceCastInst(
3272 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3273) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3274 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3275}
3276
3277AddrSpaceCastInst::AddrSpaceCastInst(
3278 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3279) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3280 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3281}
3282
Chris Lattner2f463862006-09-17 19:29:56 +00003283//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +00003284// CmpInst Classes
3285//===----------------------------------------------------------------------===//
3286
Craig Topper240b0e12015-12-15 06:11:33 +00003287CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Patelbe9ce132018-11-07 00:00:42 +00003288 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3289 Instruction *FlagsSource)
Nate Begemanc83ad0d2008-05-12 20:11:05 +00003290 : Instruction(ty, op,
Gabor Greifefe65362008-05-10 08:32:32 +00003291 OperandTraits<CmpInst>::op_begin(this),
3292 OperandTraits<CmpInst>::operands(this),
3293 InsertBefore) {
Sanjay Patelbe9ce132018-11-07 00:00:42 +00003294 Op<0>() = LHS;
3295 Op<1>() = RHS;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003296 setPredicate((Predicate)predicate);
Reid Spencer97c0e212007-04-11 13:04:48 +00003297 setName(Name);
Sanjay Patelbe9ce132018-11-07 00:00:42 +00003298 if (FlagsSource)
3299 copyIRFlags(FlagsSource);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003300}
Gabor Greifefe65362008-05-10 08:32:32 +00003301
Craig Topper240b0e12015-12-15 06:11:33 +00003302CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3303 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begemanc83ad0d2008-05-12 20:11:05 +00003304 : Instruction(ty, op,
Gabor Greifefe65362008-05-10 08:32:32 +00003305 OperandTraits<CmpInst>::op_begin(this),
3306 OperandTraits<CmpInst>::operands(this),
3307 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00003308 Op<0>() = LHS;
3309 Op<1>() = RHS;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003310 setPredicate((Predicate)predicate);
Reid Spencer97c0e212007-04-11 13:04:48 +00003311 setName(Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003312}
3313
3314CmpInst *
Craig Topper240b0e12015-12-15 06:11:33 +00003315CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003316 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00003317 if (Op == Instruction::ICmp) {
Owen Anderson333c4002009-07-09 23:48:35 +00003318 if (InsertBefore)
3319 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3320 S1, S2, Name);
3321 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003322 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson333c4002009-07-09 23:48:35 +00003323 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003324 }
Fangrui Songaf7b1832018-07-30 19:41:25 +00003325
Owen Anderson333c4002009-07-09 23:48:35 +00003326 if (InsertBefore)
3327 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3328 S1, S2, Name);
3329 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003330 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson333c4002009-07-09 23:48:35 +00003331 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003332}
3333
3334CmpInst *
Craig Topper240b0e12015-12-15 06:11:33 +00003335CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003336 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00003337 if (Op == Instruction::ICmp) {
Owen Anderson333c4002009-07-09 23:48:35 +00003338 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3339 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003340 }
Owen Anderson333c4002009-07-09 23:48:35 +00003341 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3342 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00003343}
3344
3345void CmpInst::swapOperands() {
3346 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3347 IC->swapOperands();
3348 else
3349 cast<FCmpInst>(this)->swapOperands();
3350}
3351
Duncan Sandsc449a222011-01-04 12:52:29 +00003352bool CmpInst::isCommutative() const {
3353 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencer45fb3f32006-11-20 01:22:35 +00003354 return IC->isCommutative();
3355 return cast<FCmpInst>(this)->isCommutative();
3356}
3357
Duncan Sandsc449a222011-01-04 12:52:29 +00003358bool CmpInst::isEquality() const {
3359 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencer45fb3f32006-11-20 01:22:35 +00003360 return IC->isEquality();
3361 return cast<FCmpInst>(this)->isEquality();
3362}
3363
Dan Gohman7e2dd662008-05-31 02:47:54 +00003364CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00003365 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00003366 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencer45fb3f32006-11-20 01:22:35 +00003367 case ICMP_EQ: return ICMP_NE;
3368 case ICMP_NE: return ICMP_EQ;
3369 case ICMP_UGT: return ICMP_ULE;
3370 case ICMP_ULT: return ICMP_UGE;
3371 case ICMP_UGE: return ICMP_ULT;
3372 case ICMP_ULE: return ICMP_UGT;
3373 case ICMP_SGT: return ICMP_SLE;
3374 case ICMP_SLT: return ICMP_SGE;
3375 case ICMP_SGE: return ICMP_SLT;
3376 case ICMP_SLE: return ICMP_SGT;
Reid Spencer45fb3f32006-11-20 01:22:35 +00003377
Dan Gohman7e2dd662008-05-31 02:47:54 +00003378 case FCMP_OEQ: return FCMP_UNE;
3379 case FCMP_ONE: return FCMP_UEQ;
3380 case FCMP_OGT: return FCMP_ULE;
3381 case FCMP_OLT: return FCMP_UGE;
3382 case FCMP_OGE: return FCMP_ULT;
3383 case FCMP_OLE: return FCMP_UGT;
3384 case FCMP_UEQ: return FCMP_ONE;
3385 case FCMP_UNE: return FCMP_OEQ;
3386 case FCMP_UGT: return FCMP_OLE;
3387 case FCMP_ULT: return FCMP_OGE;
3388 case FCMP_UGE: return FCMP_OLT;
3389 case FCMP_ULE: return FCMP_OGT;
3390 case FCMP_ORD: return FCMP_UNO;
3391 case FCMP_UNO: return FCMP_ORD;
3392 case FCMP_TRUE: return FCMP_FALSE;
3393 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00003394 }
3395}
3396
Tim Northover3ed44cd2016-08-17 20:25:25 +00003397StringRef CmpInst::getPredicateName(Predicate Pred) {
3398 switch (Pred) {
3399 default: return "unknown";
3400 case FCmpInst::FCMP_FALSE: return "false";
3401 case FCmpInst::FCMP_OEQ: return "oeq";
3402 case FCmpInst::FCMP_OGT: return "ogt";
3403 case FCmpInst::FCMP_OGE: return "oge";
3404 case FCmpInst::FCMP_OLT: return "olt";
3405 case FCmpInst::FCMP_OLE: return "ole";
3406 case FCmpInst::FCMP_ONE: return "one";
3407 case FCmpInst::FCMP_ORD: return "ord";
3408 case FCmpInst::FCMP_UNO: return "uno";
3409 case FCmpInst::FCMP_UEQ: return "ueq";
3410 case FCmpInst::FCMP_UGT: return "ugt";
3411 case FCmpInst::FCMP_UGE: return "uge";
3412 case FCmpInst::FCMP_ULT: return "ult";
3413 case FCmpInst::FCMP_ULE: return "ule";
3414 case FCmpInst::FCMP_UNE: return "une";
3415 case FCmpInst::FCMP_TRUE: return "true";
3416 case ICmpInst::ICMP_EQ: return "eq";
3417 case ICmpInst::ICMP_NE: return "ne";
3418 case ICmpInst::ICMP_SGT: return "sgt";
3419 case ICmpInst::ICMP_SGE: return "sge";
3420 case ICmpInst::ICMP_SLT: return "slt";
3421 case ICmpInst::ICMP_SLE: return "sle";
3422 case ICmpInst::ICMP_UGT: return "ugt";
3423 case ICmpInst::ICMP_UGE: return "uge";
3424 case ICmpInst::ICMP_ULT: return "ult";
3425 case ICmpInst::ICMP_ULE: return "ule";
3426 }
3427}
3428
Reid Spencere4d87aa2006-12-23 06:05:41 +00003429ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3430 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00003431 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songaf7b1832018-07-30 19:41:25 +00003432 case ICMP_EQ: case ICMP_NE:
3433 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencere4d87aa2006-12-23 06:05:41 +00003434 return pred;
3435 case ICMP_UGT: return ICMP_SGT;
3436 case ICMP_ULT: return ICMP_SLT;
3437 case ICMP_UGE: return ICMP_SGE;
3438 case ICMP_ULE: return ICMP_SLE;
3439 }
3440}
3441
Nick Lewycky4189a532008-01-28 03:48:02 +00003442ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3443 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00003444 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songaf7b1832018-07-30 19:41:25 +00003445 case ICMP_EQ: case ICMP_NE:
3446 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky4189a532008-01-28 03:48:02 +00003447 return pred;
3448 case ICMP_SGT: return ICMP_UGT;
3449 case ICMP_SLT: return ICMP_ULT;
3450 case ICMP_SGE: return ICMP_UGE;
3451 case ICMP_SLE: return ICMP_ULE;
3452 }
3453}
3454
Serguei Katkov5e12a212018-02-09 07:59:07 +00003455CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3456 switch (pred) {
3457 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3458 case ICMP_SGT: return ICMP_SGE;
3459 case ICMP_SLT: return ICMP_SLE;
3460 case ICMP_SGE: return ICMP_SGT;
3461 case ICMP_SLE: return ICMP_SLT;
3462 case ICMP_UGT: return ICMP_UGE;
3463 case ICMP_ULT: return ICMP_ULE;
3464 case ICMP_UGE: return ICMP_UGT;
3465 case ICMP_ULE: return ICMP_ULT;
3466
3467 case FCMP_OGT: return FCMP_OGE;
3468 case FCMP_OLT: return FCMP_OLE;
3469 case FCMP_OGE: return FCMP_OGT;
3470 case FCMP_OLE: return FCMP_OLT;
3471 case FCMP_UGT: return FCMP_UGE;
3472 case FCMP_ULT: return FCMP_ULE;
3473 case FCMP_UGE: return FCMP_UGT;
3474 case FCMP_ULE: return FCMP_ULT;
3475 }
3476}
3477
Dan Gohman7e2dd662008-05-31 02:47:54 +00003478CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00003479 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00003480 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman7e2dd662008-05-31 02:47:54 +00003481 case ICMP_EQ: case ICMP_NE:
3482 return pred;
3483 case ICMP_SGT: return ICMP_SLT;
3484 case ICMP_SLT: return ICMP_SGT;
3485 case ICMP_SGE: return ICMP_SLE;
3486 case ICMP_SLE: return ICMP_SGE;
3487 case ICMP_UGT: return ICMP_ULT;
3488 case ICMP_ULT: return ICMP_UGT;
3489 case ICMP_UGE: return ICMP_ULE;
3490 case ICMP_ULE: return ICMP_UGE;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003491
Reid Spencer45fb3f32006-11-20 01:22:35 +00003492 case FCMP_FALSE: case FCMP_TRUE:
3493 case FCMP_OEQ: case FCMP_ONE:
3494 case FCMP_UEQ: case FCMP_UNE:
3495 case FCMP_ORD: case FCMP_UNO:
3496 return pred;
3497 case FCMP_OGT: return FCMP_OLT;
3498 case FCMP_OLT: return FCMP_OGT;
3499 case FCMP_OGE: return FCMP_OLE;
3500 case FCMP_OLE: return FCMP_OGE;
3501 case FCMP_UGT: return FCMP_ULT;
3502 case FCMP_ULT: return FCMP_UGT;
3503 case FCMP_UGE: return FCMP_ULE;
3504 case FCMP_ULE: return FCMP_UGE;
3505 }
3506}
3507
Max Kazantsev38f2c0f2018-02-07 11:16:29 +00003508CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3509 switch (pred) {
3510 case ICMP_SGT: return ICMP_SGE;
3511 case ICMP_SLT: return ICMP_SLE;
3512 case ICMP_UGT: return ICMP_UGE;
3513 case ICMP_ULT: return ICMP_ULE;
3514 case FCMP_OGT: return FCMP_OGE;
3515 case FCMP_OLT: return FCMP_OLE;
3516 case FCMP_UGT: return FCMP_UGE;
3517 case FCMP_ULT: return FCMP_ULE;
3518 default: return pred;
3519 }
3520}
3521
Sanjoy Dasb9d057d2015-10-22 19:57:34 +00003522CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3523 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3524
3525 switch (pred) {
3526 default:
3527 llvm_unreachable("Unknown predicate!");
3528 case CmpInst::ICMP_ULT:
3529 return CmpInst::ICMP_SLT;
3530 case CmpInst::ICMP_ULE:
3531 return CmpInst::ICMP_SLE;
3532 case CmpInst::ICMP_UGT:
3533 return CmpInst::ICMP_SGT;
3534 case CmpInst::ICMP_UGE:
3535 return CmpInst::ICMP_SGE;
3536 }
3537}
3538
Craig Topper240b0e12015-12-15 06:11:33 +00003539bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003540 switch (predicate) {
3541 default: return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003542 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencere4d87aa2006-12-23 06:05:41 +00003543 case ICmpInst::ICMP_UGE: return true;
3544 }
3545}
3546
Craig Topper240b0e12015-12-15 06:11:33 +00003547bool CmpInst::isSigned(Predicate predicate) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003548 switch (predicate) {
3549 default: return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003550 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencere4d87aa2006-12-23 06:05:41 +00003551 case ICmpInst::ICMP_SGE: return true;
3552 }
3553}
3554
Craig Topper240b0e12015-12-15 06:11:33 +00003555bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003556 switch (predicate) {
3557 default: return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003558 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3559 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencere4d87aa2006-12-23 06:05:41 +00003560 case FCmpInst::FCMP_ORD: return true;
3561 }
3562}
Fangrui Songaf7b1832018-07-30 19:41:25 +00003563
Craig Topper240b0e12015-12-15 06:11:33 +00003564bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003565 switch (predicate) {
3566 default: return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003567 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3568 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencere4d87aa2006-12-23 06:05:41 +00003569 case FCmpInst::FCMP_UNO: return true;
3570 }
3571}
3572
Craig Topper240b0e12015-12-15 06:11:33 +00003573bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky44df0232009-10-25 03:50:03 +00003574 switch(predicate) {
3575 default: return false;
3576 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3577 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3578 }
3579}
3580
Craig Topper240b0e12015-12-15 06:11:33 +00003581bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky44df0232009-10-25 03:50:03 +00003582 switch(predicate) {
3583 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3584 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3585 default: return false;
3586 }
3587}
3588
Chad Rosier615a6cf2016-04-21 16:18:02 +00003589bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier88419b22016-04-21 14:04:54 +00003590 // If the predicates match, then we know the first condition implies the
3591 // second is true.
3592 if (Pred1 == Pred2)
3593 return true;
3594
3595 switch (Pred1) {
3596 default:
3597 break;
Chad Rosier6d282e52016-04-22 17:57:34 +00003598 case ICMP_EQ:
Chad Rosier4d87cf62016-04-25 13:25:14 +00003599 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier6d282e52016-04-22 17:57:34 +00003600 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3601 Pred2 == ICMP_SLE;
Chad Rosier14f36d72016-04-22 17:14:12 +00003602 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3603 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3604 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3605 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3606 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3607 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3608 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3609 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosier88419b22016-04-21 14:04:54 +00003610 }
3611 return false;
3612}
3613
Chad Rosier615a6cf2016-04-21 16:18:02 +00003614bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier6d282e52016-04-22 17:57:34 +00003615 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosier88419b22016-04-21 14:04:54 +00003616}
Nick Lewycky44df0232009-10-25 03:50:03 +00003617
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003618//===----------------------------------------------------------------------===//
3619// SwitchInst Implementation
3620//===----------------------------------------------------------------------===//
3621
Chris Lattneraa6e3502010-11-17 05:41:46 +00003622void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3623 assert(Value && Default && NumReserved);
3624 ReservedSpace = NumReserved;
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003625 setNumHungOffUseOperands(2);
Pete Cooperea423672015-06-10 22:38:46 +00003626 allocHungoffUses(ReservedSpace);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003627
Pete Cooper10520422015-05-21 22:48:54 +00003628 Op<0>() = Value;
3629 Op<1>() = Default;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003630}
3631
Chris Lattner910c80a2007-02-24 00:55:48 +00003632/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3633/// switch on and a default destination. The number of additional cases can
3634/// be specified here to make memory allocation more efficient. This
3635/// constructor can also autoinsert before another instruction.
3636SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3637 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003638 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3639 nullptr, 0, InsertBefore) {
Chris Lattneraa6e3502010-11-17 05:41:46 +00003640 init(Value, Default, 2+NumCases*2);
Chris Lattner910c80a2007-02-24 00:55:48 +00003641}
3642
3643/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3644/// switch on and a default destination. The number of additional cases can
3645/// be specified here to make memory allocation more efficient. This
3646/// constructor also autoinserts at the end of the specified BasicBlock.
3647SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3648 BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003649 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3650 nullptr, 0, InsertAtEnd) {
Chris Lattneraa6e3502010-11-17 05:41:46 +00003651 init(Value, Default, 2+NumCases*2);
Chris Lattner910c80a2007-02-24 00:55:48 +00003652}
3653
Misha Brukmanfd939082005-04-21 23:48:37 +00003654SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003655 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattneraa6e3502010-11-17 05:41:46 +00003656 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003657 setNumHungOffUseOperands(SI.getNumOperands());
Pete Coopercff40fc2015-06-12 17:48:05 +00003658 Use *OL = getOperandList();
3659 const Use *InOL = SI.getOperandList();
Chris Lattneraa6e3502010-11-17 05:41:46 +00003660 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +00003661 OL[i] = InOL[i];
3662 OL[i+1] = InOL[i+1];
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003663 }
Dan Gohman58cfa3b2009-08-25 22:11:20 +00003664 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003665}
3666
3667/// addCase - Add an entry to the switch instruction...
3668///
Chris Lattnerd1a32602005-02-24 05:32:09 +00003669void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003670 unsigned NewCaseIdx = getNumCases();
3671 unsigned OpNo = getNumOperands();
Chris Lattnerb12261a2005-01-29 00:35:16 +00003672 if (OpNo+2 > ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00003673 growOperands(); // Get more space!
Chris Lattnerb12261a2005-01-29 00:35:16 +00003674 // Initialize some new operands.
Chris Lattner1f377fc2005-01-29 01:05:12 +00003675 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003676 setNumHungOffUseOperands(OpNo+2);
Chandler Carruthddfada22017-04-12 07:27:28 +00003677 CaseHandle Case(this, NewCaseIdx);
Bob Wilsondb3a9e62013-09-09 19:14:35 +00003678 Case.setValue(OnVal);
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00003679 Case.setSuccessor(Dest);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003680}
3681
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003682/// removeCase - This method removes the specified case and its successor
3683/// from the switch instruction.
Chandler Carruthddfada22017-04-12 07:27:28 +00003684SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3685 unsigned idx = I->getCaseIndex();
3686
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003687 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerb12261a2005-01-29 00:35:16 +00003688
3689 unsigned NumOps = getNumOperands();
Pete Coopercff40fc2015-06-12 17:48:05 +00003690 Use *OL = getOperandList();
Chris Lattnerb12261a2005-01-29 00:35:16 +00003691
Jay Foad0faa6092011-02-01 09:22:34 +00003692 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003693 if (2 + (idx + 1) * 2 != NumOps) {
3694 OL[2 + idx * 2] = OL[NumOps - 2];
3695 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerb12261a2005-01-29 00:35:16 +00003696 }
3697
3698 // Nuke the last value.
Craig Topperec0f0bc2014-04-09 06:08:46 +00003699 OL[NumOps-2].set(nullptr);
3700 OL[NumOps-2+1].set(nullptr);
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003701 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth627e0f92017-03-26 02:49:23 +00003702
3703 return CaseIt(this, idx);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003704}
3705
Jay Foad8891ed72011-04-01 08:00:58 +00003706/// growOperands - grow operands - This grows the operand list in response
3707/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerb12261a2005-01-29 00:35:16 +00003708///
Jay Foad8891ed72011-04-01 08:00:58 +00003709void SwitchInst::growOperands() {
Gabor Greifefe65362008-05-10 08:32:32 +00003710 unsigned e = getNumOperands();
Jay Foad8891ed72011-04-01 08:00:58 +00003711 unsigned NumOps = e*3;
Chris Lattnerb12261a2005-01-29 00:35:16 +00003712
3713 ReservedSpace = NumOps;
Pete Cooper33102d22015-06-10 22:38:41 +00003714 growHungoffUses(ReservedSpace);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003715}
3716
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003717//===----------------------------------------------------------------------===//
Jay Foad1ed26ac2011-01-16 15:30:52 +00003718// IndirectBrInst Implementation
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003719//===----------------------------------------------------------------------===//
3720
Chris Lattnerab21db72009-10-28 00:19:10 +00003721void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands1df98592010-02-16 11:11:14 +00003722 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattnera4c206f2009-10-29 05:53:32 +00003723 "Address of indirectbr must be a pointer");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003724 ReservedSpace = 1+NumDests;
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003725 setNumHungOffUseOperands(1);
Pete Cooperea423672015-06-10 22:38:46 +00003726 allocHungoffUses(ReservedSpace);
3727
Pete Cooper10520422015-05-21 22:48:54 +00003728 Op<0>() = Address;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003729}
3730
3731
Jay Foad8891ed72011-04-01 08:00:58 +00003732/// growOperands - grow operands - This grows the operand list in response
3733/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003734///
Jay Foad8891ed72011-04-01 08:00:58 +00003735void IndirectBrInst::growOperands() {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003736 unsigned e = getNumOperands();
Jay Foad8891ed72011-04-01 08:00:58 +00003737 unsigned NumOps = e*2;
Fangrui Songaf7b1832018-07-30 19:41:25 +00003738
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003739 ReservedSpace = NumOps;
Pete Cooper33102d22015-06-10 22:38:41 +00003740 growHungoffUses(ReservedSpace);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003741}
3742
Chris Lattnerab21db72009-10-28 00:19:10 +00003743IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3744 Instruction *InsertBefore)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003745 : Instruction(Type::getVoidTy(Address->getContext()),
3746 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003747 init(Address, NumCases);
3748}
3749
Chris Lattnerab21db72009-10-28 00:19:10 +00003750IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3751 BasicBlock *InsertAtEnd)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003752 : Instruction(Type::getVoidTy(Address->getContext()),
3753 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003754 init(Address, NumCases);
3755}
3756
Chris Lattnerab21db72009-10-28 00:19:10 +00003757IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth09ebf7a2018-10-19 00:22:37 +00003758 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3759 nullptr, IBI.getNumOperands()) {
Pete Cooperea423672015-06-10 22:38:46 +00003760 allocHungoffUses(IBI.getNumOperands());
Pete Coopercff40fc2015-06-12 17:48:05 +00003761 Use *OL = getOperandList();
3762 const Use *InOL = IBI.getOperandList();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003763 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3764 OL[i] = InOL[i];
3765 SubclassOptionalData = IBI.SubclassOptionalData;
3766}
3767
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003768/// addDestination - Add a destination.
3769///
Chris Lattnerab21db72009-10-28 00:19:10 +00003770void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003771 unsigned OpNo = getNumOperands();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003772 if (OpNo+1 > ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00003773 growOperands(); // Get more space!
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003774 // Initialize some new operands.
3775 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003776 setNumHungOffUseOperands(OpNo+1);
Pete Coopercff40fc2015-06-12 17:48:05 +00003777 getOperandList()[OpNo] = DestBB;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003778}
3779
3780/// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00003781/// indirectbr instruction.
3782void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003783 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songaf7b1832018-07-30 19:41:25 +00003784
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003785 unsigned NumOps = getNumOperands();
Pete Coopercff40fc2015-06-12 17:48:05 +00003786 Use *OL = getOperandList();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003787
3788 // Replace this value with the last one.
3789 OL[idx+1] = OL[NumOps-1];
Fangrui Songaf7b1832018-07-30 19:41:25 +00003790
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003791 // Nuke the last value.
Craig Topperec0f0bc2014-04-09 06:08:46 +00003792 OL[NumOps-1].set(nullptr);
Pete Cooperaaa3fa62015-06-12 17:48:10 +00003793 setNumHungOffUseOperands(NumOps-1);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003794}
3795
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003796//===----------------------------------------------------------------------===//
Pete Cooperf79d2532015-06-24 20:22:23 +00003797// cloneImpl() implementations
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003798//===----------------------------------------------------------------------===//
3799
Chris Lattner795948a2004-10-15 23:52:53 +00003800// Define these methods here so vtables don't get emitted into every translation
3801// unit that uses these classes.
3802
Pete Cooperf79d2532015-06-24 20:22:23 +00003803GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003804 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattner795948a2004-10-15 23:52:53 +00003805}
3806
Cameron McInallyca8cb682018-11-13 18:15:47 +00003807UnaryOperator *UnaryOperator::cloneImpl() const {
3808 return Create(getOpcode(), Op<0>());
3809}
3810
Pete Cooperf79d2532015-06-24 20:22:23 +00003811BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003812 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattner795948a2004-10-15 23:52:53 +00003813}
3814
Pete Cooperf79d2532015-06-24 20:22:23 +00003815FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003816 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00003817}
3818
Pete Cooperf79d2532015-06-24 20:22:23 +00003819ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003820 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmane4569942008-05-23 00:36:11 +00003821}
3822
Pete Cooperf79d2532015-06-24 20:22:23 +00003823ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003824 return new ExtractValueInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003825}
3826
Pete Cooperf79d2532015-06-24 20:22:23 +00003827InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003828 return new InsertValueInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003829}
3830
Pete Cooperf79d2532015-06-24 20:22:23 +00003831AllocaInst *AllocaInst::cloneImpl() const {
David Majnemerc16fa882014-04-30 16:12:21 +00003832 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00003833 getType()->getAddressSpace(),
David Majnemerc16fa882014-04-30 16:12:21 +00003834 (Value *)getOperand(0), getAlignment());
3835 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren4bda8822016-04-01 21:41:15 +00003836 Result->setSwiftError(isSwiftError());
David Majnemerc16fa882014-04-30 16:12:21 +00003837 return Result;
Owen Anderson333c4002009-07-09 23:48:35 +00003838}
3839
Pete Cooperf79d2532015-06-24 20:22:23 +00003840LoadInst *LoadInst::cloneImpl() const {
James Y Knight4ed9d9e2019-01-14 21:37:53 +00003841 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003842 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson333c4002009-07-09 23:48:35 +00003843}
3844
Pete Cooperf79d2532015-06-24 20:22:23 +00003845StoreInst *StoreInst::cloneImpl() const {
Eli Friedman1cc5e382011-08-10 17:39:11 +00003846 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003847 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songaf7b1832018-07-30 19:41:25 +00003848
Owen Anderson333c4002009-07-09 23:48:35 +00003849}
3850
Pete Cooperf79d2532015-06-24 20:22:23 +00003851AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanff030482011-07-28 21:48:00 +00003852 AtomicCmpXchgInst *Result =
3853 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northoverca396e32014-03-11 10:48:52 +00003854 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003855 getSyncScopeID());
Eli Friedmanff030482011-07-28 21:48:00 +00003856 Result->setVolatile(isVolatile());
Tim Northover8f2a85e2014-06-13 14:24:07 +00003857 Result->setWeak(isWeak());
Eli Friedmanff030482011-07-28 21:48:00 +00003858 return Result;
3859}
3860
Pete Cooperf79d2532015-06-24 20:22:23 +00003861AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanff030482011-07-28 21:48:00 +00003862 AtomicRMWInst *Result =
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003863 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3864 getOrdering(), getSyncScopeID());
Eli Friedmanff030482011-07-28 21:48:00 +00003865 Result->setVolatile(isVolatile());
3866 return Result;
3867}
3868
Pete Cooperf79d2532015-06-24 20:22:23 +00003869FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003870 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedman47f35132011-07-25 23:16:38 +00003871}
3872
Pete Cooperf79d2532015-06-24 20:22:23 +00003873TruncInst *TruncInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003874 return new TruncInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003875}
3876
Pete Cooperf79d2532015-06-24 20:22:23 +00003877ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003878 return new ZExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003879}
3880
Pete Cooperf79d2532015-06-24 20:22:23 +00003881SExtInst *SExtInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003882 return new SExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003883}
3884
Pete Cooperf79d2532015-06-24 20:22:23 +00003885FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003886 return new FPTruncInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003887}
3888
Pete Cooperf79d2532015-06-24 20:22:23 +00003889FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003890 return new FPExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003891}
3892
Pete Cooperf79d2532015-06-24 20:22:23 +00003893UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003894 return new UIToFPInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003895}
3896
Pete Cooperf79d2532015-06-24 20:22:23 +00003897SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003898 return new SIToFPInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003899}
3900
Pete Cooperf79d2532015-06-24 20:22:23 +00003901FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003902 return new FPToUIInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003903}
3904
Pete Cooperf79d2532015-06-24 20:22:23 +00003905FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003906 return new FPToSIInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003907}
3908
Pete Cooperf79d2532015-06-24 20:22:23 +00003909PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003910 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003911}
3912
Pete Cooperf79d2532015-06-24 20:22:23 +00003913IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003914 return new IntToPtrInst(getOperand(0), getType());
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003915}
Owen Anderson333c4002009-07-09 23:48:35 +00003916
Pete Cooperf79d2532015-06-24 20:22:23 +00003917BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003918 return new BitCastInst(getOperand(0), getType());
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003919}
Reid Spencer3da59db2006-11-27 01:05:10 +00003920
Pete Cooperf79d2532015-06-24 20:22:23 +00003921AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenault59d3ae62013-11-15 01:34:59 +00003922 return new AddrSpaceCastInst(getOperand(0), getType());
3923}
3924
Pete Cooperf79d2532015-06-24 20:22:23 +00003925CallInst *CallInst::cloneImpl() const {
Sanjoy Das4afa7f12015-11-10 20:13:21 +00003926 if (hasOperandBundles()) {
3927 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3928 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3929 }
Devang Patel50b6e332009-10-27 22:16:29 +00003930 return new(getNumOperands()) CallInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003931}
3932
Pete Cooperf79d2532015-06-24 20:22:23 +00003933SelectInst *SelectInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003934 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattner00f10232006-04-08 01:18:18 +00003935}
Owen Anderson333c4002009-07-09 23:48:35 +00003936
Pete Cooperf79d2532015-06-24 20:22:23 +00003937VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003938 return new VAArgInst(getOperand(0), getType());
Chris Lattner00f10232006-04-08 01:18:18 +00003939}
Owen Anderson333c4002009-07-09 23:48:35 +00003940
Pete Cooperf79d2532015-06-24 20:22:23 +00003941ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003942 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattner00f10232006-04-08 01:18:18 +00003943}
Owen Anderson333c4002009-07-09 23:48:35 +00003944
Pete Cooperf79d2532015-06-24 20:22:23 +00003945InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner83694a92012-01-25 23:49:49 +00003946 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson333c4002009-07-09 23:48:35 +00003947}
3948
Pete Cooperf79d2532015-06-24 20:22:23 +00003949ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner83694a92012-01-25 23:49:49 +00003950 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003951}
Owen Anderson333c4002009-07-09 23:48:35 +00003952
Pete Cooperf79d2532015-06-24 20:22:23 +00003953PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel50b6e332009-10-27 22:16:29 +00003954
Pete Cooperf79d2532015-06-24 20:22:23 +00003955LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlinge6e88262011-08-12 20:24:12 +00003956 return new LandingPadInst(*this);
3957}
3958
Pete Cooperf79d2532015-06-24 20:22:23 +00003959ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel50b6e332009-10-27 22:16:29 +00003960 return new(getNumOperands()) ReturnInst(*this);
3961}
3962
Pete Cooperf79d2532015-06-24 20:22:23 +00003963BranchInst *BranchInst::cloneImpl() const {
Jay Foad8e3914d2011-01-07 20:29:02 +00003964 return new(getNumOperands()) BranchInst(*this);
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003965}
Owen Anderson333c4002009-07-09 23:48:35 +00003966
Pete Cooperf79d2532015-06-24 20:22:23 +00003967SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson333c4002009-07-09 23:48:35 +00003968
Pete Cooperf79d2532015-06-24 20:22:23 +00003969IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerab21db72009-10-28 00:19:10 +00003970 return new IndirectBrInst(*this);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003971}
3972
Pete Cooperf79d2532015-06-24 20:22:23 +00003973InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Das4afa7f12015-11-10 20:13:21 +00003974 if (hasOperandBundles()) {
3975 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3976 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3977 }
Devang Patel50b6e332009-10-27 22:16:29 +00003978 return new(getNumOperands()) InvokeInst(*this);
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003979}
Owen Anderson333c4002009-07-09 23:48:35 +00003980
Pete Cooperf79d2532015-06-24 20:22:23 +00003981ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003982
David Majnemer4a45f082015-07-31 17:58:14 +00003983CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3984 return new (getNumOperands()) CleanupReturnInst(*this);
3985}
3986
David Majnemer4a45f082015-07-31 17:58:14 +00003987CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemerde17e772015-08-15 02:46:08 +00003988 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer4a45f082015-07-31 17:58:14 +00003989}
3990
David Majnemer8cec2f22015-12-12 05:38:55 +00003991CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3992 return new CatchSwitchInst(*this);
3993}
3994
3995FuncletPadInst *FuncletPadInst::cloneImpl() const {
3996 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer4a45f082015-07-31 17:58:14 +00003997}
3998
Pete Cooperf79d2532015-06-24 20:22:23 +00003999UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky67760642009-09-27 07:38:41 +00004000 LLVMContext &Context = getContext();
Devang Patel50b6e332009-10-27 22:16:29 +00004001 return new UnreachableInst(Context);
Owen Anderson333c4002009-07-09 23:48:35 +00004002}