blob: d65c61c891a7014282d570694cddd9f92e694cfa [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
Craig Topper784929d2019-02-08 20:48:56 +0000259unsigned CallBase::getNumSubclassExtraOperandsDynamic() const {
260 assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!");
261 return cast<CallBrInst>(this)->getNumIndirectDests() + 1;
262}
263
Chandler Carruth57578aa2019-01-07 07:15:51 +0000264bool CallBase::isIndirectCall() const {
265 const Value *V = getCalledValue();
266 if (isa<Function>(V) || isa<Constant>(V))
267 return false;
268 if (const CallInst *CI = dyn_cast<CallInst>(this))
269 if (CI->isInlineAsm())
270 return false;
271 return true;
272}
273
Craig Topperc1892ec2019-01-31 17:23:29 +0000274/// Tests if this call site must be tail call optimized. Only a CallInst can
275/// be tail call optimized.
276bool CallBase::isMustTailCall() const {
277 if (auto *CI = dyn_cast<CallInst>(this))
278 return CI->isMustTailCall();
279 return false;
280}
281
282/// Tests if this call site is marked as a tail call.
283bool CallBase::isTailCall() const {
284 if (auto *CI = dyn_cast<CallInst>(this))
285 return CI->isTailCall();
286 return false;
287}
288
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000289Intrinsic::ID CallBase::getIntrinsicID() const {
290 if (auto *F = getCalledFunction())
291 return F->getIntrinsicID();
292 return Intrinsic::not_intrinsic;
293}
294
295bool CallBase::isReturnNonNull() const {
296 if (hasRetAttr(Attribute::NonNull))
297 return true;
298
299 if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
300 !NullPointerIsDefined(getCaller(),
301 getType()->getPointerAddressSpace()))
302 return true;
303
304 return false;
305}
306
Chandler Carruthe429c792018-11-22 10:31:35 +0000307Value *CallBase::getReturnedArgOperand() const {
308 unsigned Index;
309
310 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
311 return getArgOperand(Index - AttributeList::FirstArgIndex);
312 if (const Function *F = getCalledFunction())
313 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
314 Index)
315 return getArgOperand(Index - AttributeList::FirstArgIndex);
316
317 return nullptr;
318}
319
320bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
321 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
322 return true;
323
324 // Look at the callee, if available.
325 if (const Function *F = getCalledFunction())
326 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
327 return false;
328}
329
330/// Determine whether the argument or parameter has the given attribute.
331bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
332 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
333
334 if (Attrs.hasParamAttribute(ArgNo, Kind))
335 return true;
336 if (const Function *F = getCalledFunction())
337 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
338 return false;
339}
340
341bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
342 if (const Function *F = getCalledFunction())
343 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
344 return false;
345}
346
347bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
348 if (const Function *F = getCalledFunction())
349 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
350 return false;
351}
352
353CallBase::op_iterator
354CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
355 const unsigned BeginIndex) {
356 auto It = op_begin() + BeginIndex;
357 for (auto &B : Bundles)
358 It = std::copy(B.input_begin(), B.input_end(), It);
359
360 auto *ContextImpl = getContext().pImpl;
361 auto BI = Bundles.begin();
362 unsigned CurrentIndex = BeginIndex;
363
364 for (auto &BOI : bundle_op_infos()) {
365 assert(BI != Bundles.end() && "Incorrect allocation?");
366
367 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
368 BOI.Begin = CurrentIndex;
369 BOI.End = CurrentIndex + BI->input_size();
370 CurrentIndex = BOI.End;
371 BI++;
372 }
373
374 assert(BI == Bundles.end() && "Incorrect allocation?");
375
376 return It;
377}
378
379//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380// CallInst Implementation
381//===----------------------------------------------------------------------===//
382
David Blaikie348de692015-04-23 21:36:23 +0000383void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000384 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000385 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000386 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
387 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000388 setCalledOperand(Func);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389
Jay Foad5bd375a2011-07-15 08:37:34 +0000390#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000391 assert((Args.size() == FTy->getNumParams() ||
392 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000393 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000394
395 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000396 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000397 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000398 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000399#endif
400
Fangrui Song75709322018-11-17 01:44:25 +0000401 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000402
403 auto It = populateBundleOperandInfos(Bundles, Args.size());
404 (void)It;
405 assert(It + 1 == op_end() && "Should add up!");
406
Jay Foad5bd375a2011-07-15 08:37:34 +0000407 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408}
409
James Y Knightf9563902019-01-14 21:37:42 +0000410void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
411 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000412 assert(getNumOperands() == 1 && "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000413 setCalledOperand(Func);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000414
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000415 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000416
417 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418}
419
James Y Knightf9563902019-01-14 21:37:42 +0000420CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
421 Instruction *InsertBefore)
422 : CallBase(Ty->getReturnType(), Instruction::Call,
423 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) {
424 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000425}
426
James Y Knightf9563902019-01-14 21:37:42 +0000427CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
428 BasicBlock *InsertAtEnd)
429 : CallBase(Ty->getReturnType(), Instruction::Call,
430 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) {
431 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432}
433
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434CallInst::CallInst(const CallInst &CI)
Chandler Carruthe429c792018-11-22 10:31:35 +0000435 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
436 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(),
437 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000438 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000439 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000440
Jay Foad5bd375a2011-07-15 08:37:34 +0000441 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000442 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
443 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000444 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000445}
446
Sanjoy Das2d161452015-11-18 06:23:38 +0000447CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
448 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000449 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000450
James Y Knight7976eb52019-02-01 20:43:25 +0000451 auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledValue(),
452 Args, OpB, CI->getName(), InsertPt);
Sanjoy Das2d161452015-11-18 06:23:38 +0000453 NewCI->setTailCallKind(CI->getTailCallKind());
454 NewCI->setCallingConv(CI->getCallingConv());
455 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000456 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000457 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000458 return NewCI;
459}
460
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461/// IsConstantOne - Return true only if val is constant int 1
462static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000463 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000464 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
465 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000466}
467
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000468static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000469 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000470 Type *AllocTy, Value *AllocSize,
471 Value *ArraySize,
472 ArrayRef<OperandBundleDef> OpB,
473 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000474 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000475 "createMalloc needs either InsertBefore or InsertAtEnd");
476
Fangrui Songf78650a2018-07-30 19:41:25 +0000477 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000478 // bitcast (i8* malloc(typeSize)) to type*
479 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000480 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000481 if (!ArraySize)
482 ArraySize = ConstantInt::get(IntPtrTy, 1);
483 else if (ArraySize->getType() != IntPtrTy) {
484 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000485 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
486 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000487 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000488 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
489 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000490 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000492 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493 if (IsConstantOne(AllocSize)) {
494 AllocSize = ArraySize; // Operand * 1 = Operand
495 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
496 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
497 false /*ZExt*/);
498 // Malloc arg is constant product of type size and array size
499 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
500 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000501 // Multiply type size by the array size...
502 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000503 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
504 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000505 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000506 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
507 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000508 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000509 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000510
Victor Hernandez788eaab2009-09-18 19:20:02 +0000511 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000512 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000513 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
514 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000515 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
James Y Knight13680222019-02-01 02:28:03 +0000516 FunctionCallee MallocFunc = MallocF;
Victor Hernandezbb336a12009-11-10 19:53:28 +0000517 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000518 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000519 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000520 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000521 CallInst *MCall = nullptr;
522 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000523 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000524 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
525 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000526 Result = MCall;
527 if (Result->getType() != AllocPtrType)
528 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000529 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000530 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000531 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000532 Result = MCall;
533 if (Result->getType() != AllocPtrType) {
534 InsertAtEnd->getInstList().push_back(MCall);
535 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000536 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000537 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000538 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000539 MCall->setTailCall();
James Y Knight13680222019-02-01 02:28:03 +0000540 if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000541 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000542 if (!F->returnDoesNotAlias())
543 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000544 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000545 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000546
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000547 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000548}
549
550/// CreateMalloc - Generate the IR for a call to malloc:
551/// 1. Compute the malloc call's argument as the specified type's size,
552/// possibly multiplied by the array size if the array size is not
553/// constant 1.
554/// 2. Call malloc with that argument.
555/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000556Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000557 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000558 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000559 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000560 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000561 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000562 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000563}
David Majnemerfadc6db2016-04-29 08:07:22 +0000564Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
565 Type *IntPtrTy, Type *AllocTy,
566 Value *AllocSize, Value *ArraySize,
567 ArrayRef<OperandBundleDef> OpB,
568 Function *MallocF,
569 const Twine &Name) {
570 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
571 ArraySize, OpB, MallocF, Name);
572}
573
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000574/// CreateMalloc - Generate the IR for a call to malloc:
575/// 1. Compute the malloc call's argument as the specified type's size,
576/// possibly multiplied by the array size if the array size is not
577/// constant 1.
578/// 2. Call malloc with that argument.
579/// 3. Bitcast the result of the malloc call to the specified type.
580/// Note: This function does not add the bitcast to the basic block, that is the
581/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000582Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000583 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000584 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000585 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000586 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000587 ArraySize, None, MallocF, Name);
588}
589Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
590 Type *IntPtrTy, Type *AllocTy,
591 Value *AllocSize, Value *ArraySize,
592 ArrayRef<OperandBundleDef> OpB,
593 Function *MallocF, const Twine &Name) {
594 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
595 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000596}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000597
David Majnemerfadc6db2016-04-29 08:07:22 +0000598static Instruction *createFree(Value *Source,
599 ArrayRef<OperandBundleDef> Bundles,
600 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000601 BasicBlock *InsertAtEnd) {
602 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
603 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000604 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000605 "Can not free something of nonpointer type!");
606
Ana Pazosb3596022016-02-03 21:34:39 +0000607 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
608 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000609
Chris Lattner229907c2011-07-18 04:54:35 +0000610 Type *VoidTy = Type::getVoidTy(M->getContext());
611 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000612 // prototype free as "void free(void*)"
James Y Knight13680222019-02-01 02:28:03 +0000613 FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000614 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000615 Value *PtrCast = Source;
616 if (InsertBefore) {
617 if (Source->getType() != IntPtrTy)
618 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000619 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000620 } else {
621 if (Source->getType() != IntPtrTy)
622 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000623 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000624 }
625 Result->setTailCall();
James Y Knight13680222019-02-01 02:28:03 +0000626 if (Function *F = dyn_cast<Function>(FreeFunc.getCallee()))
Chris Lattner2156c222009-11-09 07:12:01 +0000627 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000628
629 return Result;
630}
631
632/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000633Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000634 return createFree(Source, None, InsertBefore, nullptr);
635}
636Instruction *CallInst::CreateFree(Value *Source,
637 ArrayRef<OperandBundleDef> Bundles,
638 Instruction *InsertBefore) {
639 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000640}
641
642/// CreateFree - Generate the IR for a call to the builtin free function.
643/// Note: This function does not add the call to the basic block, that is the
644/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000645Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000646 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
647 assert(FreeCall && "CreateFree did not create a CallInst");
648 return FreeCall;
649}
650Instruction *CallInst::CreateFree(Value *Source,
651 ArrayRef<OperandBundleDef> Bundles,
652 BasicBlock *InsertAtEnd) {
653 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000654 assert(FreeCall && "CreateFree did not create a CallInst");
655 return FreeCall;
656}
657
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000658//===----------------------------------------------------------------------===//
659// InvokeInst Implementation
660//===----------------------------------------------------------------------===//
661
David Blaikie3e807092015-05-13 18:35:26 +0000662void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
663 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000664 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000665 const Twine &NameStr) {
666 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000667
Chandler Carruthe429c792018-11-22 10:31:35 +0000668 assert((int)getNumOperands() ==
669 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
Sanjoy Das9303c242015-09-24 19:14:18 +0000670 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000671 setNormalDest(IfNormal);
672 setUnwindDest(IfException);
673 setCalledOperand(Fn);
Jay Foad5bd375a2011-07-15 08:37:34 +0000674
675#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000676 assert(((Args.size() == FTy->getNumParams()) ||
677 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000678 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000679
Jay Foad5bd375a2011-07-15 08:37:34 +0000680 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000681 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000682 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000683 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000684#endif
685
Fangrui Song75709322018-11-17 01:44:25 +0000686 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000687
688 auto It = populateBundleOperandInfos(Bundles, Args.size());
689 (void)It;
690 assert(It + 3 == op_end() && "Should add up!");
691
Jay Foad5bd375a2011-07-15 08:37:34 +0000692 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693}
694
Misha Brukmanb1c93172005-04-21 23:48:37 +0000695InvokeInst::InvokeInst(const InvokeInst &II)
Chandler Carruthe429c792018-11-22 10:31:35 +0000696 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
697 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(),
698 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000699 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000700 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000701 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
702 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000703 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
Sanjoy Das2d161452015-11-18 06:23:38 +0000706InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
707 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000708 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000709
James Y Knightd9e85a02019-02-01 20:43:34 +0000710 auto *NewII = InvokeInst::Create(II->getFunctionType(), II->getCalledValue(),
711 II->getNormalDest(), II->getUnwindDest(),
712 Args, OpB, II->getName(), InsertPt);
Sanjoy Das2d161452015-11-18 06:23:38 +0000713 NewII->setCallingConv(II->getCallingConv());
714 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000715 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000716 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000717 return NewII;
718}
719
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000720
Bill Wendlingfae14752011-08-12 20:24:12 +0000721LandingPadInst *InvokeInst::getLandingPadInst() const {
722 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
723}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000724
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725//===----------------------------------------------------------------------===//
Craig Topper784929d2019-02-08 20:48:56 +0000726// CallBrInst Implementation
727//===----------------------------------------------------------------------===//
728
729void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough,
730 ArrayRef<BasicBlock *> IndirectDests,
731 ArrayRef<Value *> Args,
732 ArrayRef<OperandBundleDef> Bundles,
733 const Twine &NameStr) {
734 this->FTy = FTy;
735
736 assert((int)getNumOperands() ==
737 ComputeNumOperands(Args.size(), IndirectDests.size(),
738 CountBundleInputs(Bundles)) &&
739 "NumOperands not set up?");
740 NumIndirectDests = IndirectDests.size();
741 setDefaultDest(Fallthrough);
742 for (unsigned i = 0; i != NumIndirectDests; ++i)
743 setIndirectDest(i, IndirectDests[i]);
744 setCalledOperand(Fn);
745
746#ifndef NDEBUG
747 assert(((Args.size() == FTy->getNumParams()) ||
748 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
749 "Calling a function with bad signature");
750
751 for (unsigned i = 0, e = Args.size(); i != e; i++)
752 assert((i >= FTy->getNumParams() ||
753 FTy->getParamType(i) == Args[i]->getType()) &&
754 "Calling a function with a bad signature!");
755#endif
756
757 std::copy(Args.begin(), Args.end(), op_begin());
758
759 auto It = populateBundleOperandInfos(Bundles, Args.size());
760 (void)It;
761 assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!");
762
763 setName(NameStr);
764}
765
766CallBrInst::CallBrInst(const CallBrInst &CBI)
767 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr,
768 OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(),
769 CBI.getNumOperands()) {
770 setCallingConv(CBI.getCallingConv());
771 std::copy(CBI.op_begin(), CBI.op_end(), op_begin());
772 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(),
773 bundle_op_info_begin());
774 SubclassOptionalData = CBI.SubclassOptionalData;
775 NumIndirectDests = CBI.NumIndirectDests;
776}
777
778CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB,
779 Instruction *InsertPt) {
780 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end());
781
782 auto *NewCBI = CallBrInst::Create(CBI->getFunctionType(),
783 CBI->getCalledValue(),
784 CBI->getDefaultDest(),
785 CBI->getIndirectDests(),
786 Args, OpB, CBI->getName(), InsertPt);
787 NewCBI->setCallingConv(CBI->getCallingConv());
788 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData;
789 NewCBI->setAttributes(CBI->getAttributes());
790 NewCBI->setDebugLoc(CBI->getDebugLoc());
791 NewCBI->NumIndirectDests = CBI->NumIndirectDests;
792 return NewCBI;
793}
794
795//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796// ReturnInst Implementation
797//===----------------------------------------------------------------------===//
798
Chris Lattner2195fc42007-02-24 00:55:48 +0000799ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000800 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
801 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
802 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000803 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000804 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000805 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000806}
807
Owen Anderson55f1c092009-08-13 21:58:54 +0000808ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000809 : Instruction(Type::getVoidTy(C), Instruction::Ret,
810 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
811 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000812 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000813 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000814}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000815
Owen Anderson55f1c092009-08-13 21:58:54 +0000816ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000817 : Instruction(Type::getVoidTy(C), Instruction::Ret,
818 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
819 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000820 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000821 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000822}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000823
Owen Anderson55f1c092009-08-13 21:58:54 +0000824ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000825 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
826 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel59643e52008-02-23 00:35:18 +0000827
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000829// ResumeInst Implementation
830//===----------------------------------------------------------------------===//
831
832ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000833 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
834 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000835 Op<0>() = RI.Op<0>();
836}
837
838ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000839 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
840 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000841 Op<0>() = Exn;
842}
843
844ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000845 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
846 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000847 Op<0>() = Exn;
848}
849
Bill Wendlingf891bf82011-07-31 06:30:59 +0000850//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000851// CleanupReturnInst Implementation
852//===----------------------------------------------------------------------===//
853
854CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000855 : Instruction(CRI.getType(), Instruction::CleanupRet,
856 OperandTraits<CleanupReturnInst>::op_end(this) -
857 CRI.getNumOperands(),
858 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000859 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000860 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000861 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000862 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000863}
864
David Majnemer8a1c45d2015-12-12 05:38:55 +0000865void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000866 if (UnwindBB)
867 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000868
David Majnemer8a1c45d2015-12-12 05:38:55 +0000869 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000870 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000871 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000872}
873
David Majnemer8a1c45d2015-12-12 05:38:55 +0000874CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
875 unsigned Values, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000876 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
877 Instruction::CleanupRet,
878 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
879 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000880 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000881}
882
David Majnemer8a1c45d2015-12-12 05:38:55 +0000883CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
884 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000885 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
886 Instruction::CleanupRet,
887 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
888 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000889 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000890}
891
David Majnemer654e1302015-07-31 17:58:14 +0000892//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000893// CatchReturnInst Implementation
894//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000895void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000896 Op<0>() = CatchPad;
897 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000898}
David Majnemer654e1302015-07-31 17:58:14 +0000899
900CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000901 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
902 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000903 Op<0>() = CRI.Op<0>();
904 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000905}
906
David Majnemer8a1c45d2015-12-12 05:38:55 +0000907CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000908 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000909 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
910 OperandTraits<CatchReturnInst>::op_begin(this), 2,
911 InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000912 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000913}
914
David Majnemer8a1c45d2015-12-12 05:38:55 +0000915CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000916 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000917 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
918 OperandTraits<CatchReturnInst>::op_begin(this), 2,
919 InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000920 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000921}
922
David Majnemer654e1302015-07-31 17:58:14 +0000923//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000924// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000925//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000926
927CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
928 unsigned NumReservedValues,
929 const Twine &NameStr,
930 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000931 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
932 InsertBefore) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000933 if (UnwindDest)
934 ++NumReservedValues;
935 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000936 setName(NameStr);
937}
938
David Majnemer8a1c45d2015-12-12 05:38:55 +0000939CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
940 unsigned NumReservedValues,
941 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000942 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
943 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000944 if (UnwindDest)
945 ++NumReservedValues;
946 init(ParentPad, UnwindDest, NumReservedValues + 1);
947 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000948}
949
David Majnemer8a1c45d2015-12-12 05:38:55 +0000950CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000951 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
952 CSI.getNumOperands()) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000953 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
954 setNumHungOffUseOperands(ReservedSpace);
955 Use *OL = getOperandList();
956 const Use *InOL = CSI.getOperandList();
957 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
958 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000959}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000960
961void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
962 unsigned NumReservedValues) {
963 assert(ParentPad && NumReservedValues);
964
965 ReservedSpace = NumReservedValues;
966 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
967 allocHungoffUses(ReservedSpace);
968
969 Op<0>() = ParentPad;
970 if (UnwindDest) {
971 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
972 setUnwindDest(UnwindDest);
973 }
974}
975
976/// growOperands - grow operands - This grows the operand list in response to a
977/// push_back style of operation. This grows the number of ops by 2 times.
978void CatchSwitchInst::growOperands(unsigned Size) {
979 unsigned NumOperands = getNumOperands();
980 assert(NumOperands >= 1);
981 if (ReservedSpace >= NumOperands + Size)
982 return;
983 ReservedSpace = (NumOperands + Size / 2) * 2;
984 growHungoffUses(ReservedSpace);
985}
986
987void CatchSwitchInst::addHandler(BasicBlock *Handler) {
988 unsigned OpNo = getNumOperands();
989 growOperands(1);
990 assert(OpNo < ReservedSpace && "Growing didn't work!");
991 setNumHungOffUseOperands(getNumOperands() + 1);
992 getOperandList()[OpNo] = Handler;
993}
994
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000995void CatchSwitchInst::removeHandler(handler_iterator HI) {
996 // Move all subsequent handlers up one.
997 Use *EndDst = op_end() - 1;
998 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
999 *CurDst = *(CurDst + 1);
1000 // Null out the last handler use.
1001 *EndDst = nullptr;
1002
1003 setNumHungOffUseOperands(getNumOperands() - 1);
1004}
1005
David Majnemer8a1c45d2015-12-12 05:38:55 +00001006//===----------------------------------------------------------------------===//
1007// FuncletPadInst Implementation
1008//===----------------------------------------------------------------------===//
1009void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1010 const Twine &NameStr) {
1011 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
Fangrui Song75709322018-11-17 01:44:25 +00001012 llvm::copy(Args, op_begin());
David Majnemer8a1c45d2015-12-12 05:38:55 +00001013 setParentPad(ParentPad);
1014 setName(NameStr);
1015}
1016
1017FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1018 : Instruction(FPI.getType(), FPI.getOpcode(),
1019 OperandTraits<FuncletPadInst>::op_end(this) -
1020 FPI.getNumOperands(),
1021 FPI.getNumOperands()) {
1022 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1023 setParentPad(FPI.getParentPad());
1024}
1025
1026FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1027 ArrayRef<Value *> Args, unsigned Values,
1028 const Twine &NameStr, Instruction *InsertBefore)
1029 : Instruction(ParentPad->getType(), Op,
1030 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1031 InsertBefore) {
1032 init(ParentPad, Args, NameStr);
1033}
1034
1035FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1036 ArrayRef<Value *> Args, unsigned Values,
1037 const Twine &NameStr, BasicBlock *InsertAtEnd)
1038 : Instruction(ParentPad->getType(), Op,
1039 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1040 InsertAtEnd) {
1041 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001042}
1043
1044//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001045// UnreachableInst Implementation
1046//===----------------------------------------------------------------------===//
1047
Fangrui Songf78650a2018-07-30 19:41:25 +00001048UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +00001049 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001050 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
1051 0, InsertBefore) {}
Owen Anderson55f1c092009-08-13 21:58:54 +00001052UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001053 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
1054 0, InsertAtEnd) {}
Chris Lattner2195fc42007-02-24 00:55:48 +00001055
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001056//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057// BranchInst Implementation
1058//===----------------------------------------------------------------------===//
1059
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001060void BranchInst::AssertOK() {
1061 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001062 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001063 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064}
1065
Chris Lattner2195fc42007-02-24 00:55:48 +00001066BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001067 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1068 OperandTraits<BranchInst>::op_end(this) - 1, 1,
1069 InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001070 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001071 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001072}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001073
Chris Lattner2195fc42007-02-24 00:55:48 +00001074BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1075 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001076 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1077 OperandTraits<BranchInst>::op_end(this) - 3, 3,
1078 InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001079 Op<-1>() = IfTrue;
1080 Op<-2>() = IfFalse;
1081 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001082#ifndef NDEBUG
1083 AssertOK();
1084#endif
1085}
1086
1087BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001088 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1089 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001090 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001091 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001092}
1093
1094BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth509e20e2018-10-19 00:22:37 +00001095 BasicBlock *InsertAtEnd)
1096 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1097 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001098 Op<-1>() = IfTrue;
1099 Op<-2>() = IfFalse;
1100 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001101#ifndef NDEBUG
1102 AssertOK();
1103#endif
1104}
1105
Chandler Carruth509e20e2018-10-19 00:22:37 +00001106BranchInst::BranchInst(const BranchInst &BI)
1107 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
1108 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1109 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001110 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111 if (BI.getNumOperands() != 1) {
1112 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001113 Op<-3>() = BI.Op<-3>();
1114 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001116 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117}
1118
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001119void BranchInst::swapSuccessors() {
1120 assert(isConditional() &&
1121 "Cannot swap successors of an unconditional branch");
1122 Op<-1>().swap(Op<-2>());
1123
1124 // Update profile metadata if present and it matches our structural
1125 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001126 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001127}
1128
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001130// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131//===----------------------------------------------------------------------===//
1132
Owen Andersonb6b25302009-07-14 23:09:55 +00001133static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001134 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001135 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001136 else {
1137 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001138 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001139 assert(Amt->getType()->isIntegerTy() &&
1140 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001141 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001142 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143}
1144
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001145AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001146 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001147 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001148
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001149AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001150 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001151 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001152
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001153AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001154 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001155 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1156
1157AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1158 const Twine &Name, BasicBlock *InsertAtEnd)
1159 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1160
1161AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1162 unsigned Align, const Twine &Name,
1163 Instruction *InsertBefore)
1164 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1165 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1166 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001167 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001168 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001169 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001170}
1171
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001172AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1173 unsigned Align, const Twine &Name,
1174 BasicBlock *InsertAtEnd)
1175 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1176 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001177 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001178 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001179 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001180 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001181}
1182
Victor Hernandez8acf2952009-10-23 21:09:37 +00001183void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001184 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001185 assert(Align <= MaximumAlignment &&
1186 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001187 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1188 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001189 assert(getAlignment() == Align && "Alignment representation error!");
1190}
1191
Victor Hernandez8acf2952009-10-23 21:09:37 +00001192bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001193 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001194 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001195 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001196}
1197
Chris Lattner8b291e62008-11-26 02:54:17 +00001198/// isStaticAlloca - Return true if this alloca is in the entry block of the
1199/// function and is a constant size. If so, the code generator will fold it
1200/// into the prolog/epilog code, so it is basically free.
1201bool AllocaInst::isStaticAlloca() const {
1202 // Must be constant size.
1203 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001204
Chris Lattner8b291e62008-11-26 02:54:17 +00001205 // Must be in the entry block.
1206 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001207 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001208}
1209
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001210//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001211// LoadInst Implementation
1212//===----------------------------------------------------------------------===//
1213
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001215 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001217 assert(!(isAtomic() && getAlignment() == 0) &&
1218 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001219}
1220
James Y Knight84c1dbd2019-01-14 21:37:53 +00001221LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1222 Instruction *InsertBef)
1223 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224
James Y Knight84c1dbd2019-01-14 21:37:53 +00001225LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1226 BasicBlock *InsertAE)
1227 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001228
David Blaikie0c28fd72015-05-20 21:46:30 +00001229LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001230 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001231 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001232
James Y Knight84c1dbd2019-01-14 21:37:53 +00001233LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001234 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001235 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001236
David Blaikieb7a029872015-04-17 19:56:21 +00001237LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001238 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001239 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001240 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241
James Y Knight84c1dbd2019-01-14 21:37:53 +00001242LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001243 unsigned Align, BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001244 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001245 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001246
David Blaikie15d9a4c2015-04-06 20:59:48 +00001247LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001248 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001249 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001250 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001251 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001252 setVolatile(isVolatile);
1253 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001254 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001255 AssertOK();
1256 setName(Name);
1257}
1258
James Y Knight84c1dbd2019-01-14 21:37:53 +00001259LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1260 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001261 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001262 : UnaryInstruction(Ty, Load, Ptr, InsertAE) {
1263 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Chris Lattner0f048162007-02-13 07:54:42 +00001264 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001265 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001266 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001267 AssertOK();
1268 setName(Name);
1269}
1270
Christopher Lamb84485702007-04-22 19:24:39 +00001271void LoadInst::setAlignment(unsigned Align) {
1272 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001273 assert(Align <= MaximumAlignment &&
1274 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001275 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001276 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001277 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001278}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279
1280//===----------------------------------------------------------------------===//
1281// StoreInst Implementation
1282//===----------------------------------------------------------------------===//
1283
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001284void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001285 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001286 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001287 "Ptr must have pointer type!");
1288 assert(getOperand(0)->getType() ==
1289 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001290 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001291 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001292 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001293}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001295StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001296 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297
1298StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001299 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001300
Misha Brukmanb1c93172005-04-21 23:48:37 +00001301StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001302 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001303 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001304
1305StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001306 BasicBlock *InsertAtEnd)
1307 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1308
1309StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1310 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001311 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001312 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001313
1314StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1315 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001316 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001317 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001318
Misha Brukmanb1c93172005-04-21 23:48:37 +00001319StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001320 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001321 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001322 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001323 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001324 OperandTraits<StoreInst>::op_begin(this),
1325 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001326 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001327 Op<0>() = val;
1328 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001329 setVolatile(isVolatile);
1330 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001331 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001332 AssertOK();
1333}
1334
1335StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001336 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001337 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001338 BasicBlock *InsertAtEnd)
1339 : Instruction(Type::getVoidTy(val->getContext()), Store,
1340 OperandTraits<StoreInst>::op_begin(this),
1341 OperandTraits<StoreInst>::operands(this),
1342 InsertAtEnd) {
1343 Op<0>() = val;
1344 Op<1>() = addr;
1345 setVolatile(isVolatile);
1346 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001347 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001348 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001349}
1350
Christopher Lamb84485702007-04-22 19:24:39 +00001351void StoreInst::setAlignment(unsigned Align) {
1352 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001353 assert(Align <= MaximumAlignment &&
1354 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001355 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001356 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001357 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001358}
1359
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001360//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001361// AtomicCmpXchgInst Implementation
1362//===----------------------------------------------------------------------===//
1363
1364void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001365 AtomicOrdering SuccessOrdering,
1366 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001367 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001368 Op<0>() = Ptr;
1369 Op<1>() = Cmp;
1370 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001371 setSuccessOrdering(SuccessOrdering);
1372 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001373 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001374
1375 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1376 "All operands must be non-null!");
1377 assert(getOperand(0)->getType()->isPointerTy() &&
1378 "Ptr must have pointer type!");
1379 assert(getOperand(1)->getType() ==
1380 cast<PointerType>(getOperand(0)->getType())->getElementType()
1381 && "Ptr must be a pointer to Cmp type!");
1382 assert(getOperand(2)->getType() ==
1383 cast<PointerType>(getOperand(0)->getType())->getElementType()
1384 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001385 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001386 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001387 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001388 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001389 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1390 "AtomicCmpXchg failure argument shall be no stronger than the success "
1391 "argument");
1392 assert(FailureOrdering != AtomicOrdering::Release &&
1393 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001394 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001395}
1396
1397AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001398 AtomicOrdering SuccessOrdering,
1399 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001400 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001401 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001402 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001403 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001404 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1405 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001406 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001407}
1408
1409AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001410 AtomicOrdering SuccessOrdering,
1411 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001412 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001413 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001414 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001415 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001416 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1417 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001418 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001419}
Tim Northover420a2162014-06-13 14:24:07 +00001420
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001421//===----------------------------------------------------------------------===//
1422// AtomicRMWInst Implementation
1423//===----------------------------------------------------------------------===//
1424
1425void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1426 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001427 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001428 Op<0>() = Ptr;
1429 Op<1>() = Val;
1430 setOperation(Operation);
1431 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001432 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001433
1434 assert(getOperand(0) && getOperand(1) &&
1435 "All operands must be non-null!");
1436 assert(getOperand(0)->getType()->isPointerTy() &&
1437 "Ptr must have pointer type!");
1438 assert(getOperand(1)->getType() ==
1439 cast<PointerType>(getOperand(0)->getType())->getElementType()
1440 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001441 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001442 "AtomicRMW instructions must be atomic!");
1443}
1444
1445AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1446 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001447 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001448 Instruction *InsertBefore)
1449 : Instruction(Val->getType(), AtomicRMW,
1450 OperandTraits<AtomicRMWInst>::op_begin(this),
1451 OperandTraits<AtomicRMWInst>::operands(this),
1452 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001453 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001454}
1455
1456AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1457 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001458 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001459 BasicBlock *InsertAtEnd)
1460 : Instruction(Val->getType(), AtomicRMW,
1461 OperandTraits<AtomicRMWInst>::op_begin(this),
1462 OperandTraits<AtomicRMWInst>::operands(this),
1463 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001464 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001465}
1466
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001467StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1468 switch (Op) {
1469 case AtomicRMWInst::Xchg:
1470 return "xchg";
1471 case AtomicRMWInst::Add:
1472 return "add";
1473 case AtomicRMWInst::Sub:
1474 return "sub";
1475 case AtomicRMWInst::And:
1476 return "and";
1477 case AtomicRMWInst::Nand:
1478 return "nand";
1479 case AtomicRMWInst::Or:
1480 return "or";
1481 case AtomicRMWInst::Xor:
1482 return "xor";
1483 case AtomicRMWInst::Max:
1484 return "max";
1485 case AtomicRMWInst::Min:
1486 return "min";
1487 case AtomicRMWInst::UMax:
1488 return "umax";
1489 case AtomicRMWInst::UMin:
1490 return "umin";
Matt Arsenault39508332019-01-22 18:18:02 +00001491 case AtomicRMWInst::FAdd:
1492 return "fadd";
1493 case AtomicRMWInst::FSub:
1494 return "fsub";
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001495 case AtomicRMWInst::BAD_BINOP:
1496 return "<invalid operation>";
1497 }
1498
1499 llvm_unreachable("invalid atomicrmw operation");
1500}
1501
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001502//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001503// FenceInst Implementation
1504//===----------------------------------------------------------------------===//
1505
Fangrui Songf78650a2018-07-30 19:41:25 +00001506FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001507 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001508 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001509 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001510 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001511 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001512}
1513
Fangrui Songf78650a2018-07-30 19:41:25 +00001514FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001515 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001516 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001517 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001518 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001519 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001520}
1521
1522//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001523// GetElementPtrInst Implementation
1524//===----------------------------------------------------------------------===//
1525
Jay Foadd1b78492011-07-25 09:48:08 +00001526void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001527 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001528 assert(getNumOperands() == 1 + IdxList.size() &&
1529 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001530 Op<0>() = Ptr;
Fangrui Song75709322018-11-17 01:44:25 +00001531 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001532 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001533}
1534
Gabor Greiff6caff662008-05-10 08:32:32 +00001535GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001536 : Instruction(GEPI.getType(), GetElementPtr,
1537 OperandTraits<GetElementPtrInst>::op_end(this) -
1538 GEPI.getNumOperands(),
1539 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001540 SourceElementType(GEPI.SourceElementType),
1541 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001542 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001543 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001544}
1545
Chris Lattner6090a422009-03-09 04:46:40 +00001546/// getIndexedType - Returns the type of the element that would be accessed with
1547/// a gep instruction with the specified parameters.
1548///
1549/// The Idxs pointer should point to a continuous piece of memory containing the
1550/// indices, either as Value* or uint64_t.
1551///
1552/// A null type is returned if the indices are invalid for the specified
1553/// pointer type.
1554///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001555template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001556static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001557 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001558 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001559 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001560
Chris Lattner6090a422009-03-09 04:46:40 +00001561 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001562 // it cannot be 'stepped over'.
1563 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001564 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001565
Dan Gohman1ecaf452008-05-31 00:58:22 +00001566 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001567 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001568 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001569 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001570 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001571 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001572 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001573 }
Craig Topperc6207612014-04-09 06:08:46 +00001574 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001575}
1576
David Blaikied288fb82015-03-30 21:41:43 +00001577Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1578 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001579}
1580
David Blaikied288fb82015-03-30 21:41:43 +00001581Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001582 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001583 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001584}
1585
David Blaikied288fb82015-03-30 21:41:43 +00001586Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1587 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001588}
1589
Chris Lattner45f15572007-04-14 00:12:57 +00001590/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1591/// zeros. If so, the result pointer and the first operand have the same
1592/// value, just potentially different types.
1593bool GetElementPtrInst::hasAllZeroIndices() const {
1594 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1595 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1596 if (!CI->isZero()) return false;
1597 } else {
1598 return false;
1599 }
1600 }
1601 return true;
1602}
1603
Chris Lattner27058292007-04-27 20:35:56 +00001604/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1605/// constant integers. If so, the result pointer and the first operand have
1606/// a constant offset between them.
1607bool GetElementPtrInst::hasAllConstantIndices() const {
1608 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1609 if (!isa<ConstantInt>(getOperand(i)))
1610 return false;
1611 }
1612 return true;
1613}
1614
Dan Gohman1b849082009-09-07 23:54:19 +00001615void GetElementPtrInst::setIsInBounds(bool B) {
1616 cast<GEPOperator>(this)->setIsInBounds(B);
1617}
Chris Lattner45f15572007-04-14 00:12:57 +00001618
Nick Lewycky28a5f252009-09-27 21:33:04 +00001619bool GetElementPtrInst::isInBounds() const {
1620 return cast<GEPOperator>(this)->isInBounds();
1621}
1622
Chandler Carruth1e140532012-12-11 10:29:10 +00001623bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1624 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001625 // Delegate to the generic GEPOperator implementation.
1626 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001627}
1628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001629//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001630// ExtractElementInst Implementation
1631//===----------------------------------------------------------------------===//
1632
1633ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001634 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001635 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001637 ExtractElement,
1638 OperandTraits<ExtractElementInst>::op_begin(this),
1639 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001640 assert(isValidOperands(Val, Index) &&
1641 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001642 Op<0>() = Val;
1643 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001644 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001645}
1646
1647ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001648 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001649 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001650 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001651 ExtractElement,
1652 OperandTraits<ExtractElementInst>::op_begin(this),
1653 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001654 assert(isValidOperands(Val, Index) &&
1655 "Invalid extractelement instruction operands!");
1656
Gabor Greif2d3024d2008-05-26 21:33:52 +00001657 Op<0>() = Val;
1658 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001659 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001660}
1661
Chris Lattner54865b32006-04-08 04:05:48 +00001662bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001663 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001664 return false;
1665 return true;
1666}
1667
Robert Bocchino23004482006-01-10 19:05:34 +00001668//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001669// InsertElementInst Implementation
1670//===----------------------------------------------------------------------===//
1671
Chris Lattner54865b32006-04-08 04:05:48 +00001672InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001673 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001674 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001675 : Instruction(Vec->getType(), InsertElement,
1676 OperandTraits<InsertElementInst>::op_begin(this),
1677 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001678 assert(isValidOperands(Vec, Elt, Index) &&
1679 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001680 Op<0>() = Vec;
1681 Op<1>() = Elt;
1682 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001683 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001684}
1685
Chris Lattner54865b32006-04-08 04:05:48 +00001686InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001687 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001688 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001689 : Instruction(Vec->getType(), InsertElement,
1690 OperandTraits<InsertElementInst>::op_begin(this),
1691 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001692 assert(isValidOperands(Vec, Elt, Index) &&
1693 "Invalid insertelement instruction operands!");
1694
Gabor Greif2d3024d2008-05-26 21:33:52 +00001695 Op<0>() = Vec;
1696 Op<1>() = Elt;
1697 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001698 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001699}
1700
Fangrui Songf78650a2018-07-30 19:41:25 +00001701bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001702 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001703 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001704 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001705
Reid Spencerd84d35b2007-02-15 02:26:10 +00001706 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001707 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001708
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001709 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001710 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001711 return true;
1712}
1713
Robert Bocchinoca27f032006-01-17 20:07:22 +00001714//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001715// ShuffleVectorInst Implementation
1716//===----------------------------------------------------------------------===//
1717
1718ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001719 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001720 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001721: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001722 cast<VectorType>(Mask->getType())->getNumElements()),
1723 ShuffleVector,
1724 OperandTraits<ShuffleVectorInst>::op_begin(this),
1725 OperandTraits<ShuffleVectorInst>::operands(this),
1726 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001727 assert(isValidOperands(V1, V2, Mask) &&
1728 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001729 Op<0>() = V1;
1730 Op<1>() = V2;
1731 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001732 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001733}
1734
1735ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001736 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001737 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001738: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1739 cast<VectorType>(Mask->getType())->getNumElements()),
1740 ShuffleVector,
1741 OperandTraits<ShuffleVectorInst>::op_begin(this),
1742 OperandTraits<ShuffleVectorInst>::operands(this),
1743 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001744 assert(isValidOperands(V1, V2, Mask) &&
1745 "Invalid shuffle vector instruction operands!");
1746
Gabor Greif2d3024d2008-05-26 21:33:52 +00001747 Op<0>() = V1;
1748 Op<1>() = V2;
1749 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001750 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001751}
1752
Sanjay Patelb276dd12019-03-31 15:01:30 +00001753void ShuffleVectorInst::commute() {
1754 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1755 int NumMaskElts = getMask()->getType()->getVectorNumElements();
1756 SmallVector<Constant*, 16> NewMask(NumMaskElts);
1757 Type *Int32Ty = Type::getInt32Ty(getContext());
1758 for (int i = 0; i != NumMaskElts; ++i) {
1759 int MaskElt = getMaskValue(i);
1760 if (MaskElt == -1) {
1761 NewMask[i] = UndefValue::get(Int32Ty);
1762 continue;
1763 }
1764 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask");
1765 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts;
1766 NewMask[i] = ConstantInt::get(Int32Ty, MaskElt);
1767 }
1768 Op<2>() = ConstantVector::get(NewMask);
1769 Op<0>().swap(Op<1>());
1770}
1771
Mon P Wang25f01062008-11-10 04:46:22 +00001772bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001773 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001774 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001775 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001776 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001777
Chris Lattner1dcb6542012-01-25 23:49:49 +00001778 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001779 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001780 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001781 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001782
1783 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001784 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1785 return true;
1786
Sanjay Patel8bd52282017-04-19 16:22:19 +00001787 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001788 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001789 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001790 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001791 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001792 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001793 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001794 return false;
1795 }
1796 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001797 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001798 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001799
Sanjay Patel8bd52282017-04-19 16:22:19 +00001800 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001801 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1802 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1803 if (CDS->getElementAsInteger(i) >= V1Size*2)
1804 return false;
1805 return true;
1806 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001807
Chris Lattner1dcb6542012-01-25 23:49:49 +00001808 // The bitcode reader can create a place holder for a forward reference
1809 // used as the shuffle mask. When this occurs, the shuffle mask will
1810 // fall into this case and fail. To avoid this error, do this bit of
1811 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001812 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001813 if (CE->getOpcode() == Instruction::UserOp1)
1814 return true;
1815
1816 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001817}
1818
Sanjay Patel2ca33602018-06-19 18:44:00 +00001819int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001820 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001821 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001822 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001823 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001824 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001825 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001826 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001827}
1828
Sanjay Patel2ca33602018-06-19 18:44:00 +00001829void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001830 SmallVectorImpl<int> &Result) {
1831 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001832
Sanjay Patel8bd52282017-04-19 16:22:19 +00001833 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001834 for (unsigned i = 0; i != NumElts; ++i)
1835 Result.push_back(CDS->getElementAsInteger(i));
1836 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001837 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001838 for (unsigned i = 0; i != NumElts; ++i) {
1839 Constant *C = Mask->getAggregateElement(i);
1840 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001841 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001842 }
1843}
1844
Sanjay Patelac619a02018-08-30 15:05:38 +00001845static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001846 assert(!Mask.empty() && "Shuffle mask must contain elements");
1847 bool UsesLHS = false;
1848 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001849 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001850 if (Mask[i] == -1)
1851 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001852 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001853 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001854 UsesLHS |= (Mask[i] < NumOpElts);
1855 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001856 if (UsesLHS && UsesRHS)
1857 return false;
1858 }
1859 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1860 return true;
1861}
1862
Sanjay Patelac619a02018-08-30 15:05:38 +00001863bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1864 // We don't have vector operand size information, so assume operands are the
1865 // same size as the mask.
1866 return isSingleSourceMaskImpl(Mask, Mask.size());
1867}
1868
1869static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1870 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001871 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001872 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001873 if (Mask[i] == -1)
1874 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001875 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001876 return false;
1877 }
1878 return true;
1879}
1880
Sanjay Patelac619a02018-08-30 15:05:38 +00001881bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1882 // We don't have vector operand size information, so assume operands are the
1883 // same size as the mask.
1884 return isIdentityMaskImpl(Mask, Mask.size());
1885}
1886
Sanjay Patel2ca33602018-06-19 18:44:00 +00001887bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1888 if (!isSingleSourceMask(Mask))
1889 return false;
1890 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1891 if (Mask[i] == -1)
1892 continue;
1893 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1894 return false;
1895 }
1896 return true;
1897}
1898
1899bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1900 if (!isSingleSourceMask(Mask))
1901 return false;
1902 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1903 if (Mask[i] == -1)
1904 continue;
1905 if (Mask[i] != 0 && Mask[i] != NumElts)
1906 return false;
1907 }
1908 return true;
1909}
1910
1911bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1912 // Select is differentiated from identity. It requires using both sources.
1913 if (isSingleSourceMask(Mask))
1914 return false;
1915 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1916 if (Mask[i] == -1)
1917 continue;
1918 if (Mask[i] != i && Mask[i] != (NumElts + i))
1919 return false;
1920 }
1921 return true;
1922}
1923
1924bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1925 // Example masks that will return true:
1926 // v1 = <a, b, c, d>
1927 // v2 = <e, f, g, h>
1928 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1929 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1930
1931 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1932 int NumElts = Mask.size();
1933 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1934 return false;
1935
1936 // 2. The first element of the mask must be either a 0 or a 1.
1937 if (Mask[0] != 0 && Mask[0] != 1)
1938 return false;
1939
1940 // 3. The difference between the first 2 elements must be equal to the
1941 // number of elements in the mask.
1942 if ((Mask[1] - Mask[0]) != NumElts)
1943 return false;
1944
1945 // 4. The difference between consecutive even-numbered and odd-numbered
1946 // elements must be equal to 2.
1947 for (int i = 2; i < NumElts; ++i) {
1948 int MaskEltVal = Mask[i];
1949 if (MaskEltVal == -1)
1950 return false;
1951 int MaskEltPrevVal = Mask[i - 2];
1952 if (MaskEltVal - MaskEltPrevVal != 2)
1953 return false;
1954 }
1955 return true;
1956}
1957
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001958bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
1959 int NumSrcElts, int &Index) {
1960 // Must extract from a single source.
1961 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
1962 return false;
1963
1964 // Must be smaller (else this is an Identity shuffle).
Fangrui Song4f2e66c2018-11-09 16:45:37 +00001965 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001966 return false;
1967
1968 // Find start of extraction, accounting that we may start with an UNDEF.
1969 int SubIndex = -1;
1970 for (int i = 0, e = Mask.size(); i != e; ++i) {
1971 int M = Mask[i];
1972 if (M < 0)
1973 continue;
1974 int Offset = (M % NumSrcElts) - i;
1975 if (0 <= SubIndex && SubIndex != Offset)
1976 return false;
1977 SubIndex = Offset;
1978 }
1979
1980 if (0 <= SubIndex) {
1981 Index = SubIndex;
1982 return true;
1983 }
1984 return false;
1985}
1986
Sanjay Patelac619a02018-08-30 15:05:38 +00001987bool ShuffleVectorInst::isIdentityWithPadding() const {
1988 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1989 int NumMaskElts = getType()->getVectorNumElements();
1990 if (NumMaskElts <= NumOpElts)
1991 return false;
1992
1993 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00001994 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00001995 if (!isIdentityMaskImpl(Mask, NumOpElts))
1996 return false;
1997
1998 // All extending must be with undef elements.
1999 for (int i = NumOpElts; i < NumMaskElts; ++i)
2000 if (Mask[i] != -1)
2001 return false;
2002
2003 return true;
2004}
2005
2006bool ShuffleVectorInst::isIdentityWithExtract() const {
2007 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
2008 int NumMaskElts = getType()->getVectorNumElements();
2009 if (NumMaskElts >= NumOpElts)
2010 return false;
2011
2012 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
2013}
Sanjay Patel2ca33602018-06-19 18:44:00 +00002014
Sanjay Patelfd4976b2018-09-20 15:21:52 +00002015bool ShuffleVectorInst::isConcat() const {
2016 // Vector concatenation is differentiated from identity with padding.
2017 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
2018 return false;
2019
2020 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
2021 int NumMaskElts = getType()->getVectorNumElements();
2022 if (NumMaskElts != NumOpElts * 2)
2023 return false;
2024
2025 // Use the mask length rather than the operands' vector lengths here. We
2026 // already know that the shuffle returns a vector twice as long as the inputs,
2027 // and neither of the inputs are undef vectors. If the mask picks consecutive
2028 // elements from both inputs, then this is a concatenation of the inputs.
2029 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
2030}
2031
Dan Gohman12fce772008-05-15 19:50:34 +00002032//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00002033// InsertValueInst Class
2034//===----------------------------------------------------------------------===//
2035
Fangrui Songf78650a2018-07-30 19:41:25 +00002036void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00002037 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002038 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00002039
2040 // There's no fundamental reason why we require at least one index
2041 // (other than weirdness with &*IdxBegin being invalid; see
2042 // getelementptr's init routine for example). But there's no
2043 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002044 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00002045
2046 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00002047 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00002048 Op<0>() = Agg;
2049 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00002050
Jay Foad57aa6362011-07-13 10:26:04 +00002051 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002052 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002053}
2054
2055InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00002056 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002057 OperandTraits<InsertValueInst>::op_begin(this), 2),
2058 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00002059 Op<0>() = IVI.getOperand(0);
2060 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002061 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002062}
2063
2064//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002065// ExtractValueInst Class
2066//===----------------------------------------------------------------------===//
2067
Jay Foad57aa6362011-07-13 10:26:04 +00002068void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002069 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002070
Jay Foad57aa6362011-07-13 10:26:04 +00002071 // There's no fundamental reason why we require at least one index.
2072 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002073 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002074
Jay Foad57aa6362011-07-13 10:26:04 +00002075 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002076 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002077}
2078
2079ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002080 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002081 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002082 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002083}
2084
Dan Gohman12fce772008-05-15 19:50:34 +00002085// getIndexedType - Returns the type of the element that would be extracted
2086// with an extractvalue instruction with the specified parameters.
2087//
2088// A null type is returned if the indices are invalid for the specified
2089// pointer type.
2090//
Chris Lattner229907c2011-07-18 04:54:35 +00002091Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002092 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002093 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002094 // We can't use CompositeType::indexValid(Index) here.
2095 // indexValid() always returns true for arrays because getelementptr allows
2096 // out-of-bounds indices. Since we don't allow those for extractvalue and
2097 // insertvalue we need to check array indexing manually.
2098 // Since the only other types we can index into are struct types it's just
2099 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002100 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002101 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002102 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002103 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002104 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002105 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002106 } else {
2107 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002108 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002109 }
2110
2111 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002112 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002113 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002114}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002115
2116//===----------------------------------------------------------------------===//
Cameron McInallycbde0d92018-11-13 18:15:47 +00002117// UnaryOperator Class
2118//===----------------------------------------------------------------------===//
2119
2120UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2121 Type *Ty, const Twine &Name,
2122 Instruction *InsertBefore)
2123 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2124 Op<0>() = S;
2125 setName(Name);
2126 AssertOK();
2127}
2128
2129UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2130 Type *Ty, const Twine &Name,
2131 BasicBlock *InsertAtEnd)
2132 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2133 Op<0>() = S;
2134 setName(Name);
2135 AssertOK();
2136}
2137
2138UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2139 const Twine &Name,
2140 Instruction *InsertBefore) {
2141 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2142}
2143
2144UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2145 const Twine &Name,
2146 BasicBlock *InsertAtEnd) {
2147 UnaryOperator *Res = Create(Op, S, Name);
2148 InsertAtEnd->getInstList().push_back(Res);
2149 return Res;
2150}
2151
2152void UnaryOperator::AssertOK() {
2153 Value *LHS = getOperand(0);
2154 (void)LHS; // Silence warnings.
2155#ifndef NDEBUG
2156 switch (getOpcode()) {
2157 case FNeg:
2158 assert(getType() == LHS->getType() &&
2159 "Unary operation should return same type as operand!");
2160 assert(getType()->isFPOrFPVectorTy() &&
2161 "Tried to create a floating-point operation on a "
2162 "non-floating-point type!");
2163 break;
2164 default: llvm_unreachable("Invalid opcode provided");
2165 }
2166#endif
2167}
2168
2169//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002170// BinaryOperator Class
2171//===----------------------------------------------------------------------===//
2172
Chris Lattner2195fc42007-02-24 00:55:48 +00002173BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002174 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002175 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002176 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002177 OperandTraits<BinaryOperator>::op_begin(this),
2178 OperandTraits<BinaryOperator>::operands(this),
2179 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002180 Op<0>() = S1;
2181 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002182 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002183 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002184}
2185
Fangrui Songf78650a2018-07-30 19:41:25 +00002186BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002187 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002188 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002189 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002190 OperandTraits<BinaryOperator>::op_begin(this),
2191 OperandTraits<BinaryOperator>::operands(this),
2192 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002193 Op<0>() = S1;
2194 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002195 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002196 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002197}
2198
Craig Topper700892f2017-06-26 07:15:59 +00002199void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002200 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002201 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002202 assert(LHS->getType() == RHS->getType() &&
2203 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002204#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00002205 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002206 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002207 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002208 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002209 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002210 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002211 "Tried to create an integer operation on a non-integer type!");
2212 break;
2213 case FAdd: case FSub:
2214 case FMul:
2215 assert(getType() == LHS->getType() &&
2216 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002217 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002218 "Tried to create a floating-point operation on a "
2219 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002220 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002221 case UDiv:
2222 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002223 assert(getType() == LHS->getType() &&
2224 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002225 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002226 "Incorrect operand type (not integer) for S/UDIV");
2227 break;
2228 case FDiv:
2229 assert(getType() == LHS->getType() &&
2230 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002231 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002232 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002233 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002234 case URem:
2235 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002236 assert(getType() == LHS->getType() &&
2237 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002238 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002239 "Incorrect operand type (not integer) for S/UREM");
2240 break;
2241 case FRem:
2242 assert(getType() == LHS->getType() &&
2243 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002244 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002245 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002246 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002247 case Shl:
2248 case LShr:
2249 case AShr:
2250 assert(getType() == LHS->getType() &&
2251 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002252 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002253 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002254 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002255 case And: case Or:
2256 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002257 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002258 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002259 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002260 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002261 break;
Craig Topper700892f2017-06-26 07:15:59 +00002262 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002263 }
2264#endif
2265}
2266
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002267BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002268 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002269 Instruction *InsertBefore) {
2270 assert(S1->getType() == S2->getType() &&
2271 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002272 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002273}
2274
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002275BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002276 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002277 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002278 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002279 InsertAtEnd->getInstList().push_back(Res);
2280 return Res;
2281}
2282
Dan Gohman5476cfd2009-08-12 16:23:25 +00002283BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002284 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002285 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002286 return new BinaryOperator(Instruction::Sub,
2287 zero, Op,
2288 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002289}
2290
Dan Gohman5476cfd2009-08-12 16:23:25 +00002291BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002292 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002293 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002294 return new BinaryOperator(Instruction::Sub,
2295 zero, Op,
2296 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002297}
2298
Dan Gohman4ab44202009-12-18 02:58:50 +00002299BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2300 Instruction *InsertBefore) {
2301 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2302 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2303}
2304
2305BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2306 BasicBlock *InsertAtEnd) {
2307 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2308 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2309}
2310
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002311BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2312 Instruction *InsertBefore) {
2313 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2314 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2315}
2316
2317BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2318 BasicBlock *InsertAtEnd) {
2319 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2320 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2321}
2322
Dan Gohman5476cfd2009-08-12 16:23:25 +00002323BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002324 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002325 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002326 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002327 Op->getType(), Name, InsertBefore);
2328}
2329
Dan Gohman5476cfd2009-08-12 16:23:25 +00002330BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002331 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002332 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002333 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002334 Op->getType(), Name, InsertAtEnd);
2335}
2336
Dan Gohman5476cfd2009-08-12 16:23:25 +00002337BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002338 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002339 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002340 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002341 Op->getType(), Name, InsertBefore);
2342}
2343
Dan Gohman5476cfd2009-08-12 16:23:25 +00002344BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002345 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002346 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002347 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002348 Op->getType(), Name, InsertAtEnd);
2349}
2350
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002351// Exchange the two operands to this instruction. This instruction is safe to
2352// use on any binary instruction and does not modify the semantics of the
2353// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2354// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002355bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002356 if (!isCommutative())
2357 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002358 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002359 return false;
2360}
2361
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002362//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002363// FPMathOperator Class
2364//===----------------------------------------------------------------------===//
2365
Duncan Sands05f4df82012-04-16 16:28:59 +00002366float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002367 const MDNode *MD =
2368 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002369 if (!MD)
2370 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002371 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002372 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002373}
2374
Duncan Sands05f4df82012-04-16 16:28:59 +00002375//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002376// CastInst Class
2377//===----------------------------------------------------------------------===//
2378
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379// Just determine if this cast only deals with integral->integral conversion.
2380bool CastInst::isIntegerCast() const {
2381 switch (getOpcode()) {
2382 default: return false;
2383 case Instruction::ZExt:
2384 case Instruction::SExt:
2385 case Instruction::Trunc:
2386 return true;
2387 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002388 return getOperand(0)->getType()->isIntegerTy() &&
2389 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002391}
2392
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393bool CastInst::isLosslessCast() const {
2394 // Only BitCast can be lossless, exit fast if we're not BitCast
2395 if (getOpcode() != Instruction::BitCast)
2396 return false;
2397
2398 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002399 Type *SrcTy = getOperand(0)->getType();
2400 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 if (SrcTy == DstTy)
2402 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002403
Reid Spencer8d9336d2006-12-31 05:26:44 +00002404 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002405 if (SrcTy->isPointerTy())
2406 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 return false; // Other types have no identity values
2408}
2409
2410/// This function determines if the CastInst does not require any bits to be
2411/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002412/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002414/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002415/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002416/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002417/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002418bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002419 Type *SrcTy,
2420 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002421 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002422 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002423 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 case Instruction::Trunc:
2425 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002426 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 case Instruction::FPTrunc:
2428 case Instruction::FPExt:
2429 case Instruction::UIToFP:
2430 case Instruction::SIToFP:
2431 case Instruction::FPToUI:
2432 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002433 case Instruction::AddrSpaceCast:
2434 // TODO: Target informations may give a more accurate answer here.
2435 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 case Instruction::BitCast:
2437 return true; // BitCast never modifies bits.
2438 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002439 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002440 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002442 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002443 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 }
2445}
2446
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002447bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002448 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002449}
2450
2451/// This function determines if a pair of casts can be eliminated and what
2452/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453/// instructions like this:
2454/// * %F = firstOpcode SrcTy %x to MidTy
2455/// * %S = secondOpcode MidTy %F to DstTy
2456/// The function returns a resultOpcode so these two casts can be replaced with:
2457/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002458/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459unsigned CastInst::isEliminableCastPair(
2460 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002461 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2462 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 // Define the 144 possibilities for these two cast instructions. The values
2464 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002465 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002466 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 // the following cast properties:
2468 //
2469 // Size Compare Source Destination
2470 // Operator Src ? Size Type Sign Type Sign
2471 // -------- ------------ ------------------- ---------------------
2472 // TRUNC > Integer Any Integral Any
2473 // ZEXT < Integral Unsigned Integer Any
2474 // SEXT < Integral Signed Integer Any
2475 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002476 // FPTOSI n/a FloatPt n/a Integral Signed
2477 // UITOFP n/a Integral Unsigned FloatPt n/a
2478 // SITOFP n/a Integral Signed FloatPt n/a
2479 // FPTRUNC > FloatPt n/a FloatPt n/a
2480 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 // PTRTOINT n/a Pointer n/a Integral Unsigned
2482 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002483 // BITCAST = FirstClass n/a FirstClass n/a
2484 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002485 //
2486 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002487 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2488 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002489 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002490 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002491 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002492 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002493 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2495 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 // T F F U S F F P I B A -+
2497 // R Z S P P I I T P 2 N T S |
2498 // U E E 2 2 2 2 R E I T C C +- secondOp
2499 // N X X U S F F N X N 2 V V |
2500 // C T T I I P P C T T P T T -+
2501 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002502 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002503 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2504 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2505 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2506 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2507 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002508 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002509 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002510 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2511 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2512 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2513 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002515
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002516 // TODO: This logic could be encoded into the table above and handled in the
2517 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002518 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002519 // merging. However, any pair of bitcasts are allowed.
2520 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2521 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2522 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002523
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002524 // Check if any of the casts convert scalars <-> vectors.
2525 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2526 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2527 if (!AreBothBitcasts)
2528 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529
2530 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2531 [secondOp-Instruction::CastOpsBegin];
2532 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002533 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002536 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002539 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002542 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002543 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002544 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002545 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002546 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 return firstOp;
2548 return 0;
2549 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002550 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002551 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002552 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553 return firstOp;
2554 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002555 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002556 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002557 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002558 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559 return secondOp;
2560 return 0;
2561 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002562 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002563 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002564 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565 return secondOp;
2566 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002567 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002568 // Cannot simplify if address spaces are different!
2569 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2570 return 0;
2571
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002572 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002573 // We can still fold this without knowing the actual sizes as long we
2574 // know that the intermediate pointer is the largest possible
2575 // pointer size.
2576 // FIXME: Is this always true?
2577 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002578 return Instruction::BitCast;
2579
2580 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002581 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002582 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002583 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584 if (MidSize >= PtrSize)
2585 return Instruction::BitCast;
2586 return 0;
2587 }
2588 case 8: {
2589 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2590 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2591 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002592 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2593 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594 if (SrcSize == DstSize)
2595 return Instruction::BitCast;
2596 else if (SrcSize < DstSize)
2597 return firstOp;
2598 return secondOp;
2599 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002600 case 9:
2601 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002603 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002605 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002606 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002607 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002608 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2609 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610 if (SrcSize <= PtrSize && SrcSize == DstSize)
2611 return Instruction::BitCast;
2612 return 0;
2613 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002614 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002615 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2616 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2617 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2618 return Instruction::AddrSpaceCast;
2619 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002620 case 13:
2621 // FIXME: this state can be merged with (1), but the following assert
2622 // is useful to check the correcteness of the sequence due to semantic
2623 // change of bitcast.
2624 assert(
2625 SrcTy->isPtrOrPtrVectorTy() &&
2626 MidTy->isPtrOrPtrVectorTy() &&
2627 DstTy->isPtrOrPtrVectorTy() &&
2628 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2629 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2630 "Illegal addrspacecast, bitcast sequence!");
2631 // Allowed, use first cast's opcode
2632 return firstOp;
2633 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002634 // bitcast, addrspacecast -> addrspacecast if the element type of
2635 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002636 if (SrcTy->getScalarType()->getPointerElementType() ==
2637 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002638 return Instruction::AddrSpaceCast;
2639 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002640 case 15:
2641 // FIXME: this state can be merged with (1), but the following assert
2642 // is useful to check the correcteness of the sequence due to semantic
2643 // change of bitcast.
2644 assert(
2645 SrcTy->isIntOrIntVectorTy() &&
2646 MidTy->isPtrOrPtrVectorTy() &&
2647 DstTy->isPtrOrPtrVectorTy() &&
2648 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2649 "Illegal inttoptr, bitcast sequence!");
2650 // Allowed, use first cast's opcode
2651 return firstOp;
2652 case 16:
2653 // FIXME: this state can be merged with (2), but the following assert
2654 // is useful to check the correcteness of the sequence due to semantic
2655 // change of bitcast.
2656 assert(
2657 SrcTy->isPtrOrPtrVectorTy() &&
2658 MidTy->isPtrOrPtrVectorTy() &&
2659 DstTy->isIntOrIntVectorTy() &&
2660 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2661 "Illegal bitcast, ptrtoint sequence!");
2662 // Allowed, use second cast's opcode
2663 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002664 case 17:
2665 // (sitofp (zext x)) -> (uitofp x)
2666 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002667 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002668 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002670 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 default:
Craig Topperc514b542012-02-05 22:14:15 +00002672 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674}
2675
Fangrui Songf78650a2018-07-30 19:41:25 +00002676CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002677 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002678 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 // Construct and return the appropriate CastInst subclass
2680 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002681 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2682 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2683 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2684 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2685 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2686 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2687 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2688 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2689 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2690 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2691 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2692 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2693 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2694 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696}
2697
Chris Lattner229907c2011-07-18 04:54:35 +00002698CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002699 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002700 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701 // Construct and return the appropriate CastInst subclass
2702 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002703 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2704 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2705 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2706 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2707 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2708 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2709 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2710 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2711 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2712 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2713 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2714 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2715 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2716 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
Fangrui Songf78650a2018-07-30 19:41:25 +00002720CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002721 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002722 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002723 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002724 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2725 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002726}
2727
Fangrui Songf78650a2018-07-30 19:41:25 +00002728CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002729 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002730 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002731 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002732 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2733 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002734}
2735
Fangrui Songf78650a2018-07-30 19:41:25 +00002736CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002737 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002738 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002739 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2741 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002742}
2743
Fangrui Songf78650a2018-07-30 19:41:25 +00002744CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002745 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002746 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002747 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002748 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2749 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002750}
2751
Chris Lattner229907c2011-07-18 04:54:35 +00002752CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002753 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002754 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002755 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002756 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2757 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002758}
2759
Chris Lattner229907c2011-07-18 04:54:35 +00002760CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002761 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002762 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002763 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002764 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2765 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002766}
2767
Chris Lattner229907c2011-07-18 04:54:35 +00002768CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002769 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002770 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002771 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2772 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2773 "Invalid cast");
2774 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002775 assert((!Ty->isVectorTy() ||
2776 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002777 "Invalid cast");
2778
Matt Arsenault065ced92013-07-31 00:17:33 +00002779 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002780 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002781
Matt Arsenault740980e2014-07-14 17:24:35 +00002782 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002783}
2784
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002785/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002786CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2787 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002788 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002789 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2790 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002791 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002792 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002793 assert((!Ty->isVectorTy() ||
2794 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002795 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002796
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002797 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002798 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002799
Matt Arsenault740980e2014-07-14 17:24:35 +00002800 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2801}
2802
2803CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2804 Value *S, Type *Ty,
2805 const Twine &Name,
2806 BasicBlock *InsertAtEnd) {
2807 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2808 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2809
2810 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2811 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2812
2813 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2814}
2815
2816CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2817 Value *S, Type *Ty,
2818 const Twine &Name,
2819 Instruction *InsertBefore) {
2820 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2821 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2822
2823 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002824 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2825
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002826 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002827}
2828
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002829CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2830 const Twine &Name,
2831 Instruction *InsertBefore) {
2832 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2833 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2834 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2835 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2836
2837 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2838}
2839
Matt Arsenault740980e2014-07-14 17:24:35 +00002840CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002841 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002842 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002843 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002844 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002845 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2846 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002847 Instruction::CastOps opcode =
2848 (SrcBits == DstBits ? Instruction::BitCast :
2849 (SrcBits > DstBits ? Instruction::Trunc :
2850 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002851 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002852}
2853
Fangrui Songf78650a2018-07-30 19:41:25 +00002854CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002855 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002856 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002857 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002858 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002859 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2860 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002861 Instruction::CastOps opcode =
2862 (SrcBits == DstBits ? Instruction::BitCast :
2863 (SrcBits > DstBits ? Instruction::Trunc :
2864 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002865 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002866}
2867
Fangrui Songf78650a2018-07-30 19:41:25 +00002868CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2869 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002870 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002871 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002872 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002873 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2874 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002875 Instruction::CastOps opcode =
2876 (SrcBits == DstBits ? Instruction::BitCast :
2877 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002878 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002879}
2880
Fangrui Songf78650a2018-07-30 19:41:25 +00002881CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2882 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002883 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002884 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002885 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002886 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2887 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002888 Instruction::CastOps opcode =
2889 (SrcBits == DstBits ? Instruction::BitCast :
2890 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002891 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002892}
2893
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002894// Check whether it is valid to call getCastOpcode for these types.
2895// This routine must be kept in sync with getCastOpcode.
2896bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2897 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2898 return false;
2899
2900 if (SrcTy == DestTy)
2901 return true;
2902
2903 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2904 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2905 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2906 // An element by element cast. Valid if casting the elements is valid.
2907 SrcTy = SrcVecTy->getElementType();
2908 DestTy = DestVecTy->getElementType();
2909 }
2910
2911 // Get the bit sizes, we'll need these
2912 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2913 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2914
2915 // Run through the possibilities ...
2916 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002917 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002918 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002919 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002920 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002921 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002922 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002923 // Casting from something else
2924 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002925 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002926 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2927 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002928 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002929 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002930 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002931 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002932 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002933 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002934 return false;
2935 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002936 if (DestTy->isVectorTy()) // Casting to vector
2937 return DestBits == SrcBits;
2938 if (DestTy->isPointerTy()) { // Casting to pointer
2939 if (SrcTy->isPointerTy()) // Casting from pointer
2940 return true;
2941 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002942 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002943 if (DestTy->isX86_MMXTy()) {
2944 if (SrcTy->isVectorTy())
2945 return DestBits == SrcBits; // 64-bit vector to MMX
2946 return false;
2947 } // Casting to something else
2948 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002949}
2950
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002951bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2952 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2953 return false;
2954
2955 if (SrcTy == DestTy)
2956 return true;
2957
2958 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2959 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2960 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2961 // An element by element cast. Valid if casting the elements is valid.
2962 SrcTy = SrcVecTy->getElementType();
2963 DestTy = DestVecTy->getElementType();
2964 }
2965 }
2966 }
2967
2968 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2969 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2970 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2971 }
2972 }
2973
2974 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2975 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2976
2977 // Could still have vectors of pointers if the number of elements doesn't
2978 // match
2979 if (SrcBits == 0 || DestBits == 0)
2980 return false;
2981
2982 if (SrcBits != DestBits)
2983 return false;
2984
2985 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2986 return false;
2987
2988 return true;
2989}
2990
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002991bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002992 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002993 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002994 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2995 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002996 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2997 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002998 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2999 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00003000 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3001 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003002
3003 return isBitCastable(SrcTy, DestTy);
3004}
3005
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003006// Provide a way to get a "cast" where the cast opcode is inferred from the
3007// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003008// logic in the castIsValid function below. This axiom should hold:
3009// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3010// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003012// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003014CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003015 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3016 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017
Duncan Sands55e50902008-01-06 10:12:28 +00003018 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3019 "Only first class types are castable!");
3020
Duncan Sandsa8514532011-05-18 07:13:41 +00003021 if (SrcTy == DestTy)
3022 return BitCast;
3023
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003024 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003025 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3026 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003027 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3028 // An element by element cast. Find the appropriate opcode based on the
3029 // element types.
3030 SrcTy = SrcVecTy->getElementType();
3031 DestTy = DestVecTy->getElementType();
3032 }
3033
3034 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003035 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3036 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003037
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003039 if (DestTy->isIntegerTy()) { // Casting to integral
3040 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 if (DestBits < SrcBits)
3042 return Trunc; // int -> smaller int
3043 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003044 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 return SExt; // signed -> SEXT
3046 else
3047 return ZExt; // unsigned -> ZEXT
3048 } else {
3049 return BitCast; // Same size, No-op cast
3050 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003051 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00003052 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 return FPToSI; // FP -> sint
3054 else
Fangrui Songf78650a2018-07-30 19:41:25 +00003055 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003056 } else if (SrcTy->isVectorTy()) {
3057 assert(DestBits == SrcBits &&
3058 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059 return BitCast; // Same size, no-op cast
3060 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003061 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003062 "Casting from a value that is not first-class type");
3063 return PtrToInt; // ptr -> int
3064 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003065 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3066 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003067 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068 return SIToFP; // sint -> FP
3069 else
3070 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003071 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 if (DestBits < SrcBits) {
3073 return FPTrunc; // FP -> smaller FP
3074 } else if (DestBits > SrcBits) {
3075 return FPExt; // FP -> larger FP
3076 } else {
3077 return BitCast; // same size, no-op cast
3078 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003079 } else if (SrcTy->isVectorTy()) {
3080 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003081 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003082 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003084 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003085 } else if (DestTy->isVectorTy()) {
3086 assert(DestBits == SrcBits &&
3087 "Illegal cast to vector (wrong type or size)");
3088 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003089 } else if (DestTy->isPointerTy()) {
3090 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003091 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3092 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003093 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003094 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003097 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003098 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003099 if (SrcTy->isVectorTy()) {
3100 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003101 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003102 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003103 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003104 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003105 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106}
3107
3108//===----------------------------------------------------------------------===//
3109// CastInst SubClass Constructors
3110//===----------------------------------------------------------------------===//
3111
3112/// Check that the construction parameters for a CastInst are correct. This
3113/// could be broken out into the separate constructors but it is useful to have
3114/// it in one place and to eliminate the redundant code for getting the sizes
3115/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00003116bool
Chris Lattner229907c2011-07-18 04:54:35 +00003117CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003118 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003119 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003120
Chris Lattner37bc78a2010-01-26 21:51:43 +00003121 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3122 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123 return false;
3124
3125 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003126 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3127 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128
Duncan Sands7f646562011-05-18 09:21:57 +00003129 // If these are vector types, get the lengths of the vectors (using zero for
3130 // scalar types means that checking that vector lengths match also checks that
3131 // scalars are not being converted to vectors or vectors to scalars).
3132 unsigned SrcLength = SrcTy->isVectorTy() ?
3133 cast<VectorType>(SrcTy)->getNumElements() : 0;
3134 unsigned DstLength = DstTy->isVectorTy() ?
3135 cast<VectorType>(DstTy)->getNumElements() : 0;
3136
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137 // Switch on the opcode provided
3138 switch (op) {
3139 default: return false; // This is an input error
3140 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003141 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3142 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003144 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3145 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00003146 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003147 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3148 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003150 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3151 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003153 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3154 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003157 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3158 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003161 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3162 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003164 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003165 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003166 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3167 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3168 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003169 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003170 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003171 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003172 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003173 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3174 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3175 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003176 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003177 case Instruction::BitCast: {
3178 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3179 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3180
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181 // BitCast implies a no-op cast of type only. No bits change.
3182 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003183 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184 return false;
3185
Alp Tokerf907b892013-12-05 05:44:44 +00003186 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003187 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003188 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003189 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3190
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003191 // If both are pointers then the address spaces must match.
3192 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3193 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003194
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003195 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003196 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3197 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3198 if (SrcVecTy && DstVecTy)
3199 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3200 if (SrcVecTy)
3201 return SrcVecTy->getNumElements() == 1;
3202 if (DstVecTy)
3203 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003204
3205 return true;
3206 }
3207 case Instruction::AddrSpaceCast: {
3208 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3209 if (!SrcPtrTy)
3210 return false;
3211
3212 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3213 if (!DstPtrTy)
3214 return false;
3215
3216 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3217 return false;
3218
3219 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3220 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3221 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3222
3223 return false;
3224 }
3225
3226 return true;
3227 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228 }
3229}
3230
3231TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003232 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235}
3236
3237TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003239) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
3243ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003245) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003251) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003255 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003256) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258}
3259
Jeff Cohencc08c832006-12-02 02:22:01 +00003260SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003261 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003262) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
3265
3266FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003267 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003268) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270}
3271
3272FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003273 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003274) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276}
3277
3278FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003279 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003280) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282}
3283
3284FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003285 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003286) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288}
3289
3290UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003291 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003292) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294}
3295
3296UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003297 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003298) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300}
3301
3302SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003303 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003304) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306}
3307
3308SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003309 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003310) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312}
3313
3314FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003315 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003316) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318}
3319
3320FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003321 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003322) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324}
3325
3326FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003327 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003328) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330}
3331
3332FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003333 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003334) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336}
3337
3338PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003339 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003340) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342}
3343
3344PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003345 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003346) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003348}
3349
3350IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003351 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003352) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003354}
3355
3356IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003357 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003358) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003360}
3361
3362BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003363 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003364) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003366}
3367
3368BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003369 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003370) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003372}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003373
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003374AddrSpaceCastInst::AddrSpaceCastInst(
3375 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3376) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3378}
3379
3380AddrSpaceCastInst::AddrSpaceCastInst(
3381 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3382) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3384}
3385
Chris Lattnerf16dc002006-09-17 19:29:56 +00003386//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003387// CmpInst Classes
3388//===----------------------------------------------------------------------===//
3389
Craig Topper1c3f2832015-12-15 06:11:33 +00003390CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Pateld1172a02018-11-07 00:00:42 +00003391 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3392 Instruction *FlagsSource)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003393 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003394 OperandTraits<CmpInst>::op_begin(this),
3395 OperandTraits<CmpInst>::operands(this),
3396 InsertBefore) {
Sanjay Pateld1172a02018-11-07 00:00:42 +00003397 Op<0>() = LHS;
3398 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003399 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003400 setName(Name);
Sanjay Pateld1172a02018-11-07 00:00:42 +00003401 if (FlagsSource)
3402 copyIRFlags(FlagsSource);
Reid Spencerd9436b62006-11-20 01:22:35 +00003403}
Gabor Greiff6caff662008-05-10 08:32:32 +00003404
Craig Topper1c3f2832015-12-15 06:11:33 +00003405CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3406 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003407 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003408 OperandTraits<CmpInst>::op_begin(this),
3409 OperandTraits<CmpInst>::operands(this),
3410 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003411 Op<0>() = LHS;
3412 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003413 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003414 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003415}
3416
3417CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003418CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003419 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003420 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003421 if (InsertBefore)
3422 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3423 S1, S2, Name);
3424 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003425 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003426 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003427 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003428
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003429 if (InsertBefore)
3430 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3431 S1, S2, Name);
3432 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003433 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003434 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003435}
3436
3437CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003438CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003439 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003440 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003441 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3442 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003443 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003444 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3445 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003446}
3447
3448void CmpInst::swapOperands() {
3449 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3450 IC->swapOperands();
3451 else
3452 cast<FCmpInst>(this)->swapOperands();
3453}
3454
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003455bool CmpInst::isCommutative() const {
3456 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003457 return IC->isCommutative();
3458 return cast<FCmpInst>(this)->isCommutative();
3459}
3460
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003461bool CmpInst::isEquality() const {
3462 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003463 return IC->isEquality();
3464 return cast<FCmpInst>(this)->isEquality();
3465}
3466
Dan Gohman4e724382008-05-31 02:47:54 +00003467CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003468 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003469 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003470 case ICMP_EQ: return ICMP_NE;
3471 case ICMP_NE: return ICMP_EQ;
3472 case ICMP_UGT: return ICMP_ULE;
3473 case ICMP_ULT: return ICMP_UGE;
3474 case ICMP_UGE: return ICMP_ULT;
3475 case ICMP_ULE: return ICMP_UGT;
3476 case ICMP_SGT: return ICMP_SLE;
3477 case ICMP_SLT: return ICMP_SGE;
3478 case ICMP_SGE: return ICMP_SLT;
3479 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003480
Dan Gohman4e724382008-05-31 02:47:54 +00003481 case FCMP_OEQ: return FCMP_UNE;
3482 case FCMP_ONE: return FCMP_UEQ;
3483 case FCMP_OGT: return FCMP_ULE;
3484 case FCMP_OLT: return FCMP_UGE;
3485 case FCMP_OGE: return FCMP_ULT;
3486 case FCMP_OLE: return FCMP_UGT;
3487 case FCMP_UEQ: return FCMP_ONE;
3488 case FCMP_UNE: return FCMP_OEQ;
3489 case FCMP_UGT: return FCMP_OLE;
3490 case FCMP_ULT: return FCMP_OGE;
3491 case FCMP_UGE: return FCMP_OLT;
3492 case FCMP_ULE: return FCMP_OGT;
3493 case FCMP_ORD: return FCMP_UNO;
3494 case FCMP_UNO: return FCMP_ORD;
3495 case FCMP_TRUE: return FCMP_FALSE;
3496 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003497 }
3498}
3499
Tim Northoverde3aea0412016-08-17 20:25:25 +00003500StringRef CmpInst::getPredicateName(Predicate Pred) {
3501 switch (Pred) {
3502 default: return "unknown";
3503 case FCmpInst::FCMP_FALSE: return "false";
3504 case FCmpInst::FCMP_OEQ: return "oeq";
3505 case FCmpInst::FCMP_OGT: return "ogt";
3506 case FCmpInst::FCMP_OGE: return "oge";
3507 case FCmpInst::FCMP_OLT: return "olt";
3508 case FCmpInst::FCMP_OLE: return "ole";
3509 case FCmpInst::FCMP_ONE: return "one";
3510 case FCmpInst::FCMP_ORD: return "ord";
3511 case FCmpInst::FCMP_UNO: return "uno";
3512 case FCmpInst::FCMP_UEQ: return "ueq";
3513 case FCmpInst::FCMP_UGT: return "ugt";
3514 case FCmpInst::FCMP_UGE: return "uge";
3515 case FCmpInst::FCMP_ULT: return "ult";
3516 case FCmpInst::FCMP_ULE: return "ule";
3517 case FCmpInst::FCMP_UNE: return "une";
3518 case FCmpInst::FCMP_TRUE: return "true";
3519 case ICmpInst::ICMP_EQ: return "eq";
3520 case ICmpInst::ICMP_NE: return "ne";
3521 case ICmpInst::ICMP_SGT: return "sgt";
3522 case ICmpInst::ICMP_SGE: return "sge";
3523 case ICmpInst::ICMP_SLT: return "slt";
3524 case ICmpInst::ICMP_SLE: return "sle";
3525 case ICmpInst::ICMP_UGT: return "ugt";
3526 case ICmpInst::ICMP_UGE: return "uge";
3527 case ICmpInst::ICMP_ULT: return "ult";
3528 case ICmpInst::ICMP_ULE: return "ule";
3529 }
3530}
3531
Reid Spencer266e42b2006-12-23 06:05:41 +00003532ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3533 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003534 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003535 case ICMP_EQ: case ICMP_NE:
3536 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003537 return pred;
3538 case ICMP_UGT: return ICMP_SGT;
3539 case ICMP_ULT: return ICMP_SLT;
3540 case ICMP_UGE: return ICMP_SGE;
3541 case ICMP_ULE: return ICMP_SLE;
3542 }
3543}
3544
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003545ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3546 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003547 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003548 case ICMP_EQ: case ICMP_NE:
3549 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003550 return pred;
3551 case ICMP_SGT: return ICMP_UGT;
3552 case ICMP_SLT: return ICMP_ULT;
3553 case ICMP_SGE: return ICMP_UGE;
3554 case ICMP_SLE: return ICMP_ULE;
3555 }
3556}
3557
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003558CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3559 switch (pred) {
3560 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3561 case ICMP_SGT: return ICMP_SGE;
3562 case ICMP_SLT: return ICMP_SLE;
3563 case ICMP_SGE: return ICMP_SGT;
3564 case ICMP_SLE: return ICMP_SLT;
3565 case ICMP_UGT: return ICMP_UGE;
3566 case ICMP_ULT: return ICMP_ULE;
3567 case ICMP_UGE: return ICMP_UGT;
3568 case ICMP_ULE: return ICMP_ULT;
3569
3570 case FCMP_OGT: return FCMP_OGE;
3571 case FCMP_OLT: return FCMP_OLE;
3572 case FCMP_OGE: return FCMP_OGT;
3573 case FCMP_OLE: return FCMP_OLT;
3574 case FCMP_UGT: return FCMP_UGE;
3575 case FCMP_ULT: return FCMP_ULE;
3576 case FCMP_UGE: return FCMP_UGT;
3577 case FCMP_ULE: return FCMP_ULT;
3578 }
3579}
3580
Dan Gohman4e724382008-05-31 02:47:54 +00003581CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003582 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003583 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003584 case ICMP_EQ: case ICMP_NE:
3585 return pred;
3586 case ICMP_SGT: return ICMP_SLT;
3587 case ICMP_SLT: return ICMP_SGT;
3588 case ICMP_SGE: return ICMP_SLE;
3589 case ICMP_SLE: return ICMP_SGE;
3590 case ICMP_UGT: return ICMP_ULT;
3591 case ICMP_ULT: return ICMP_UGT;
3592 case ICMP_UGE: return ICMP_ULE;
3593 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003594
Reid Spencerd9436b62006-11-20 01:22:35 +00003595 case FCMP_FALSE: case FCMP_TRUE:
3596 case FCMP_OEQ: case FCMP_ONE:
3597 case FCMP_UEQ: case FCMP_UNE:
3598 case FCMP_ORD: case FCMP_UNO:
3599 return pred;
3600 case FCMP_OGT: return FCMP_OLT;
3601 case FCMP_OLT: return FCMP_OGT;
3602 case FCMP_OGE: return FCMP_OLE;
3603 case FCMP_OLE: return FCMP_OGE;
3604 case FCMP_UGT: return FCMP_ULT;
3605 case FCMP_ULT: return FCMP_UGT;
3606 case FCMP_UGE: return FCMP_ULE;
3607 case FCMP_ULE: return FCMP_UGE;
3608 }
3609}
3610
Max Kazantsevb299ade2018-02-07 11:16:29 +00003611CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3612 switch (pred) {
3613 case ICMP_SGT: return ICMP_SGE;
3614 case ICMP_SLT: return ICMP_SLE;
3615 case ICMP_UGT: return ICMP_UGE;
3616 case ICMP_ULT: return ICMP_ULE;
3617 case FCMP_OGT: return FCMP_OGE;
3618 case FCMP_OLT: return FCMP_OLE;
3619 case FCMP_UGT: return FCMP_UGE;
3620 case FCMP_ULT: return FCMP_ULE;
3621 default: return pred;
3622 }
3623}
3624
Sanjoy Das6e78b172015-10-22 19:57:34 +00003625CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3626 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3627
3628 switch (pred) {
3629 default:
3630 llvm_unreachable("Unknown predicate!");
3631 case CmpInst::ICMP_ULT:
3632 return CmpInst::ICMP_SLT;
3633 case CmpInst::ICMP_ULE:
3634 return CmpInst::ICMP_SLE;
3635 case CmpInst::ICMP_UGT:
3636 return CmpInst::ICMP_SGT;
3637 case CmpInst::ICMP_UGE:
3638 return CmpInst::ICMP_SGE;
3639 }
3640}
3641
Craig Topper1c3f2832015-12-15 06:11:33 +00003642bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003643 switch (predicate) {
3644 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003645 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003646 case ICmpInst::ICMP_UGE: return true;
3647 }
3648}
3649
Craig Topper1c3f2832015-12-15 06:11:33 +00003650bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003651 switch (predicate) {
3652 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003653 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003654 case ICmpInst::ICMP_SGE: return true;
3655 }
3656}
3657
Craig Topper1c3f2832015-12-15 06:11:33 +00003658bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003659 switch (predicate) {
3660 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003661 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3662 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003663 case FCmpInst::FCMP_ORD: return true;
3664 }
3665}
Fangrui Songf78650a2018-07-30 19:41:25 +00003666
Craig Topper1c3f2832015-12-15 06:11:33 +00003667bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003668 switch (predicate) {
3669 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003670 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3671 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003672 case FCmpInst::FCMP_UNO: return true;
3673 }
3674}
3675
Craig Topper1c3f2832015-12-15 06:11:33 +00003676bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003677 switch(predicate) {
3678 default: return false;
3679 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3680 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3681 }
3682}
3683
Craig Topper1c3f2832015-12-15 06:11:33 +00003684bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003685 switch(predicate) {
3686 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3687 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3688 default: return false;
3689 }
3690}
3691
Chad Rosier99bc4802016-04-21 16:18:02 +00003692bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003693 // If the predicates match, then we know the first condition implies the
3694 // second is true.
3695 if (Pred1 == Pred2)
3696 return true;
3697
3698 switch (Pred1) {
3699 default:
3700 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003701 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003702 // 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 +00003703 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3704 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003705 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3706 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3707 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3708 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3709 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3710 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3711 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3712 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003713 }
3714 return false;
3715}
3716
Chad Rosier99bc4802016-04-21 16:18:02 +00003717bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003718 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003719}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003720
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003721//===----------------------------------------------------------------------===//
3722// SwitchInst Implementation
3723//===----------------------------------------------------------------------===//
3724
Chris Lattnerbaf00152010-11-17 05:41:46 +00003725void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3726 assert(Value && Default && NumReserved);
3727 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003728 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003729 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003730
Pete Coopereb31b682015-05-21 22:48:54 +00003731 Op<0>() = Value;
3732 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003733}
3734
Chris Lattner2195fc42007-02-24 00:55:48 +00003735/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3736/// switch on and a default destination. The number of additional cases can
3737/// be specified here to make memory allocation more efficient. This
3738/// constructor can also autoinsert before another instruction.
3739SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3740 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003741 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3742 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003743 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003744}
3745
3746/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3747/// switch on and a default destination. The number of additional cases can
3748/// be specified here to make memory allocation more efficient. This
3749/// constructor also autoinserts at the end of the specified BasicBlock.
3750SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3751 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003752 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3753 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003754 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003755}
3756
Misha Brukmanb1c93172005-04-21 23:48:37 +00003757SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003758 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003759 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003760 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003761 Use *OL = getOperandList();
3762 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003763 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003764 OL[i] = InOL[i];
3765 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003766 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003767 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003768}
3769
3770/// addCase - Add an entry to the switch instruction...
3771///
Chris Lattner47ac1872005-02-24 05:32:09 +00003772void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003773 unsigned NewCaseIdx = getNumCases();
3774 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003775 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003776 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003777 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003778 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003779 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003780 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003781 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003782 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003783}
3784
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003785/// removeCase - This method removes the specified case and its successor
3786/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003787SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3788 unsigned idx = I->getCaseIndex();
3789
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003790 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003791
3792 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003793 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003794
Jay Foad14277722011-02-01 09:22:34 +00003795 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003796 if (2 + (idx + 1) * 2 != NumOps) {
3797 OL[2 + idx * 2] = OL[NumOps - 2];
3798 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003799 }
3800
3801 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003802 OL[NumOps-2].set(nullptr);
3803 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003804 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003805
3806 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003807}
3808
Jay Foade98f29d2011-04-01 08:00:58 +00003809/// growOperands - grow operands - This grows the operand list in response
3810/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003811///
Jay Foade98f29d2011-04-01 08:00:58 +00003812void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003813 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003814 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003815
3816 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003817 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003818}
3819
Chris Lattner3ed871f2009-10-27 19:13:16 +00003820//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003821// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003822//===----------------------------------------------------------------------===//
3823
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003824void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003825 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003826 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003827 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003828 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003829 allocHungoffUses(ReservedSpace);
3830
Pete Coopereb31b682015-05-21 22:48:54 +00003831 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003832}
3833
3834
Jay Foade98f29d2011-04-01 08:00:58 +00003835/// growOperands - grow operands - This grows the operand list in response
3836/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003837///
Jay Foade98f29d2011-04-01 08:00:58 +00003838void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003839 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003840 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003841
Chris Lattner3ed871f2009-10-27 19:13:16 +00003842 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003843 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003844}
3845
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003846IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3847 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003848 : Instruction(Type::getVoidTy(Address->getContext()),
3849 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003850 init(Address, NumCases);
3851}
3852
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003853IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3854 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003855 : Instruction(Type::getVoidTy(Address->getContext()),
3856 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003857 init(Address, NumCases);
3858}
3859
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003860IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003861 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3862 nullptr, IBI.getNumOperands()) {
Pete Cooper3fc30402015-06-10 22:38:46 +00003863 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003864 Use *OL = getOperandList();
3865 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003866 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3867 OL[i] = InOL[i];
3868 SubclassOptionalData = IBI.SubclassOptionalData;
3869}
3870
Chris Lattner3ed871f2009-10-27 19:13:16 +00003871/// addDestination - Add a destination.
3872///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003873void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003874 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003875 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003876 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003877 // Initialize some new operands.
3878 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003879 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003880 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003881}
3882
3883/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003884/// indirectbr instruction.
3885void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003886 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003887
Chris Lattner3ed871f2009-10-27 19:13:16 +00003888 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003889 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003890
3891 // Replace this value with the last one.
3892 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003893
Chris Lattner3ed871f2009-10-27 19:13:16 +00003894 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003895 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003896 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003897}
3898
Chris Lattner3ed871f2009-10-27 19:13:16 +00003899//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003900// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003901//===----------------------------------------------------------------------===//
3902
Chris Lattnerf22be932004-10-15 23:52:53 +00003903// Define these methods here so vtables don't get emitted into every translation
3904// unit that uses these classes.
3905
Pete Cooper75403d72015-06-24 20:22:23 +00003906GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003908}
3909
Cameron McInallycbde0d92018-11-13 18:15:47 +00003910UnaryOperator *UnaryOperator::cloneImpl() const {
3911 return Create(getOpcode(), Op<0>());
3912}
3913
Pete Cooper75403d72015-06-24 20:22:23 +00003914BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003915 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003916}
3917
Pete Cooper75403d72015-06-24 20:22:23 +00003918FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003919 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003920}
3921
Pete Cooper75403d72015-06-24 20:22:23 +00003922ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003923 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003924}
3925
Pete Cooper75403d72015-06-24 20:22:23 +00003926ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003927 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003928}
3929
Pete Cooper75403d72015-06-24 20:22:23 +00003930InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003931 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003932}
3933
Pete Cooper75403d72015-06-24 20:22:23 +00003934AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003935 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003936 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003937 (Value *)getOperand(0), getAlignment());
3938 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003939 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003940 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941}
3942
Pete Cooper75403d72015-06-24 20:22:23 +00003943LoadInst *LoadInst::cloneImpl() const {
James Y Knight84c1dbd2019-01-14 21:37:53 +00003944 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003945 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003946}
3947
Pete Cooper75403d72015-06-24 20:22:23 +00003948StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003949 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003950 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003951
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003952}
3953
Pete Cooper75403d72015-06-24 20:22:23 +00003954AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003955 AtomicCmpXchgInst *Result =
3956 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003957 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003958 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003959 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003960 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003961 return Result;
3962}
3963
Pete Cooper75403d72015-06-24 20:22:23 +00003964AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003965 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003966 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3967 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003968 Result->setVolatile(isVolatile());
3969 return Result;
3970}
3971
Pete Cooper75403d72015-06-24 20:22:23 +00003972FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003973 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003974}
3975
Pete Cooper75403d72015-06-24 20:22:23 +00003976TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003977 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003978}
3979
Pete Cooper75403d72015-06-24 20:22:23 +00003980ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003981 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003982}
3983
Pete Cooper75403d72015-06-24 20:22:23 +00003984SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003985 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003986}
3987
Pete Cooper75403d72015-06-24 20:22:23 +00003988FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003989 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003990}
3991
Pete Cooper75403d72015-06-24 20:22:23 +00003992FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003993 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003994}
3995
Pete Cooper75403d72015-06-24 20:22:23 +00003996UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003997 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003998}
3999
Pete Cooper75403d72015-06-24 20:22:23 +00004000SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004001 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004002}
4003
Pete Cooper75403d72015-06-24 20:22:23 +00004004FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004005 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004006}
4007
Pete Cooper75403d72015-06-24 20:22:23 +00004008FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004009 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004010}
4011
Pete Cooper75403d72015-06-24 20:22:23 +00004012PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004013 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004014}
4015
Pete Cooper75403d72015-06-24 20:22:23 +00004016IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004017 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004018}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004019
Pete Cooper75403d72015-06-24 20:22:23 +00004020BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004021 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004022}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00004023
Pete Cooper75403d72015-06-24 20:22:23 +00004024AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004025 return new AddrSpaceCastInst(getOperand(0), getType());
4026}
4027
Pete Cooper75403d72015-06-24 20:22:23 +00004028CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004029 if (hasOperandBundles()) {
4030 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4031 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4032 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004033 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004034}
4035
Pete Cooper75403d72015-06-24 20:22:23 +00004036SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004037 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004038}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004039
Pete Cooper75403d72015-06-24 20:22:23 +00004040VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004041 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004042}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004043
Pete Cooper75403d72015-06-24 20:22:23 +00004044ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004045 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004046}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004047
Pete Cooper75403d72015-06-24 20:22:23 +00004048InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004049 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004050}
4051
Pete Cooper75403d72015-06-24 20:22:23 +00004052ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004053 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004054}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004055
Pete Cooper75403d72015-06-24 20:22:23 +00004056PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004057
Pete Cooper75403d72015-06-24 20:22:23 +00004058LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004059 return new LandingPadInst(*this);
4060}
4061
Pete Cooper75403d72015-06-24 20:22:23 +00004062ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004063 return new(getNumOperands()) ReturnInst(*this);
4064}
4065
Pete Cooper75403d72015-06-24 20:22:23 +00004066BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004067 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004068}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004069
Pete Cooper75403d72015-06-24 20:22:23 +00004070SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004071
Pete Cooper75403d72015-06-24 20:22:23 +00004072IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004073 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004074}
4075
Pete Cooper75403d72015-06-24 20:22:23 +00004076InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004077 if (hasOperandBundles()) {
4078 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4079 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4080 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004081 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004082}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004083
Craig Topper784929d2019-02-08 20:48:56 +00004084CallBrInst *CallBrInst::cloneImpl() const {
4085 if (hasOperandBundles()) {
4086 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4087 return new (getNumOperands(), DescriptorBytes) CallBrInst(*this);
4088 }
4089 return new (getNumOperands()) CallBrInst(*this);
4090}
4091
Pete Cooper75403d72015-06-24 20:22:23 +00004092ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004093
David Majnemer654e1302015-07-31 17:58:14 +00004094CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4095 return new (getNumOperands()) CleanupReturnInst(*this);
4096}
4097
David Majnemer654e1302015-07-31 17:58:14 +00004098CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004099 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004100}
4101
David Majnemer8a1c45d2015-12-12 05:38:55 +00004102CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4103 return new CatchSwitchInst(*this);
4104}
4105
4106FuncletPadInst *FuncletPadInst::cloneImpl() const {
4107 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004108}
4109
Pete Cooper75403d72015-06-24 20:22:23 +00004110UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004111 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004112 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004113}