blob: d88779bae738cefb1536d05eb58608c817cd8da0 [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001//===- Instructions.cpp - Implement the LLVM instructions -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-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 Carruth219b89b2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-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 Zelenkod761e2c2017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Chandler Carruth05b5bd82018-12-27 23:40:17 +000030#include "llvm/IR/Intrinsics.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000031#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-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 Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000040#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000041#include <algorithm>
42#include <cassert>
43#include <cstdint>
44#include <vector>
45
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000046using namespace llvm;
47
Chris Lattner3e13b8c2008-01-02 23:42:30 +000048//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-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 Lattner3e13b8c2008-01-02 23:42:30 +000065// CallSite Class
66//===----------------------------------------------------------------------===//
67
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000068User::op_iterator CallSite::getCallee() const {
Chandler Carruthe429c792018-11-22 10:31:35 +000069 return cast<CallBase>(getInstruction())->op_end() - 1;
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000070}
71
Gordon Henriksen14a55692007-12-10 02:14:30 +000072//===----------------------------------------------------------------------===//
Chris Lattner88107952008-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 Majnemerb611e3f2015-08-14 05:09:07 +000081
82 if (Op1->getType()->isTokenTy())
83 return "select values cannot have token type";
84
Chris Lattner229907c2011-07-18 04:54:35 +000085 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000086 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000087 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000088 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000089 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000090 if (!ET)
Chris Lattner88107952008-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 Anderson55f1c092009-08-13 21:58:54 +000095 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000096 return "select condition must be i1 or <n x i1>";
97 }
Craig Topperc6207612014-04-09 06:08:46 +000098 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000099}
100
Chris Lattner88107952008-12-29 00:12:50 +0000101//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102// PHINode Class
103//===----------------------------------------------------------------------===//
104
105PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000106 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
107 ReservedSpace(PN.getNumOperands()) {
108 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-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 Gohmanc8a27f22009-08-25 22:11:20 +0000111 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112}
113
Chris Lattnerafdb3de2005-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 Foad61ea0e42011-06-23 09:09:15 +0000117 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-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 Foad61ea0e42011-06-23 09:09:15 +0000122 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-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 Lattnerafdb3de2005-01-29 00:35:16 +0000126
127 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000128 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000129 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130
131 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000132 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000134 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135 eraseFromParent();
136 }
137 return Removed;
138}
139
Jay Foade98f29d2011-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 Lattnerafdb3de2005-01-29 00:35:16 +0000143///
Jay Foade98f29d2011-04-01 08:00:58 +0000144void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 unsigned e = getNumOperands();
Jay Foad61ea0e42011-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 Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000150 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151}
152
Nate Begemanb3923212005-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 Sands7412f6e2010-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 Lopes90c76df2012-07-03 17:10:28 +0000159 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
160 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000161 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000162 // The case where the first value is this PHI.
163 ConstantValue = getIncomingValue(i);
164 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000165 if (ConstantValue == this)
166 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000167 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000168}
169
Nicolai Haehnle13d90f32016-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 Wendlingfae14752011-08-12 20:24:12 +0000188//===----------------------------------------------------------------------===//
189// LandingPadInst Implementation
190//===----------------------------------------------------------------------===//
191
David Majnemer7fddecc2015-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 Wendlingfae14752011-08-12 20:24:12 +0000196}
197
David Majnemer7fddecc2015-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 Wendlingfae14752011-08-12 20:24:12 +0000202}
203
204LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000205 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
206 LP.getNumOperands()),
207 ReservedSpace(LP.getNumOperands()) {
208 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000209 Use *OL = getOperandList();
210 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-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 Majnemer7fddecc2015-06-17 20:52:32 +0000217LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000218 const Twine &NameStr,
219 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000220 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000221}
222
David Majnemer7fddecc2015-06-17 20:52:32 +0000223LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000224 const Twine &NameStr,
225 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000226 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000227}
228
David Majnemer7fddecc2015-06-17 20:52:32 +0000229void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000230 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000231 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000232 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-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 Majnemer7fddecc2015-06-17 20:52:32 +0000242 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000243 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000244}
245
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000246void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000247 unsigned OpNo = getNumOperands();
248 growOperands(1);
249 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000250 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000251 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000252}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253
254//===----------------------------------------------------------------------===//
Chandler Carruthe429c792018-11-22 10:31:35 +0000255// CallBase Implementation
256//===----------------------------------------------------------------------===//
257
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000258Function *CallBase::getCaller() { return getParent()->getParent(); }
259
Chandler Carruth57578aa2019-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 Carruth05b5bd82018-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 Carruthe429c792018-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 Evlogimenos93a7c062004-07-29 12:33:25 +0000361// CallInst Implementation
362//===----------------------------------------------------------------------===//
363
David Blaikie348de692015-04-23 21:36:23 +0000364void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000365 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000366 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000367 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
368 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000369 setCalledOperand(Func);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370
Jay Foad5bd375a2011-07-15 08:37:34 +0000371#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000372 assert((Args.size() == FTy->getNumParams() ||
373 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000374 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000375
376 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000377 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000378 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000379 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000380#endif
381
Fangrui Song75709322018-11-17 01:44:25 +0000382 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-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 Foad5bd375a2011-07-15 08:37:34 +0000388 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389}
390
James Y Knightf9563902019-01-14 21:37:42 +0000391void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
392 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000393 assert(getNumOperands() == 1 && "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000394 setCalledOperand(Func);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000395
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000396 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000397
398 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399}
400
James Y Knightf9563902019-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 Evlogimenos93a7c062004-07-29 12:33:25 +0000406}
407
James Y Knightf9563902019-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 Evlogimenos93a7c062004-07-29 12:33:25 +0000413}
414
Misha Brukmanb1c93172005-04-21 23:48:37 +0000415CallInst::CallInst(const CallInst &CI)
Chandler Carruthe429c792018-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 Kleckner118e1bf2014-05-06 20:08:20 +0000419 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000420 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000421
Jay Foad5bd375a2011-07-15 08:37:34 +0000422 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000423 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
424 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000425 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426}
427
Sanjoy Das2d161452015-11-18 06:23:38 +0000428CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
429 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000430 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-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 Dasb8dced52015-12-09 01:01:28 +0000437 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000438 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000439 return NewCI;
440}
441
Hal Finkele87ad542016-07-10 23:01:32 +0000442
Reid Klecknera0b45f42017-05-03 18:17:31 +0000443
Hal Finkele87ad542016-07-10 23:01:32 +0000444
Eric Christopher901b1a72008-05-16 20:39:43 +0000445
Amaury Secheta65a2372016-06-15 05:14:29 +0000446
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000447
Reid Klecknera0b45f42017-05-03 18:17:31 +0000448
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000449
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000450
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451/// IsConstantOne - Return true only if val is constant int 1
452static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000453 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000454 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
455 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000456}
457
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000458static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000459 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000460 Type *AllocTy, Value *AllocSize,
461 Value *ArraySize,
462 ArrayRef<OperandBundleDef> OpB,
463 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000464 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465 "createMalloc needs either InsertBefore or InsertAtEnd");
466
Fangrui Songf78650a2018-07-30 19:41:25 +0000467 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000468 // bitcast (i8* malloc(typeSize)) to type*
469 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000470 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 if (!ArraySize)
472 ArraySize = ConstantInt::get(IntPtrTy, 1);
473 else if (ArraySize->getType() != IntPtrTy) {
474 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000475 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
476 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000477 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000478 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
479 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000480 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000481
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000482 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-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 Cheng1d9d4bd2009-09-10 04:36:43 +0000491 // Multiply type size by the array size...
492 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000493 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
494 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000496 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
497 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000498 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000499 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000500
Victor Hernandez788eaab2009-09-18 19:20:02 +0000501 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000503 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
504 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000505 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000506 Value *MallocFunc = MallocF;
507 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000508 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000509 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000510 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000511 CallInst *MCall = nullptr;
512 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000513 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000514 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
515 InsertBefore);
Victor Hernandezc7d6a832009-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 Hernandezf3db9152009-11-07 00:16:28 +0000519 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000520 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000521 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-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 Hernandezf3db9152009-11-07 00:16:28 +0000526 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000527 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000528 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000529 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000530 if (Function *F = dyn_cast<Function>(MallocFunc)) {
531 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000532 if (!F->returnDoesNotAlias())
533 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000534 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000535 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000536
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000537 return Result;
Evan Cheng1d9d4bd2009-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 Lewyckybb1410e2009-10-17 23:52:26 +0000546Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000547 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000548 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000549 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000550 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000551 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000552 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000553}
David Majnemerfadc6db2016-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 Cheng1d9d4bd2009-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 Lewyckybb1410e2009-10-17 23:52:26 +0000572Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000573 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000574 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000575 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000576 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-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 Cheng1d9d4bd2009-09-10 04:36:43 +0000586}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000587
David Majnemerfadc6db2016-04-29 08:07:22 +0000588static Instruction *createFree(Value *Source,
589 ArrayRef<OperandBundleDef> Bundles,
590 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000591 BasicBlock *InsertAtEnd) {
592 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
593 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000594 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000595 "Can not free something of nonpointer type!");
596
Ana Pazosb3596022016-02-03 21:34:39 +0000597 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
598 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000599
Chris Lattner229907c2011-07-18 04:54:35 +0000600 Type *VoidTy = Type::getVoidTy(M->getContext());
601 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000602 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000603 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000604 CallInst *Result = nullptr;
Victor Hernandeze2971492009-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 Majnemerfadc6db2016-04-29 08:07:22 +0000609 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000610 } else {
611 if (Source->getType() != IntPtrTy)
612 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000613 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000614 }
615 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000616 if (Function *F = dyn_cast<Function>(FreeFunc))
617 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-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 Pazosb3596022016-02-03 21:34:39 +0000623Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-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 Hernandeze2971492009-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 Pazosb3596022016-02-03 21:34:39 +0000635Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-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 Hernandeze2971492009-10-24 04:23:03 +0000644 assert(FreeCall && "CreateFree did not create a CallInst");
645 return FreeCall;
646}
647
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648//===----------------------------------------------------------------------===//
649// InvokeInst Implementation
650//===----------------------------------------------------------------------===//
651
David Blaikie3e807092015-05-13 18:35:26 +0000652void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
653 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000654 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000655 const Twine &NameStr) {
656 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000657
Chandler Carruthe429c792018-11-22 10:31:35 +0000658 assert((int)getNumOperands() ==
659 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
Sanjoy Das9303c242015-09-24 19:14:18 +0000660 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000661 setNormalDest(IfNormal);
662 setUnwindDest(IfException);
663 setCalledOperand(Fn);
Jay Foad5bd375a2011-07-15 08:37:34 +0000664
665#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000666 assert(((Args.size() == FTy->getNumParams()) ||
667 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000668 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000669
Jay Foad5bd375a2011-07-15 08:37:34 +0000670 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000671 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000672 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000673 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000674#endif
675
Fangrui Song75709322018-11-17 01:44:25 +0000676 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-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 Foad5bd375a2011-07-15 08:37:34 +0000682 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000683}
684
Misha Brukmanb1c93172005-04-21 23:48:37 +0000685InvokeInst::InvokeInst(const InvokeInst &II)
Chandler Carruthe429c792018-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 Lattnerb9c86512009-12-29 02:14:09 +0000689 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000690 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000691 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
692 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000693 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
Sanjoy Das2d161452015-11-18 06:23:38 +0000696InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
697 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000698 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-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 Dasb8dced52015-12-09 01:01:28 +0000705 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000706 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000707 return NewII;
708}
709
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000710
Bill Wendlingfae14752011-08-12 20:24:12 +0000711LandingPadInst *InvokeInst::getLandingPadInst() const {
712 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
713}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000714
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715//===----------------------------------------------------------------------===//
716// ReturnInst Implementation
717//===----------------------------------------------------------------------===//
718
Chris Lattner2195fc42007-02-24 00:55:48 +0000719ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth509e20e2018-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 Gohmanfa1211f2008-07-23 00:34:11 +0000723 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000724 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000725 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000726}
727
Owen Anderson55f1c092009-08-13 21:58:54 +0000728ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000729 : Instruction(Type::getVoidTy(C), Instruction::Ret,
730 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
731 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000732 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000733 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000734}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000735
Owen Anderson55f1c092009-08-13 21:58:54 +0000736ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000737 : Instruction(Type::getVoidTy(C), Instruction::Ret,
738 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
739 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000740 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000741 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000742}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000743
Owen Anderson55f1c092009-08-13 21:58:54 +0000744ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000745 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
746 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel59643e52008-02-23 00:35:18 +0000747
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000749// ResumeInst Implementation
750//===----------------------------------------------------------------------===//
751
752ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000753 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
754 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000755 Op<0>() = RI.Op<0>();
756}
757
758ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000759 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
760 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000761 Op<0>() = Exn;
762}
763
764ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000765 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
766 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000767 Op<0>() = Exn;
768}
769
Bill Wendlingf891bf82011-07-31 06:30:59 +0000770//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000771// CleanupReturnInst Implementation
772//===----------------------------------------------------------------------===//
773
774CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000775 : Instruction(CRI.getType(), Instruction::CleanupRet,
776 OperandTraits<CleanupReturnInst>::op_end(this) -
777 CRI.getNumOperands(),
778 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000779 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000780 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000781 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000782 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000783}
784
David Majnemer8a1c45d2015-12-12 05:38:55 +0000785void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000786 if (UnwindBB)
787 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000788
David Majnemer8a1c45d2015-12-12 05:38:55 +0000789 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000790 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000791 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000792}
793
David Majnemer8a1c45d2015-12-12 05:38:55 +0000794CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
795 unsigned Values, Instruction *InsertBefore)
Chandler Carruth509e20e2018-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 Tremoulet8220bcc2015-08-23 00:26:33 +0000800 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000801}
802
David Majnemer8a1c45d2015-12-12 05:38:55 +0000803CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
804 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-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 Tremoulet8220bcc2015-08-23 00:26:33 +0000809 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000810}
811
David Majnemer654e1302015-07-31 17:58:14 +0000812//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000813// CatchReturnInst Implementation
814//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000815void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000816 Op<0>() = CatchPad;
817 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000818}
David Majnemer654e1302015-07-31 17:58:14 +0000819
820CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000821 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
822 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000823 Op<0>() = CRI.Op<0>();
824 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000825}
826
David Majnemer8a1c45d2015-12-12 05:38:55 +0000827CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000828 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000829 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
830 OperandTraits<CatchReturnInst>::op_begin(this), 2,
831 InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000832 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000833}
834
David Majnemer8a1c45d2015-12-12 05:38:55 +0000835CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000836 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000837 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
838 OperandTraits<CatchReturnInst>::op_begin(this), 2,
839 InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000840 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000841}
842
David Majnemer654e1302015-07-31 17:58:14 +0000843//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000844// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000845//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000846
847CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
848 unsigned NumReservedValues,
849 const Twine &NameStr,
850 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000851 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
852 InsertBefore) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000853 if (UnwindDest)
854 ++NumReservedValues;
855 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000856 setName(NameStr);
857}
858
David Majnemer8a1c45d2015-12-12 05:38:55 +0000859CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
860 unsigned NumReservedValues,
861 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000862 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
863 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000864 if (UnwindDest)
865 ++NumReservedValues;
866 init(ParentPad, UnwindDest, NumReservedValues + 1);
867 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000868}
869
David Majnemer8a1c45d2015-12-12 05:38:55 +0000870CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000871 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
872 CSI.getNumOperands()) {
David Majnemer8a1c45d2015-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 Majnemer654e1302015-07-31 17:58:14 +0000879}
David Majnemer8a1c45d2015-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 Tremoulet0d808882016-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 Majnemer8a1c45d2015-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 Song75709322018-11-17 01:44:25 +0000932 llvm::copy(Args, op_begin());
David Majnemer8a1c45d2015-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 Majnemer654e1302015-07-31 17:58:14 +0000962}
963
964//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000965// UnreachableInst Implementation
966//===----------------------------------------------------------------------===//
967
Fangrui Songf78650a2018-07-30 19:41:25 +0000968UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +0000969 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000970 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
971 0, InsertBefore) {}
Owen Anderson55f1c092009-08-13 21:58:54 +0000972UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000973 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
974 0, InsertAtEnd) {}
Chris Lattner2195fc42007-02-24 00:55:48 +0000975
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000976//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000977// BranchInst Implementation
978//===----------------------------------------------------------------------===//
979
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000980void BranchInst::AssertOK() {
981 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000982 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000983 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984}
985
Chris Lattner2195fc42007-02-24 00:55:48 +0000986BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000987 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
988 OperandTraits<BranchInst>::op_end(this) - 1, 1,
989 InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000990 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000991 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000992}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000993
Chris Lattner2195fc42007-02-24 00:55:48 +0000994BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
995 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000996 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
997 OperandTraits<BranchInst>::op_end(this) - 3, 3,
998 InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000999 Op<-1>() = IfTrue;
1000 Op<-2>() = IfFalse;
1001 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001002#ifndef NDEBUG
1003 AssertOK();
1004#endif
1005}
1006
1007BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001008 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1009 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001010 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001011 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001012}
1013
1014BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth509e20e2018-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 Greifc91aa9b2009-03-12 18:34:49 +00001018 Op<-1>() = IfTrue;
1019 Op<-2>() = IfFalse;
1020 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001021#ifndef NDEBUG
1022 AssertOK();
1023#endif
1024}
1025
Chandler Carruth509e20e2018-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 Greifc91aa9b2009-03-12 18:34:49 +00001030 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001031 if (BI.getNumOperands() != 1) {
1032 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001033 Op<-3>() = BI.Op<-3>();
1034 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001035 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001036 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037}
1038
Chandler Carruth3e8aa652011-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 Lidc491402016-08-23 15:39:03 +00001046 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001047}
1048
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001049//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001050// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051//===----------------------------------------------------------------------===//
1052
Owen Andersonb6b25302009-07-14 23:09:55 +00001053static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001054 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001055 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001056 else {
1057 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001058 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001059 assert(Amt->getType()->isIntegerTy() &&
1060 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001061 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001062 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063}
1064
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001065AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001066 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001067 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001068
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001069AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001070 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001071 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001072
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001073AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001074 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-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 Gohmanaa583d72008-03-24 16:55:58 +00001087 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001088 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001089 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090}
1091
Matt Arsenault3c1fc762017-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 Blaikiebf0a42a2015-04-29 23:00:35 +00001097 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001098 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001099 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001100 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101}
1102
Victor Hernandez8acf2952009-10-23 21:09:37 +00001103void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001104 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001105 assert(Align <= MaximumAlignment &&
1106 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001107 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1108 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001109 assert(getAlignment() == Align && "Alignment representation error!");
1110}
1111
Victor Hernandez8acf2952009-10-23 21:09:37 +00001112bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001113 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001114 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001115 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001116}
1117
Chris Lattner8b291e62008-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 Songf78650a2018-07-30 19:41:25 +00001124
Chris Lattner8b291e62008-11-26 02:54:17 +00001125 // Must be in the entry block.
1126 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001127 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001128}
1129
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131// LoadInst Implementation
1132//===----------------------------------------------------------------------===//
1133
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001134void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001135 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001137 assert(!(isAtomic() && getAlignment() == 0) &&
1138 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001139}
1140
Daniel Dunbar4975db62009-07-25 04:41:11 +00001141LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001142 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143
Daniel Dunbar4975db62009-07-25 04:41:11 +00001144LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001145 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001146
David Blaikie0c28fd72015-05-20 21:46:30 +00001147LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001148 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001149 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001150
1151LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1152 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001153 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001154
David Blaikieb7a029872015-04-17 19:56:21 +00001155LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001156 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001157 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001158 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001159
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001160LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001161 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001162 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001163 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001164
David Blaikie15d9a4c2015-04-06 20:59:48 +00001165LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001166 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001167 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001168 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001169 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001170 setVolatile(isVolatile);
1171 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001172 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001173 AssertOK();
1174 setName(Name);
1175}
1176
Fangrui Songf78650a2018-07-30 19:41:25 +00001177LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001178 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001179 SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001181 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001182 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001183 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001184 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001185 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001186 AssertOK();
1187 setName(Name);
1188}
1189
Daniel Dunbar27096822009-08-11 18:11:15 +00001190LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1191 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1192 Load, Ptr, InsertBef) {
1193 setVolatile(false);
1194 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001195 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001196 AssertOK();
1197 if (Name && Name[0]) setName(Name);
1198}
1199
1200LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1201 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1202 Load, Ptr, InsertAE) {
1203 setVolatile(false);
1204 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001205 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001206 AssertOK();
1207 if (Name && Name[0]) setName(Name);
1208}
1209
David Blaikie0c28fd72015-05-20 21:46:30 +00001210LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001211 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001212 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1213 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001214 setVolatile(isVolatile);
1215 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001216 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001217 AssertOK();
1218 if (Name && Name[0]) setName(Name);
1219}
1220
1221LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1222 BasicBlock *InsertAE)
1223 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1224 Load, Ptr, InsertAE) {
1225 setVolatile(isVolatile);
1226 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001227 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001228 AssertOK();
1229 if (Name && Name[0]) setName(Name);
1230}
1231
Christopher Lamb84485702007-04-22 19:24:39 +00001232void LoadInst::setAlignment(unsigned Align) {
1233 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001234 assert(Align <= MaximumAlignment &&
1235 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001236 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001237 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001238 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001239}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001240
1241//===----------------------------------------------------------------------===//
1242// StoreInst Implementation
1243//===----------------------------------------------------------------------===//
1244
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001245void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001246 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001247 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001248 "Ptr must have pointer type!");
1249 assert(getOperand(0)->getType() ==
1250 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001251 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001252 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001253 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001254}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001255
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001256StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001257 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001258
1259StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001260 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001261
Misha Brukmanb1c93172005-04-21 23:48:37 +00001262StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001263 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001264 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001265
1266StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001267 BasicBlock *InsertAtEnd)
1268 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1269
1270StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1271 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001272 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001273 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001274
1275StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1276 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001277 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001278 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001279
Misha Brukmanb1c93172005-04-21 23:48:37 +00001280StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001281 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001282 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001283 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001284 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001285 OperandTraits<StoreInst>::op_begin(this),
1286 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001287 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001288 Op<0>() = val;
1289 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001290 setVolatile(isVolatile);
1291 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001292 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001293 AssertOK();
1294}
1295
1296StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001297 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001298 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001299 BasicBlock *InsertAtEnd)
1300 : Instruction(Type::getVoidTy(val->getContext()), Store,
1301 OperandTraits<StoreInst>::op_begin(this),
1302 OperandTraits<StoreInst>::operands(this),
1303 InsertAtEnd) {
1304 Op<0>() = val;
1305 Op<1>() = addr;
1306 setVolatile(isVolatile);
1307 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001308 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001309 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310}
1311
Christopher Lamb84485702007-04-22 19:24:39 +00001312void StoreInst::setAlignment(unsigned Align) {
1313 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001314 assert(Align <= MaximumAlignment &&
1315 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001316 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001317 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001318 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001319}
1320
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001322// AtomicCmpXchgInst Implementation
1323//===----------------------------------------------------------------------===//
1324
1325void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001326 AtomicOrdering SuccessOrdering,
1327 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001328 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001329 Op<0>() = Ptr;
1330 Op<1>() = Cmp;
1331 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001332 setSuccessOrdering(SuccessOrdering);
1333 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001334 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001335
1336 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1337 "All operands must be non-null!");
1338 assert(getOperand(0)->getType()->isPointerTy() &&
1339 "Ptr must have pointer type!");
1340 assert(getOperand(1)->getType() ==
1341 cast<PointerType>(getOperand(0)->getType())->getElementType()
1342 && "Ptr must be a pointer to Cmp type!");
1343 assert(getOperand(2)->getType() ==
1344 cast<PointerType>(getOperand(0)->getType())->getElementType()
1345 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001346 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001347 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001348 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001349 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001350 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1351 "AtomicCmpXchg failure argument shall be no stronger than the success "
1352 "argument");
1353 assert(FailureOrdering != AtomicOrdering::Release &&
1354 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001355 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001356}
1357
1358AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001359 AtomicOrdering SuccessOrdering,
1360 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001361 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001362 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001363 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001364 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001365 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1366 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001367 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001368}
1369
1370AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001371 AtomicOrdering SuccessOrdering,
1372 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001373 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001374 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001375 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001376 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001377 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1378 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001379 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001380}
Tim Northover420a2162014-06-13 14:24:07 +00001381
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001382//===----------------------------------------------------------------------===//
1383// AtomicRMWInst Implementation
1384//===----------------------------------------------------------------------===//
1385
1386void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1387 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001388 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001389 Op<0>() = Ptr;
1390 Op<1>() = Val;
1391 setOperation(Operation);
1392 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001393 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001394
1395 assert(getOperand(0) && getOperand(1) &&
1396 "All operands must be non-null!");
1397 assert(getOperand(0)->getType()->isPointerTy() &&
1398 "Ptr must have pointer type!");
1399 assert(getOperand(1)->getType() ==
1400 cast<PointerType>(getOperand(0)->getType())->getElementType()
1401 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001402 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001403 "AtomicRMW instructions must be atomic!");
1404}
1405
1406AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1407 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001408 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001409 Instruction *InsertBefore)
1410 : Instruction(Val->getType(), AtomicRMW,
1411 OperandTraits<AtomicRMWInst>::op_begin(this),
1412 OperandTraits<AtomicRMWInst>::operands(this),
1413 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001414 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001415}
1416
1417AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1418 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001419 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001420 BasicBlock *InsertAtEnd)
1421 : Instruction(Val->getType(), AtomicRMW,
1422 OperandTraits<AtomicRMWInst>::op_begin(this),
1423 OperandTraits<AtomicRMWInst>::operands(this),
1424 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001425 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001426}
1427
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001428StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1429 switch (Op) {
1430 case AtomicRMWInst::Xchg:
1431 return "xchg";
1432 case AtomicRMWInst::Add:
1433 return "add";
1434 case AtomicRMWInst::Sub:
1435 return "sub";
1436 case AtomicRMWInst::And:
1437 return "and";
1438 case AtomicRMWInst::Nand:
1439 return "nand";
1440 case AtomicRMWInst::Or:
1441 return "or";
1442 case AtomicRMWInst::Xor:
1443 return "xor";
1444 case AtomicRMWInst::Max:
1445 return "max";
1446 case AtomicRMWInst::Min:
1447 return "min";
1448 case AtomicRMWInst::UMax:
1449 return "umax";
1450 case AtomicRMWInst::UMin:
1451 return "umin";
1452 case AtomicRMWInst::BAD_BINOP:
1453 return "<invalid operation>";
1454 }
1455
1456 llvm_unreachable("invalid atomicrmw operation");
1457}
1458
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001459//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001460// FenceInst Implementation
1461//===----------------------------------------------------------------------===//
1462
Fangrui Songf78650a2018-07-30 19:41:25 +00001463FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001464 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001465 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001466 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001467 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001468 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001469}
1470
Fangrui Songf78650a2018-07-30 19:41:25 +00001471FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001472 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001473 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001474 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001475 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001476 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001477}
1478
1479//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001480// GetElementPtrInst Implementation
1481//===----------------------------------------------------------------------===//
1482
Jay Foadd1b78492011-07-25 09:48:08 +00001483void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001484 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001485 assert(getNumOperands() == 1 + IdxList.size() &&
1486 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001487 Op<0>() = Ptr;
Fangrui Song75709322018-11-17 01:44:25 +00001488 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001489 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001490}
1491
Gabor Greiff6caff662008-05-10 08:32:32 +00001492GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001493 : Instruction(GEPI.getType(), GetElementPtr,
1494 OperandTraits<GetElementPtrInst>::op_end(this) -
1495 GEPI.getNumOperands(),
1496 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001497 SourceElementType(GEPI.SourceElementType),
1498 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001499 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001500 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001501}
1502
Chris Lattner6090a422009-03-09 04:46:40 +00001503/// getIndexedType - Returns the type of the element that would be accessed with
1504/// a gep instruction with the specified parameters.
1505///
1506/// The Idxs pointer should point to a continuous piece of memory containing the
1507/// indices, either as Value* or uint64_t.
1508///
1509/// A null type is returned if the indices are invalid for the specified
1510/// pointer type.
1511///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001512template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001513static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001514 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001515 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001516 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001517
Chris Lattner6090a422009-03-09 04:46:40 +00001518 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001519 // it cannot be 'stepped over'.
1520 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001521 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001522
Dan Gohman1ecaf452008-05-31 00:58:22 +00001523 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001524 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001525 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001526 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001527 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001528 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001529 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 }
Craig Topperc6207612014-04-09 06:08:46 +00001531 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001532}
1533
David Blaikied288fb82015-03-30 21:41:43 +00001534Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1535 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001536}
1537
David Blaikied288fb82015-03-30 21:41:43 +00001538Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001539 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001540 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001541}
1542
David Blaikied288fb82015-03-30 21:41:43 +00001543Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1544 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001545}
1546
Chris Lattner45f15572007-04-14 00:12:57 +00001547/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1548/// zeros. If so, the result pointer and the first operand have the same
1549/// value, just potentially different types.
1550bool GetElementPtrInst::hasAllZeroIndices() const {
1551 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1552 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1553 if (!CI->isZero()) return false;
1554 } else {
1555 return false;
1556 }
1557 }
1558 return true;
1559}
1560
Chris Lattner27058292007-04-27 20:35:56 +00001561/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1562/// constant integers. If so, the result pointer and the first operand have
1563/// a constant offset between them.
1564bool GetElementPtrInst::hasAllConstantIndices() const {
1565 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1566 if (!isa<ConstantInt>(getOperand(i)))
1567 return false;
1568 }
1569 return true;
1570}
1571
Dan Gohman1b849082009-09-07 23:54:19 +00001572void GetElementPtrInst::setIsInBounds(bool B) {
1573 cast<GEPOperator>(this)->setIsInBounds(B);
1574}
Chris Lattner45f15572007-04-14 00:12:57 +00001575
Nick Lewycky28a5f252009-09-27 21:33:04 +00001576bool GetElementPtrInst::isInBounds() const {
1577 return cast<GEPOperator>(this)->isInBounds();
1578}
1579
Chandler Carruth1e140532012-12-11 10:29:10 +00001580bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1581 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001582 // Delegate to the generic GEPOperator implementation.
1583 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001584}
1585
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001586//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001587// ExtractElementInst Implementation
1588//===----------------------------------------------------------------------===//
1589
1590ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001591 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001592 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001593 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001594 ExtractElement,
1595 OperandTraits<ExtractElementInst>::op_begin(this),
1596 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001597 assert(isValidOperands(Val, Index) &&
1598 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001599 Op<0>() = Val;
1600 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001601 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001602}
1603
1604ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001605 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001606 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001607 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001608 ExtractElement,
1609 OperandTraits<ExtractElementInst>::op_begin(this),
1610 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001611 assert(isValidOperands(Val, Index) &&
1612 "Invalid extractelement instruction operands!");
1613
Gabor Greif2d3024d2008-05-26 21:33:52 +00001614 Op<0>() = Val;
1615 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001616 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001617}
1618
Chris Lattner54865b32006-04-08 04:05:48 +00001619bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001620 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001621 return false;
1622 return true;
1623}
1624
Robert Bocchino23004482006-01-10 19:05:34 +00001625//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001626// InsertElementInst Implementation
1627//===----------------------------------------------------------------------===//
1628
Chris Lattner54865b32006-04-08 04:05:48 +00001629InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001630 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001631 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001632 : Instruction(Vec->getType(), InsertElement,
1633 OperandTraits<InsertElementInst>::op_begin(this),
1634 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001635 assert(isValidOperands(Vec, Elt, Index) &&
1636 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001637 Op<0>() = Vec;
1638 Op<1>() = Elt;
1639 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001640 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001641}
1642
Chris Lattner54865b32006-04-08 04:05:48 +00001643InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001644 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001645 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001646 : Instruction(Vec->getType(), InsertElement,
1647 OperandTraits<InsertElementInst>::op_begin(this),
1648 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001649 assert(isValidOperands(Vec, Elt, Index) &&
1650 "Invalid insertelement instruction operands!");
1651
Gabor Greif2d3024d2008-05-26 21:33:52 +00001652 Op<0>() = Vec;
1653 Op<1>() = Elt;
1654 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001655 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001656}
1657
Fangrui Songf78650a2018-07-30 19:41:25 +00001658bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001659 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001660 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001661 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001662
Reid Spencerd84d35b2007-02-15 02:26:10 +00001663 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001664 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001665
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001666 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001667 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001668 return true;
1669}
1670
Robert Bocchinoca27f032006-01-17 20:07:22 +00001671//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001672// ShuffleVectorInst Implementation
1673//===----------------------------------------------------------------------===//
1674
1675ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001676 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001677 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001678: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001679 cast<VectorType>(Mask->getType())->getNumElements()),
1680 ShuffleVector,
1681 OperandTraits<ShuffleVectorInst>::op_begin(this),
1682 OperandTraits<ShuffleVectorInst>::operands(this),
1683 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001684 assert(isValidOperands(V1, V2, Mask) &&
1685 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001686 Op<0>() = V1;
1687 Op<1>() = V2;
1688 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001689 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001690}
1691
1692ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001693 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001694 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001695: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1696 cast<VectorType>(Mask->getType())->getNumElements()),
1697 ShuffleVector,
1698 OperandTraits<ShuffleVectorInst>::op_begin(this),
1699 OperandTraits<ShuffleVectorInst>::operands(this),
1700 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001701 assert(isValidOperands(V1, V2, Mask) &&
1702 "Invalid shuffle vector instruction operands!");
1703
Gabor Greif2d3024d2008-05-26 21:33:52 +00001704 Op<0>() = V1;
1705 Op<1>() = V2;
1706 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001707 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001708}
1709
Mon P Wang25f01062008-11-10 04:46:22 +00001710bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001711 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001712 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001713 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001714 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001715
Chris Lattner1dcb6542012-01-25 23:49:49 +00001716 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001717 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001718 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001719 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001720
1721 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001722 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1723 return true;
1724
Sanjay Patel8bd52282017-04-19 16:22:19 +00001725 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001726 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001727 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001728 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001729 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001730 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001731 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001732 return false;
1733 }
1734 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001735 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001736 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001737
Sanjay Patel8bd52282017-04-19 16:22:19 +00001738 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001739 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1740 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1741 if (CDS->getElementAsInteger(i) >= V1Size*2)
1742 return false;
1743 return true;
1744 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001745
Chris Lattner1dcb6542012-01-25 23:49:49 +00001746 // The bitcode reader can create a place holder for a forward reference
1747 // used as the shuffle mask. When this occurs, the shuffle mask will
1748 // fall into this case and fail. To avoid this error, do this bit of
1749 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001750 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001751 if (CE->getOpcode() == Instruction::UserOp1)
1752 return true;
1753
1754 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001755}
1756
Sanjay Patel2ca33602018-06-19 18:44:00 +00001757int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001758 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001759 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001760 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001761 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001762 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001763 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001764 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001765}
1766
Sanjay Patel2ca33602018-06-19 18:44:00 +00001767void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001768 SmallVectorImpl<int> &Result) {
1769 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001770
Sanjay Patel8bd52282017-04-19 16:22:19 +00001771 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001772 for (unsigned i = 0; i != NumElts; ++i)
1773 Result.push_back(CDS->getElementAsInteger(i));
1774 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001775 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001776 for (unsigned i = 0; i != NumElts; ++i) {
1777 Constant *C = Mask->getAggregateElement(i);
1778 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001779 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001780 }
1781}
1782
Sanjay Patelac619a02018-08-30 15:05:38 +00001783static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001784 assert(!Mask.empty() && "Shuffle mask must contain elements");
1785 bool UsesLHS = false;
1786 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001787 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001788 if (Mask[i] == -1)
1789 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001790 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001791 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001792 UsesLHS |= (Mask[i] < NumOpElts);
1793 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001794 if (UsesLHS && UsesRHS)
1795 return false;
1796 }
1797 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1798 return true;
1799}
1800
Sanjay Patelac619a02018-08-30 15:05:38 +00001801bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1802 // We don't have vector operand size information, so assume operands are the
1803 // same size as the mask.
1804 return isSingleSourceMaskImpl(Mask, Mask.size());
1805}
1806
1807static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1808 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001809 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001810 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001811 if (Mask[i] == -1)
1812 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001813 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001814 return false;
1815 }
1816 return true;
1817}
1818
Sanjay Patelac619a02018-08-30 15:05:38 +00001819bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1820 // We don't have vector operand size information, so assume operands are the
1821 // same size as the mask.
1822 return isIdentityMaskImpl(Mask, Mask.size());
1823}
1824
Sanjay Patel2ca33602018-06-19 18:44:00 +00001825bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1826 if (!isSingleSourceMask(Mask))
1827 return false;
1828 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1829 if (Mask[i] == -1)
1830 continue;
1831 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1832 return false;
1833 }
1834 return true;
1835}
1836
1837bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1838 if (!isSingleSourceMask(Mask))
1839 return false;
1840 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1841 if (Mask[i] == -1)
1842 continue;
1843 if (Mask[i] != 0 && Mask[i] != NumElts)
1844 return false;
1845 }
1846 return true;
1847}
1848
1849bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1850 // Select is differentiated from identity. It requires using both sources.
1851 if (isSingleSourceMask(Mask))
1852 return false;
1853 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1854 if (Mask[i] == -1)
1855 continue;
1856 if (Mask[i] != i && Mask[i] != (NumElts + i))
1857 return false;
1858 }
1859 return true;
1860}
1861
1862bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1863 // Example masks that will return true:
1864 // v1 = <a, b, c, d>
1865 // v2 = <e, f, g, h>
1866 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1867 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1868
1869 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1870 int NumElts = Mask.size();
1871 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1872 return false;
1873
1874 // 2. The first element of the mask must be either a 0 or a 1.
1875 if (Mask[0] != 0 && Mask[0] != 1)
1876 return false;
1877
1878 // 3. The difference between the first 2 elements must be equal to the
1879 // number of elements in the mask.
1880 if ((Mask[1] - Mask[0]) != NumElts)
1881 return false;
1882
1883 // 4. The difference between consecutive even-numbered and odd-numbered
1884 // elements must be equal to 2.
1885 for (int i = 2; i < NumElts; ++i) {
1886 int MaskEltVal = Mask[i];
1887 if (MaskEltVal == -1)
1888 return false;
1889 int MaskEltPrevVal = Mask[i - 2];
1890 if (MaskEltVal - MaskEltPrevVal != 2)
1891 return false;
1892 }
1893 return true;
1894}
1895
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001896bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
1897 int NumSrcElts, int &Index) {
1898 // Must extract from a single source.
1899 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
1900 return false;
1901
1902 // Must be smaller (else this is an Identity shuffle).
Fangrui Song4f2e66c2018-11-09 16:45:37 +00001903 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001904 return false;
1905
1906 // Find start of extraction, accounting that we may start with an UNDEF.
1907 int SubIndex = -1;
1908 for (int i = 0, e = Mask.size(); i != e; ++i) {
1909 int M = Mask[i];
1910 if (M < 0)
1911 continue;
1912 int Offset = (M % NumSrcElts) - i;
1913 if (0 <= SubIndex && SubIndex != Offset)
1914 return false;
1915 SubIndex = Offset;
1916 }
1917
1918 if (0 <= SubIndex) {
1919 Index = SubIndex;
1920 return true;
1921 }
1922 return false;
1923}
1924
Sanjay Patelac619a02018-08-30 15:05:38 +00001925bool ShuffleVectorInst::isIdentityWithPadding() const {
1926 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1927 int NumMaskElts = getType()->getVectorNumElements();
1928 if (NumMaskElts <= NumOpElts)
1929 return false;
1930
1931 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00001932 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00001933 if (!isIdentityMaskImpl(Mask, NumOpElts))
1934 return false;
1935
1936 // All extending must be with undef elements.
1937 for (int i = NumOpElts; i < NumMaskElts; ++i)
1938 if (Mask[i] != -1)
1939 return false;
1940
1941 return true;
1942}
1943
1944bool ShuffleVectorInst::isIdentityWithExtract() const {
1945 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1946 int NumMaskElts = getType()->getVectorNumElements();
1947 if (NumMaskElts >= NumOpElts)
1948 return false;
1949
1950 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
1951}
Sanjay Patel2ca33602018-06-19 18:44:00 +00001952
Sanjay Patelfd4976b2018-09-20 15:21:52 +00001953bool ShuffleVectorInst::isConcat() const {
1954 // Vector concatenation is differentiated from identity with padding.
1955 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
1956 return false;
1957
1958 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1959 int NumMaskElts = getType()->getVectorNumElements();
1960 if (NumMaskElts != NumOpElts * 2)
1961 return false;
1962
1963 // Use the mask length rather than the operands' vector lengths here. We
1964 // already know that the shuffle returns a vector twice as long as the inputs,
1965 // and neither of the inputs are undef vectors. If the mask picks consecutive
1966 // elements from both inputs, then this is a concatenation of the inputs.
1967 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
1968}
1969
Dan Gohman12fce772008-05-15 19:50:34 +00001970//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001971// InsertValueInst Class
1972//===----------------------------------------------------------------------===//
1973
Fangrui Songf78650a2018-07-30 19:41:25 +00001974void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00001975 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001976 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001977
1978 // There's no fundamental reason why we require at least one index
1979 // (other than weirdness with &*IdxBegin being invalid; see
1980 // getelementptr's init routine for example). But there's no
1981 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001982 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001983
1984 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001985 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001986 Op<0>() = Agg;
1987 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001988
Jay Foad57aa6362011-07-13 10:26:04 +00001989 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001990 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001991}
1992
1993InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001994 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001995 OperandTraits<InsertValueInst>::op_begin(this), 2),
1996 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001997 Op<0>() = IVI.getOperand(0);
1998 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001999 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002000}
2001
2002//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002003// ExtractValueInst Class
2004//===----------------------------------------------------------------------===//
2005
Jay Foad57aa6362011-07-13 10:26:04 +00002006void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002007 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002008
Jay Foad57aa6362011-07-13 10:26:04 +00002009 // There's no fundamental reason why we require at least one index.
2010 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002011 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002012
Jay Foad57aa6362011-07-13 10:26:04 +00002013 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002014 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002015}
2016
2017ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002018 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002019 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002020 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002021}
2022
Dan Gohman12fce772008-05-15 19:50:34 +00002023// getIndexedType - Returns the type of the element that would be extracted
2024// with an extractvalue instruction with the specified parameters.
2025//
2026// A null type is returned if the indices are invalid for the specified
2027// pointer type.
2028//
Chris Lattner229907c2011-07-18 04:54:35 +00002029Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002030 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002031 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002032 // We can't use CompositeType::indexValid(Index) here.
2033 // indexValid() always returns true for arrays because getelementptr allows
2034 // out-of-bounds indices. Since we don't allow those for extractvalue and
2035 // insertvalue we need to check array indexing manually.
2036 // Since the only other types we can index into are struct types it's just
2037 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002038 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002039 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002040 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002041 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002042 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002043 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002044 } else {
2045 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002046 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002047 }
2048
2049 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002050 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002051 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002052}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002053
2054//===----------------------------------------------------------------------===//
Cameron McInallycbde0d92018-11-13 18:15:47 +00002055// UnaryOperator Class
2056//===----------------------------------------------------------------------===//
2057
2058UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2059 Type *Ty, const Twine &Name,
2060 Instruction *InsertBefore)
2061 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2062 Op<0>() = S;
2063 setName(Name);
2064 AssertOK();
2065}
2066
2067UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2068 Type *Ty, const Twine &Name,
2069 BasicBlock *InsertAtEnd)
2070 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2071 Op<0>() = S;
2072 setName(Name);
2073 AssertOK();
2074}
2075
2076UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2077 const Twine &Name,
2078 Instruction *InsertBefore) {
2079 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2080}
2081
2082UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2083 const Twine &Name,
2084 BasicBlock *InsertAtEnd) {
2085 UnaryOperator *Res = Create(Op, S, Name);
2086 InsertAtEnd->getInstList().push_back(Res);
2087 return Res;
2088}
2089
2090void UnaryOperator::AssertOK() {
2091 Value *LHS = getOperand(0);
2092 (void)LHS; // Silence warnings.
2093#ifndef NDEBUG
2094 switch (getOpcode()) {
2095 case FNeg:
2096 assert(getType() == LHS->getType() &&
2097 "Unary operation should return same type as operand!");
2098 assert(getType()->isFPOrFPVectorTy() &&
2099 "Tried to create a floating-point operation on a "
2100 "non-floating-point type!");
2101 break;
2102 default: llvm_unreachable("Invalid opcode provided");
2103 }
2104#endif
2105}
2106
2107//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002108// BinaryOperator Class
2109//===----------------------------------------------------------------------===//
2110
Chris Lattner2195fc42007-02-24 00:55:48 +00002111BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002112 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002113 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002114 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002115 OperandTraits<BinaryOperator>::op_begin(this),
2116 OperandTraits<BinaryOperator>::operands(this),
2117 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002118 Op<0>() = S1;
2119 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002120 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002121 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002122}
2123
Fangrui Songf78650a2018-07-30 19:41:25 +00002124BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002125 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002126 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002127 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002128 OperandTraits<BinaryOperator>::op_begin(this),
2129 OperandTraits<BinaryOperator>::operands(this),
2130 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002131 Op<0>() = S1;
2132 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002133 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002134 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002135}
2136
Craig Topper700892f2017-06-26 07:15:59 +00002137void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002138 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002139 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002140 assert(LHS->getType() == RHS->getType() &&
2141 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002142#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00002143 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002144 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002145 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002146 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002147 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002148 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002149 "Tried to create an integer operation on a non-integer type!");
2150 break;
2151 case FAdd: case FSub:
2152 case FMul:
2153 assert(getType() == LHS->getType() &&
2154 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002155 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002156 "Tried to create a floating-point operation on a "
2157 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002158 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002159 case UDiv:
2160 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002161 assert(getType() == LHS->getType() &&
2162 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002163 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002164 "Incorrect operand type (not integer) for S/UDIV");
2165 break;
2166 case FDiv:
2167 assert(getType() == LHS->getType() &&
2168 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002169 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002170 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002171 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002172 case URem:
2173 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002174 assert(getType() == LHS->getType() &&
2175 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002176 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002177 "Incorrect operand type (not integer) for S/UREM");
2178 break;
2179 case FRem:
2180 assert(getType() == LHS->getType() &&
2181 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002182 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002183 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002184 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002185 case Shl:
2186 case LShr:
2187 case AShr:
2188 assert(getType() == LHS->getType() &&
2189 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002190 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002191 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002192 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002193 case And: case Or:
2194 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002195 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002196 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002197 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002198 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002199 break;
Craig Topper700892f2017-06-26 07:15:59 +00002200 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002201 }
2202#endif
2203}
2204
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002205BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002206 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002207 Instruction *InsertBefore) {
2208 assert(S1->getType() == S2->getType() &&
2209 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002210 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002211}
2212
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002213BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002214 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002215 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002217 InsertAtEnd->getInstList().push_back(Res);
2218 return Res;
2219}
2220
Dan Gohman5476cfd2009-08-12 16:23:25 +00002221BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002222 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002223 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002224 return new BinaryOperator(Instruction::Sub,
2225 zero, Op,
2226 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002227}
2228
Dan Gohman5476cfd2009-08-12 16:23:25 +00002229BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002230 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002231 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002232 return new BinaryOperator(Instruction::Sub,
2233 zero, Op,
2234 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002235}
2236
Dan Gohman4ab44202009-12-18 02:58:50 +00002237BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2238 Instruction *InsertBefore) {
2239 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2240 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2241}
2242
2243BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2244 BasicBlock *InsertAtEnd) {
2245 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2246 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2247}
2248
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002249BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2250 Instruction *InsertBefore) {
2251 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2252 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2253}
2254
2255BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2256 BasicBlock *InsertAtEnd) {
2257 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2258 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2259}
2260
Dan Gohman5476cfd2009-08-12 16:23:25 +00002261BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002262 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002263 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002264 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002265 Op->getType(), Name, InsertBefore);
2266}
2267
Dan Gohman5476cfd2009-08-12 16:23:25 +00002268BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002269 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002270 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002271 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002272 Op->getType(), Name, InsertAtEnd);
2273}
2274
Dan Gohman5476cfd2009-08-12 16:23:25 +00002275BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002276 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002277 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002278 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002279 Op->getType(), Name, InsertBefore);
2280}
2281
Dan Gohman5476cfd2009-08-12 16:23:25 +00002282BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002283 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002284 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002285 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002286 Op->getType(), Name, InsertAtEnd);
2287}
2288
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002289// Exchange the two operands to this instruction. This instruction is safe to
2290// use on any binary instruction and does not modify the semantics of the
2291// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2292// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002293bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002294 if (!isCommutative())
2295 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002296 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002297 return false;
2298}
2299
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002300//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002301// FPMathOperator Class
2302//===----------------------------------------------------------------------===//
2303
Duncan Sands05f4df82012-04-16 16:28:59 +00002304float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002305 const MDNode *MD =
2306 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002307 if (!MD)
2308 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002309 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002310 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002311}
2312
Duncan Sands05f4df82012-04-16 16:28:59 +00002313//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002314// CastInst Class
2315//===----------------------------------------------------------------------===//
2316
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317// Just determine if this cast only deals with integral->integral conversion.
2318bool CastInst::isIntegerCast() const {
2319 switch (getOpcode()) {
2320 default: return false;
2321 case Instruction::ZExt:
2322 case Instruction::SExt:
2323 case Instruction::Trunc:
2324 return true;
2325 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002326 return getOperand(0)->getType()->isIntegerTy() &&
2327 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002329}
2330
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331bool CastInst::isLosslessCast() const {
2332 // Only BitCast can be lossless, exit fast if we're not BitCast
2333 if (getOpcode() != Instruction::BitCast)
2334 return false;
2335
2336 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002337 Type *SrcTy = getOperand(0)->getType();
2338 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 if (SrcTy == DstTy)
2340 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002341
Reid Spencer8d9336d2006-12-31 05:26:44 +00002342 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002343 if (SrcTy->isPointerTy())
2344 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 return false; // Other types have no identity values
2346}
2347
2348/// This function determines if the CastInst does not require any bits to be
2349/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002350/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002352/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002353/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002354/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002355/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002356bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002357 Type *SrcTy,
2358 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002359 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002360 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002361 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 case Instruction::Trunc:
2363 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002364 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 case Instruction::FPTrunc:
2366 case Instruction::FPExt:
2367 case Instruction::UIToFP:
2368 case Instruction::SIToFP:
2369 case Instruction::FPToUI:
2370 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002371 case Instruction::AddrSpaceCast:
2372 // TODO: Target informations may give a more accurate answer here.
2373 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374 case Instruction::BitCast:
2375 return true; // BitCast never modifies bits.
2376 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002377 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002378 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002380 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002381 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 }
2383}
2384
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002385bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002386 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002387}
2388
2389/// This function determines if a pair of casts can be eliminated and what
2390/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391/// instructions like this:
2392/// * %F = firstOpcode SrcTy %x to MidTy
2393/// * %S = secondOpcode MidTy %F to DstTy
2394/// The function returns a resultOpcode so these two casts can be replaced with:
2395/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002396/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397unsigned CastInst::isEliminableCastPair(
2398 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002399 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2400 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 // Define the 144 possibilities for these two cast instructions. The values
2402 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002403 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002404 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 // the following cast properties:
2406 //
2407 // Size Compare Source Destination
2408 // Operator Src ? Size Type Sign Type Sign
2409 // -------- ------------ ------------------- ---------------------
2410 // TRUNC > Integer Any Integral Any
2411 // ZEXT < Integral Unsigned Integer Any
2412 // SEXT < Integral Signed Integer Any
2413 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002414 // FPTOSI n/a FloatPt n/a Integral Signed
2415 // UITOFP n/a Integral Unsigned FloatPt n/a
2416 // SITOFP n/a Integral Signed FloatPt n/a
2417 // FPTRUNC > FloatPt n/a FloatPt n/a
2418 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 // PTRTOINT n/a Pointer n/a Integral Unsigned
2420 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002421 // BITCAST = FirstClass n/a FirstClass n/a
2422 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002423 //
2424 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002425 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2426 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002427 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002428 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002429 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002430 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002431 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2433 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002434 // T F F U S F F P I B A -+
2435 // R Z S P P I I T P 2 N T S |
2436 // U E E 2 2 2 2 R E I T C C +- secondOp
2437 // N X X U S F F N X N 2 V V |
2438 // C T T I I P P C T T P T T -+
2439 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002440 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002441 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2442 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2443 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2444 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2445 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002446 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002447 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002448 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2449 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2450 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2451 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002453
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002454 // TODO: This logic could be encoded into the table above and handled in the
2455 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002456 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002457 // merging. However, any pair of bitcasts are allowed.
2458 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2459 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2460 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002461
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002462 // Check if any of the casts convert scalars <-> vectors.
2463 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2464 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2465 if (!AreBothBitcasts)
2466 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467
2468 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2469 [secondOp-Instruction::CastOpsBegin];
2470 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002471 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002472 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002474 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002475 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002477 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002478 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002480 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002481 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002482 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002483 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002484 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 return firstOp;
2486 return 0;
2487 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002489 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002490 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 return firstOp;
2492 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002493 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002494 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002495 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002496 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 return secondOp;
2498 return 0;
2499 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002500 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002501 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002502 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503 return secondOp;
2504 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002505 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002506 // Cannot simplify if address spaces are different!
2507 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2508 return 0;
2509
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002510 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002511 // We can still fold this without knowing the actual sizes as long we
2512 // know that the intermediate pointer is the largest possible
2513 // pointer size.
2514 // FIXME: Is this always true?
2515 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002516 return Instruction::BitCast;
2517
2518 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002519 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002520 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002521 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 if (MidSize >= PtrSize)
2523 return Instruction::BitCast;
2524 return 0;
2525 }
2526 case 8: {
2527 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2528 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2529 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002530 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2531 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 if (SrcSize == DstSize)
2533 return Instruction::BitCast;
2534 else if (SrcSize < DstSize)
2535 return firstOp;
2536 return secondOp;
2537 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002538 case 9:
2539 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002541 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002543 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002544 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002545 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002546 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2547 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 if (SrcSize <= PtrSize && SrcSize == DstSize)
2549 return Instruction::BitCast;
2550 return 0;
2551 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002552 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002553 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2554 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2555 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2556 return Instruction::AddrSpaceCast;
2557 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002558 case 13:
2559 // FIXME: this state can be merged with (1), but the following assert
2560 // is useful to check the correcteness of the sequence due to semantic
2561 // change of bitcast.
2562 assert(
2563 SrcTy->isPtrOrPtrVectorTy() &&
2564 MidTy->isPtrOrPtrVectorTy() &&
2565 DstTy->isPtrOrPtrVectorTy() &&
2566 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2567 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2568 "Illegal addrspacecast, bitcast sequence!");
2569 // Allowed, use first cast's opcode
2570 return firstOp;
2571 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002572 // bitcast, addrspacecast -> addrspacecast if the element type of
2573 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002574 if (SrcTy->getScalarType()->getPointerElementType() ==
2575 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002576 return Instruction::AddrSpaceCast;
2577 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002578 case 15:
2579 // FIXME: this state can be merged with (1), but the following assert
2580 // is useful to check the correcteness of the sequence due to semantic
2581 // change of bitcast.
2582 assert(
2583 SrcTy->isIntOrIntVectorTy() &&
2584 MidTy->isPtrOrPtrVectorTy() &&
2585 DstTy->isPtrOrPtrVectorTy() &&
2586 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2587 "Illegal inttoptr, bitcast sequence!");
2588 // Allowed, use first cast's opcode
2589 return firstOp;
2590 case 16:
2591 // FIXME: this state can be merged with (2), but the following assert
2592 // is useful to check the correcteness of the sequence due to semantic
2593 // change of bitcast.
2594 assert(
2595 SrcTy->isPtrOrPtrVectorTy() &&
2596 MidTy->isPtrOrPtrVectorTy() &&
2597 DstTy->isIntOrIntVectorTy() &&
2598 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2599 "Illegal bitcast, ptrtoint sequence!");
2600 // Allowed, use second cast's opcode
2601 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002602 case 17:
2603 // (sitofp (zext x)) -> (uitofp x)
2604 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002605 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002606 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002608 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 default:
Craig Topperc514b542012-02-05 22:14:15 +00002610 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
Fangrui Songf78650a2018-07-30 19:41:25 +00002614CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002616 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617 // Construct and return the appropriate CastInst subclass
2618 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002619 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2620 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2621 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2622 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2623 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2624 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2625 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2626 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2627 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2628 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2629 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2630 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2631 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2632 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634}
2635
Chris Lattner229907c2011-07-18 04:54:35 +00002636CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002638 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639 // Construct and return the appropriate CastInst subclass
2640 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002641 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2642 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2643 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2644 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2645 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2646 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2647 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2648 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2649 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2650 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2651 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2652 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2653 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2654 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656}
2657
Fangrui Songf78650a2018-07-30 19:41:25 +00002658CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002660 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002661 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002662 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2663 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002664}
2665
Fangrui Songf78650a2018-07-30 19:41:25 +00002666CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002667 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002668 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002669 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002670 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2671 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002672}
2673
Fangrui Songf78650a2018-07-30 19:41:25 +00002674CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002676 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002677 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002678 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2679 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002680}
2681
Fangrui Songf78650a2018-07-30 19:41:25 +00002682CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002683 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002684 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002685 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002686 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2687 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002688}
2689
Chris Lattner229907c2011-07-18 04:54:35 +00002690CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002691 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002692 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002693 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002694 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2695 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002696}
2697
Chris Lattner229907c2011-07-18 04:54:35 +00002698CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002699 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002700 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002701 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002702 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2703 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002704}
2705
Chris Lattner229907c2011-07-18 04:54:35 +00002706CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002707 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002708 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002709 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2710 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2711 "Invalid cast");
2712 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002713 assert((!Ty->isVectorTy() ||
2714 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002715 "Invalid cast");
2716
Matt Arsenault065ced92013-07-31 00:17:33 +00002717 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002718 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002719
Matt Arsenault740980e2014-07-14 17:24:35 +00002720 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002721}
2722
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002723/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002724CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2725 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002726 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002727 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2728 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002729 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002730 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002731 assert((!Ty->isVectorTy() ||
2732 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002733 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002734
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002735 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002736 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002737
Matt Arsenault740980e2014-07-14 17:24:35 +00002738 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2739}
2740
2741CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2742 Value *S, Type *Ty,
2743 const Twine &Name,
2744 BasicBlock *InsertAtEnd) {
2745 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2746 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2747
2748 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2749 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2750
2751 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2752}
2753
2754CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2755 Value *S, Type *Ty,
2756 const Twine &Name,
2757 Instruction *InsertBefore) {
2758 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2759 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2760
2761 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002762 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2763
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002764 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002765}
2766
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002767CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2768 const Twine &Name,
2769 Instruction *InsertBefore) {
2770 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2771 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2772 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2773 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2774
2775 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2776}
2777
Matt Arsenault740980e2014-07-14 17:24:35 +00002778CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002779 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002780 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002781 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002782 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002783 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2784 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002785 Instruction::CastOps opcode =
2786 (SrcBits == DstBits ? Instruction::BitCast :
2787 (SrcBits > DstBits ? Instruction::Trunc :
2788 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002789 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002790}
2791
Fangrui Songf78650a2018-07-30 19:41:25 +00002792CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002793 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002794 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002795 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002796 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002797 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2798 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002799 Instruction::CastOps opcode =
2800 (SrcBits == DstBits ? Instruction::BitCast :
2801 (SrcBits > DstBits ? Instruction::Trunc :
2802 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002803 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002804}
2805
Fangrui Songf78650a2018-07-30 19:41:25 +00002806CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2807 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002808 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002809 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002810 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002811 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2812 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002813 Instruction::CastOps opcode =
2814 (SrcBits == DstBits ? Instruction::BitCast :
2815 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002816 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002817}
2818
Fangrui Songf78650a2018-07-30 19:41:25 +00002819CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2820 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002821 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002822 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002823 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002824 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2825 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002826 Instruction::CastOps opcode =
2827 (SrcBits == DstBits ? Instruction::BitCast :
2828 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002829 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002830}
2831
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002832// Check whether it is valid to call getCastOpcode for these types.
2833// This routine must be kept in sync with getCastOpcode.
2834bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2835 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2836 return false;
2837
2838 if (SrcTy == DestTy)
2839 return true;
2840
2841 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2842 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2843 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2844 // An element by element cast. Valid if casting the elements is valid.
2845 SrcTy = SrcVecTy->getElementType();
2846 DestTy = DestVecTy->getElementType();
2847 }
2848
2849 // Get the bit sizes, we'll need these
2850 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2851 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2852
2853 // Run through the possibilities ...
2854 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002855 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002856 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002857 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002858 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002859 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002860 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002861 // Casting from something else
2862 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002863 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002864 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2865 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002866 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002867 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002868 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002869 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002870 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002871 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002872 return false;
2873 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002874 if (DestTy->isVectorTy()) // Casting to vector
2875 return DestBits == SrcBits;
2876 if (DestTy->isPointerTy()) { // Casting to pointer
2877 if (SrcTy->isPointerTy()) // Casting from pointer
2878 return true;
2879 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002880 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002881 if (DestTy->isX86_MMXTy()) {
2882 if (SrcTy->isVectorTy())
2883 return DestBits == SrcBits; // 64-bit vector to MMX
2884 return false;
2885 } // Casting to something else
2886 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002887}
2888
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002889bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2890 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2891 return false;
2892
2893 if (SrcTy == DestTy)
2894 return true;
2895
2896 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2897 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2898 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2899 // An element by element cast. Valid if casting the elements is valid.
2900 SrcTy = SrcVecTy->getElementType();
2901 DestTy = DestVecTy->getElementType();
2902 }
2903 }
2904 }
2905
2906 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2907 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2908 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2909 }
2910 }
2911
2912 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2913 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2914
2915 // Could still have vectors of pointers if the number of elements doesn't
2916 // match
2917 if (SrcBits == 0 || DestBits == 0)
2918 return false;
2919
2920 if (SrcBits != DestBits)
2921 return false;
2922
2923 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2924 return false;
2925
2926 return true;
2927}
2928
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002929bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002930 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002931 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002932 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2933 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002934 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2935 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002936 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2937 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002938 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2939 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002940
2941 return isBitCastable(SrcTy, DestTy);
2942}
2943
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002944// Provide a way to get a "cast" where the cast opcode is inferred from the
2945// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002946// logic in the castIsValid function below. This axiom should hold:
2947// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2948// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002950// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002952CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002953 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2954 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955
Duncan Sands55e50902008-01-06 10:12:28 +00002956 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2957 "Only first class types are castable!");
2958
Duncan Sandsa8514532011-05-18 07:13:41 +00002959 if (SrcTy == DestTy)
2960 return BitCast;
2961
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002962 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002963 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2964 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002965 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2966 // An element by element cast. Find the appropriate opcode based on the
2967 // element types.
2968 SrcTy = SrcVecTy->getElementType();
2969 DestTy = DestVecTy->getElementType();
2970 }
2971
2972 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002973 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2974 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002975
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002976 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002977 if (DestTy->isIntegerTy()) { // Casting to integral
2978 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979 if (DestBits < SrcBits)
2980 return Trunc; // int -> smaller int
2981 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002982 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 return SExt; // signed -> SEXT
2984 else
2985 return ZExt; // unsigned -> ZEXT
2986 } else {
2987 return BitCast; // Same size, No-op cast
2988 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002989 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00002990 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 return FPToSI; // FP -> sint
2992 else
Fangrui Songf78650a2018-07-30 19:41:25 +00002993 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002994 } else if (SrcTy->isVectorTy()) {
2995 assert(DestBits == SrcBits &&
2996 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997 return BitCast; // Same size, no-op cast
2998 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002999 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000 "Casting from a value that is not first-class type");
3001 return PtrToInt; // ptr -> int
3002 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003003 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3004 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003005 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006 return SIToFP; // sint -> FP
3007 else
3008 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003009 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003010 if (DestBits < SrcBits) {
3011 return FPTrunc; // FP -> smaller FP
3012 } else if (DestBits > SrcBits) {
3013 return FPExt; // FP -> larger FP
3014 } else {
3015 return BitCast; // same size, no-op cast
3016 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003017 } else if (SrcTy->isVectorTy()) {
3018 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003019 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003020 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003022 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003023 } else if (DestTy->isVectorTy()) {
3024 assert(DestBits == SrcBits &&
3025 "Illegal cast to vector (wrong type or size)");
3026 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003027 } else if (DestTy->isPointerTy()) {
3028 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003029 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3030 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003032 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003035 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003036 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003037 if (SrcTy->isVectorTy()) {
3038 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003039 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003040 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003041 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003043 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003044}
3045
3046//===----------------------------------------------------------------------===//
3047// CastInst SubClass Constructors
3048//===----------------------------------------------------------------------===//
3049
3050/// Check that the construction parameters for a CastInst are correct. This
3051/// could be broken out into the separate constructors but it is useful to have
3052/// it in one place and to eliminate the redundant code for getting the sizes
3053/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00003054bool
Chris Lattner229907c2011-07-18 04:54:35 +00003055CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003057 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003058
Chris Lattner37bc78a2010-01-26 21:51:43 +00003059 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3060 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061 return false;
3062
3063 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003064 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3065 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003066
Duncan Sands7f646562011-05-18 09:21:57 +00003067 // If these are vector types, get the lengths of the vectors (using zero for
3068 // scalar types means that checking that vector lengths match also checks that
3069 // scalars are not being converted to vectors or vectors to scalars).
3070 unsigned SrcLength = SrcTy->isVectorTy() ?
3071 cast<VectorType>(SrcTy)->getNumElements() : 0;
3072 unsigned DstLength = DstTy->isVectorTy() ?
3073 cast<VectorType>(DstTy)->getNumElements() : 0;
3074
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 // Switch on the opcode provided
3076 switch (op) {
3077 default: return false; // This is an input error
3078 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003079 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3080 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003081 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003082 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3083 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00003084 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003085 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3086 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003087 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003088 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3089 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003090 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003091 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3092 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003093 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003094 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003095 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3096 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003097 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003098 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003099 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3100 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003102 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003103 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003104 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3105 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3106 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003107 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003108 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003109 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003110 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003111 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3112 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3113 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003114 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003115 case Instruction::BitCast: {
3116 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3117 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3118
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119 // BitCast implies a no-op cast of type only. No bits change.
3120 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003121 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003122 return false;
3123
Alp Tokerf907b892013-12-05 05:44:44 +00003124 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003125 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003126 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003127 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3128
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003129 // If both are pointers then the address spaces must match.
3130 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3131 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003132
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003133 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003134 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3135 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3136 if (SrcVecTy && DstVecTy)
3137 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3138 if (SrcVecTy)
3139 return SrcVecTy->getNumElements() == 1;
3140 if (DstVecTy)
3141 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003142
3143 return true;
3144 }
3145 case Instruction::AddrSpaceCast: {
3146 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3147 if (!SrcPtrTy)
3148 return false;
3149
3150 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3151 if (!DstPtrTy)
3152 return false;
3153
3154 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3155 return false;
3156
3157 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3158 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3159 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3160
3161 return false;
3162 }
3163
3164 return true;
3165 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166 }
3167}
3168
3169TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003170 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003172 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173}
3174
3175TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003176 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003177) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003178 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179}
3180
3181ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003182 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003183) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003184 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185}
3186
3187ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003188 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003189) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003190 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191}
3192SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003193 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003194) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003195 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196}
3197
Jeff Cohencc08c832006-12-02 02:22:01 +00003198SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003199 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003200) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003201 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202}
3203
3204FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003205 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003206) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003207 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208}
3209
3210FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003211 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003212) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003213 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214}
3215
3216FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003217 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003218) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003219 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220}
3221
3222FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003223 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003224) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003225 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226}
3227
3228UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003229 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003230) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003231 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232}
3233
3234UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003235 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003236) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003237 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238}
3239
3240SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003241 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003242) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003243 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244}
3245
3246SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003247 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003248) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003249 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003250}
3251
3252FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003253 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003254) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003255 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003256}
3257
3258FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003259 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003260) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003261 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262}
3263
3264FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003265 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003266) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003267 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003268}
3269
3270FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003271 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003272) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003273 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003274}
3275
3276PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003277 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003278) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003279 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003280}
3281
3282PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003283 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003284) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003285 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003286}
3287
3288IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003289 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003290) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003291 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003292}
3293
3294IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003295 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003296) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003297 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003298}
3299
3300BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003301 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003302) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003303 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003304}
3305
3306BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003307 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003308) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003309 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003310}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003311
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003312AddrSpaceCastInst::AddrSpaceCastInst(
3313 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3314) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3315 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3316}
3317
3318AddrSpaceCastInst::AddrSpaceCastInst(
3319 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3320) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3321 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3322}
3323
Chris Lattnerf16dc002006-09-17 19:29:56 +00003324//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003325// CmpInst Classes
3326//===----------------------------------------------------------------------===//
3327
Craig Topper1c3f2832015-12-15 06:11:33 +00003328CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Pateld1172a02018-11-07 00:00:42 +00003329 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3330 Instruction *FlagsSource)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003331 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003332 OperandTraits<CmpInst>::op_begin(this),
3333 OperandTraits<CmpInst>::operands(this),
3334 InsertBefore) {
Sanjay Pateld1172a02018-11-07 00:00:42 +00003335 Op<0>() = LHS;
3336 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003337 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003338 setName(Name);
Sanjay Pateld1172a02018-11-07 00:00:42 +00003339 if (FlagsSource)
3340 copyIRFlags(FlagsSource);
Reid Spencerd9436b62006-11-20 01:22:35 +00003341}
Gabor Greiff6caff662008-05-10 08:32:32 +00003342
Craig Topper1c3f2832015-12-15 06:11:33 +00003343CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3344 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003345 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003346 OperandTraits<CmpInst>::op_begin(this),
3347 OperandTraits<CmpInst>::operands(this),
3348 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003349 Op<0>() = LHS;
3350 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003351 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003352 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003353}
3354
3355CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003356CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003357 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003358 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003359 if (InsertBefore)
3360 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3361 S1, S2, Name);
3362 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003363 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003364 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003365 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003366
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003367 if (InsertBefore)
3368 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3369 S1, S2, Name);
3370 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003371 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003372 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003373}
3374
3375CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003376CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003377 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003378 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003379 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3380 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003381 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003382 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3383 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003384}
3385
3386void CmpInst::swapOperands() {
3387 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3388 IC->swapOperands();
3389 else
3390 cast<FCmpInst>(this)->swapOperands();
3391}
3392
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003393bool CmpInst::isCommutative() const {
3394 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003395 return IC->isCommutative();
3396 return cast<FCmpInst>(this)->isCommutative();
3397}
3398
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003399bool CmpInst::isEquality() const {
3400 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003401 return IC->isEquality();
3402 return cast<FCmpInst>(this)->isEquality();
3403}
3404
Dan Gohman4e724382008-05-31 02:47:54 +00003405CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003406 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003407 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003408 case ICMP_EQ: return ICMP_NE;
3409 case ICMP_NE: return ICMP_EQ;
3410 case ICMP_UGT: return ICMP_ULE;
3411 case ICMP_ULT: return ICMP_UGE;
3412 case ICMP_UGE: return ICMP_ULT;
3413 case ICMP_ULE: return ICMP_UGT;
3414 case ICMP_SGT: return ICMP_SLE;
3415 case ICMP_SLT: return ICMP_SGE;
3416 case ICMP_SGE: return ICMP_SLT;
3417 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003418
Dan Gohman4e724382008-05-31 02:47:54 +00003419 case FCMP_OEQ: return FCMP_UNE;
3420 case FCMP_ONE: return FCMP_UEQ;
3421 case FCMP_OGT: return FCMP_ULE;
3422 case FCMP_OLT: return FCMP_UGE;
3423 case FCMP_OGE: return FCMP_ULT;
3424 case FCMP_OLE: return FCMP_UGT;
3425 case FCMP_UEQ: return FCMP_ONE;
3426 case FCMP_UNE: return FCMP_OEQ;
3427 case FCMP_UGT: return FCMP_OLE;
3428 case FCMP_ULT: return FCMP_OGE;
3429 case FCMP_UGE: return FCMP_OLT;
3430 case FCMP_ULE: return FCMP_OGT;
3431 case FCMP_ORD: return FCMP_UNO;
3432 case FCMP_UNO: return FCMP_ORD;
3433 case FCMP_TRUE: return FCMP_FALSE;
3434 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003435 }
3436}
3437
Tim Northoverde3aea0412016-08-17 20:25:25 +00003438StringRef CmpInst::getPredicateName(Predicate Pred) {
3439 switch (Pred) {
3440 default: return "unknown";
3441 case FCmpInst::FCMP_FALSE: return "false";
3442 case FCmpInst::FCMP_OEQ: return "oeq";
3443 case FCmpInst::FCMP_OGT: return "ogt";
3444 case FCmpInst::FCMP_OGE: return "oge";
3445 case FCmpInst::FCMP_OLT: return "olt";
3446 case FCmpInst::FCMP_OLE: return "ole";
3447 case FCmpInst::FCMP_ONE: return "one";
3448 case FCmpInst::FCMP_ORD: return "ord";
3449 case FCmpInst::FCMP_UNO: return "uno";
3450 case FCmpInst::FCMP_UEQ: return "ueq";
3451 case FCmpInst::FCMP_UGT: return "ugt";
3452 case FCmpInst::FCMP_UGE: return "uge";
3453 case FCmpInst::FCMP_ULT: return "ult";
3454 case FCmpInst::FCMP_ULE: return "ule";
3455 case FCmpInst::FCMP_UNE: return "une";
3456 case FCmpInst::FCMP_TRUE: return "true";
3457 case ICmpInst::ICMP_EQ: return "eq";
3458 case ICmpInst::ICMP_NE: return "ne";
3459 case ICmpInst::ICMP_SGT: return "sgt";
3460 case ICmpInst::ICMP_SGE: return "sge";
3461 case ICmpInst::ICMP_SLT: return "slt";
3462 case ICmpInst::ICMP_SLE: return "sle";
3463 case ICmpInst::ICMP_UGT: return "ugt";
3464 case ICmpInst::ICMP_UGE: return "uge";
3465 case ICmpInst::ICMP_ULT: return "ult";
3466 case ICmpInst::ICMP_ULE: return "ule";
3467 }
3468}
3469
Reid Spencer266e42b2006-12-23 06:05:41 +00003470ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3471 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003472 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003473 case ICMP_EQ: case ICMP_NE:
3474 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003475 return pred;
3476 case ICMP_UGT: return ICMP_SGT;
3477 case ICMP_ULT: return ICMP_SLT;
3478 case ICMP_UGE: return ICMP_SGE;
3479 case ICMP_ULE: return ICMP_SLE;
3480 }
3481}
3482
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003483ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3484 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003485 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003486 case ICMP_EQ: case ICMP_NE:
3487 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003488 return pred;
3489 case ICMP_SGT: return ICMP_UGT;
3490 case ICMP_SLT: return ICMP_ULT;
3491 case ICMP_SGE: return ICMP_UGE;
3492 case ICMP_SLE: return ICMP_ULE;
3493 }
3494}
3495
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003496CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3497 switch (pred) {
3498 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3499 case ICMP_SGT: return ICMP_SGE;
3500 case ICMP_SLT: return ICMP_SLE;
3501 case ICMP_SGE: return ICMP_SGT;
3502 case ICMP_SLE: return ICMP_SLT;
3503 case ICMP_UGT: return ICMP_UGE;
3504 case ICMP_ULT: return ICMP_ULE;
3505 case ICMP_UGE: return ICMP_UGT;
3506 case ICMP_ULE: return ICMP_ULT;
3507
3508 case FCMP_OGT: return FCMP_OGE;
3509 case FCMP_OLT: return FCMP_OLE;
3510 case FCMP_OGE: return FCMP_OGT;
3511 case FCMP_OLE: return FCMP_OLT;
3512 case FCMP_UGT: return FCMP_UGE;
3513 case FCMP_ULT: return FCMP_ULE;
3514 case FCMP_UGE: return FCMP_UGT;
3515 case FCMP_ULE: return FCMP_ULT;
3516 }
3517}
3518
Dan Gohman4e724382008-05-31 02:47:54 +00003519CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003520 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003521 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003522 case ICMP_EQ: case ICMP_NE:
3523 return pred;
3524 case ICMP_SGT: return ICMP_SLT;
3525 case ICMP_SLT: return ICMP_SGT;
3526 case ICMP_SGE: return ICMP_SLE;
3527 case ICMP_SLE: return ICMP_SGE;
3528 case ICMP_UGT: return ICMP_ULT;
3529 case ICMP_ULT: return ICMP_UGT;
3530 case ICMP_UGE: return ICMP_ULE;
3531 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003532
Reid Spencerd9436b62006-11-20 01:22:35 +00003533 case FCMP_FALSE: case FCMP_TRUE:
3534 case FCMP_OEQ: case FCMP_ONE:
3535 case FCMP_UEQ: case FCMP_UNE:
3536 case FCMP_ORD: case FCMP_UNO:
3537 return pred;
3538 case FCMP_OGT: return FCMP_OLT;
3539 case FCMP_OLT: return FCMP_OGT;
3540 case FCMP_OGE: return FCMP_OLE;
3541 case FCMP_OLE: return FCMP_OGE;
3542 case FCMP_UGT: return FCMP_ULT;
3543 case FCMP_ULT: return FCMP_UGT;
3544 case FCMP_UGE: return FCMP_ULE;
3545 case FCMP_ULE: return FCMP_UGE;
3546 }
3547}
3548
Max Kazantsevb299ade2018-02-07 11:16:29 +00003549CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3550 switch (pred) {
3551 case ICMP_SGT: return ICMP_SGE;
3552 case ICMP_SLT: return ICMP_SLE;
3553 case ICMP_UGT: return ICMP_UGE;
3554 case ICMP_ULT: return ICMP_ULE;
3555 case FCMP_OGT: return FCMP_OGE;
3556 case FCMP_OLT: return FCMP_OLE;
3557 case FCMP_UGT: return FCMP_UGE;
3558 case FCMP_ULT: return FCMP_ULE;
3559 default: return pred;
3560 }
3561}
3562
Sanjoy Das6e78b172015-10-22 19:57:34 +00003563CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3564 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3565
3566 switch (pred) {
3567 default:
3568 llvm_unreachable("Unknown predicate!");
3569 case CmpInst::ICMP_ULT:
3570 return CmpInst::ICMP_SLT;
3571 case CmpInst::ICMP_ULE:
3572 return CmpInst::ICMP_SLE;
3573 case CmpInst::ICMP_UGT:
3574 return CmpInst::ICMP_SGT;
3575 case CmpInst::ICMP_UGE:
3576 return CmpInst::ICMP_SGE;
3577 }
3578}
3579
Craig Topper1c3f2832015-12-15 06:11:33 +00003580bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003581 switch (predicate) {
3582 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003583 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003584 case ICmpInst::ICMP_UGE: return true;
3585 }
3586}
3587
Craig Topper1c3f2832015-12-15 06:11:33 +00003588bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003589 switch (predicate) {
3590 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003591 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003592 case ICmpInst::ICMP_SGE: return true;
3593 }
3594}
3595
Craig Topper1c3f2832015-12-15 06:11:33 +00003596bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003597 switch (predicate) {
3598 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003599 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3600 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003601 case FCmpInst::FCMP_ORD: return true;
3602 }
3603}
Fangrui Songf78650a2018-07-30 19:41:25 +00003604
Craig Topper1c3f2832015-12-15 06:11:33 +00003605bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003606 switch (predicate) {
3607 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003608 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3609 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003610 case FCmpInst::FCMP_UNO: return true;
3611 }
3612}
3613
Craig Topper1c3f2832015-12-15 06:11:33 +00003614bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003615 switch(predicate) {
3616 default: return false;
3617 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3618 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3619 }
3620}
3621
Craig Topper1c3f2832015-12-15 06:11:33 +00003622bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003623 switch(predicate) {
3624 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3625 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3626 default: return false;
3627 }
3628}
3629
Chad Rosier99bc4802016-04-21 16:18:02 +00003630bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003631 // If the predicates match, then we know the first condition implies the
3632 // second is true.
3633 if (Pred1 == Pred2)
3634 return true;
3635
3636 switch (Pred1) {
3637 default:
3638 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003639 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003640 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003641 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3642 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003643 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3644 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3645 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3646 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3647 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3648 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3649 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3650 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003651 }
3652 return false;
3653}
3654
Chad Rosier99bc4802016-04-21 16:18:02 +00003655bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003656 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003657}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003658
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003659//===----------------------------------------------------------------------===//
3660// SwitchInst Implementation
3661//===----------------------------------------------------------------------===//
3662
Chris Lattnerbaf00152010-11-17 05:41:46 +00003663void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3664 assert(Value && Default && NumReserved);
3665 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003666 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003667 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003668
Pete Coopereb31b682015-05-21 22:48:54 +00003669 Op<0>() = Value;
3670 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003671}
3672
Chris Lattner2195fc42007-02-24 00:55:48 +00003673/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3674/// switch on and a default destination. The number of additional cases can
3675/// be specified here to make memory allocation more efficient. This
3676/// constructor can also autoinsert before another instruction.
3677SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3678 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003679 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3680 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003681 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003682}
3683
3684/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3685/// switch on and a default destination. The number of additional cases can
3686/// be specified here to make memory allocation more efficient. This
3687/// constructor also autoinserts at the end of the specified BasicBlock.
3688SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3689 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003690 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3691 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003692 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003693}
3694
Misha Brukmanb1c93172005-04-21 23:48:37 +00003695SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003696 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003697 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003698 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003699 Use *OL = getOperandList();
3700 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003701 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003702 OL[i] = InOL[i];
3703 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003704 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003705 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003706}
3707
3708/// addCase - Add an entry to the switch instruction...
3709///
Chris Lattner47ac1872005-02-24 05:32:09 +00003710void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003711 unsigned NewCaseIdx = getNumCases();
3712 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003713 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003714 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003715 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003716 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003717 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003718 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003719 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003720 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003721}
3722
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003723/// removeCase - This method removes the specified case and its successor
3724/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003725SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3726 unsigned idx = I->getCaseIndex();
3727
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003728 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003729
3730 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003731 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003732
Jay Foad14277722011-02-01 09:22:34 +00003733 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003734 if (2 + (idx + 1) * 2 != NumOps) {
3735 OL[2 + idx * 2] = OL[NumOps - 2];
3736 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003737 }
3738
3739 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003740 OL[NumOps-2].set(nullptr);
3741 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003742 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003743
3744 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003745}
3746
Jay Foade98f29d2011-04-01 08:00:58 +00003747/// growOperands - grow operands - This grows the operand list in response
3748/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003749///
Jay Foade98f29d2011-04-01 08:00:58 +00003750void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003751 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003752 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003753
3754 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003755 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003756}
3757
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003759// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003760//===----------------------------------------------------------------------===//
3761
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003762void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003763 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003764 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003765 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003766 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003767 allocHungoffUses(ReservedSpace);
3768
Pete Coopereb31b682015-05-21 22:48:54 +00003769 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003770}
3771
3772
Jay Foade98f29d2011-04-01 08:00:58 +00003773/// growOperands - grow operands - This grows the operand list in response
3774/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003775///
Jay Foade98f29d2011-04-01 08:00:58 +00003776void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003777 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003778 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003779
Chris Lattner3ed871f2009-10-27 19:13:16 +00003780 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003781 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003782}
3783
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003784IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3785 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003786 : Instruction(Type::getVoidTy(Address->getContext()),
3787 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788 init(Address, NumCases);
3789}
3790
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003791IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3792 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003793 : Instruction(Type::getVoidTy(Address->getContext()),
3794 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003795 init(Address, NumCases);
3796}
3797
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003798IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003799 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3800 nullptr, IBI.getNumOperands()) {
Pete Cooper3fc30402015-06-10 22:38:46 +00003801 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003802 Use *OL = getOperandList();
3803 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003804 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3805 OL[i] = InOL[i];
3806 SubclassOptionalData = IBI.SubclassOptionalData;
3807}
3808
Chris Lattner3ed871f2009-10-27 19:13:16 +00003809/// addDestination - Add a destination.
3810///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003811void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003812 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003813 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003814 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003815 // Initialize some new operands.
3816 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003817 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003818 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003819}
3820
3821/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003822/// indirectbr instruction.
3823void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003824 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003825
Chris Lattner3ed871f2009-10-27 19:13:16 +00003826 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003827 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828
3829 // Replace this value with the last one.
3830 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003831
Chris Lattner3ed871f2009-10-27 19:13:16 +00003832 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003833 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003834 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003835}
3836
Chris Lattner3ed871f2009-10-27 19:13:16 +00003837//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003838// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003839//===----------------------------------------------------------------------===//
3840
Chris Lattnerf22be932004-10-15 23:52:53 +00003841// Define these methods here so vtables don't get emitted into every translation
3842// unit that uses these classes.
3843
Pete Cooper75403d72015-06-24 20:22:23 +00003844GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003845 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003846}
3847
Cameron McInallycbde0d92018-11-13 18:15:47 +00003848UnaryOperator *UnaryOperator::cloneImpl() const {
3849 return Create(getOpcode(), Op<0>());
3850}
3851
Pete Cooper75403d72015-06-24 20:22:23 +00003852BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003853 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003854}
3855
Pete Cooper75403d72015-06-24 20:22:23 +00003856FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003857 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003858}
3859
Pete Cooper75403d72015-06-24 20:22:23 +00003860ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003861 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003862}
3863
Pete Cooper75403d72015-06-24 20:22:23 +00003864ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003865 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003866}
3867
Pete Cooper75403d72015-06-24 20:22:23 +00003868InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003869 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003870}
3871
Pete Cooper75403d72015-06-24 20:22:23 +00003872AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003873 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003874 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003875 (Value *)getOperand(0), getAlignment());
3876 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003877 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003878 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003879}
3880
Pete Cooper75403d72015-06-24 20:22:23 +00003881LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003882 return new LoadInst(getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003883 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003884}
3885
Pete Cooper75403d72015-06-24 20:22:23 +00003886StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003887 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003888 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003889
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003890}
3891
Pete Cooper75403d72015-06-24 20:22:23 +00003892AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003893 AtomicCmpXchgInst *Result =
3894 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003895 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003896 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003897 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003898 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003899 return Result;
3900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003903 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003904 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3905 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003906 Result->setVolatile(isVolatile());
3907 return Result;
3908}
3909
Pete Cooper75403d72015-06-24 20:22:23 +00003910FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003911 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003912}
3913
Pete Cooper75403d72015-06-24 20:22:23 +00003914TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003915 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003916}
3917
Pete Cooper75403d72015-06-24 20:22:23 +00003918ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003919 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003920}
3921
Pete Cooper75403d72015-06-24 20:22:23 +00003922SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003923 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003924}
3925
Pete Cooper75403d72015-06-24 20:22:23 +00003926FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003927 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003928}
3929
Pete Cooper75403d72015-06-24 20:22:23 +00003930FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003931 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003932}
3933
Pete Cooper75403d72015-06-24 20:22:23 +00003934UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003935 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003936}
3937
Pete Cooper75403d72015-06-24 20:22:23 +00003938SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003939 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003940}
3941
Pete Cooper75403d72015-06-24 20:22:23 +00003942FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003943 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003944}
3945
Pete Cooper75403d72015-06-24 20:22:23 +00003946FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003947 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003948}
3949
Pete Cooper75403d72015-06-24 20:22:23 +00003950PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003951 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003952}
3953
Pete Cooper75403d72015-06-24 20:22:23 +00003954IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003955 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003956}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957
Pete Cooper75403d72015-06-24 20:22:23 +00003958BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003959 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003960}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003961
Pete Cooper75403d72015-06-24 20:22:23 +00003962AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003963 return new AddrSpaceCastInst(getOperand(0), getType());
3964}
3965
Pete Cooper75403d72015-06-24 20:22:23 +00003966CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003967 if (hasOperandBundles()) {
3968 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3969 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3970 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003971 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003972}
3973
Pete Cooper75403d72015-06-24 20:22:23 +00003974SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003975 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003976}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003977
Pete Cooper75403d72015-06-24 20:22:23 +00003978VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003979 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003980}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003981
Pete Cooper75403d72015-06-24 20:22:23 +00003982ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003983 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003984}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003985
Pete Cooper75403d72015-06-24 20:22:23 +00003986InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003987 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003988}
3989
Pete Cooper75403d72015-06-24 20:22:23 +00003990ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003991 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003992}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003993
Pete Cooper75403d72015-06-24 20:22:23 +00003994PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003995
Pete Cooper75403d72015-06-24 20:22:23 +00003996LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003997 return new LandingPadInst(*this);
3998}
3999
Pete Cooper75403d72015-06-24 20:22:23 +00004000ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004001 return new(getNumOperands()) ReturnInst(*this);
4002}
4003
Pete Cooper75403d72015-06-24 20:22:23 +00004004BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004005 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004006}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004007
Pete Cooper75403d72015-06-24 20:22:23 +00004008SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004009
Pete Cooper75403d72015-06-24 20:22:23 +00004010IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004011 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004012}
4013
Pete Cooper75403d72015-06-24 20:22:23 +00004014InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004015 if (hasOperandBundles()) {
4016 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4017 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4018 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004019 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004020}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004021
Pete Cooper75403d72015-06-24 20:22:23 +00004022ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004023
David Majnemer654e1302015-07-31 17:58:14 +00004024CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4025 return new (getNumOperands()) CleanupReturnInst(*this);
4026}
4027
David Majnemer654e1302015-07-31 17:58:14 +00004028CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004029 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004030}
4031
David Majnemer8a1c45d2015-12-12 05:38:55 +00004032CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4033 return new CatchSwitchInst(*this);
4034}
4035
4036FuncletPadInst *FuncletPadInst::cloneImpl() const {
4037 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004038}
4039
Pete Cooper75403d72015-06-24 20:22:23 +00004040UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004041 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004042 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004043}