blob: f48a970fd401731abfb278975f2aaa770cadafcd [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00007//===----------------------------------------------------------------------===//
8//
Chris Lattnerafdb3de2005-01-29 00:35:16 +00009// This file implements all of the non-inline methods for the LLVM instruction
10// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000016#include "llvm/ADT/None.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000022#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000027#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
Chandler Carruth05b5bd82018-12-27 23:40:17 +000029#include "llvm/IR/Intrinsics.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-06-26 06:17:00 +000048// AllocaInst Class
49//===----------------------------------------------------------------------===//
50
51Optional<uint64_t>
52AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
53 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
54 if (isArrayAllocation()) {
55 auto C = dyn_cast<ConstantInt>(getArraySize());
56 if (!C)
57 return None;
58 Size *= C->getZExtValue();
59 }
60 return Size;
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000064// CallSite Class
65//===----------------------------------------------------------------------===//
66
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000067User::op_iterator CallSite::getCallee() const {
Chandler Carruthe429c792018-11-22 10:31:35 +000068 return cast<CallBase>(getInstruction())->op_end() - 1;
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000069}
70
Gordon Henriksen14a55692007-12-10 02:14:30 +000071//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000072// SelectInst Class
73//===----------------------------------------------------------------------===//
74
75/// areInvalidOperands - Return a string if the specified operands are invalid
76/// for a select operation, otherwise return null.
77const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
78 if (Op1->getType() != Op2->getType())
79 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000080
81 if (Op1->getType()->isTokenTy())
82 return "select values cannot have token type";
83
Chris Lattner229907c2011-07-18 04:54:35 +000084 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000085 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000086 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000087 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000088 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000089 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000090 return "selected values for vector select must be vectors";
91 if (ET->getNumElements() != VT->getNumElements())
92 return "vector select requires selected vectors to have "
93 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000094 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000095 return "select condition must be i1 or <n x i1>";
96 }
Craig Topperc6207612014-04-09 06:08:46 +000097 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000098}
99
Chris Lattner88107952008-12-29 00:12:50 +0000100//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// PHINode Class
102//===----------------------------------------------------------------------===//
103
104PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000105 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
106 ReservedSpace(PN.getNumOperands()) {
107 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000108 std::copy(PN.op_begin(), PN.op_end(), op_begin());
109 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000110 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111}
112
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113// removeIncomingValue - Remove an incoming value. This is useful if a
114// predecessor basic block is deleted.
115Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000116 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // Move everything after this operand down.
119 //
120 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000123 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
124 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125
126 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000127 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000128 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000129
130 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000131 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000133 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000134 eraseFromParent();
135 }
136 return Removed;
137}
138
Jay Foade98f29d2011-04-01 08:00:58 +0000139/// growOperands - grow operands - This grows the operand list in response
140/// to a push_back style of operation. This grows the number of ops by 1.5
141/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000142///
Jay Foade98f29d2011-04-01 08:00:58 +0000143void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000144 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000145 unsigned NumOps = e + e / 2;
146 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
147
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000148 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000149 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150}
151
Nate Begemanb3923212005-08-04 23:24:19 +0000152/// hasConstantValue - If the specified PHI node always merges together the same
153/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154Value *PHINode::hasConstantValue() const {
155 // Exploit the fact that phi nodes always have at least one entry.
156 Value *ConstantValue = getIncomingValue(0);
157 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000158 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
159 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000160 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000161 // The case where the first value is this PHI.
162 ConstantValue = getIncomingValue(i);
163 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000164 if (ConstantValue == this)
165 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000166 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000167}
168
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000169/// hasConstantOrUndefValue - Whether the specified PHI node always merges
170/// together the same value, assuming that undefs result in the same value as
171/// non-undefs.
172/// Unlike \ref hasConstantValue, this does not return a value because the
173/// unique non-undef incoming value need not dominate the PHI node.
174bool PHINode::hasConstantOrUndefValue() const {
175 Value *ConstantValue = nullptr;
176 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
177 Value *Incoming = getIncomingValue(i);
178 if (Incoming != this && !isa<UndefValue>(Incoming)) {
179 if (ConstantValue && ConstantValue != Incoming)
180 return false;
181 ConstantValue = Incoming;
182 }
183 }
184 return true;
185}
186
Bill Wendlingfae14752011-08-12 20:24:12 +0000187//===----------------------------------------------------------------------===//
188// LandingPadInst Implementation
189//===----------------------------------------------------------------------===//
190
David Majnemer7fddecc2015-06-17 20:52:32 +0000191LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
192 const Twine &NameStr, Instruction *InsertBefore)
193 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
194 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000195}
196
David Majnemer7fddecc2015-06-17 20:52:32 +0000197LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
198 const Twine &NameStr, BasicBlock *InsertAtEnd)
199 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
200 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000201}
202
203LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000204 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
205 LP.getNumOperands()),
206 ReservedSpace(LP.getNumOperands()) {
207 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000208 Use *OL = getOperandList();
209 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000210 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
211 OL[I] = InOL[I];
212
213 setCleanup(LP.isCleanup());
214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 const Twine &NameStr,
218 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000219 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220}
221
David Majnemer7fddecc2015-06-17 20:52:32 +0000222LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000223 const Twine &NameStr,
224 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000225 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000226}
227
David Majnemer7fddecc2015-06-17 20:52:32 +0000228void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000229 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000230 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000231 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000232 setName(NameStr);
233 setCleanup(false);
234}
235
236/// growOperands - grow operands - This grows the operand list in response to a
237/// push_back style of operation. This grows the number of ops by 2 times.
238void LandingPadInst::growOperands(unsigned Size) {
239 unsigned e = getNumOperands();
240 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000241 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000242 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000243}
244
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000245void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000246 unsigned OpNo = getNumOperands();
247 growOperands(1);
248 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000249 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000250 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000251}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252
253//===----------------------------------------------------------------------===//
Chandler Carruthe429c792018-11-22 10:31:35 +0000254// CallBase Implementation
255//===----------------------------------------------------------------------===//
256
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000257Function *CallBase::getCaller() { return getParent()->getParent(); }
258
Chandler Carruth57578aa2019-01-07 07:15:51 +0000259bool CallBase::isIndirectCall() const {
260 const Value *V = getCalledValue();
261 if (isa<Function>(V) || isa<Constant>(V))
262 return false;
263 if (const CallInst *CI = dyn_cast<CallInst>(this))
264 if (CI->isInlineAsm())
265 return false;
266 return true;
267}
268
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000269Intrinsic::ID CallBase::getIntrinsicID() const {
270 if (auto *F = getCalledFunction())
271 return F->getIntrinsicID();
272 return Intrinsic::not_intrinsic;
273}
274
275bool CallBase::isReturnNonNull() const {
276 if (hasRetAttr(Attribute::NonNull))
277 return true;
278
279 if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
280 !NullPointerIsDefined(getCaller(),
281 getType()->getPointerAddressSpace()))
282 return true;
283
284 return false;
285}
286
Chandler Carruthe429c792018-11-22 10:31:35 +0000287Value *CallBase::getReturnedArgOperand() const {
288 unsigned Index;
289
290 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
291 return getArgOperand(Index - AttributeList::FirstArgIndex);
292 if (const Function *F = getCalledFunction())
293 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
294 Index)
295 return getArgOperand(Index - AttributeList::FirstArgIndex);
296
297 return nullptr;
298}
299
300bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
301 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
302 return true;
303
304 // Look at the callee, if available.
305 if (const Function *F = getCalledFunction())
306 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
307 return false;
308}
309
310/// Determine whether the argument or parameter has the given attribute.
311bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
312 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
313
314 if (Attrs.hasParamAttribute(ArgNo, Kind))
315 return true;
316 if (const Function *F = getCalledFunction())
317 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
318 return false;
319}
320
321bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
322 if (const Function *F = getCalledFunction())
323 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
324 return false;
325}
326
327bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
328 if (const Function *F = getCalledFunction())
329 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
330 return false;
331}
332
333CallBase::op_iterator
334CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
335 const unsigned BeginIndex) {
336 auto It = op_begin() + BeginIndex;
337 for (auto &B : Bundles)
338 It = std::copy(B.input_begin(), B.input_end(), It);
339
340 auto *ContextImpl = getContext().pImpl;
341 auto BI = Bundles.begin();
342 unsigned CurrentIndex = BeginIndex;
343
344 for (auto &BOI : bundle_op_infos()) {
345 assert(BI != Bundles.end() && "Incorrect allocation?");
346
347 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
348 BOI.Begin = CurrentIndex;
349 BOI.End = CurrentIndex + BI->input_size();
350 CurrentIndex = BOI.End;
351 BI++;
352 }
353
354 assert(BI == Bundles.end() && "Incorrect allocation?");
355
356 return It;
357}
358
359//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360// CallInst Implementation
361//===----------------------------------------------------------------------===//
362
David Blaikie348de692015-04-23 21:36:23 +0000363void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000364 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000365 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000366 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
367 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000368 setCalledOperand(Func);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369
Jay Foad5bd375a2011-07-15 08:37:34 +0000370#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000371 assert((Args.size() == FTy->getNumParams() ||
372 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000373 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000374
375 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000376 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000377 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000378 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000379#endif
380
Fangrui Song75709322018-11-17 01:44:25 +0000381 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000382
383 auto It = populateBundleOperandInfos(Bundles, Args.size());
384 (void)It;
385 assert(It + 1 == op_end() && "Should add up!");
386
Jay Foad5bd375a2011-07-15 08:37:34 +0000387 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000388}
389
James Y Knightf9563902019-01-14 21:37:42 +0000390void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
391 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000392 assert(getNumOperands() == 1 && "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000393 setCalledOperand(Func);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000394
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000395 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000396
397 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000398}
399
James Y Knightf9563902019-01-14 21:37:42 +0000400CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
401 Instruction *InsertBefore)
402 : CallBase(Ty->getReturnType(), Instruction::Call,
403 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) {
404 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405}
406
James Y Knightf9563902019-01-14 21:37:42 +0000407CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
408 BasicBlock *InsertAtEnd)
409 : CallBase(Ty->getReturnType(), Instruction::Call,
410 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) {
411 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000412}
413
Misha Brukmanb1c93172005-04-21 23:48:37 +0000414CallInst::CallInst(const CallInst &CI)
Chandler Carruthe429c792018-11-22 10:31:35 +0000415 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
416 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(),
417 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000418 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000419 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000420
Jay Foad5bd375a2011-07-15 08:37:34 +0000421 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000422 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
423 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000424 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000425}
426
Sanjoy Das2d161452015-11-18 06:23:38 +0000427CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
428 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000429 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000430
431 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
432 InsertPt);
433 NewCI->setTailCallKind(CI->getTailCallKind());
434 NewCI->setCallingConv(CI->getCallingConv());
435 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000436 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000437 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000438 return NewCI;
439}
440
Hal Finkele87ad542016-07-10 23:01:32 +0000441
Reid Klecknera0b45f42017-05-03 18:17:31 +0000442
Hal Finkele87ad542016-07-10 23:01:32 +0000443
Eric Christopher901b1a72008-05-16 20:39:43 +0000444
Amaury Secheta65a2372016-06-15 05:14:29 +0000445
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000446
Reid Klecknera0b45f42017-05-03 18:17:31 +0000447
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000448
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000449
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000450/// IsConstantOne - Return true only if val is constant int 1
451static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000452 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000453 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
454 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455}
456
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000457static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000458 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000459 Type *AllocTy, Value *AllocSize,
460 Value *ArraySize,
461 ArrayRef<OperandBundleDef> OpB,
462 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000463 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000464 "createMalloc needs either InsertBefore or InsertAtEnd");
465
Fangrui Songf78650a2018-07-30 19:41:25 +0000466 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000467 // bitcast (i8* malloc(typeSize)) to type*
468 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000469 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000470 if (!ArraySize)
471 ArraySize = ConstantInt::get(IntPtrTy, 1);
472 else if (ArraySize->getType() != IntPtrTy) {
473 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000474 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
475 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000476 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000477 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
478 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000479 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000480
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000481 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000482 if (IsConstantOne(AllocSize)) {
483 AllocSize = ArraySize; // Operand * 1 = Operand
484 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
485 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
486 false /*ZExt*/);
487 // Malloc arg is constant product of type size and array size
488 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
489 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490 // Multiply type size by the array size...
491 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000492 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
493 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000494 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000495 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
496 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000498 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000499
Victor Hernandez788eaab2009-09-18 19:20:02 +0000500 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000501 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000502 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
503 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000504 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000505 Value *MallocFunc = MallocF;
506 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000507 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000508 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000509 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000510 CallInst *MCall = nullptr;
511 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000512 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000513 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
514 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000515 Result = MCall;
516 if (Result->getType() != AllocPtrType)
517 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000518 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000519 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000520 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000521 Result = MCall;
522 if (Result->getType() != AllocPtrType) {
523 InsertAtEnd->getInstList().push_back(MCall);
524 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000525 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000526 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000527 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000528 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000529 if (Function *F = dyn_cast<Function>(MallocFunc)) {
530 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000531 if (!F->returnDoesNotAlias())
532 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000533 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000534 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000535
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000536 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000537}
538
539/// CreateMalloc - Generate the IR for a call to malloc:
540/// 1. Compute the malloc call's argument as the specified type's size,
541/// possibly multiplied by the array size if the array size is not
542/// constant 1.
543/// 2. Call malloc with that argument.
544/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000545Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000546 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000547 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000548 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000549 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000550 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000551 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000552}
David Majnemerfadc6db2016-04-29 08:07:22 +0000553Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
554 Type *IntPtrTy, Type *AllocTy,
555 Value *AllocSize, Value *ArraySize,
556 ArrayRef<OperandBundleDef> OpB,
557 Function *MallocF,
558 const Twine &Name) {
559 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
560 ArraySize, OpB, MallocF, Name);
561}
562
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000563/// CreateMalloc - Generate the IR for a call to malloc:
564/// 1. Compute the malloc call's argument as the specified type's size,
565/// possibly multiplied by the array size if the array size is not
566/// constant 1.
567/// 2. Call malloc with that argument.
568/// 3. Bitcast the result of the malloc call to the specified type.
569/// Note: This function does not add the bitcast to the basic block, that is the
570/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000571Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000572 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000573 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000574 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000575 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000576 ArraySize, None, MallocF, Name);
577}
578Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
579 Type *IntPtrTy, Type *AllocTy,
580 Value *AllocSize, Value *ArraySize,
581 ArrayRef<OperandBundleDef> OpB,
582 Function *MallocF, const Twine &Name) {
583 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
584 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000585}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000586
David Majnemerfadc6db2016-04-29 08:07:22 +0000587static Instruction *createFree(Value *Source,
588 ArrayRef<OperandBundleDef> Bundles,
589 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000590 BasicBlock *InsertAtEnd) {
591 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
592 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000593 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000594 "Can not free something of nonpointer type!");
595
Ana Pazosb3596022016-02-03 21:34:39 +0000596 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
597 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000598
Chris Lattner229907c2011-07-18 04:54:35 +0000599 Type *VoidTy = Type::getVoidTy(M->getContext());
600 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000601 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000602 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000603 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000604 Value *PtrCast = Source;
605 if (InsertBefore) {
606 if (Source->getType() != IntPtrTy)
607 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000608 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000609 } else {
610 if (Source->getType() != IntPtrTy)
611 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000612 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000613 }
614 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000615 if (Function *F = dyn_cast<Function>(FreeFunc))
616 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000617
618 return Result;
619}
620
621/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000622Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000623 return createFree(Source, None, InsertBefore, nullptr);
624}
625Instruction *CallInst::CreateFree(Value *Source,
626 ArrayRef<OperandBundleDef> Bundles,
627 Instruction *InsertBefore) {
628 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000629}
630
631/// CreateFree - Generate the IR for a call to the builtin free function.
632/// Note: This function does not add the call to the basic block, that is the
633/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000634Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000635 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
636 assert(FreeCall && "CreateFree did not create a CallInst");
637 return FreeCall;
638}
639Instruction *CallInst::CreateFree(Value *Source,
640 ArrayRef<OperandBundleDef> Bundles,
641 BasicBlock *InsertAtEnd) {
642 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000643 assert(FreeCall && "CreateFree did not create a CallInst");
644 return FreeCall;
645}
646
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000647//===----------------------------------------------------------------------===//
648// InvokeInst Implementation
649//===----------------------------------------------------------------------===//
650
David Blaikie3e807092015-05-13 18:35:26 +0000651void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
652 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000653 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000654 const Twine &NameStr) {
655 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000656
Chandler Carruthe429c792018-11-22 10:31:35 +0000657 assert((int)getNumOperands() ==
658 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
Sanjoy Das9303c242015-09-24 19:14:18 +0000659 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000660 setNormalDest(IfNormal);
661 setUnwindDest(IfException);
662 setCalledOperand(Fn);
Jay Foad5bd375a2011-07-15 08:37:34 +0000663
664#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000665 assert(((Args.size() == FTy->getNumParams()) ||
666 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000667 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000668
Jay Foad5bd375a2011-07-15 08:37:34 +0000669 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000670 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000671 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000672 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000673#endif
674
Fangrui Song75709322018-11-17 01:44:25 +0000675 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000676
677 auto It = populateBundleOperandInfos(Bundles, Args.size());
678 (void)It;
679 assert(It + 3 == op_end() && "Should add up!");
680
Jay Foad5bd375a2011-07-15 08:37:34 +0000681 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682}
683
Misha Brukmanb1c93172005-04-21 23:48:37 +0000684InvokeInst::InvokeInst(const InvokeInst &II)
Chandler Carruthe429c792018-11-22 10:31:35 +0000685 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
686 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(),
687 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000688 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000689 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000690 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
691 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000692 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693}
694
Sanjoy Das2d161452015-11-18 06:23:38 +0000695InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
696 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000697 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000698
699 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
700 II->getUnwindDest(), Args, OpB,
701 II->getName(), InsertPt);
702 NewII->setCallingConv(II->getCallingConv());
703 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000704 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000705 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000706 return NewII;
707}
708
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000709
Bill Wendlingfae14752011-08-12 20:24:12 +0000710LandingPadInst *InvokeInst::getLandingPadInst() const {
711 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
712}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000713
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714//===----------------------------------------------------------------------===//
715// ReturnInst Implementation
716//===----------------------------------------------------------------------===//
717
Chris Lattner2195fc42007-02-24 00:55:48 +0000718ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000719 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
720 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
721 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000722 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000723 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000724 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000725}
726
Owen Anderson55f1c092009-08-13 21:58:54 +0000727ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000728 : Instruction(Type::getVoidTy(C), Instruction::Ret,
729 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
730 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000731 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000732 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000733}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000734
Owen Anderson55f1c092009-08-13 21:58:54 +0000735ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000736 : Instruction(Type::getVoidTy(C), Instruction::Ret,
737 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
738 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000739 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000740 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000741}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000742
Owen Anderson55f1c092009-08-13 21:58:54 +0000743ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000744 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
745 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel59643e52008-02-23 00:35:18 +0000746
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000748// ResumeInst Implementation
749//===----------------------------------------------------------------------===//
750
751ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000752 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
753 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000754 Op<0>() = RI.Op<0>();
755}
756
757ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000758 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
759 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000760 Op<0>() = Exn;
761}
762
763ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000764 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
765 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000766 Op<0>() = Exn;
767}
768
Bill Wendlingf891bf82011-07-31 06:30:59 +0000769//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000770// CleanupReturnInst Implementation
771//===----------------------------------------------------------------------===//
772
773CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000774 : Instruction(CRI.getType(), Instruction::CleanupRet,
775 OperandTraits<CleanupReturnInst>::op_end(this) -
776 CRI.getNumOperands(),
777 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000778 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000779 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000780 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000781 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000782}
783
David Majnemer8a1c45d2015-12-12 05:38:55 +0000784void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000785 if (UnwindBB)
786 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000787
David Majnemer8a1c45d2015-12-12 05:38:55 +0000788 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000789 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000790 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000791}
792
David Majnemer8a1c45d2015-12-12 05:38:55 +0000793CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
794 unsigned Values, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000795 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
796 Instruction::CleanupRet,
797 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
798 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000799 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000800}
801
David Majnemer8a1c45d2015-12-12 05:38:55 +0000802CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
803 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000804 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
805 Instruction::CleanupRet,
806 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
807 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000808 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000809}
810
David Majnemer654e1302015-07-31 17:58:14 +0000811//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000812// CatchReturnInst Implementation
813//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000814void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000815 Op<0>() = CatchPad;
816 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000817}
David Majnemer654e1302015-07-31 17:58:14 +0000818
819CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000820 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
821 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000822 Op<0>() = CRI.Op<0>();
823 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000824}
825
David Majnemer8a1c45d2015-12-12 05:38:55 +0000826CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000827 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000828 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
829 OperandTraits<CatchReturnInst>::op_begin(this), 2,
830 InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000831 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000832}
833
David Majnemer8a1c45d2015-12-12 05:38:55 +0000834CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000835 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000836 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
837 OperandTraits<CatchReturnInst>::op_begin(this), 2,
838 InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000839 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000840}
841
David Majnemer654e1302015-07-31 17:58:14 +0000842//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000843// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000844//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000845
846CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
847 unsigned NumReservedValues,
848 const Twine &NameStr,
849 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000850 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
851 InsertBefore) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000852 if (UnwindDest)
853 ++NumReservedValues;
854 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000855 setName(NameStr);
856}
857
David Majnemer8a1c45d2015-12-12 05:38:55 +0000858CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
859 unsigned NumReservedValues,
860 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000861 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
862 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000863 if (UnwindDest)
864 ++NumReservedValues;
865 init(ParentPad, UnwindDest, NumReservedValues + 1);
866 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000867}
868
David Majnemer8a1c45d2015-12-12 05:38:55 +0000869CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000870 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
871 CSI.getNumOperands()) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000872 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
873 setNumHungOffUseOperands(ReservedSpace);
874 Use *OL = getOperandList();
875 const Use *InOL = CSI.getOperandList();
876 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
877 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000878}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000879
880void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
881 unsigned NumReservedValues) {
882 assert(ParentPad && NumReservedValues);
883
884 ReservedSpace = NumReservedValues;
885 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
886 allocHungoffUses(ReservedSpace);
887
888 Op<0>() = ParentPad;
889 if (UnwindDest) {
890 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
891 setUnwindDest(UnwindDest);
892 }
893}
894
895/// growOperands - grow operands - This grows the operand list in response to a
896/// push_back style of operation. This grows the number of ops by 2 times.
897void CatchSwitchInst::growOperands(unsigned Size) {
898 unsigned NumOperands = getNumOperands();
899 assert(NumOperands >= 1);
900 if (ReservedSpace >= NumOperands + Size)
901 return;
902 ReservedSpace = (NumOperands + Size / 2) * 2;
903 growHungoffUses(ReservedSpace);
904}
905
906void CatchSwitchInst::addHandler(BasicBlock *Handler) {
907 unsigned OpNo = getNumOperands();
908 growOperands(1);
909 assert(OpNo < ReservedSpace && "Growing didn't work!");
910 setNumHungOffUseOperands(getNumOperands() + 1);
911 getOperandList()[OpNo] = Handler;
912}
913
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000914void CatchSwitchInst::removeHandler(handler_iterator HI) {
915 // Move all subsequent handlers up one.
916 Use *EndDst = op_end() - 1;
917 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
918 *CurDst = *(CurDst + 1);
919 // Null out the last handler use.
920 *EndDst = nullptr;
921
922 setNumHungOffUseOperands(getNumOperands() - 1);
923}
924
David Majnemer8a1c45d2015-12-12 05:38:55 +0000925//===----------------------------------------------------------------------===//
926// FuncletPadInst Implementation
927//===----------------------------------------------------------------------===//
928void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
929 const Twine &NameStr) {
930 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
Fangrui Song75709322018-11-17 01:44:25 +0000931 llvm::copy(Args, op_begin());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000932 setParentPad(ParentPad);
933 setName(NameStr);
934}
935
936FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
937 : Instruction(FPI.getType(), FPI.getOpcode(),
938 OperandTraits<FuncletPadInst>::op_end(this) -
939 FPI.getNumOperands(),
940 FPI.getNumOperands()) {
941 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
942 setParentPad(FPI.getParentPad());
943}
944
945FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
946 ArrayRef<Value *> Args, unsigned Values,
947 const Twine &NameStr, Instruction *InsertBefore)
948 : Instruction(ParentPad->getType(), Op,
949 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
950 InsertBefore) {
951 init(ParentPad, Args, NameStr);
952}
953
954FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
955 ArrayRef<Value *> Args, unsigned Values,
956 const Twine &NameStr, BasicBlock *InsertAtEnd)
957 : Instruction(ParentPad->getType(), Op,
958 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
959 InsertAtEnd) {
960 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000961}
962
963//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000964// UnreachableInst Implementation
965//===----------------------------------------------------------------------===//
966
Fangrui Songf78650a2018-07-30 19:41:25 +0000967UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +0000968 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000969 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
970 0, InsertBefore) {}
Owen Anderson55f1c092009-08-13 21:58:54 +0000971UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000972 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
973 0, InsertAtEnd) {}
Chris Lattner2195fc42007-02-24 00:55:48 +0000974
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000975//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976// BranchInst Implementation
977//===----------------------------------------------------------------------===//
978
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979void BranchInst::AssertOK() {
980 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000981 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000982 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983}
984
Chris Lattner2195fc42007-02-24 00:55:48 +0000985BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000986 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
987 OperandTraits<BranchInst>::op_end(this) - 1, 1,
988 InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000989 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000990 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000991}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000992
Chris Lattner2195fc42007-02-24 00:55:48 +0000993BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
994 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000995 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
996 OperandTraits<BranchInst>::op_end(this) - 3, 3,
997 InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000998 Op<-1>() = IfTrue;
999 Op<-2>() = IfFalse;
1000 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001001#ifndef NDEBUG
1002 AssertOK();
1003#endif
1004}
1005
1006BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001007 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1008 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001009 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001010 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001011}
1012
1013BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth509e20e2018-10-19 00:22:37 +00001014 BasicBlock *InsertAtEnd)
1015 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1016 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001017 Op<-1>() = IfTrue;
1018 Op<-2>() = IfFalse;
1019 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001020#ifndef NDEBUG
1021 AssertOK();
1022#endif
1023}
1024
Chandler Carruth509e20e2018-10-19 00:22:37 +00001025BranchInst::BranchInst(const BranchInst &BI)
1026 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
1027 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1028 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001029 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001030 if (BI.getNumOperands() != 1) {
1031 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001032 Op<-3>() = BI.Op<-3>();
1033 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001035 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001036}
1037
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001038void BranchInst::swapSuccessors() {
1039 assert(isConditional() &&
1040 "Cannot swap successors of an unconditional branch");
1041 Op<-1>().swap(Op<-2>());
1042
1043 // Update profile metadata if present and it matches our structural
1044 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001045 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001046}
1047
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001049// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050//===----------------------------------------------------------------------===//
1051
Owen Andersonb6b25302009-07-14 23:09:55 +00001052static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001053 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001054 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001055 else {
1056 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001057 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001058 assert(Amt->getType()->isIntegerTy() &&
1059 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001060 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001061 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062}
1063
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001064AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001065 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001066 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001067
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001068AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001069 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001070 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001071
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001072AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001073 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001074 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1075
1076AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1077 const Twine &Name, BasicBlock *InsertAtEnd)
1078 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1079
1080AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1081 unsigned Align, const Twine &Name,
1082 Instruction *InsertBefore)
1083 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1084 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1085 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001086 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001087 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001088 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001089}
1090
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001091AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1092 unsigned Align, const Twine &Name,
1093 BasicBlock *InsertAtEnd)
1094 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1095 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001096 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001097 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001098 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001099 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001100}
1101
Victor Hernandez8acf2952009-10-23 21:09:37 +00001102void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001103 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001104 assert(Align <= MaximumAlignment &&
1105 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001106 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1107 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001108 assert(getAlignment() == Align && "Alignment representation error!");
1109}
1110
Victor Hernandez8acf2952009-10-23 21:09:37 +00001111bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001112 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001113 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001114 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115}
1116
Chris Lattner8b291e62008-11-26 02:54:17 +00001117/// isStaticAlloca - Return true if this alloca is in the entry block of the
1118/// function and is a constant size. If so, the code generator will fold it
1119/// into the prolog/epilog code, so it is basically free.
1120bool AllocaInst::isStaticAlloca() const {
1121 // Must be constant size.
1122 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001123
Chris Lattner8b291e62008-11-26 02:54:17 +00001124 // Must be in the entry block.
1125 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001126 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001127}
1128
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130// LoadInst Implementation
1131//===----------------------------------------------------------------------===//
1132
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001133void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001134 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001135 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001136 assert(!(isAtomic() && getAlignment() == 0) &&
1137 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001138}
1139
James Y Knight84c1dbd2019-01-14 21:37:53 +00001140LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1141 Instruction *InsertBef)
1142 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143
James Y Knight84c1dbd2019-01-14 21:37:53 +00001144LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1145 BasicBlock *InsertAE)
1146 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001147
David Blaikie0c28fd72015-05-20 21:46:30 +00001148LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001150 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001151
James Y Knight84c1dbd2019-01-14 21:37:53 +00001152LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001153 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001154 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001155
David Blaikieb7a029872015-04-17 19:56:21 +00001156LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001157 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001158 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001159 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001160
James Y Knight84c1dbd2019-01-14 21:37:53 +00001161LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001162 unsigned Align, BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001163 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001164 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001165
David Blaikie15d9a4c2015-04-06 20:59:48 +00001166LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001167 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001168 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001169 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001170 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001171 setVolatile(isVolatile);
1172 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001173 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001174 AssertOK();
1175 setName(Name);
1176}
1177
James Y Knight84c1dbd2019-01-14 21:37:53 +00001178LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1179 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001181 : UnaryInstruction(Ty, Load, Ptr, InsertAE) {
1182 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
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
Christopher Lamb84485702007-04-22 19:24:39 +00001190void LoadInst::setAlignment(unsigned Align) {
1191 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001192 assert(Align <= MaximumAlignment &&
1193 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001194 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001195 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001196 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001197}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198
1199//===----------------------------------------------------------------------===//
1200// StoreInst Implementation
1201//===----------------------------------------------------------------------===//
1202
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001204 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001205 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001206 "Ptr must have pointer type!");
1207 assert(getOperand(0)->getType() ==
1208 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001209 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001210 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001211 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001212}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001215 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001216
1217StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001218 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001219
Misha Brukmanb1c93172005-04-21 23:48:37 +00001220StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001221 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001222 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001223
1224StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001225 BasicBlock *InsertAtEnd)
1226 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1227
1228StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1229 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001230 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001231 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001232
1233StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1234 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001235 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001236 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001237
Misha Brukmanb1c93172005-04-21 23:48:37 +00001238StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001239 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001240 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001241 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001242 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001243 OperandTraits<StoreInst>::op_begin(this),
1244 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001245 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001246 Op<0>() = val;
1247 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001248 setVolatile(isVolatile);
1249 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001250 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001251 AssertOK();
1252}
1253
1254StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001255 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001256 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001257 BasicBlock *InsertAtEnd)
1258 : Instruction(Type::getVoidTy(val->getContext()), Store,
1259 OperandTraits<StoreInst>::op_begin(this),
1260 OperandTraits<StoreInst>::operands(this),
1261 InsertAtEnd) {
1262 Op<0>() = val;
1263 Op<1>() = addr;
1264 setVolatile(isVolatile);
1265 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001266 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001267 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001268}
1269
Christopher Lamb84485702007-04-22 19:24:39 +00001270void StoreInst::setAlignment(unsigned Align) {
1271 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001272 assert(Align <= MaximumAlignment &&
1273 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001274 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001275 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001276 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001277}
1278
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001280// AtomicCmpXchgInst Implementation
1281//===----------------------------------------------------------------------===//
1282
1283void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001284 AtomicOrdering SuccessOrdering,
1285 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001286 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001287 Op<0>() = Ptr;
1288 Op<1>() = Cmp;
1289 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001290 setSuccessOrdering(SuccessOrdering);
1291 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001292 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001293
1294 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1295 "All operands must be non-null!");
1296 assert(getOperand(0)->getType()->isPointerTy() &&
1297 "Ptr must have pointer type!");
1298 assert(getOperand(1)->getType() ==
1299 cast<PointerType>(getOperand(0)->getType())->getElementType()
1300 && "Ptr must be a pointer to Cmp type!");
1301 assert(getOperand(2)->getType() ==
1302 cast<PointerType>(getOperand(0)->getType())->getElementType()
1303 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001304 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001305 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001306 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001307 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001308 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1309 "AtomicCmpXchg failure argument shall be no stronger than the success "
1310 "argument");
1311 assert(FailureOrdering != AtomicOrdering::Release &&
1312 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001313 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001314}
1315
1316AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001317 AtomicOrdering SuccessOrdering,
1318 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001319 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001320 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001321 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001322 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001323 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1324 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001325 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001326}
1327
1328AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001329 AtomicOrdering SuccessOrdering,
1330 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001331 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001332 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001333 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001334 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001335 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1336 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001337 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001338}
Tim Northover420a2162014-06-13 14:24:07 +00001339
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001340//===----------------------------------------------------------------------===//
1341// AtomicRMWInst Implementation
1342//===----------------------------------------------------------------------===//
1343
1344void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1345 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001346 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001347 Op<0>() = Ptr;
1348 Op<1>() = Val;
1349 setOperation(Operation);
1350 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001351 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001352
1353 assert(getOperand(0) && getOperand(1) &&
1354 "All operands must be non-null!");
1355 assert(getOperand(0)->getType()->isPointerTy() &&
1356 "Ptr must have pointer type!");
1357 assert(getOperand(1)->getType() ==
1358 cast<PointerType>(getOperand(0)->getType())->getElementType()
1359 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001360 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001361 "AtomicRMW instructions must be atomic!");
1362}
1363
1364AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1365 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001366 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001367 Instruction *InsertBefore)
1368 : Instruction(Val->getType(), AtomicRMW,
1369 OperandTraits<AtomicRMWInst>::op_begin(this),
1370 OperandTraits<AtomicRMWInst>::operands(this),
1371 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001372 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001373}
1374
1375AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1376 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001377 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001378 BasicBlock *InsertAtEnd)
1379 : Instruction(Val->getType(), AtomicRMW,
1380 OperandTraits<AtomicRMWInst>::op_begin(this),
1381 OperandTraits<AtomicRMWInst>::operands(this),
1382 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001383 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001384}
1385
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001386StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1387 switch (Op) {
1388 case AtomicRMWInst::Xchg:
1389 return "xchg";
1390 case AtomicRMWInst::Add:
1391 return "add";
1392 case AtomicRMWInst::Sub:
1393 return "sub";
1394 case AtomicRMWInst::And:
1395 return "and";
1396 case AtomicRMWInst::Nand:
1397 return "nand";
1398 case AtomicRMWInst::Or:
1399 return "or";
1400 case AtomicRMWInst::Xor:
1401 return "xor";
1402 case AtomicRMWInst::Max:
1403 return "max";
1404 case AtomicRMWInst::Min:
1405 return "min";
1406 case AtomicRMWInst::UMax:
1407 return "umax";
1408 case AtomicRMWInst::UMin:
1409 return "umin";
Matt Arsenault39508332019-01-22 18:18:02 +00001410 case AtomicRMWInst::FAdd:
1411 return "fadd";
1412 case AtomicRMWInst::FSub:
1413 return "fsub";
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001414 case AtomicRMWInst::BAD_BINOP:
1415 return "<invalid operation>";
1416 }
1417
1418 llvm_unreachable("invalid atomicrmw operation");
1419}
1420
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001421//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001422// FenceInst Implementation
1423//===----------------------------------------------------------------------===//
1424
Fangrui Songf78650a2018-07-30 19:41:25 +00001425FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001426 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001427 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001428 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001429 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001430 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001431}
1432
Fangrui Songf78650a2018-07-30 19:41:25 +00001433FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001434 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001435 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001436 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001437 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001438 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001439}
1440
1441//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001442// GetElementPtrInst Implementation
1443//===----------------------------------------------------------------------===//
1444
Jay Foadd1b78492011-07-25 09:48:08 +00001445void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001446 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001447 assert(getNumOperands() == 1 + IdxList.size() &&
1448 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001449 Op<0>() = Ptr;
Fangrui Song75709322018-11-17 01:44:25 +00001450 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001451 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001452}
1453
Gabor Greiff6caff662008-05-10 08:32:32 +00001454GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001455 : Instruction(GEPI.getType(), GetElementPtr,
1456 OperandTraits<GetElementPtrInst>::op_end(this) -
1457 GEPI.getNumOperands(),
1458 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001459 SourceElementType(GEPI.SourceElementType),
1460 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001461 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001462 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001463}
1464
Chris Lattner6090a422009-03-09 04:46:40 +00001465/// getIndexedType - Returns the type of the element that would be accessed with
1466/// a gep instruction with the specified parameters.
1467///
1468/// The Idxs pointer should point to a continuous piece of memory containing the
1469/// indices, either as Value* or uint64_t.
1470///
1471/// A null type is returned if the indices are invalid for the specified
1472/// pointer type.
1473///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001474template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001475static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001476 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001477 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001478 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001479
Chris Lattner6090a422009-03-09 04:46:40 +00001480 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001481 // it cannot be 'stepped over'.
1482 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001483 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001484
Dan Gohman1ecaf452008-05-31 00:58:22 +00001485 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001486 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001487 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001488 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001489 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001490 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001491 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001492 }
Craig Topperc6207612014-04-09 06:08:46 +00001493 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001494}
1495
David Blaikied288fb82015-03-30 21:41:43 +00001496Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1497 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001498}
1499
David Blaikied288fb82015-03-30 21:41:43 +00001500Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001501 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001502 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001503}
1504
David Blaikied288fb82015-03-30 21:41:43 +00001505Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1506 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001507}
1508
Chris Lattner45f15572007-04-14 00:12:57 +00001509/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1510/// zeros. If so, the result pointer and the first operand have the same
1511/// value, just potentially different types.
1512bool GetElementPtrInst::hasAllZeroIndices() const {
1513 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1514 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1515 if (!CI->isZero()) return false;
1516 } else {
1517 return false;
1518 }
1519 }
1520 return true;
1521}
1522
Chris Lattner27058292007-04-27 20:35:56 +00001523/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1524/// constant integers. If so, the result pointer and the first operand have
1525/// a constant offset between them.
1526bool GetElementPtrInst::hasAllConstantIndices() const {
1527 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1528 if (!isa<ConstantInt>(getOperand(i)))
1529 return false;
1530 }
1531 return true;
1532}
1533
Dan Gohman1b849082009-09-07 23:54:19 +00001534void GetElementPtrInst::setIsInBounds(bool B) {
1535 cast<GEPOperator>(this)->setIsInBounds(B);
1536}
Chris Lattner45f15572007-04-14 00:12:57 +00001537
Nick Lewycky28a5f252009-09-27 21:33:04 +00001538bool GetElementPtrInst::isInBounds() const {
1539 return cast<GEPOperator>(this)->isInBounds();
1540}
1541
Chandler Carruth1e140532012-12-11 10:29:10 +00001542bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1543 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001544 // Delegate to the generic GEPOperator implementation.
1545 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001546}
1547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001548//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001549// ExtractElementInst Implementation
1550//===----------------------------------------------------------------------===//
1551
1552ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001553 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001554 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001555 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001556 ExtractElement,
1557 OperandTraits<ExtractElementInst>::op_begin(this),
1558 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001559 assert(isValidOperands(Val, Index) &&
1560 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001561 Op<0>() = Val;
1562 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001563 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001564}
1565
1566ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001567 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001568 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001569 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001570 ExtractElement,
1571 OperandTraits<ExtractElementInst>::op_begin(this),
1572 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001573 assert(isValidOperands(Val, Index) &&
1574 "Invalid extractelement instruction operands!");
1575
Gabor Greif2d3024d2008-05-26 21:33:52 +00001576 Op<0>() = Val;
1577 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001578 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001579}
1580
Chris Lattner54865b32006-04-08 04:05:48 +00001581bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001582 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001583 return false;
1584 return true;
1585}
1586
Robert Bocchino23004482006-01-10 19:05:34 +00001587//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001588// InsertElementInst Implementation
1589//===----------------------------------------------------------------------===//
1590
Chris Lattner54865b32006-04-08 04:05:48 +00001591InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001592 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001593 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001594 : Instruction(Vec->getType(), InsertElement,
1595 OperandTraits<InsertElementInst>::op_begin(this),
1596 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001597 assert(isValidOperands(Vec, Elt, Index) &&
1598 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001599 Op<0>() = Vec;
1600 Op<1>() = Elt;
1601 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001602 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001603}
1604
Chris Lattner54865b32006-04-08 04:05:48 +00001605InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001606 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001607 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001608 : Instruction(Vec->getType(), InsertElement,
1609 OperandTraits<InsertElementInst>::op_begin(this),
1610 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001611 assert(isValidOperands(Vec, Elt, Index) &&
1612 "Invalid insertelement instruction operands!");
1613
Gabor Greif2d3024d2008-05-26 21:33:52 +00001614 Op<0>() = Vec;
1615 Op<1>() = Elt;
1616 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001617 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001618}
1619
Fangrui Songf78650a2018-07-30 19:41:25 +00001620bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001621 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001622 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001623 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001624
Reid Spencerd84d35b2007-02-15 02:26:10 +00001625 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001626 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001627
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001628 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001629 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001630 return true;
1631}
1632
Robert Bocchinoca27f032006-01-17 20:07:22 +00001633//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001634// ShuffleVectorInst Implementation
1635//===----------------------------------------------------------------------===//
1636
1637ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001638 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001639 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001640: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001641 cast<VectorType>(Mask->getType())->getNumElements()),
1642 ShuffleVector,
1643 OperandTraits<ShuffleVectorInst>::op_begin(this),
1644 OperandTraits<ShuffleVectorInst>::operands(this),
1645 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001646 assert(isValidOperands(V1, V2, Mask) &&
1647 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001648 Op<0>() = V1;
1649 Op<1>() = V2;
1650 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001651 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001652}
1653
1654ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001655 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001656 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001657: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1658 cast<VectorType>(Mask->getType())->getNumElements()),
1659 ShuffleVector,
1660 OperandTraits<ShuffleVectorInst>::op_begin(this),
1661 OperandTraits<ShuffleVectorInst>::operands(this),
1662 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001663 assert(isValidOperands(V1, V2, Mask) &&
1664 "Invalid shuffle vector instruction operands!");
1665
Gabor Greif2d3024d2008-05-26 21:33:52 +00001666 Op<0>() = V1;
1667 Op<1>() = V2;
1668 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001669 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001670}
1671
Mon P Wang25f01062008-11-10 04:46:22 +00001672bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001673 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001674 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001675 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001676 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001677
Chris Lattner1dcb6542012-01-25 23:49:49 +00001678 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001679 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001680 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001681 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001682
1683 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001684 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1685 return true;
1686
Sanjay Patel8bd52282017-04-19 16:22:19 +00001687 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001688 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001689 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001690 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001691 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001692 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001693 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001694 return false;
1695 }
1696 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001697 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001698 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001699
Sanjay Patel8bd52282017-04-19 16:22:19 +00001700 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001701 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1702 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1703 if (CDS->getElementAsInteger(i) >= V1Size*2)
1704 return false;
1705 return true;
1706 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001707
Chris Lattner1dcb6542012-01-25 23:49:49 +00001708 // The bitcode reader can create a place holder for a forward reference
1709 // used as the shuffle mask. When this occurs, the shuffle mask will
1710 // fall into this case and fail. To avoid this error, do this bit of
1711 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001712 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001713 if (CE->getOpcode() == Instruction::UserOp1)
1714 return true;
1715
1716 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001717}
1718
Sanjay Patel2ca33602018-06-19 18:44:00 +00001719int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001720 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001721 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001722 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001723 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001724 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001725 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001726 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001727}
1728
Sanjay Patel2ca33602018-06-19 18:44:00 +00001729void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001730 SmallVectorImpl<int> &Result) {
1731 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001732
Sanjay Patel8bd52282017-04-19 16:22:19 +00001733 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001734 for (unsigned i = 0; i != NumElts; ++i)
1735 Result.push_back(CDS->getElementAsInteger(i));
1736 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001737 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001738 for (unsigned i = 0; i != NumElts; ++i) {
1739 Constant *C = Mask->getAggregateElement(i);
1740 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001741 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001742 }
1743}
1744
Sanjay Patelac619a02018-08-30 15:05:38 +00001745static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001746 assert(!Mask.empty() && "Shuffle mask must contain elements");
1747 bool UsesLHS = false;
1748 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001749 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001750 if (Mask[i] == -1)
1751 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001752 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001753 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001754 UsesLHS |= (Mask[i] < NumOpElts);
1755 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001756 if (UsesLHS && UsesRHS)
1757 return false;
1758 }
1759 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1760 return true;
1761}
1762
Sanjay Patelac619a02018-08-30 15:05:38 +00001763bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1764 // We don't have vector operand size information, so assume operands are the
1765 // same size as the mask.
1766 return isSingleSourceMaskImpl(Mask, Mask.size());
1767}
1768
1769static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1770 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001771 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001772 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001773 if (Mask[i] == -1)
1774 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001775 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001776 return false;
1777 }
1778 return true;
1779}
1780
Sanjay Patelac619a02018-08-30 15:05:38 +00001781bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1782 // We don't have vector operand size information, so assume operands are the
1783 // same size as the mask.
1784 return isIdentityMaskImpl(Mask, Mask.size());
1785}
1786
Sanjay Patel2ca33602018-06-19 18:44:00 +00001787bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1788 if (!isSingleSourceMask(Mask))
1789 return false;
1790 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1791 if (Mask[i] == -1)
1792 continue;
1793 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1794 return false;
1795 }
1796 return true;
1797}
1798
1799bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1800 if (!isSingleSourceMask(Mask))
1801 return false;
1802 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1803 if (Mask[i] == -1)
1804 continue;
1805 if (Mask[i] != 0 && Mask[i] != NumElts)
1806 return false;
1807 }
1808 return true;
1809}
1810
1811bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1812 // Select is differentiated from identity. It requires using both sources.
1813 if (isSingleSourceMask(Mask))
1814 return false;
1815 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1816 if (Mask[i] == -1)
1817 continue;
1818 if (Mask[i] != i && Mask[i] != (NumElts + i))
1819 return false;
1820 }
1821 return true;
1822}
1823
1824bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1825 // Example masks that will return true:
1826 // v1 = <a, b, c, d>
1827 // v2 = <e, f, g, h>
1828 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1829 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1830
1831 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1832 int NumElts = Mask.size();
1833 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1834 return false;
1835
1836 // 2. The first element of the mask must be either a 0 or a 1.
1837 if (Mask[0] != 0 && Mask[0] != 1)
1838 return false;
1839
1840 // 3. The difference between the first 2 elements must be equal to the
1841 // number of elements in the mask.
1842 if ((Mask[1] - Mask[0]) != NumElts)
1843 return false;
1844
1845 // 4. The difference between consecutive even-numbered and odd-numbered
1846 // elements must be equal to 2.
1847 for (int i = 2; i < NumElts; ++i) {
1848 int MaskEltVal = Mask[i];
1849 if (MaskEltVal == -1)
1850 return false;
1851 int MaskEltPrevVal = Mask[i - 2];
1852 if (MaskEltVal - MaskEltPrevVal != 2)
1853 return false;
1854 }
1855 return true;
1856}
1857
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001858bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
1859 int NumSrcElts, int &Index) {
1860 // Must extract from a single source.
1861 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
1862 return false;
1863
1864 // Must be smaller (else this is an Identity shuffle).
Fangrui Song4f2e66c2018-11-09 16:45:37 +00001865 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001866 return false;
1867
1868 // Find start of extraction, accounting that we may start with an UNDEF.
1869 int SubIndex = -1;
1870 for (int i = 0, e = Mask.size(); i != e; ++i) {
1871 int M = Mask[i];
1872 if (M < 0)
1873 continue;
1874 int Offset = (M % NumSrcElts) - i;
1875 if (0 <= SubIndex && SubIndex != Offset)
1876 return false;
1877 SubIndex = Offset;
1878 }
1879
1880 if (0 <= SubIndex) {
1881 Index = SubIndex;
1882 return true;
1883 }
1884 return false;
1885}
1886
Sanjay Patelac619a02018-08-30 15:05:38 +00001887bool ShuffleVectorInst::isIdentityWithPadding() const {
1888 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1889 int NumMaskElts = getType()->getVectorNumElements();
1890 if (NumMaskElts <= NumOpElts)
1891 return false;
1892
1893 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00001894 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00001895 if (!isIdentityMaskImpl(Mask, NumOpElts))
1896 return false;
1897
1898 // All extending must be with undef elements.
1899 for (int i = NumOpElts; i < NumMaskElts; ++i)
1900 if (Mask[i] != -1)
1901 return false;
1902
1903 return true;
1904}
1905
1906bool ShuffleVectorInst::isIdentityWithExtract() const {
1907 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1908 int NumMaskElts = getType()->getVectorNumElements();
1909 if (NumMaskElts >= NumOpElts)
1910 return false;
1911
1912 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
1913}
Sanjay Patel2ca33602018-06-19 18:44:00 +00001914
Sanjay Patelfd4976b2018-09-20 15:21:52 +00001915bool ShuffleVectorInst::isConcat() const {
1916 // Vector concatenation is differentiated from identity with padding.
1917 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
1918 return false;
1919
1920 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1921 int NumMaskElts = getType()->getVectorNumElements();
1922 if (NumMaskElts != NumOpElts * 2)
1923 return false;
1924
1925 // Use the mask length rather than the operands' vector lengths here. We
1926 // already know that the shuffle returns a vector twice as long as the inputs,
1927 // and neither of the inputs are undef vectors. If the mask picks consecutive
1928 // elements from both inputs, then this is a concatenation of the inputs.
1929 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
1930}
1931
Dan Gohman12fce772008-05-15 19:50:34 +00001932//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001933// InsertValueInst Class
1934//===----------------------------------------------------------------------===//
1935
Fangrui Songf78650a2018-07-30 19:41:25 +00001936void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00001937 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001938 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001939
1940 // There's no fundamental reason why we require at least one index
1941 // (other than weirdness with &*IdxBegin being invalid; see
1942 // getelementptr's init routine for example). But there's no
1943 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001944 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001945
1946 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001947 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001948 Op<0>() = Agg;
1949 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001950
Jay Foad57aa6362011-07-13 10:26:04 +00001951 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001952 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001953}
1954
1955InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001956 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001957 OperandTraits<InsertValueInst>::op_begin(this), 2),
1958 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001959 Op<0>() = IVI.getOperand(0);
1960 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001961 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001962}
1963
1964//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001965// ExtractValueInst Class
1966//===----------------------------------------------------------------------===//
1967
Jay Foad57aa6362011-07-13 10:26:04 +00001968void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001969 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001970
Jay Foad57aa6362011-07-13 10:26:04 +00001971 // There's no fundamental reason why we require at least one index.
1972 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001973 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001974
Jay Foad57aa6362011-07-13 10:26:04 +00001975 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001976 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001977}
1978
1979ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001980 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001981 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001982 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001983}
1984
Dan Gohman12fce772008-05-15 19:50:34 +00001985// getIndexedType - Returns the type of the element that would be extracted
1986// with an extractvalue instruction with the specified parameters.
1987//
1988// A null type is returned if the indices are invalid for the specified
1989// pointer type.
1990//
Chris Lattner229907c2011-07-18 04:54:35 +00001991Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001992 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001993 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001994 // We can't use CompositeType::indexValid(Index) here.
1995 // indexValid() always returns true for arrays because getelementptr allows
1996 // out-of-bounds indices. Since we don't allow those for extractvalue and
1997 // insertvalue we need to check array indexing manually.
1998 // Since the only other types we can index into are struct types it's just
1999 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002000 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002001 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002002 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002003 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002004 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002005 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002006 } else {
2007 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002008 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002009 }
2010
2011 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002012 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002013 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002014}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002015
2016//===----------------------------------------------------------------------===//
Cameron McInallycbde0d92018-11-13 18:15:47 +00002017// UnaryOperator Class
2018//===----------------------------------------------------------------------===//
2019
2020UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2021 Type *Ty, const Twine &Name,
2022 Instruction *InsertBefore)
2023 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2024 Op<0>() = S;
2025 setName(Name);
2026 AssertOK();
2027}
2028
2029UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2030 Type *Ty, const Twine &Name,
2031 BasicBlock *InsertAtEnd)
2032 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2033 Op<0>() = S;
2034 setName(Name);
2035 AssertOK();
2036}
2037
2038UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2039 const Twine &Name,
2040 Instruction *InsertBefore) {
2041 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2042}
2043
2044UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2045 const Twine &Name,
2046 BasicBlock *InsertAtEnd) {
2047 UnaryOperator *Res = Create(Op, S, Name);
2048 InsertAtEnd->getInstList().push_back(Res);
2049 return Res;
2050}
2051
2052void UnaryOperator::AssertOK() {
2053 Value *LHS = getOperand(0);
2054 (void)LHS; // Silence warnings.
2055#ifndef NDEBUG
2056 switch (getOpcode()) {
2057 case FNeg:
2058 assert(getType() == LHS->getType() &&
2059 "Unary operation should return same type as operand!");
2060 assert(getType()->isFPOrFPVectorTy() &&
2061 "Tried to create a floating-point operation on a "
2062 "non-floating-point type!");
2063 break;
2064 default: llvm_unreachable("Invalid opcode provided");
2065 }
2066#endif
2067}
2068
2069//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002070// BinaryOperator Class
2071//===----------------------------------------------------------------------===//
2072
Chris Lattner2195fc42007-02-24 00:55:48 +00002073BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002074 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002075 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002076 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002077 OperandTraits<BinaryOperator>::op_begin(this),
2078 OperandTraits<BinaryOperator>::operands(this),
2079 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002080 Op<0>() = S1;
2081 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002082 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002083 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002084}
2085
Fangrui Songf78650a2018-07-30 19:41:25 +00002086BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002087 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002088 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002089 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002090 OperandTraits<BinaryOperator>::op_begin(this),
2091 OperandTraits<BinaryOperator>::operands(this),
2092 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002093 Op<0>() = S1;
2094 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002095 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002096 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002097}
2098
Craig Topper700892f2017-06-26 07:15:59 +00002099void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002100 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002101 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002102 assert(LHS->getType() == RHS->getType() &&
2103 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00002105 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002106 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002107 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002108 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002109 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002110 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002111 "Tried to create an integer operation on a non-integer type!");
2112 break;
2113 case FAdd: case FSub:
2114 case FMul:
2115 assert(getType() == LHS->getType() &&
2116 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002117 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002118 "Tried to create a floating-point operation on a "
2119 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002120 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002121 case UDiv:
2122 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002123 assert(getType() == LHS->getType() &&
2124 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002125 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002126 "Incorrect operand type (not integer) for S/UDIV");
2127 break;
2128 case FDiv:
2129 assert(getType() == LHS->getType() &&
2130 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002131 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002132 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002133 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002134 case URem:
2135 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002136 assert(getType() == LHS->getType() &&
2137 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002138 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002139 "Incorrect operand type (not integer) for S/UREM");
2140 break;
2141 case FRem:
2142 assert(getType() == LHS->getType() &&
2143 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002144 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002145 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002146 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002147 case Shl:
2148 case LShr:
2149 case AShr:
2150 assert(getType() == LHS->getType() &&
2151 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002152 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002153 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002154 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002155 case And: case Or:
2156 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002157 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002158 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002159 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002160 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002161 break;
Craig Topper700892f2017-06-26 07:15:59 +00002162 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002163 }
2164#endif
2165}
2166
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002168 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169 Instruction *InsertBefore) {
2170 assert(S1->getType() == S2->getType() &&
2171 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002172 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173}
2174
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002176 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002178 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002179 InsertAtEnd->getInstList().push_back(Res);
2180 return Res;
2181}
2182
Dan Gohman5476cfd2009-08-12 16:23:25 +00002183BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002184 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002185 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002186 return new BinaryOperator(Instruction::Sub,
2187 zero, Op,
2188 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002189}
2190
Dan Gohman5476cfd2009-08-12 16:23:25 +00002191BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002192 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002193 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002194 return new BinaryOperator(Instruction::Sub,
2195 zero, Op,
2196 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002197}
2198
Dan Gohman4ab44202009-12-18 02:58:50 +00002199BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2200 Instruction *InsertBefore) {
2201 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2202 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2203}
2204
2205BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2206 BasicBlock *InsertAtEnd) {
2207 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2208 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2209}
2210
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002211BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2212 Instruction *InsertBefore) {
2213 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2214 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2215}
2216
2217BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2218 BasicBlock *InsertAtEnd) {
2219 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2220 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2221}
2222
Dan Gohman5476cfd2009-08-12 16:23:25 +00002223BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002224 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002225 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002226 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002227 Op->getType(), Name, InsertBefore);
2228}
2229
Dan Gohman5476cfd2009-08-12 16:23:25 +00002230BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002231 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002232 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002233 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002234 Op->getType(), Name, InsertAtEnd);
2235}
2236
Dan Gohman5476cfd2009-08-12 16:23:25 +00002237BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002238 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002239 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002240 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002241 Op->getType(), Name, InsertBefore);
2242}
2243
Dan Gohman5476cfd2009-08-12 16:23:25 +00002244BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002245 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002246 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002247 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002248 Op->getType(), Name, InsertAtEnd);
2249}
2250
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002251// Exchange the two operands to this instruction. This instruction is safe to
2252// use on any binary instruction and does not modify the semantics of the
2253// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2254// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002255bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002256 if (!isCommutative())
2257 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002258 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002259 return false;
2260}
2261
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002262//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002263// FPMathOperator Class
2264//===----------------------------------------------------------------------===//
2265
Duncan Sands05f4df82012-04-16 16:28:59 +00002266float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002267 const MDNode *MD =
2268 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002269 if (!MD)
2270 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002271 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002272 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002273}
2274
Duncan Sands05f4df82012-04-16 16:28:59 +00002275//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002276// CastInst Class
2277//===----------------------------------------------------------------------===//
2278
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279// Just determine if this cast only deals with integral->integral conversion.
2280bool CastInst::isIntegerCast() const {
2281 switch (getOpcode()) {
2282 default: return false;
2283 case Instruction::ZExt:
2284 case Instruction::SExt:
2285 case Instruction::Trunc:
2286 return true;
2287 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002288 return getOperand(0)->getType()->isIntegerTy() &&
2289 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002291}
2292
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293bool CastInst::isLosslessCast() const {
2294 // Only BitCast can be lossless, exit fast if we're not BitCast
2295 if (getOpcode() != Instruction::BitCast)
2296 return false;
2297
2298 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002299 Type *SrcTy = getOperand(0)->getType();
2300 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 if (SrcTy == DstTy)
2302 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002303
Reid Spencer8d9336d2006-12-31 05:26:44 +00002304 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002305 if (SrcTy->isPointerTy())
2306 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 return false; // Other types have no identity values
2308}
2309
2310/// This function determines if the CastInst does not require any bits to be
2311/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002312/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002314/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002315/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002316/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002317/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002318bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002319 Type *SrcTy,
2320 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002321 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002322 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002323 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 case Instruction::Trunc:
2325 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002326 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002327 case Instruction::FPTrunc:
2328 case Instruction::FPExt:
2329 case Instruction::UIToFP:
2330 case Instruction::SIToFP:
2331 case Instruction::FPToUI:
2332 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002333 case Instruction::AddrSpaceCast:
2334 // TODO: Target informations may give a more accurate answer here.
2335 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 case Instruction::BitCast:
2337 return true; // BitCast never modifies bits.
2338 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002339 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002340 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002342 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002343 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 }
2345}
2346
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002347bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002348 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002349}
2350
2351/// This function determines if a pair of casts can be eliminated and what
2352/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353/// instructions like this:
2354/// * %F = firstOpcode SrcTy %x to MidTy
2355/// * %S = secondOpcode MidTy %F to DstTy
2356/// The function returns a resultOpcode so these two casts can be replaced with:
2357/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002358/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359unsigned CastInst::isEliminableCastPair(
2360 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002361 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2362 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 // Define the 144 possibilities for these two cast instructions. The values
2364 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002365 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002366 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 // the following cast properties:
2368 //
2369 // Size Compare Source Destination
2370 // Operator Src ? Size Type Sign Type Sign
2371 // -------- ------------ ------------------- ---------------------
2372 // TRUNC > Integer Any Integral Any
2373 // ZEXT < Integral Unsigned Integer Any
2374 // SEXT < Integral Signed Integer Any
2375 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002376 // FPTOSI n/a FloatPt n/a Integral Signed
2377 // UITOFP n/a Integral Unsigned FloatPt n/a
2378 // SITOFP n/a Integral Signed FloatPt n/a
2379 // FPTRUNC > FloatPt n/a FloatPt n/a
2380 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 // PTRTOINT n/a Pointer n/a Integral Unsigned
2382 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002383 // BITCAST = FirstClass n/a FirstClass n/a
2384 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002385 //
2386 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002387 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2388 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002389 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002390 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002391 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002392 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002393 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2395 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002396 // T F F U S F F P I B A -+
2397 // R Z S P P I I T P 2 N T S |
2398 // U E E 2 2 2 2 R E I T C C +- secondOp
2399 // N X X U S F F N X N 2 V V |
2400 // C T T I I P P C T T P T T -+
2401 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002402 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2404 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2405 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2406 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2407 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002408 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002409 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002410 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2411 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2412 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2413 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002415
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002416 // TODO: This logic could be encoded into the table above and handled in the
2417 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002418 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002419 // merging. However, any pair of bitcasts are allowed.
2420 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2421 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2422 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002423
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002424 // Check if any of the casts convert scalars <-> vectors.
2425 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2426 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2427 if (!AreBothBitcasts)
2428 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429
2430 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2431 [secondOp-Instruction::CastOpsBegin];
2432 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002433 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002434 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002436 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002437 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002439 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002440 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002442 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002443 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002444 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002445 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002446 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 return firstOp;
2448 return 0;
2449 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002450 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002451 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002452 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 return firstOp;
2454 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002455 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002456 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002457 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002458 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 return secondOp;
2460 return 0;
2461 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002463 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002464 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 return secondOp;
2466 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002467 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // Cannot simplify if address spaces are different!
2469 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2470 return 0;
2471
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002472 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002473 // We can still fold this without knowing the actual sizes as long we
2474 // know that the intermediate pointer is the largest possible
2475 // pointer size.
2476 // FIXME: Is this always true?
2477 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002478 return Instruction::BitCast;
2479
2480 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002481 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002482 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002483 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 if (MidSize >= PtrSize)
2485 return Instruction::BitCast;
2486 return 0;
2487 }
2488 case 8: {
2489 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2490 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2491 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002492 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2493 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 if (SrcSize == DstSize)
2495 return Instruction::BitCast;
2496 else if (SrcSize < DstSize)
2497 return firstOp;
2498 return secondOp;
2499 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002500 case 9:
2501 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002503 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002505 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002506 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002507 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002508 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2509 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 if (SrcSize <= PtrSize && SrcSize == DstSize)
2511 return Instruction::BitCast;
2512 return 0;
2513 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002514 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002515 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2516 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2517 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2518 return Instruction::AddrSpaceCast;
2519 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002520 case 13:
2521 // FIXME: this state can be merged with (1), but the following assert
2522 // is useful to check the correcteness of the sequence due to semantic
2523 // change of bitcast.
2524 assert(
2525 SrcTy->isPtrOrPtrVectorTy() &&
2526 MidTy->isPtrOrPtrVectorTy() &&
2527 DstTy->isPtrOrPtrVectorTy() &&
2528 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2529 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2530 "Illegal addrspacecast, bitcast sequence!");
2531 // Allowed, use first cast's opcode
2532 return firstOp;
2533 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002534 // bitcast, addrspacecast -> addrspacecast if the element type of
2535 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002536 if (SrcTy->getScalarType()->getPointerElementType() ==
2537 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002538 return Instruction::AddrSpaceCast;
2539 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 case 15:
2541 // FIXME: this state can be merged with (1), but the following assert
2542 // is useful to check the correcteness of the sequence due to semantic
2543 // change of bitcast.
2544 assert(
2545 SrcTy->isIntOrIntVectorTy() &&
2546 MidTy->isPtrOrPtrVectorTy() &&
2547 DstTy->isPtrOrPtrVectorTy() &&
2548 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2549 "Illegal inttoptr, bitcast sequence!");
2550 // Allowed, use first cast's opcode
2551 return firstOp;
2552 case 16:
2553 // FIXME: this state can be merged with (2), but the following assert
2554 // is useful to check the correcteness of the sequence due to semantic
2555 // change of bitcast.
2556 assert(
2557 SrcTy->isPtrOrPtrVectorTy() &&
2558 MidTy->isPtrOrPtrVectorTy() &&
2559 DstTy->isIntOrIntVectorTy() &&
2560 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2561 "Illegal bitcast, ptrtoint sequence!");
2562 // Allowed, use second cast's opcode
2563 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002564 case 17:
2565 // (sitofp (zext x)) -> (uitofp x)
2566 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002567 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002568 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002570 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 default:
Craig Topperc514b542012-02-05 22:14:15 +00002572 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574}
2575
Fangrui Songf78650a2018-07-30 19:41:25 +00002576CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002578 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 // Construct and return the appropriate CastInst subclass
2580 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002581 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2582 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2583 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2584 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2585 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2586 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2587 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2588 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2589 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2590 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2591 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2592 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2593 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2594 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596}
2597
Chris Lattner229907c2011-07-18 04:54:35 +00002598CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002599 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002600 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601 // Construct and return the appropriate CastInst subclass
2602 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002603 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2604 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2605 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2606 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2607 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2608 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2609 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2610 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2611 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2612 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2613 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2614 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2615 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2616 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
Fangrui Songf78650a2018-07-30 19:41:25 +00002620CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002622 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002623 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002624 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2625 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002626}
2627
Fangrui Songf78650a2018-07-30 19:41:25 +00002628CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002629 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002630 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002631 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002632 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2633 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002634}
2635
Fangrui Songf78650a2018-07-30 19:41:25 +00002636CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002638 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002640 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2641 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002642}
2643
Fangrui Songf78650a2018-07-30 19:41:25 +00002644CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002646 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002647 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002648 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2649 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002650}
2651
Chris Lattner229907c2011-07-18 04:54:35 +00002652CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002653 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002654 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002655 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002656 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2657 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002658}
2659
Chris Lattner229907c2011-07-18 04:54:35 +00002660CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002661 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002662 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002663 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002664 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2665 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002666}
2667
Chris Lattner229907c2011-07-18 04:54:35 +00002668CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002670 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002671 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2672 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2673 "Invalid cast");
2674 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002675 assert((!Ty->isVectorTy() ||
2676 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002677 "Invalid cast");
2678
Matt Arsenault065ced92013-07-31 00:17:33 +00002679 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002680 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002681
Matt Arsenault740980e2014-07-14 17:24:35 +00002682 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002683}
2684
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002685/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002686CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2687 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002688 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002689 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2690 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002691 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002692 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002693 assert((!Ty->isVectorTy() ||
2694 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002695 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002696
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002697 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002698 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002699
Matt Arsenault740980e2014-07-14 17:24:35 +00002700 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2701}
2702
2703CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2704 Value *S, Type *Ty,
2705 const Twine &Name,
2706 BasicBlock *InsertAtEnd) {
2707 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2708 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2709
2710 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2711 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2712
2713 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2714}
2715
2716CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2717 Value *S, Type *Ty,
2718 const Twine &Name,
2719 Instruction *InsertBefore) {
2720 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2721 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2722
2723 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002724 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2725
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002726 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002727}
2728
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002729CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2730 const Twine &Name,
2731 Instruction *InsertBefore) {
2732 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2733 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2734 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2735 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2736
2737 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2738}
2739
Matt Arsenault740980e2014-07-14 17:24:35 +00002740CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002741 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002742 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002743 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002744 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002745 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2746 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002747 Instruction::CastOps opcode =
2748 (SrcBits == DstBits ? Instruction::BitCast :
2749 (SrcBits > DstBits ? Instruction::Trunc :
2750 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002751 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002752}
2753
Fangrui Songf78650a2018-07-30 19:41:25 +00002754CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002755 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002756 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002757 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002758 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002759 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2760 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002761 Instruction::CastOps opcode =
2762 (SrcBits == DstBits ? Instruction::BitCast :
2763 (SrcBits > DstBits ? Instruction::Trunc :
2764 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002765 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002766}
2767
Fangrui Songf78650a2018-07-30 19:41:25 +00002768CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2769 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002770 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002771 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002772 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002773 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2774 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002775 Instruction::CastOps opcode =
2776 (SrcBits == DstBits ? Instruction::BitCast :
2777 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002778 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002779}
2780
Fangrui Songf78650a2018-07-30 19:41:25 +00002781CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2782 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002783 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002784 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002785 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002786 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2787 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002788 Instruction::CastOps opcode =
2789 (SrcBits == DstBits ? Instruction::BitCast :
2790 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002791 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002792}
2793
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002794// Check whether it is valid to call getCastOpcode for these types.
2795// This routine must be kept in sync with getCastOpcode.
2796bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2797 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2798 return false;
2799
2800 if (SrcTy == DestTy)
2801 return true;
2802
2803 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2804 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2805 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2806 // An element by element cast. Valid if casting the elements is valid.
2807 SrcTy = SrcVecTy->getElementType();
2808 DestTy = DestVecTy->getElementType();
2809 }
2810
2811 // Get the bit sizes, we'll need these
2812 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2813 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2814
2815 // Run through the possibilities ...
2816 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002817 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002818 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002819 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002820 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002821 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002822 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002823 // Casting from something else
2824 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002825 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002826 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2827 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002828 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002829 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002830 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002831 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002832 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002833 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002834 return false;
2835 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002836 if (DestTy->isVectorTy()) // Casting to vector
2837 return DestBits == SrcBits;
2838 if (DestTy->isPointerTy()) { // Casting to pointer
2839 if (SrcTy->isPointerTy()) // Casting from pointer
2840 return true;
2841 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002842 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002843 if (DestTy->isX86_MMXTy()) {
2844 if (SrcTy->isVectorTy())
2845 return DestBits == SrcBits; // 64-bit vector to MMX
2846 return false;
2847 } // Casting to something else
2848 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002849}
2850
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002851bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2852 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2853 return false;
2854
2855 if (SrcTy == DestTy)
2856 return true;
2857
2858 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2859 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2860 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2861 // An element by element cast. Valid if casting the elements is valid.
2862 SrcTy = SrcVecTy->getElementType();
2863 DestTy = DestVecTy->getElementType();
2864 }
2865 }
2866 }
2867
2868 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2869 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2870 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2871 }
2872 }
2873
2874 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2875 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2876
2877 // Could still have vectors of pointers if the number of elements doesn't
2878 // match
2879 if (SrcBits == 0 || DestBits == 0)
2880 return false;
2881
2882 if (SrcBits != DestBits)
2883 return false;
2884
2885 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2886 return false;
2887
2888 return true;
2889}
2890
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002891bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002892 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002893 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002894 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2895 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002896 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2897 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002898 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2899 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002900 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2901 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002902
2903 return isBitCastable(SrcTy, DestTy);
2904}
2905
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002906// Provide a way to get a "cast" where the cast opcode is inferred from the
2907// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002908// logic in the castIsValid function below. This axiom should hold:
2909// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2910// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002912// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002914CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002915 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2916 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917
Duncan Sands55e50902008-01-06 10:12:28 +00002918 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2919 "Only first class types are castable!");
2920
Duncan Sandsa8514532011-05-18 07:13:41 +00002921 if (SrcTy == DestTy)
2922 return BitCast;
2923
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002924 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002925 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2926 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002927 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2928 // An element by element cast. Find the appropriate opcode based on the
2929 // element types.
2930 SrcTy = SrcVecTy->getElementType();
2931 DestTy = DestVecTy->getElementType();
2932 }
2933
2934 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002935 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2936 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002937
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002939 if (DestTy->isIntegerTy()) { // Casting to integral
2940 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 if (DestBits < SrcBits)
2942 return Trunc; // int -> smaller int
2943 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002944 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 return SExt; // signed -> SEXT
2946 else
2947 return ZExt; // unsigned -> ZEXT
2948 } else {
2949 return BitCast; // Same size, No-op cast
2950 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002951 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00002952 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953 return FPToSI; // FP -> sint
2954 else
Fangrui Songf78650a2018-07-30 19:41:25 +00002955 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002956 } else if (SrcTy->isVectorTy()) {
2957 assert(DestBits == SrcBits &&
2958 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959 return BitCast; // Same size, no-op cast
2960 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002961 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002962 "Casting from a value that is not first-class type");
2963 return PtrToInt; // ptr -> int
2964 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002965 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2966 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002967 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002968 return SIToFP; // sint -> FP
2969 else
2970 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002971 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002972 if (DestBits < SrcBits) {
2973 return FPTrunc; // FP -> smaller FP
2974 } else if (DestBits > SrcBits) {
2975 return FPExt; // FP -> larger FP
2976 } else {
2977 return BitCast; // same size, no-op cast
2978 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002979 } else if (SrcTy->isVectorTy()) {
2980 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002981 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002982 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002984 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002985 } else if (DestTy->isVectorTy()) {
2986 assert(DestBits == SrcBits &&
2987 "Illegal cast to vector (wrong type or size)");
2988 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002989 } else if (DestTy->isPointerTy()) {
2990 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002991 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2992 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002993 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002994 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002997 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002998 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002999 if (SrcTy->isVectorTy()) {
3000 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003001 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003002 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003003 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003004 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003005 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006}
3007
3008//===----------------------------------------------------------------------===//
3009// CastInst SubClass Constructors
3010//===----------------------------------------------------------------------===//
3011
3012/// Check that the construction parameters for a CastInst are correct. This
3013/// could be broken out into the separate constructors but it is useful to have
3014/// it in one place and to eliminate the redundant code for getting the sizes
3015/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00003016bool
Chris Lattner229907c2011-07-18 04:54:35 +00003017CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003018 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003019 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003020
Chris Lattner37bc78a2010-01-26 21:51:43 +00003021 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3022 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023 return false;
3024
3025 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003026 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3027 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003028
Duncan Sands7f646562011-05-18 09:21:57 +00003029 // If these are vector types, get the lengths of the vectors (using zero for
3030 // scalar types means that checking that vector lengths match also checks that
3031 // scalars are not being converted to vectors or vectors to scalars).
3032 unsigned SrcLength = SrcTy->isVectorTy() ?
3033 cast<VectorType>(SrcTy)->getNumElements() : 0;
3034 unsigned DstLength = DstTy->isVectorTy() ?
3035 cast<VectorType>(DstTy)->getNumElements() : 0;
3036
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003037 // Switch on the opcode provided
3038 switch (op) {
3039 default: return false; // This is an input error
3040 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003041 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3042 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003044 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3045 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00003046 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003047 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3048 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003050 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3051 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003052 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003053 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3054 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003057 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3058 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003061 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3062 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003063 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003064 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003065 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003066 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3067 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3068 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003069 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003070 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003071 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003072 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003073 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3074 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3075 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003076 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003077 case Instruction::BitCast: {
3078 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3079 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3080
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003081 // BitCast implies a no-op cast of type only. No bits change.
3082 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003083 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003084 return false;
3085
Alp Tokerf907b892013-12-05 05:44:44 +00003086 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003087 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003088 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003089 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3090
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003091 // If both are pointers then the address spaces must match.
3092 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3093 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003094
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003095 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003096 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3097 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3098 if (SrcVecTy && DstVecTy)
3099 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3100 if (SrcVecTy)
3101 return SrcVecTy->getNumElements() == 1;
3102 if (DstVecTy)
3103 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003104
3105 return true;
3106 }
3107 case Instruction::AddrSpaceCast: {
3108 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3109 if (!SrcPtrTy)
3110 return false;
3111
3112 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3113 if (!DstPtrTy)
3114 return false;
3115
3116 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3117 return false;
3118
3119 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3120 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3121 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3122
3123 return false;
3124 }
3125
3126 return true;
3127 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128 }
3129}
3130
3131TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003132 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003133) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003134 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135}
3136
3137TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003138 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003139) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003140 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003141}
3142
3143ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003144 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003145) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003147}
3148
3149ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003150 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003151) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003152 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003153}
3154SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003155 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003156) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003157 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003158}
3159
Jeff Cohencc08c832006-12-02 02:22:01 +00003160SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003161 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003162) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003163 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003164}
3165
3166FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003167 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003168) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003169 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003170}
3171
3172FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003173 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003174) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003175 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003176}
3177
3178FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003179 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003180) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003181 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003182}
3183
3184FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003185 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003186) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003187 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003188}
3189
3190UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003191 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003192) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003193 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003194}
3195
3196UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003197 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003198) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003199 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003200}
3201
3202SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003203 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003204) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003205 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003206}
3207
3208SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003209 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003210) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003212}
3213
3214FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003216) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218}
3219
3220FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003221 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003222) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003224}
3225
3226FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003228) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
3232FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003234) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237
3238PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003239 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003240) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003241 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242}
3243
3244PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003246) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
3250IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003252) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003258) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003264) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
3268BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003270) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003273
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003274AddrSpaceCastInst::AddrSpaceCastInst(
3275 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3276) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3278}
3279
3280AddrSpaceCastInst::AddrSpaceCastInst(
3281 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3282) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3284}
3285
Chris Lattnerf16dc002006-09-17 19:29:56 +00003286//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003287// CmpInst Classes
3288//===----------------------------------------------------------------------===//
3289
Craig Topper1c3f2832015-12-15 06:11:33 +00003290CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Pateld1172a02018-11-07 00:00:42 +00003291 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3292 Instruction *FlagsSource)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003293 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003294 OperandTraits<CmpInst>::op_begin(this),
3295 OperandTraits<CmpInst>::operands(this),
3296 InsertBefore) {
Sanjay Pateld1172a02018-11-07 00:00:42 +00003297 Op<0>() = LHS;
3298 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003299 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003300 setName(Name);
Sanjay Pateld1172a02018-11-07 00:00:42 +00003301 if (FlagsSource)
3302 copyIRFlags(FlagsSource);
Reid Spencerd9436b62006-11-20 01:22:35 +00003303}
Gabor Greiff6caff662008-05-10 08:32:32 +00003304
Craig Topper1c3f2832015-12-15 06:11:33 +00003305CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3306 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003307 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003308 OperandTraits<CmpInst>::op_begin(this),
3309 OperandTraits<CmpInst>::operands(this),
3310 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003311 Op<0>() = LHS;
3312 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003313 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003314 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003315}
3316
3317CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003318CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003319 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003320 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321 if (InsertBefore)
3322 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3323 S1, S2, Name);
3324 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003325 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003327 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003328
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329 if (InsertBefore)
3330 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3331 S1, S2, Name);
3332 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003333 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003335}
3336
3337CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003338CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003339 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003340 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003341 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3342 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003343 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003344 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3345 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003346}
3347
3348void CmpInst::swapOperands() {
3349 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3350 IC->swapOperands();
3351 else
3352 cast<FCmpInst>(this)->swapOperands();
3353}
3354
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003355bool CmpInst::isCommutative() const {
3356 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003357 return IC->isCommutative();
3358 return cast<FCmpInst>(this)->isCommutative();
3359}
3360
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003361bool CmpInst::isEquality() const {
3362 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003363 return IC->isEquality();
3364 return cast<FCmpInst>(this)->isEquality();
3365}
3366
Dan Gohman4e724382008-05-31 02:47:54 +00003367CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003368 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003369 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003370 case ICMP_EQ: return ICMP_NE;
3371 case ICMP_NE: return ICMP_EQ;
3372 case ICMP_UGT: return ICMP_ULE;
3373 case ICMP_ULT: return ICMP_UGE;
3374 case ICMP_UGE: return ICMP_ULT;
3375 case ICMP_ULE: return ICMP_UGT;
3376 case ICMP_SGT: return ICMP_SLE;
3377 case ICMP_SLT: return ICMP_SGE;
3378 case ICMP_SGE: return ICMP_SLT;
3379 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003380
Dan Gohman4e724382008-05-31 02:47:54 +00003381 case FCMP_OEQ: return FCMP_UNE;
3382 case FCMP_ONE: return FCMP_UEQ;
3383 case FCMP_OGT: return FCMP_ULE;
3384 case FCMP_OLT: return FCMP_UGE;
3385 case FCMP_OGE: return FCMP_ULT;
3386 case FCMP_OLE: return FCMP_UGT;
3387 case FCMP_UEQ: return FCMP_ONE;
3388 case FCMP_UNE: return FCMP_OEQ;
3389 case FCMP_UGT: return FCMP_OLE;
3390 case FCMP_ULT: return FCMP_OGE;
3391 case FCMP_UGE: return FCMP_OLT;
3392 case FCMP_ULE: return FCMP_OGT;
3393 case FCMP_ORD: return FCMP_UNO;
3394 case FCMP_UNO: return FCMP_ORD;
3395 case FCMP_TRUE: return FCMP_FALSE;
3396 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003397 }
3398}
3399
Tim Northoverde3aea0412016-08-17 20:25:25 +00003400StringRef CmpInst::getPredicateName(Predicate Pred) {
3401 switch (Pred) {
3402 default: return "unknown";
3403 case FCmpInst::FCMP_FALSE: return "false";
3404 case FCmpInst::FCMP_OEQ: return "oeq";
3405 case FCmpInst::FCMP_OGT: return "ogt";
3406 case FCmpInst::FCMP_OGE: return "oge";
3407 case FCmpInst::FCMP_OLT: return "olt";
3408 case FCmpInst::FCMP_OLE: return "ole";
3409 case FCmpInst::FCMP_ONE: return "one";
3410 case FCmpInst::FCMP_ORD: return "ord";
3411 case FCmpInst::FCMP_UNO: return "uno";
3412 case FCmpInst::FCMP_UEQ: return "ueq";
3413 case FCmpInst::FCMP_UGT: return "ugt";
3414 case FCmpInst::FCMP_UGE: return "uge";
3415 case FCmpInst::FCMP_ULT: return "ult";
3416 case FCmpInst::FCMP_ULE: return "ule";
3417 case FCmpInst::FCMP_UNE: return "une";
3418 case FCmpInst::FCMP_TRUE: return "true";
3419 case ICmpInst::ICMP_EQ: return "eq";
3420 case ICmpInst::ICMP_NE: return "ne";
3421 case ICmpInst::ICMP_SGT: return "sgt";
3422 case ICmpInst::ICMP_SGE: return "sge";
3423 case ICmpInst::ICMP_SLT: return "slt";
3424 case ICmpInst::ICMP_SLE: return "sle";
3425 case ICmpInst::ICMP_UGT: return "ugt";
3426 case ICmpInst::ICMP_UGE: return "uge";
3427 case ICmpInst::ICMP_ULT: return "ult";
3428 case ICmpInst::ICMP_ULE: return "ule";
3429 }
3430}
3431
Reid Spencer266e42b2006-12-23 06:05:41 +00003432ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3433 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003434 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003435 case ICMP_EQ: case ICMP_NE:
3436 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003437 return pred;
3438 case ICMP_UGT: return ICMP_SGT;
3439 case ICMP_ULT: return ICMP_SLT;
3440 case ICMP_UGE: return ICMP_SGE;
3441 case ICMP_ULE: return ICMP_SLE;
3442 }
3443}
3444
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003445ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3446 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003447 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003448 case ICMP_EQ: case ICMP_NE:
3449 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003450 return pred;
3451 case ICMP_SGT: return ICMP_UGT;
3452 case ICMP_SLT: return ICMP_ULT;
3453 case ICMP_SGE: return ICMP_UGE;
3454 case ICMP_SLE: return ICMP_ULE;
3455 }
3456}
3457
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003458CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3459 switch (pred) {
3460 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3461 case ICMP_SGT: return ICMP_SGE;
3462 case ICMP_SLT: return ICMP_SLE;
3463 case ICMP_SGE: return ICMP_SGT;
3464 case ICMP_SLE: return ICMP_SLT;
3465 case ICMP_UGT: return ICMP_UGE;
3466 case ICMP_ULT: return ICMP_ULE;
3467 case ICMP_UGE: return ICMP_UGT;
3468 case ICMP_ULE: return ICMP_ULT;
3469
3470 case FCMP_OGT: return FCMP_OGE;
3471 case FCMP_OLT: return FCMP_OLE;
3472 case FCMP_OGE: return FCMP_OGT;
3473 case FCMP_OLE: return FCMP_OLT;
3474 case FCMP_UGT: return FCMP_UGE;
3475 case FCMP_ULT: return FCMP_ULE;
3476 case FCMP_UGE: return FCMP_UGT;
3477 case FCMP_ULE: return FCMP_ULT;
3478 }
3479}
3480
Dan Gohman4e724382008-05-31 02:47:54 +00003481CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003482 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003483 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003484 case ICMP_EQ: case ICMP_NE:
3485 return pred;
3486 case ICMP_SGT: return ICMP_SLT;
3487 case ICMP_SLT: return ICMP_SGT;
3488 case ICMP_SGE: return ICMP_SLE;
3489 case ICMP_SLE: return ICMP_SGE;
3490 case ICMP_UGT: return ICMP_ULT;
3491 case ICMP_ULT: return ICMP_UGT;
3492 case ICMP_UGE: return ICMP_ULE;
3493 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003494
Reid Spencerd9436b62006-11-20 01:22:35 +00003495 case FCMP_FALSE: case FCMP_TRUE:
3496 case FCMP_OEQ: case FCMP_ONE:
3497 case FCMP_UEQ: case FCMP_UNE:
3498 case FCMP_ORD: case FCMP_UNO:
3499 return pred;
3500 case FCMP_OGT: return FCMP_OLT;
3501 case FCMP_OLT: return FCMP_OGT;
3502 case FCMP_OGE: return FCMP_OLE;
3503 case FCMP_OLE: return FCMP_OGE;
3504 case FCMP_UGT: return FCMP_ULT;
3505 case FCMP_ULT: return FCMP_UGT;
3506 case FCMP_UGE: return FCMP_ULE;
3507 case FCMP_ULE: return FCMP_UGE;
3508 }
3509}
3510
Max Kazantsevb299ade2018-02-07 11:16:29 +00003511CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3512 switch (pred) {
3513 case ICMP_SGT: return ICMP_SGE;
3514 case ICMP_SLT: return ICMP_SLE;
3515 case ICMP_UGT: return ICMP_UGE;
3516 case ICMP_ULT: return ICMP_ULE;
3517 case FCMP_OGT: return FCMP_OGE;
3518 case FCMP_OLT: return FCMP_OLE;
3519 case FCMP_UGT: return FCMP_UGE;
3520 case FCMP_ULT: return FCMP_ULE;
3521 default: return pred;
3522 }
3523}
3524
Sanjoy Das6e78b172015-10-22 19:57:34 +00003525CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3526 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3527
3528 switch (pred) {
3529 default:
3530 llvm_unreachable("Unknown predicate!");
3531 case CmpInst::ICMP_ULT:
3532 return CmpInst::ICMP_SLT;
3533 case CmpInst::ICMP_ULE:
3534 return CmpInst::ICMP_SLE;
3535 case CmpInst::ICMP_UGT:
3536 return CmpInst::ICMP_SGT;
3537 case CmpInst::ICMP_UGE:
3538 return CmpInst::ICMP_SGE;
3539 }
3540}
3541
Craig Topper1c3f2832015-12-15 06:11:33 +00003542bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003543 switch (predicate) {
3544 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003545 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003546 case ICmpInst::ICMP_UGE: return true;
3547 }
3548}
3549
Craig Topper1c3f2832015-12-15 06:11:33 +00003550bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003551 switch (predicate) {
3552 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003553 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003554 case ICmpInst::ICMP_SGE: return true;
3555 }
3556}
3557
Craig Topper1c3f2832015-12-15 06:11:33 +00003558bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003559 switch (predicate) {
3560 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003561 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3562 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003563 case FCmpInst::FCMP_ORD: return true;
3564 }
3565}
Fangrui Songf78650a2018-07-30 19:41:25 +00003566
Craig Topper1c3f2832015-12-15 06:11:33 +00003567bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003568 switch (predicate) {
3569 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003570 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3571 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003572 case FCmpInst::FCMP_UNO: return true;
3573 }
3574}
3575
Craig Topper1c3f2832015-12-15 06:11:33 +00003576bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003577 switch(predicate) {
3578 default: return false;
3579 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3580 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3581 }
3582}
3583
Craig Topper1c3f2832015-12-15 06:11:33 +00003584bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003585 switch(predicate) {
3586 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3587 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3588 default: return false;
3589 }
3590}
3591
Chad Rosier99bc4802016-04-21 16:18:02 +00003592bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003593 // If the predicates match, then we know the first condition implies the
3594 // second is true.
3595 if (Pred1 == Pred2)
3596 return true;
3597
3598 switch (Pred1) {
3599 default:
3600 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003601 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003602 // 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 +00003603 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3604 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003605 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3606 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3607 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3608 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3609 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3610 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3611 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3612 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003613 }
3614 return false;
3615}
3616
Chad Rosier99bc4802016-04-21 16:18:02 +00003617bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003618 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003619}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003620
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003621//===----------------------------------------------------------------------===//
3622// SwitchInst Implementation
3623//===----------------------------------------------------------------------===//
3624
Chris Lattnerbaf00152010-11-17 05:41:46 +00003625void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3626 assert(Value && Default && NumReserved);
3627 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003628 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003629 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003630
Pete Coopereb31b682015-05-21 22:48:54 +00003631 Op<0>() = Value;
3632 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003633}
3634
Chris Lattner2195fc42007-02-24 00:55:48 +00003635/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3636/// switch on and a default destination. The number of additional cases can
3637/// be specified here to make memory allocation more efficient. This
3638/// constructor can also autoinsert before another instruction.
3639SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3640 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003641 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3642 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003643 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003644}
3645
3646/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3647/// switch on and a default destination. The number of additional cases can
3648/// be specified here to make memory allocation more efficient. This
3649/// constructor also autoinserts at the end of the specified BasicBlock.
3650SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3651 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003652 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3653 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003654 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003655}
3656
Misha Brukmanb1c93172005-04-21 23:48:37 +00003657SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003658 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003659 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003660 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003661 Use *OL = getOperandList();
3662 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003663 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003664 OL[i] = InOL[i];
3665 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003666 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003667 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003668}
3669
3670/// addCase - Add an entry to the switch instruction...
3671///
Chris Lattner47ac1872005-02-24 05:32:09 +00003672void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003673 unsigned NewCaseIdx = getNumCases();
3674 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003675 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003676 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003677 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003678 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003679 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003680 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003681 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003682 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003683}
3684
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003685/// removeCase - This method removes the specified case and its successor
3686/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003687SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3688 unsigned idx = I->getCaseIndex();
3689
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003690 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003691
3692 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003693 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003694
Jay Foad14277722011-02-01 09:22:34 +00003695 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003696 if (2 + (idx + 1) * 2 != NumOps) {
3697 OL[2 + idx * 2] = OL[NumOps - 2];
3698 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003699 }
3700
3701 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003702 OL[NumOps-2].set(nullptr);
3703 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003704 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003705
3706 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003707}
3708
Jay Foade98f29d2011-04-01 08:00:58 +00003709/// growOperands - grow operands - This grows the operand list in response
3710/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003711///
Jay Foade98f29d2011-04-01 08:00:58 +00003712void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003713 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003714 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003715
3716 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003717 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003718}
3719
Chris Lattner3ed871f2009-10-27 19:13:16 +00003720//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003721// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003722//===----------------------------------------------------------------------===//
3723
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003724void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003725 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003726 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003727 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003728 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003729 allocHungoffUses(ReservedSpace);
3730
Pete Coopereb31b682015-05-21 22:48:54 +00003731 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003732}
3733
3734
Jay Foade98f29d2011-04-01 08:00:58 +00003735/// growOperands - grow operands - This grows the operand list in response
3736/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737///
Jay Foade98f29d2011-04-01 08:00:58 +00003738void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003740 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003741
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003743 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003744}
3745
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003746IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3747 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003748 : Instruction(Type::getVoidTy(Address->getContext()),
3749 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003750 init(Address, NumCases);
3751}
3752
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003753IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3754 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003755 : Instruction(Type::getVoidTy(Address->getContext()),
3756 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003757 init(Address, NumCases);
3758}
3759
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003760IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003761 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3762 nullptr, IBI.getNumOperands()) {
Pete Cooper3fc30402015-06-10 22:38:46 +00003763 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003764 Use *OL = getOperandList();
3765 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003766 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3767 OL[i] = InOL[i];
3768 SubclassOptionalData = IBI.SubclassOptionalData;
3769}
3770
Chris Lattner3ed871f2009-10-27 19:13:16 +00003771/// addDestination - Add a destination.
3772///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003773void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003774 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003775 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003776 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003777 // Initialize some new operands.
3778 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003779 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003780 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003781}
3782
3783/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003784/// indirectbr instruction.
3785void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003786 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003787
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003789 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003790
3791 // Replace this value with the last one.
3792 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003793
Chris Lattner3ed871f2009-10-27 19:13:16 +00003794 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003795 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003796 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003797}
3798
Chris Lattner3ed871f2009-10-27 19:13:16 +00003799//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003800// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003801//===----------------------------------------------------------------------===//
3802
Chris Lattnerf22be932004-10-15 23:52:53 +00003803// Define these methods here so vtables don't get emitted into every translation
3804// unit that uses these classes.
3805
Pete Cooper75403d72015-06-24 20:22:23 +00003806GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003807 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003808}
3809
Cameron McInallycbde0d92018-11-13 18:15:47 +00003810UnaryOperator *UnaryOperator::cloneImpl() const {
3811 return Create(getOpcode(), Op<0>());
3812}
3813
Pete Cooper75403d72015-06-24 20:22:23 +00003814BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003815 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003816}
3817
Pete Cooper75403d72015-06-24 20:22:23 +00003818FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003819 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003820}
3821
Pete Cooper75403d72015-06-24 20:22:23 +00003822ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003823 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003824}
3825
Pete Cooper75403d72015-06-24 20:22:23 +00003826ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003827 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003828}
3829
Pete Cooper75403d72015-06-24 20:22:23 +00003830InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003831 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003832}
3833
Pete Cooper75403d72015-06-24 20:22:23 +00003834AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003835 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003836 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003837 (Value *)getOperand(0), getAlignment());
3838 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003839 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003840 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003841}
3842
Pete Cooper75403d72015-06-24 20:22:23 +00003843LoadInst *LoadInst::cloneImpl() const {
James Y Knight84c1dbd2019-01-14 21:37:53 +00003844 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003845 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003846}
3847
Pete Cooper75403d72015-06-24 20:22:23 +00003848StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003849 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003850 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003851
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003852}
3853
Pete Cooper75403d72015-06-24 20:22:23 +00003854AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003855 AtomicCmpXchgInst *Result =
3856 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003857 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003858 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003859 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003860 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003861 return Result;
3862}
3863
Pete Cooper75403d72015-06-24 20:22:23 +00003864AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003865 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003866 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3867 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003868 Result->setVolatile(isVolatile());
3869 return Result;
3870}
3871
Pete Cooper75403d72015-06-24 20:22:23 +00003872FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003873 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003874}
3875
Pete Cooper75403d72015-06-24 20:22:23 +00003876TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003877 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003878}
3879
Pete Cooper75403d72015-06-24 20:22:23 +00003880ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003881 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003882}
3883
Pete Cooper75403d72015-06-24 20:22:23 +00003884SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003885 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003886}
3887
Pete Cooper75403d72015-06-24 20:22:23 +00003888FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003889 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003890}
3891
Pete Cooper75403d72015-06-24 20:22:23 +00003892FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003893 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003894}
3895
Pete Cooper75403d72015-06-24 20:22:23 +00003896UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003897 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003898}
3899
Pete Cooper75403d72015-06-24 20:22:23 +00003900SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003901 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003902}
3903
Pete Cooper75403d72015-06-24 20:22:23 +00003904FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003905 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003906}
3907
Pete Cooper75403d72015-06-24 20:22:23 +00003908FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003909 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003910}
3911
Pete Cooper75403d72015-06-24 20:22:23 +00003912PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003913 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914}
3915
Pete Cooper75403d72015-06-24 20:22:23 +00003916IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003917 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003918}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919
Pete Cooper75403d72015-06-24 20:22:23 +00003920BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003921 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003922}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003923
Pete Cooper75403d72015-06-24 20:22:23 +00003924AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003925 return new AddrSpaceCastInst(getOperand(0), getType());
3926}
3927
Pete Cooper75403d72015-06-24 20:22:23 +00003928CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003929 if (hasOperandBundles()) {
3930 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3931 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3932 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003933 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003934}
3935
Pete Cooper75403d72015-06-24 20:22:23 +00003936SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003937 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003938}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003939
Pete Cooper75403d72015-06-24 20:22:23 +00003940VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003941 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003942}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943
Pete Cooper75403d72015-06-24 20:22:23 +00003944ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003945 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003946}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003947
Pete Cooper75403d72015-06-24 20:22:23 +00003948InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003949 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003950}
3951
Pete Cooper75403d72015-06-24 20:22:23 +00003952ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003953 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003954}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955
Pete Cooper75403d72015-06-24 20:22:23 +00003956PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003957
Pete Cooper75403d72015-06-24 20:22:23 +00003958LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003959 return new LandingPadInst(*this);
3960}
3961
Pete Cooper75403d72015-06-24 20:22:23 +00003962ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003963 return new(getNumOperands()) ReturnInst(*this);
3964}
3965
Pete Cooper75403d72015-06-24 20:22:23 +00003966BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003967 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003968}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003969
Pete Cooper75403d72015-06-24 20:22:23 +00003970SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003971
Pete Cooper75403d72015-06-24 20:22:23 +00003972IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003973 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003974}
3975
Pete Cooper75403d72015-06-24 20:22:23 +00003976InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003977 if (hasOperandBundles()) {
3978 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3979 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3980 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003981 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003982}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003983
Pete Cooper75403d72015-06-24 20:22:23 +00003984ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003985
David Majnemer654e1302015-07-31 17:58:14 +00003986CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3987 return new (getNumOperands()) CleanupReturnInst(*this);
3988}
3989
David Majnemer654e1302015-07-31 17:58:14 +00003990CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003991 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003992}
3993
David Majnemer8a1c45d2015-12-12 05:38:55 +00003994CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3995 return new CatchSwitchInst(*this);
3996}
3997
3998FuncletPadInst *FuncletPadInst::cloneImpl() const {
3999 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004000}
4001
Pete Cooper75403d72015-06-24 20:22:23 +00004002UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004003 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004004 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004005}