blob: ce281e0ed38ba9020f9bb8bf5d6ffa27e5bebf25 [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"
Wei Mi01f8d552019-04-22 17:04:51 +000031#include "llvm/IR/MDBuilder.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000032#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000035#include "llvm/IR/Type.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/AtomicOrdering.h"
38#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000040#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000041#include <algorithm>
42#include <cassert>
43#include <cstdint>
44#include <vector>
45
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000046using namespace llvm;
47
Chris Lattner3e13b8c2008-01-02 23:42:30 +000048//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-06-26 06:17:00 +000049// AllocaInst Class
50//===----------------------------------------------------------------------===//
51
52Optional<uint64_t>
53AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
54 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
55 if (isArrayAllocation()) {
56 auto C = dyn_cast<ConstantInt>(getArraySize());
57 if (!C)
58 return None;
59 Size *= C->getZExtValue();
60 }
61 return Size;
62}
63
64//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000065// CallSite Class
66//===----------------------------------------------------------------------===//
67
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000068User::op_iterator CallSite::getCallee() const {
Chandler Carruthe429c792018-11-22 10:31:35 +000069 return cast<CallBase>(getInstruction())->op_end() - 1;
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000070}
71
Gordon Henriksen14a55692007-12-10 02:14:30 +000072//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000073// SelectInst Class
74//===----------------------------------------------------------------------===//
75
76/// areInvalidOperands - Return a string if the specified operands are invalid
77/// for a select operation, otherwise return null.
78const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
79 if (Op1->getType() != Op2->getType())
80 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000081
82 if (Op1->getType()->isTokenTy())
83 return "select values cannot have token type";
84
Chris Lattner229907c2011-07-18 04:54:35 +000085 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000086 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000087 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000088 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000089 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000090 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000091 return "selected values for vector select must be vectors";
92 if (ET->getNumElements() != VT->getNumElements())
93 return "vector select requires selected vectors to have "
94 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000095 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000096 return "select condition must be i1 or <n x i1>";
97 }
Craig Topperc6207612014-04-09 06:08:46 +000098 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000099}
100
Chris Lattner88107952008-12-29 00:12:50 +0000101//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102// PHINode Class
103//===----------------------------------------------------------------------===//
104
105PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000106 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
107 ReservedSpace(PN.getNumOperands()) {
108 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 std::copy(PN.op_begin(), PN.op_end(), op_begin());
110 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000111 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112}
113
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000114// removeIncomingValue - Remove an incoming value. This is useful if a
115// predecessor basic block is deleted.
116Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000117 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118
119 // Move everything after this operand down.
120 //
121 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000122 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000124 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
125 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000126
127 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000128 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000129 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130
131 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000132 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000134 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135 eraseFromParent();
136 }
137 return Removed;
138}
139
Jay Foade98f29d2011-04-01 08:00:58 +0000140/// growOperands - grow operands - This grows the operand list in response
141/// to a push_back style of operation. This grows the number of ops by 1.5
142/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000143///
Jay Foade98f29d2011-04-01 08:00:58 +0000144void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000146 unsigned NumOps = e + e / 2;
147 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000150 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151}
152
Nate Begemanb3923212005-08-04 23:24:19 +0000153/// hasConstantValue - If the specified PHI node always merges together the same
154/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000155Value *PHINode::hasConstantValue() const {
156 // Exploit the fact that phi nodes always have at least one entry.
157 Value *ConstantValue = getIncomingValue(0);
158 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000159 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
160 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000161 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000162 // The case where the first value is this PHI.
163 ConstantValue = getIncomingValue(i);
164 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000165 if (ConstantValue == this)
166 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000167 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000168}
169
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000170/// hasConstantOrUndefValue - Whether the specified PHI node always merges
171/// together the same value, assuming that undefs result in the same value as
172/// non-undefs.
173/// Unlike \ref hasConstantValue, this does not return a value because the
174/// unique non-undef incoming value need not dominate the PHI node.
175bool PHINode::hasConstantOrUndefValue() const {
176 Value *ConstantValue = nullptr;
177 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
178 Value *Incoming = getIncomingValue(i);
179 if (Incoming != this && !isa<UndefValue>(Incoming)) {
180 if (ConstantValue && ConstantValue != Incoming)
181 return false;
182 ConstantValue = Incoming;
183 }
184 }
185 return true;
186}
187
Bill Wendlingfae14752011-08-12 20:24:12 +0000188//===----------------------------------------------------------------------===//
189// LandingPadInst Implementation
190//===----------------------------------------------------------------------===//
191
David Majnemer7fddecc2015-06-17 20:52:32 +0000192LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
193 const Twine &NameStr, Instruction *InsertBefore)
194 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
195 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000196}
197
David Majnemer7fddecc2015-06-17 20:52:32 +0000198LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
199 const Twine &NameStr, BasicBlock *InsertAtEnd)
200 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
201 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000202}
203
204LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000205 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
206 LP.getNumOperands()),
207 ReservedSpace(LP.getNumOperands()) {
208 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000209 Use *OL = getOperandList();
210 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
212 OL[I] = InOL[I];
213
214 setCleanup(LP.isCleanup());
215}
216
David Majnemer7fddecc2015-06-17 20:52:32 +0000217LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000218 const Twine &NameStr,
219 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000220 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000221}
222
David Majnemer7fddecc2015-06-17 20:52:32 +0000223LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000224 const Twine &NameStr,
225 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000226 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000227}
228
David Majnemer7fddecc2015-06-17 20:52:32 +0000229void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000230 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000231 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000232 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000233 setName(NameStr);
234 setCleanup(false);
235}
236
237/// growOperands - grow operands - This grows the operand list in response to a
238/// push_back style of operation. This grows the number of ops by 2 times.
239void LandingPadInst::growOperands(unsigned Size) {
240 unsigned e = getNumOperands();
241 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000242 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000243 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000244}
245
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000246void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000247 unsigned OpNo = getNumOperands();
248 growOperands(1);
249 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000250 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000251 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000252}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253
254//===----------------------------------------------------------------------===//
Chandler Carruthe429c792018-11-22 10:31:35 +0000255// CallBase Implementation
256//===----------------------------------------------------------------------===//
257
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000258Function *CallBase::getCaller() { return getParent()->getParent(); }
259
Craig Topper784929d2019-02-08 20:48:56 +0000260unsigned CallBase::getNumSubclassExtraOperandsDynamic() const {
261 assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!");
262 return cast<CallBrInst>(this)->getNumIndirectDests() + 1;
263}
264
Chandler Carruth57578aa2019-01-07 07:15:51 +0000265bool CallBase::isIndirectCall() const {
266 const Value *V = getCalledValue();
267 if (isa<Function>(V) || isa<Constant>(V))
268 return false;
269 if (const CallInst *CI = dyn_cast<CallInst>(this))
270 if (CI->isInlineAsm())
271 return false;
272 return true;
273}
274
Craig Topperc1892ec2019-01-31 17:23:29 +0000275/// Tests if this call site must be tail call optimized. Only a CallInst can
276/// be tail call optimized.
277bool CallBase::isMustTailCall() const {
278 if (auto *CI = dyn_cast<CallInst>(this))
279 return CI->isMustTailCall();
280 return false;
281}
282
283/// Tests if this call site is marked as a tail call.
284bool CallBase::isTailCall() const {
285 if (auto *CI = dyn_cast<CallInst>(this))
286 return CI->isTailCall();
287 return false;
288}
289
Chandler Carruth05b5bd82018-12-27 23:40:17 +0000290Intrinsic::ID CallBase::getIntrinsicID() const {
291 if (auto *F = getCalledFunction())
292 return F->getIntrinsicID();
293 return Intrinsic::not_intrinsic;
294}
295
296bool CallBase::isReturnNonNull() const {
297 if (hasRetAttr(Attribute::NonNull))
298 return true;
299
300 if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
301 !NullPointerIsDefined(getCaller(),
302 getType()->getPointerAddressSpace()))
303 return true;
304
305 return false;
306}
307
Chandler Carruthe429c792018-11-22 10:31:35 +0000308Value *CallBase::getReturnedArgOperand() const {
309 unsigned Index;
310
311 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
312 return getArgOperand(Index - AttributeList::FirstArgIndex);
313 if (const Function *F = getCalledFunction())
314 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
315 Index)
316 return getArgOperand(Index - AttributeList::FirstArgIndex);
317
318 return nullptr;
319}
320
321bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
322 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
323 return true;
324
325 // Look at the callee, if available.
326 if (const Function *F = getCalledFunction())
327 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
328 return false;
329}
330
331/// Determine whether the argument or parameter has the given attribute.
332bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
333 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
334
335 if (Attrs.hasParamAttribute(ArgNo, Kind))
336 return true;
337 if (const Function *F = getCalledFunction())
338 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
339 return false;
340}
341
342bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
343 if (const Function *F = getCalledFunction())
344 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
345 return false;
346}
347
348bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
349 if (const Function *F = getCalledFunction())
350 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
351 return false;
352}
353
354CallBase::op_iterator
355CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
356 const unsigned BeginIndex) {
357 auto It = op_begin() + BeginIndex;
358 for (auto &B : Bundles)
359 It = std::copy(B.input_begin(), B.input_end(), It);
360
361 auto *ContextImpl = getContext().pImpl;
362 auto BI = Bundles.begin();
363 unsigned CurrentIndex = BeginIndex;
364
365 for (auto &BOI : bundle_op_infos()) {
366 assert(BI != Bundles.end() && "Incorrect allocation?");
367
368 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
369 BOI.Begin = CurrentIndex;
370 BOI.End = CurrentIndex + BI->input_size();
371 CurrentIndex = BOI.End;
372 BI++;
373 }
374
375 assert(BI == Bundles.end() && "Incorrect allocation?");
376
377 return It;
378}
379
380//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381// CallInst Implementation
382//===----------------------------------------------------------------------===//
383
David Blaikie348de692015-04-23 21:36:23 +0000384void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000385 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000386 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000387 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
388 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000389 setCalledOperand(Func);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390
Jay Foad5bd375a2011-07-15 08:37:34 +0000391#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000392 assert((Args.size() == FTy->getNumParams() ||
393 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000394 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000395
396 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000397 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000398 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000399 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000400#endif
401
Fangrui Song75709322018-11-17 01:44:25 +0000402 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000403
404 auto It = populateBundleOperandInfos(Bundles, Args.size());
405 (void)It;
406 assert(It + 1 == op_end() && "Should add up!");
407
Jay Foad5bd375a2011-07-15 08:37:34 +0000408 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409}
410
James Y Knightf9563902019-01-14 21:37:42 +0000411void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) {
412 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000413 assert(getNumOperands() == 1 && "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000414 setCalledOperand(Func);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000415
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000416 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000417
418 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000419}
420
James Y Knightf9563902019-01-14 21:37:42 +0000421CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
422 Instruction *InsertBefore)
423 : CallBase(Ty->getReturnType(), Instruction::Call,
424 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) {
425 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426}
427
James Y Knightf9563902019-01-14 21:37:42 +0000428CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name,
429 BasicBlock *InsertAtEnd)
430 : CallBase(Ty->getReturnType(), Instruction::Call,
431 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) {
432 init(Ty, Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433}
434
Misha Brukmanb1c93172005-04-21 23:48:37 +0000435CallInst::CallInst(const CallInst &CI)
Chandler Carruthe429c792018-11-22 10:31:35 +0000436 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
437 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(),
438 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000439 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000440 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000441
Jay Foad5bd375a2011-07-15 08:37:34 +0000442 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000443 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
444 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000445 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000446}
447
Sanjoy Das2d161452015-11-18 06:23:38 +0000448CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
449 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000450 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000451
James Y Knight7976eb52019-02-01 20:43:25 +0000452 auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledValue(),
453 Args, OpB, CI->getName(), InsertPt);
Sanjoy Das2d161452015-11-18 06:23:38 +0000454 NewCI->setTailCallKind(CI->getTailCallKind());
455 NewCI->setCallingConv(CI->getCallingConv());
456 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000457 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000458 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000459 return NewCI;
460}
461
Wei Mi01f8d552019-04-22 17:04:51 +0000462// Update profile weight for call instruction by scaling it using the ratio
463// of S/T. The meaning of "branch_weights" meta data for call instruction is
464// transfered to represent call count.
465void CallInst::updateProfWeight(uint64_t S, uint64_t T) {
466 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
467 if (ProfileData == nullptr)
468 return;
469
470 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
471 if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
472 !ProfDataName->getString().equals("VP")))
473 return;
474
475 MDBuilder MDB(getContext());
476 SmallVector<Metadata *, 3> Vals;
477 Vals.push_back(ProfileData->getOperand(0));
478 APInt APS(128, S), APT(128, T);
479 if (ProfDataName->getString().equals("branch_weights") &&
480 ProfileData->getNumOperands() > 0) {
481 // Using APInt::div may be expensive, but most cases should fit 64 bits.
482 APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1))
483 ->getValue()
484 .getZExtValue());
485 Val *= APS;
486 Vals.push_back(MDB.createConstant(ConstantInt::get(
487 Type::getInt64Ty(getContext()), Val.udiv(APT).getLimitedValue())));
488 } else if (ProfDataName->getString().equals("VP"))
489 for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
490 // The first value is the key of the value profile, which will not change.
491 Vals.push_back(ProfileData->getOperand(i));
492 // Using APInt::div may be expensive, but most cases should fit 64 bits.
493 APInt Val(128,
494 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
495 ->getValue()
496 .getZExtValue());
497 Val *= APS;
498 Vals.push_back(MDB.createConstant(
499 ConstantInt::get(Type::getInt64Ty(getContext()),
500 Val.udiv(APT).getLimitedValue())));
501 }
502 setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
503}
504
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000505/// IsConstantOne - Return true only if val is constant int 1
506static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000507 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000508 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
509 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000510}
511
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000512static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000513 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000514 Type *AllocTy, Value *AllocSize,
515 Value *ArraySize,
516 ArrayRef<OperandBundleDef> OpB,
517 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000518 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000519 "createMalloc needs either InsertBefore or InsertAtEnd");
520
Fangrui Songf78650a2018-07-30 19:41:25 +0000521 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000522 // bitcast (i8* malloc(typeSize)) to type*
523 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000524 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000525 if (!ArraySize)
526 ArraySize = ConstantInt::get(IntPtrTy, 1);
527 else if (ArraySize->getType() != IntPtrTy) {
528 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000529 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
530 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000531 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000532 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
533 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000534 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000535
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000536 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000537 if (IsConstantOne(AllocSize)) {
538 AllocSize = ArraySize; // Operand * 1 = Operand
539 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
540 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
541 false /*ZExt*/);
542 // Malloc arg is constant product of type size and array size
543 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
544 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000545 // Multiply type size by the array size...
546 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000547 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
548 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000549 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000550 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
551 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000552 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000553 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000554
Victor Hernandez788eaab2009-09-18 19:20:02 +0000555 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000556 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000557 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
558 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000559 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
James Y Knight13680222019-02-01 02:28:03 +0000560 FunctionCallee MallocFunc = MallocF;
Victor Hernandezbb336a12009-11-10 19:53:28 +0000561 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000562 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000563 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000564 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000565 CallInst *MCall = nullptr;
566 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000567 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000568 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
569 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000570 Result = MCall;
571 if (Result->getType() != AllocPtrType)
572 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000573 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000574 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000575 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000576 Result = MCall;
577 if (Result->getType() != AllocPtrType) {
578 InsertAtEnd->getInstList().push_back(MCall);
579 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000580 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000581 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000582 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000583 MCall->setTailCall();
James Y Knight13680222019-02-01 02:28:03 +0000584 if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000585 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000586 if (!F->returnDoesNotAlias())
587 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000588 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000589 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000590
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000591 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000592}
593
594/// CreateMalloc - Generate the IR for a call to malloc:
595/// 1. Compute the malloc call's argument as the specified type's size,
596/// possibly multiplied by the array size if the array size is not
597/// constant 1.
598/// 2. Call malloc with that argument.
599/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000600Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000601 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000602 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000603 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000604 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000605 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000606 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000607}
David Majnemerfadc6db2016-04-29 08:07:22 +0000608Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
609 Type *IntPtrTy, Type *AllocTy,
610 Value *AllocSize, Value *ArraySize,
611 ArrayRef<OperandBundleDef> OpB,
612 Function *MallocF,
613 const Twine &Name) {
614 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
615 ArraySize, OpB, MallocF, Name);
616}
617
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000618/// CreateMalloc - Generate the IR for a call to malloc:
619/// 1. Compute the malloc call's argument as the specified type's size,
620/// possibly multiplied by the array size if the array size is not
621/// constant 1.
622/// 2. Call malloc with that argument.
623/// 3. Bitcast the result of the malloc call to the specified type.
624/// Note: This function does not add the bitcast to the basic block, that is the
625/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000626Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000627 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000628 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000629 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000630 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000631 ArraySize, None, MallocF, Name);
632}
633Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
634 Type *IntPtrTy, Type *AllocTy,
635 Value *AllocSize, Value *ArraySize,
636 ArrayRef<OperandBundleDef> OpB,
637 Function *MallocF, const Twine &Name) {
638 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
639 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000640}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000641
David Majnemerfadc6db2016-04-29 08:07:22 +0000642static Instruction *createFree(Value *Source,
643 ArrayRef<OperandBundleDef> Bundles,
644 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000645 BasicBlock *InsertAtEnd) {
646 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
647 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000648 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000649 "Can not free something of nonpointer type!");
650
Ana Pazosb3596022016-02-03 21:34:39 +0000651 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
652 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000653
Chris Lattner229907c2011-07-18 04:54:35 +0000654 Type *VoidTy = Type::getVoidTy(M->getContext());
655 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000656 // prototype free as "void free(void*)"
James Y Knight13680222019-02-01 02:28:03 +0000657 FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000658 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000659 Value *PtrCast = Source;
660 if (InsertBefore) {
661 if (Source->getType() != IntPtrTy)
662 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000663 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000664 } else {
665 if (Source->getType() != IntPtrTy)
666 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000667 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000668 }
669 Result->setTailCall();
James Y Knight13680222019-02-01 02:28:03 +0000670 if (Function *F = dyn_cast<Function>(FreeFunc.getCallee()))
Chris Lattner2156c222009-11-09 07:12:01 +0000671 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000672
673 return Result;
674}
675
676/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000677Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000678 return createFree(Source, None, InsertBefore, nullptr);
679}
680Instruction *CallInst::CreateFree(Value *Source,
681 ArrayRef<OperandBundleDef> Bundles,
682 Instruction *InsertBefore) {
683 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000684}
685
686/// CreateFree - Generate the IR for a call to the builtin free function.
687/// Note: This function does not add the call to the basic block, that is the
688/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000689Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000690 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
691 assert(FreeCall && "CreateFree did not create a CallInst");
692 return FreeCall;
693}
694Instruction *CallInst::CreateFree(Value *Source,
695 ArrayRef<OperandBundleDef> Bundles,
696 BasicBlock *InsertAtEnd) {
697 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000698 assert(FreeCall && "CreateFree did not create a CallInst");
699 return FreeCall;
700}
701
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702//===----------------------------------------------------------------------===//
703// InvokeInst Implementation
704//===----------------------------------------------------------------------===//
705
David Blaikie3e807092015-05-13 18:35:26 +0000706void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
707 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000708 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000709 const Twine &NameStr) {
710 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000711
Chandler Carruthe429c792018-11-22 10:31:35 +0000712 assert((int)getNumOperands() ==
713 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
Sanjoy Das9303c242015-09-24 19:14:18 +0000714 "NumOperands not set up?");
Chandler Carruthe429c792018-11-22 10:31:35 +0000715 setNormalDest(IfNormal);
716 setUnwindDest(IfException);
717 setCalledOperand(Fn);
Jay Foad5bd375a2011-07-15 08:37:34 +0000718
719#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000720 assert(((Args.size() == FTy->getNumParams()) ||
721 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000722 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000723
Jay Foad5bd375a2011-07-15 08:37:34 +0000724 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000725 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000726 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000727 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000728#endif
729
Fangrui Song75709322018-11-17 01:44:25 +0000730 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000731
732 auto It = populateBundleOperandInfos(Bundles, Args.size());
733 (void)It;
734 assert(It + 3 == op_end() && "Should add up!");
735
Jay Foad5bd375a2011-07-15 08:37:34 +0000736 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737}
738
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739InvokeInst::InvokeInst(const InvokeInst &II)
Chandler Carruthe429c792018-11-22 10:31:35 +0000740 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
741 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(),
742 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000743 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000744 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000745 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
746 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000747 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748}
749
Sanjoy Das2d161452015-11-18 06:23:38 +0000750InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
751 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000752 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000753
James Y Knightd9e85a02019-02-01 20:43:34 +0000754 auto *NewII = InvokeInst::Create(II->getFunctionType(), II->getCalledValue(),
755 II->getNormalDest(), II->getUnwindDest(),
756 Args, OpB, II->getName(), InsertPt);
Sanjoy Das2d161452015-11-18 06:23:38 +0000757 NewII->setCallingConv(II->getCallingConv());
758 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000759 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000760 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000761 return NewII;
762}
763
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000764
Bill Wendlingfae14752011-08-12 20:24:12 +0000765LandingPadInst *InvokeInst::getLandingPadInst() const {
766 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
767}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000768
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769//===----------------------------------------------------------------------===//
Craig Topper784929d2019-02-08 20:48:56 +0000770// CallBrInst Implementation
771//===----------------------------------------------------------------------===//
772
773void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough,
774 ArrayRef<BasicBlock *> IndirectDests,
775 ArrayRef<Value *> Args,
776 ArrayRef<OperandBundleDef> Bundles,
777 const Twine &NameStr) {
778 this->FTy = FTy;
779
780 assert((int)getNumOperands() ==
781 ComputeNumOperands(Args.size(), IndirectDests.size(),
782 CountBundleInputs(Bundles)) &&
783 "NumOperands not set up?");
784 NumIndirectDests = IndirectDests.size();
785 setDefaultDest(Fallthrough);
786 for (unsigned i = 0; i != NumIndirectDests; ++i)
787 setIndirectDest(i, IndirectDests[i]);
788 setCalledOperand(Fn);
789
790#ifndef NDEBUG
791 assert(((Args.size() == FTy->getNumParams()) ||
792 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
793 "Calling a function with bad signature");
794
795 for (unsigned i = 0, e = Args.size(); i != e; i++)
796 assert((i >= FTy->getNumParams() ||
797 FTy->getParamType(i) == Args[i]->getType()) &&
798 "Calling a function with a bad signature!");
799#endif
800
801 std::copy(Args.begin(), Args.end(), op_begin());
802
803 auto It = populateBundleOperandInfos(Bundles, Args.size());
804 (void)It;
805 assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!");
806
807 setName(NameStr);
808}
809
810CallBrInst::CallBrInst(const CallBrInst &CBI)
811 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr,
812 OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(),
813 CBI.getNumOperands()) {
814 setCallingConv(CBI.getCallingConv());
815 std::copy(CBI.op_begin(), CBI.op_end(), op_begin());
816 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(),
817 bundle_op_info_begin());
818 SubclassOptionalData = CBI.SubclassOptionalData;
819 NumIndirectDests = CBI.NumIndirectDests;
820}
821
822CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB,
823 Instruction *InsertPt) {
824 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end());
825
826 auto *NewCBI = CallBrInst::Create(CBI->getFunctionType(),
827 CBI->getCalledValue(),
828 CBI->getDefaultDest(),
829 CBI->getIndirectDests(),
830 Args, OpB, CBI->getName(), InsertPt);
831 NewCBI->setCallingConv(CBI->getCallingConv());
832 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData;
833 NewCBI->setAttributes(CBI->getAttributes());
834 NewCBI->setDebugLoc(CBI->getDebugLoc());
835 NewCBI->NumIndirectDests = CBI->NumIndirectDests;
836 return NewCBI;
837}
838
839//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840// ReturnInst Implementation
841//===----------------------------------------------------------------------===//
842
Chris Lattner2195fc42007-02-24 00:55:48 +0000843ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000844 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
845 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
846 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000847 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000848 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000849 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000850}
851
Owen Anderson55f1c092009-08-13 21:58:54 +0000852ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000853 : Instruction(Type::getVoidTy(C), Instruction::Ret,
854 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
855 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000856 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000857 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000858}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000859
Owen Anderson55f1c092009-08-13 21:58:54 +0000860ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000861 : Instruction(Type::getVoidTy(C), Instruction::Ret,
862 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
863 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000864 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000865 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000866}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000867
Owen Anderson55f1c092009-08-13 21:58:54 +0000868ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000869 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
870 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel59643e52008-02-23 00:35:18 +0000871
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000872//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000873// ResumeInst Implementation
874//===----------------------------------------------------------------------===//
875
876ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000877 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
878 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000879 Op<0>() = RI.Op<0>();
880}
881
882ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000883 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
884 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000885 Op<0>() = Exn;
886}
887
888ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000889 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
890 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000891 Op<0>() = Exn;
892}
893
Bill Wendlingf891bf82011-07-31 06:30:59 +0000894//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000895// CleanupReturnInst Implementation
896//===----------------------------------------------------------------------===//
897
898CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000899 : Instruction(CRI.getType(), Instruction::CleanupRet,
900 OperandTraits<CleanupReturnInst>::op_end(this) -
901 CRI.getNumOperands(),
902 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000903 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000904 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000905 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000906 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000907}
908
David Majnemer8a1c45d2015-12-12 05:38:55 +0000909void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000910 if (UnwindBB)
911 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000912
David Majnemer8a1c45d2015-12-12 05:38:55 +0000913 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000914 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000915 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000916}
917
David Majnemer8a1c45d2015-12-12 05:38:55 +0000918CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
919 unsigned Values, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000920 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
921 Instruction::CleanupRet,
922 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
923 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000924 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000925}
926
David Majnemer8a1c45d2015-12-12 05:38:55 +0000927CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
928 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000929 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
930 Instruction::CleanupRet,
931 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
932 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000933 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000934}
935
David Majnemer654e1302015-07-31 17:58:14 +0000936//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000937// CatchReturnInst Implementation
938//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000939void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000940 Op<0>() = CatchPad;
941 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000942}
David Majnemer654e1302015-07-31 17:58:14 +0000943
944CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000945 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
946 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000947 Op<0>() = CRI.Op<0>();
948 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000949}
950
David Majnemer8a1c45d2015-12-12 05:38:55 +0000951CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000952 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000953 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
954 OperandTraits<CatchReturnInst>::op_begin(this), 2,
955 InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000956 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000957}
958
David Majnemer8a1c45d2015-12-12 05:38:55 +0000959CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000960 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000961 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
962 OperandTraits<CatchReturnInst>::op_begin(this), 2,
963 InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000964 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000965}
966
David Majnemer654e1302015-07-31 17:58:14 +0000967//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000968// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000969//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000970
971CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
972 unsigned NumReservedValues,
973 const Twine &NameStr,
974 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000975 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
976 InsertBefore) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000977 if (UnwindDest)
978 ++NumReservedValues;
979 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000980 setName(NameStr);
981}
982
David Majnemer8a1c45d2015-12-12 05:38:55 +0000983CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
984 unsigned NumReservedValues,
985 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000986 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
987 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000988 if (UnwindDest)
989 ++NumReservedValues;
990 init(ParentPad, UnwindDest, NumReservedValues + 1);
991 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000992}
993
David Majnemer8a1c45d2015-12-12 05:38:55 +0000994CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000995 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
996 CSI.getNumOperands()) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000997 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
998 setNumHungOffUseOperands(ReservedSpace);
999 Use *OL = getOperandList();
1000 const Use *InOL = CSI.getOperandList();
1001 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1002 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001003}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001004
1005void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1006 unsigned NumReservedValues) {
1007 assert(ParentPad && NumReservedValues);
1008
1009 ReservedSpace = NumReservedValues;
1010 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1011 allocHungoffUses(ReservedSpace);
1012
1013 Op<0>() = ParentPad;
1014 if (UnwindDest) {
1015 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1016 setUnwindDest(UnwindDest);
1017 }
1018}
1019
1020/// growOperands - grow operands - This grows the operand list in response to a
1021/// push_back style of operation. This grows the number of ops by 2 times.
1022void CatchSwitchInst::growOperands(unsigned Size) {
1023 unsigned NumOperands = getNumOperands();
1024 assert(NumOperands >= 1);
1025 if (ReservedSpace >= NumOperands + Size)
1026 return;
1027 ReservedSpace = (NumOperands + Size / 2) * 2;
1028 growHungoffUses(ReservedSpace);
1029}
1030
1031void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1032 unsigned OpNo = getNumOperands();
1033 growOperands(1);
1034 assert(OpNo < ReservedSpace && "Growing didn't work!");
1035 setNumHungOffUseOperands(getNumOperands() + 1);
1036 getOperandList()[OpNo] = Handler;
1037}
1038
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001039void CatchSwitchInst::removeHandler(handler_iterator HI) {
1040 // Move all subsequent handlers up one.
1041 Use *EndDst = op_end() - 1;
1042 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1043 *CurDst = *(CurDst + 1);
1044 // Null out the last handler use.
1045 *EndDst = nullptr;
1046
1047 setNumHungOffUseOperands(getNumOperands() - 1);
1048}
1049
David Majnemer8a1c45d2015-12-12 05:38:55 +00001050//===----------------------------------------------------------------------===//
1051// FuncletPadInst Implementation
1052//===----------------------------------------------------------------------===//
1053void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1054 const Twine &NameStr) {
1055 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
Fangrui Song75709322018-11-17 01:44:25 +00001056 llvm::copy(Args, op_begin());
David Majnemer8a1c45d2015-12-12 05:38:55 +00001057 setParentPad(ParentPad);
1058 setName(NameStr);
1059}
1060
1061FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1062 : Instruction(FPI.getType(), FPI.getOpcode(),
1063 OperandTraits<FuncletPadInst>::op_end(this) -
1064 FPI.getNumOperands(),
1065 FPI.getNumOperands()) {
1066 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1067 setParentPad(FPI.getParentPad());
1068}
1069
1070FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1071 ArrayRef<Value *> Args, unsigned Values,
1072 const Twine &NameStr, Instruction *InsertBefore)
1073 : Instruction(ParentPad->getType(), Op,
1074 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1075 InsertBefore) {
1076 init(ParentPad, Args, NameStr);
1077}
1078
1079FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1080 ArrayRef<Value *> Args, unsigned Values,
1081 const Twine &NameStr, BasicBlock *InsertAtEnd)
1082 : Instruction(ParentPad->getType(), Op,
1083 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1084 InsertAtEnd) {
1085 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001086}
1087
1088//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001089// UnreachableInst Implementation
1090//===----------------------------------------------------------------------===//
1091
Fangrui Songf78650a2018-07-30 19:41:25 +00001092UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +00001093 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001094 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
1095 0, InsertBefore) {}
Owen Anderson55f1c092009-08-13 21:58:54 +00001096UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001097 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
1098 0, InsertAtEnd) {}
Chris Lattner2195fc42007-02-24 00:55:48 +00001099
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001100//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101// BranchInst Implementation
1102//===----------------------------------------------------------------------===//
1103
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001104void BranchInst::AssertOK() {
1105 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001106 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108}
1109
Chris Lattner2195fc42007-02-24 00:55:48 +00001110BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001111 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1112 OperandTraits<BranchInst>::op_end(this) - 1, 1,
1113 InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001114 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001115 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001116}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001117
Chris Lattner2195fc42007-02-24 00:55:48 +00001118BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1119 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001120 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1121 OperandTraits<BranchInst>::op_end(this) - 3, 3,
1122 InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001123 Op<-1>() = IfTrue;
1124 Op<-2>() = IfFalse;
1125 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001126#ifndef NDEBUG
1127 AssertOK();
1128#endif
1129}
1130
1131BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00001132 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1133 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001134 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001135 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001136}
1137
1138BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth509e20e2018-10-19 00:22:37 +00001139 BasicBlock *InsertAtEnd)
1140 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
1141 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001142 Op<-1>() = IfTrue;
1143 Op<-2>() = IfFalse;
1144 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001145#ifndef NDEBUG
1146 AssertOK();
1147#endif
1148}
1149
Chandler Carruth509e20e2018-10-19 00:22:37 +00001150BranchInst::BranchInst(const BranchInst &BI)
1151 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
1152 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1153 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001154 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001155 if (BI.getNumOperands() != 1) {
1156 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001157 Op<-3>() = BI.Op<-3>();
1158 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001159 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001160 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001161}
1162
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001163void BranchInst::swapSuccessors() {
1164 assert(isConditional() &&
1165 "Cannot swap successors of an unconditional branch");
1166 Op<-1>().swap(Op<-2>());
1167
1168 // Update profile metadata if present and it matches our structural
1169 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001170 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001171}
1172
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001173//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001174// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175//===----------------------------------------------------------------------===//
1176
Owen Andersonb6b25302009-07-14 23:09:55 +00001177static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001178 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001179 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001180 else {
1181 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001182 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001183 assert(Amt->getType()->isIntegerTy() &&
1184 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001185 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001186 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187}
1188
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001189AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001190 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001191 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001192
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001193AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001194 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001195 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001196
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001197AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001198 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001199 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1200
1201AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1202 const Twine &Name, BasicBlock *InsertAtEnd)
1203 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1204
1205AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1206 unsigned Align, const Twine &Name,
1207 Instruction *InsertBefore)
1208 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1209 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1210 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001211 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001212 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001213 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214}
1215
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001216AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1217 unsigned Align, const Twine &Name,
1218 BasicBlock *InsertAtEnd)
1219 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1220 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001221 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001222 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001223 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001224 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001225}
1226
Victor Hernandez8acf2952009-10-23 21:09:37 +00001227void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001228 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001229 assert(Align <= MaximumAlignment &&
1230 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001231 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1232 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001233 assert(getAlignment() == Align && "Alignment representation error!");
1234}
1235
Victor Hernandez8acf2952009-10-23 21:09:37 +00001236bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001237 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001238 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001239 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001240}
1241
Chris Lattner8b291e62008-11-26 02:54:17 +00001242/// isStaticAlloca - Return true if this alloca is in the entry block of the
1243/// function and is a constant size. If so, the code generator will fold it
1244/// into the prolog/epilog code, so it is basically free.
1245bool AllocaInst::isStaticAlloca() const {
1246 // Must be constant size.
1247 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001248
Chris Lattner8b291e62008-11-26 02:54:17 +00001249 // Must be in the entry block.
1250 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001251 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001252}
1253
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001254//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001255// LoadInst Implementation
1256//===----------------------------------------------------------------------===//
1257
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001258void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001259 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001260 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001261 assert(!(isAtomic() && getAlignment() == 0) &&
1262 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001263}
1264
James Y Knight84c1dbd2019-01-14 21:37:53 +00001265LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1266 Instruction *InsertBef)
1267 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001268
James Y Knight84c1dbd2019-01-14 21:37:53 +00001269LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name,
1270 BasicBlock *InsertAE)
1271 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001272
David Blaikie0c28fd72015-05-20 21:46:30 +00001273LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001274 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001275 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001276
James Y Knight84c1dbd2019-01-14 21:37:53 +00001277LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001278 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001279 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001280
David Blaikieb7a029872015-04-17 19:56:21 +00001281LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001282 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001283 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001284 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285
James Y Knight84c1dbd2019-01-14 21:37:53 +00001286LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001287 unsigned Align, BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001288 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001289 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001290
David Blaikie15d9a4c2015-04-06 20:59:48 +00001291LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001292 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001293 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001294 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001295 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001296 setVolatile(isVolatile);
1297 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001298 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001299 AssertOK();
1300 setName(Name);
1301}
1302
James Y Knight84c1dbd2019-01-14 21:37:53 +00001303LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1304 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001305 BasicBlock *InsertAE)
James Y Knight84c1dbd2019-01-14 21:37:53 +00001306 : UnaryInstruction(Ty, Load, Ptr, InsertAE) {
1307 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Chris Lattner0f048162007-02-13 07:54:42 +00001308 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001309 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001310 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001311 AssertOK();
1312 setName(Name);
1313}
1314
Christopher Lamb84485702007-04-22 19:24:39 +00001315void LoadInst::setAlignment(unsigned Align) {
1316 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001317 assert(Align <= MaximumAlignment &&
1318 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001319 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001320 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001321 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001322}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001323
1324//===----------------------------------------------------------------------===//
1325// StoreInst Implementation
1326//===----------------------------------------------------------------------===//
1327
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001328void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001329 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001330 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001331 "Ptr must have pointer type!");
1332 assert(getOperand(0)->getType() ==
1333 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001334 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001335 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001336 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001337}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001339StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001340 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001341
1342StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001343 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001344
Misha Brukmanb1c93172005-04-21 23:48:37 +00001345StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001346 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001347 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001348
1349StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001350 BasicBlock *InsertAtEnd)
1351 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1352
1353StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1354 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001355 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001356 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001357
1358StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1359 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001360 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001361 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001362
Misha Brukmanb1c93172005-04-21 23:48:37 +00001363StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001364 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001365 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001366 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001367 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001368 OperandTraits<StoreInst>::op_begin(this),
1369 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001370 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001371 Op<0>() = val;
1372 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001373 setVolatile(isVolatile);
1374 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001375 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001376 AssertOK();
1377}
1378
1379StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001380 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001381 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001382 BasicBlock *InsertAtEnd)
1383 : Instruction(Type::getVoidTy(val->getContext()), Store,
1384 OperandTraits<StoreInst>::op_begin(this),
1385 OperandTraits<StoreInst>::operands(this),
1386 InsertAtEnd) {
1387 Op<0>() = val;
1388 Op<1>() = addr;
1389 setVolatile(isVolatile);
1390 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001391 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001392 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001393}
1394
Christopher Lamb84485702007-04-22 19:24:39 +00001395void StoreInst::setAlignment(unsigned Align) {
1396 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001397 assert(Align <= MaximumAlignment &&
1398 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001399 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001400 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001401 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001402}
1403
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001404//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001405// AtomicCmpXchgInst Implementation
1406//===----------------------------------------------------------------------===//
1407
1408void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001409 AtomicOrdering SuccessOrdering,
1410 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001411 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001412 Op<0>() = Ptr;
1413 Op<1>() = Cmp;
1414 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001415 setSuccessOrdering(SuccessOrdering);
1416 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001417 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001418
1419 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1420 "All operands must be non-null!");
1421 assert(getOperand(0)->getType()->isPointerTy() &&
1422 "Ptr must have pointer type!");
1423 assert(getOperand(1)->getType() ==
1424 cast<PointerType>(getOperand(0)->getType())->getElementType()
1425 && "Ptr must be a pointer to Cmp type!");
1426 assert(getOperand(2)->getType() ==
1427 cast<PointerType>(getOperand(0)->getType())->getElementType()
1428 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001429 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001430 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001431 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001432 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001433 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1434 "AtomicCmpXchg failure argument shall be no stronger than the success "
1435 "argument");
1436 assert(FailureOrdering != AtomicOrdering::Release &&
1437 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001438 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001439}
1440
1441AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001442 AtomicOrdering SuccessOrdering,
1443 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001444 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001445 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001446 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001447 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001448 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1449 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001450 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001451}
1452
1453AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001454 AtomicOrdering SuccessOrdering,
1455 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001456 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001457 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001458 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001459 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001460 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1461 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001462 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001463}
Tim Northover420a2162014-06-13 14:24:07 +00001464
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001465//===----------------------------------------------------------------------===//
1466// AtomicRMWInst Implementation
1467//===----------------------------------------------------------------------===//
1468
1469void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1470 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001471 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001472 Op<0>() = Ptr;
1473 Op<1>() = Val;
1474 setOperation(Operation);
1475 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001476 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001477
1478 assert(getOperand(0) && getOperand(1) &&
1479 "All operands must be non-null!");
1480 assert(getOperand(0)->getType()->isPointerTy() &&
1481 "Ptr must have pointer type!");
1482 assert(getOperand(1)->getType() ==
1483 cast<PointerType>(getOperand(0)->getType())->getElementType()
1484 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001485 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001486 "AtomicRMW instructions must be atomic!");
1487}
1488
1489AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1490 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001491 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001492 Instruction *InsertBefore)
1493 : Instruction(Val->getType(), AtomicRMW,
1494 OperandTraits<AtomicRMWInst>::op_begin(this),
1495 OperandTraits<AtomicRMWInst>::operands(this),
1496 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001497 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001498}
1499
1500AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1501 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001502 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001503 BasicBlock *InsertAtEnd)
1504 : Instruction(Val->getType(), AtomicRMW,
1505 OperandTraits<AtomicRMWInst>::op_begin(this),
1506 OperandTraits<AtomicRMWInst>::operands(this),
1507 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001508 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001509}
1510
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001511StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1512 switch (Op) {
1513 case AtomicRMWInst::Xchg:
1514 return "xchg";
1515 case AtomicRMWInst::Add:
1516 return "add";
1517 case AtomicRMWInst::Sub:
1518 return "sub";
1519 case AtomicRMWInst::And:
1520 return "and";
1521 case AtomicRMWInst::Nand:
1522 return "nand";
1523 case AtomicRMWInst::Or:
1524 return "or";
1525 case AtomicRMWInst::Xor:
1526 return "xor";
1527 case AtomicRMWInst::Max:
1528 return "max";
1529 case AtomicRMWInst::Min:
1530 return "min";
1531 case AtomicRMWInst::UMax:
1532 return "umax";
1533 case AtomicRMWInst::UMin:
1534 return "umin";
Matt Arsenault39508332019-01-22 18:18:02 +00001535 case AtomicRMWInst::FAdd:
1536 return "fadd";
1537 case AtomicRMWInst::FSub:
1538 return "fsub";
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001539 case AtomicRMWInst::BAD_BINOP:
1540 return "<invalid operation>";
1541 }
1542
1543 llvm_unreachable("invalid atomicrmw operation");
1544}
1545
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001546//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001547// FenceInst Implementation
1548//===----------------------------------------------------------------------===//
1549
Fangrui Songf78650a2018-07-30 19:41:25 +00001550FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001551 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001552 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001553 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001554 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001555 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001556}
1557
Fangrui Songf78650a2018-07-30 19:41:25 +00001558FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001559 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001560 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001561 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001562 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001563 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001564}
1565
1566//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001567// GetElementPtrInst Implementation
1568//===----------------------------------------------------------------------===//
1569
Jay Foadd1b78492011-07-25 09:48:08 +00001570void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001571 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001572 assert(getNumOperands() == 1 + IdxList.size() &&
1573 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001574 Op<0>() = Ptr;
Fangrui Song75709322018-11-17 01:44:25 +00001575 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001576 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001577}
1578
Gabor Greiff6caff662008-05-10 08:32:32 +00001579GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001580 : Instruction(GEPI.getType(), GetElementPtr,
1581 OperandTraits<GetElementPtrInst>::op_end(this) -
1582 GEPI.getNumOperands(),
1583 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001584 SourceElementType(GEPI.SourceElementType),
1585 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001586 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001587 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001588}
1589
Chris Lattner6090a422009-03-09 04:46:40 +00001590/// getIndexedType - Returns the type of the element that would be accessed with
1591/// a gep instruction with the specified parameters.
1592///
1593/// The Idxs pointer should point to a continuous piece of memory containing the
1594/// indices, either as Value* or uint64_t.
1595///
1596/// A null type is returned if the indices are invalid for the specified
1597/// pointer type.
1598///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001599template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001600static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001601 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001602 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001603 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001604
Chris Lattner6090a422009-03-09 04:46:40 +00001605 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001606 // it cannot be 'stepped over'.
1607 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001608 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001609
Dan Gohman1ecaf452008-05-31 00:58:22 +00001610 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001611 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001612 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001613 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001614 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001615 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001616 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001617 }
Craig Topperc6207612014-04-09 06:08:46 +00001618 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001619}
1620
David Blaikied288fb82015-03-30 21:41:43 +00001621Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1622 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001623}
1624
David Blaikied288fb82015-03-30 21:41:43 +00001625Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001626 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001627 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001628}
1629
David Blaikied288fb82015-03-30 21:41:43 +00001630Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1631 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001632}
1633
Chris Lattner45f15572007-04-14 00:12:57 +00001634/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1635/// zeros. If so, the result pointer and the first operand have the same
1636/// value, just potentially different types.
1637bool GetElementPtrInst::hasAllZeroIndices() const {
1638 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1639 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1640 if (!CI->isZero()) return false;
1641 } else {
1642 return false;
1643 }
1644 }
1645 return true;
1646}
1647
Chris Lattner27058292007-04-27 20:35:56 +00001648/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1649/// constant integers. If so, the result pointer and the first operand have
1650/// a constant offset between them.
1651bool GetElementPtrInst::hasAllConstantIndices() const {
1652 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1653 if (!isa<ConstantInt>(getOperand(i)))
1654 return false;
1655 }
1656 return true;
1657}
1658
Dan Gohman1b849082009-09-07 23:54:19 +00001659void GetElementPtrInst::setIsInBounds(bool B) {
1660 cast<GEPOperator>(this)->setIsInBounds(B);
1661}
Chris Lattner45f15572007-04-14 00:12:57 +00001662
Nick Lewycky28a5f252009-09-27 21:33:04 +00001663bool GetElementPtrInst::isInBounds() const {
1664 return cast<GEPOperator>(this)->isInBounds();
1665}
1666
Chandler Carruth1e140532012-12-11 10:29:10 +00001667bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1668 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001669 // Delegate to the generic GEPOperator implementation.
1670 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001671}
1672
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001674// ExtractElementInst Implementation
1675//===----------------------------------------------------------------------===//
1676
1677ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001678 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001679 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001680 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001681 ExtractElement,
1682 OperandTraits<ExtractElementInst>::op_begin(this),
1683 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001684 assert(isValidOperands(Val, Index) &&
1685 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001686 Op<0>() = Val;
1687 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001688 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001689}
1690
1691ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001692 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001693 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001694 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001695 ExtractElement,
1696 OperandTraits<ExtractElementInst>::op_begin(this),
1697 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001698 assert(isValidOperands(Val, Index) &&
1699 "Invalid extractelement instruction operands!");
1700
Gabor Greif2d3024d2008-05-26 21:33:52 +00001701 Op<0>() = Val;
1702 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001703 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001704}
1705
Chris Lattner54865b32006-04-08 04:05:48 +00001706bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001707 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001708 return false;
1709 return true;
1710}
1711
Robert Bocchino23004482006-01-10 19:05:34 +00001712//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001713// InsertElementInst Implementation
1714//===----------------------------------------------------------------------===//
1715
Chris Lattner54865b32006-04-08 04:05:48 +00001716InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001717 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001718 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001719 : Instruction(Vec->getType(), InsertElement,
1720 OperandTraits<InsertElementInst>::op_begin(this),
1721 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001722 assert(isValidOperands(Vec, Elt, Index) &&
1723 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001724 Op<0>() = Vec;
1725 Op<1>() = Elt;
1726 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001727 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001728}
1729
Chris Lattner54865b32006-04-08 04:05:48 +00001730InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001731 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001732 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001733 : Instruction(Vec->getType(), InsertElement,
1734 OperandTraits<InsertElementInst>::op_begin(this),
1735 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001736 assert(isValidOperands(Vec, Elt, Index) &&
1737 "Invalid insertelement instruction operands!");
1738
Gabor Greif2d3024d2008-05-26 21:33:52 +00001739 Op<0>() = Vec;
1740 Op<1>() = Elt;
1741 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001742 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001743}
1744
Fangrui Songf78650a2018-07-30 19:41:25 +00001745bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001746 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001747 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001748 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001749
Reid Spencerd84d35b2007-02-15 02:26:10 +00001750 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001751 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001752
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001753 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001754 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001755 return true;
1756}
1757
Robert Bocchinoca27f032006-01-17 20:07:22 +00001758//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001759// ShuffleVectorInst Implementation
1760//===----------------------------------------------------------------------===//
1761
1762ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001763 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001764 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001765: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001766 cast<VectorType>(Mask->getType())->getNumElements()),
1767 ShuffleVector,
1768 OperandTraits<ShuffleVectorInst>::op_begin(this),
1769 OperandTraits<ShuffleVectorInst>::operands(this),
1770 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001771 assert(isValidOperands(V1, V2, Mask) &&
1772 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001773 Op<0>() = V1;
1774 Op<1>() = V2;
1775 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001776 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001777}
1778
1779ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001780 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001781 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001782: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1783 cast<VectorType>(Mask->getType())->getNumElements()),
1784 ShuffleVector,
1785 OperandTraits<ShuffleVectorInst>::op_begin(this),
1786 OperandTraits<ShuffleVectorInst>::operands(this),
1787 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001788 assert(isValidOperands(V1, V2, Mask) &&
1789 "Invalid shuffle vector instruction operands!");
1790
Gabor Greif2d3024d2008-05-26 21:33:52 +00001791 Op<0>() = V1;
1792 Op<1>() = V2;
1793 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001794 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001795}
1796
Sanjay Patelb276dd12019-03-31 15:01:30 +00001797void ShuffleVectorInst::commute() {
1798 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1799 int NumMaskElts = getMask()->getType()->getVectorNumElements();
1800 SmallVector<Constant*, 16> NewMask(NumMaskElts);
1801 Type *Int32Ty = Type::getInt32Ty(getContext());
1802 for (int i = 0; i != NumMaskElts; ++i) {
1803 int MaskElt = getMaskValue(i);
1804 if (MaskElt == -1) {
1805 NewMask[i] = UndefValue::get(Int32Ty);
1806 continue;
1807 }
1808 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask");
1809 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts;
1810 NewMask[i] = ConstantInt::get(Int32Ty, MaskElt);
1811 }
1812 Op<2>() = ConstantVector::get(NewMask);
1813 Op<0>().swap(Op<1>());
1814}
1815
Mon P Wang25f01062008-11-10 04:46:22 +00001816bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001817 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001818 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001819 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001820 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001821
Chris Lattner1dcb6542012-01-25 23:49:49 +00001822 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001823 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001824 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001825 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001826
1827 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001828 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1829 return true;
1830
Sanjay Patel8bd52282017-04-19 16:22:19 +00001831 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001832 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001833 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001834 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001835 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001836 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001837 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001838 return false;
1839 }
1840 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001841 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001842 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001843
Sanjay Patel8bd52282017-04-19 16:22:19 +00001844 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001845 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1846 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1847 if (CDS->getElementAsInteger(i) >= V1Size*2)
1848 return false;
1849 return true;
1850 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001851
Chris Lattner1dcb6542012-01-25 23:49:49 +00001852 // The bitcode reader can create a place holder for a forward reference
1853 // used as the shuffle mask. When this occurs, the shuffle mask will
1854 // fall into this case and fail. To avoid this error, do this bit of
1855 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001856 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001857 if (CE->getOpcode() == Instruction::UserOp1)
1858 return true;
1859
1860 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001861}
1862
Sanjay Patel2ca33602018-06-19 18:44:00 +00001863int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001864 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001865 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001866 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001867 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001868 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001869 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001870 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001871}
1872
Sanjay Patel2ca33602018-06-19 18:44:00 +00001873void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001874 SmallVectorImpl<int> &Result) {
1875 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001876
Sanjay Patel8bd52282017-04-19 16:22:19 +00001877 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001878 for (unsigned i = 0; i != NumElts; ++i)
1879 Result.push_back(CDS->getElementAsInteger(i));
1880 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001881 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882 for (unsigned i = 0; i != NumElts; ++i) {
1883 Constant *C = Mask->getAggregateElement(i);
1884 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001885 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001886 }
1887}
1888
Sanjay Patelac619a02018-08-30 15:05:38 +00001889static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001890 assert(!Mask.empty() && "Shuffle mask must contain elements");
1891 bool UsesLHS = false;
1892 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001893 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001894 if (Mask[i] == -1)
1895 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001896 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001897 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001898 UsesLHS |= (Mask[i] < NumOpElts);
1899 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001900 if (UsesLHS && UsesRHS)
1901 return false;
1902 }
1903 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1904 return true;
1905}
1906
Sanjay Patelac619a02018-08-30 15:05:38 +00001907bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1908 // We don't have vector operand size information, so assume operands are the
1909 // same size as the mask.
1910 return isSingleSourceMaskImpl(Mask, Mask.size());
1911}
1912
1913static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1914 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001915 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001916 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001917 if (Mask[i] == -1)
1918 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001919 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001920 return false;
1921 }
1922 return true;
1923}
1924
Sanjay Patelac619a02018-08-30 15:05:38 +00001925bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1926 // We don't have vector operand size information, so assume operands are the
1927 // same size as the mask.
1928 return isIdentityMaskImpl(Mask, Mask.size());
1929}
1930
Sanjay Patel2ca33602018-06-19 18:44:00 +00001931bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1932 if (!isSingleSourceMask(Mask))
1933 return false;
1934 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1935 if (Mask[i] == -1)
1936 continue;
1937 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1938 return false;
1939 }
1940 return true;
1941}
1942
1943bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1944 if (!isSingleSourceMask(Mask))
1945 return false;
1946 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1947 if (Mask[i] == -1)
1948 continue;
1949 if (Mask[i] != 0 && Mask[i] != NumElts)
1950 return false;
1951 }
1952 return true;
1953}
1954
1955bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1956 // Select is differentiated from identity. It requires using both sources.
1957 if (isSingleSourceMask(Mask))
1958 return false;
1959 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1960 if (Mask[i] == -1)
1961 continue;
1962 if (Mask[i] != i && Mask[i] != (NumElts + i))
1963 return false;
1964 }
1965 return true;
1966}
1967
1968bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1969 // Example masks that will return true:
1970 // v1 = <a, b, c, d>
1971 // v2 = <e, f, g, h>
1972 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1973 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1974
1975 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1976 int NumElts = Mask.size();
1977 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1978 return false;
1979
1980 // 2. The first element of the mask must be either a 0 or a 1.
1981 if (Mask[0] != 0 && Mask[0] != 1)
1982 return false;
1983
1984 // 3. The difference between the first 2 elements must be equal to the
1985 // number of elements in the mask.
1986 if ((Mask[1] - Mask[0]) != NumElts)
1987 return false;
1988
1989 // 4. The difference between consecutive even-numbered and odd-numbered
1990 // elements must be equal to 2.
1991 for (int i = 2; i < NumElts; ++i) {
1992 int MaskEltVal = Mask[i];
1993 if (MaskEltVal == -1)
1994 return false;
1995 int MaskEltPrevVal = Mask[i - 2];
1996 if (MaskEltVal - MaskEltPrevVal != 2)
1997 return false;
1998 }
1999 return true;
2000}
2001
Simon Pilgrimd0c71602018-11-09 16:28:19 +00002002bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
2003 int NumSrcElts, int &Index) {
2004 // Must extract from a single source.
2005 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
2006 return false;
2007
2008 // Must be smaller (else this is an Identity shuffle).
Fangrui Song4f2e66c2018-11-09 16:45:37 +00002009 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrimd0c71602018-11-09 16:28:19 +00002010 return false;
2011
2012 // Find start of extraction, accounting that we may start with an UNDEF.
2013 int SubIndex = -1;
2014 for (int i = 0, e = Mask.size(); i != e; ++i) {
2015 int M = Mask[i];
2016 if (M < 0)
2017 continue;
2018 int Offset = (M % NumSrcElts) - i;
2019 if (0 <= SubIndex && SubIndex != Offset)
2020 return false;
2021 SubIndex = Offset;
2022 }
2023
2024 if (0 <= SubIndex) {
2025 Index = SubIndex;
2026 return true;
2027 }
2028 return false;
2029}
2030
Sanjay Patelac619a02018-08-30 15:05:38 +00002031bool ShuffleVectorInst::isIdentityWithPadding() const {
2032 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
2033 int NumMaskElts = getType()->getVectorNumElements();
2034 if (NumMaskElts <= NumOpElts)
2035 return false;
2036
2037 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00002038 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00002039 if (!isIdentityMaskImpl(Mask, NumOpElts))
2040 return false;
2041
2042 // All extending must be with undef elements.
2043 for (int i = NumOpElts; i < NumMaskElts; ++i)
2044 if (Mask[i] != -1)
2045 return false;
2046
2047 return true;
2048}
2049
2050bool ShuffleVectorInst::isIdentityWithExtract() const {
2051 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
2052 int NumMaskElts = getType()->getVectorNumElements();
2053 if (NumMaskElts >= NumOpElts)
2054 return false;
2055
2056 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
2057}
Sanjay Patel2ca33602018-06-19 18:44:00 +00002058
Sanjay Patelfd4976b2018-09-20 15:21:52 +00002059bool ShuffleVectorInst::isConcat() const {
2060 // Vector concatenation is differentiated from identity with padding.
2061 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
2062 return false;
2063
2064 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
2065 int NumMaskElts = getType()->getVectorNumElements();
2066 if (NumMaskElts != NumOpElts * 2)
2067 return false;
2068
2069 // Use the mask length rather than the operands' vector lengths here. We
2070 // already know that the shuffle returns a vector twice as long as the inputs,
2071 // and neither of the inputs are undef vectors. If the mask picks consecutive
2072 // elements from both inputs, then this is a concatenation of the inputs.
2073 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
2074}
2075
Dan Gohman12fce772008-05-15 19:50:34 +00002076//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00002077// InsertValueInst Class
2078//===----------------------------------------------------------------------===//
2079
Fangrui Songf78650a2018-07-30 19:41:25 +00002080void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00002081 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002082 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00002083
2084 // There's no fundamental reason why we require at least one index
2085 // (other than weirdness with &*IdxBegin being invalid; see
2086 // getelementptr's init routine for example). But there's no
2087 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002088 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00002089
2090 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00002091 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00002092 Op<0>() = Agg;
2093 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00002094
Jay Foad57aa6362011-07-13 10:26:04 +00002095 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002096 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002097}
2098
2099InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00002100 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002101 OperandTraits<InsertValueInst>::op_begin(this), 2),
2102 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00002103 Op<0>() = IVI.getOperand(0);
2104 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002105 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002106}
2107
2108//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002109// ExtractValueInst Class
2110//===----------------------------------------------------------------------===//
2111
Jay Foad57aa6362011-07-13 10:26:04 +00002112void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002113 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002114
Jay Foad57aa6362011-07-13 10:26:04 +00002115 // There's no fundamental reason why we require at least one index.
2116 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002117 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002118
Jay Foad57aa6362011-07-13 10:26:04 +00002119 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002120 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002121}
2122
2123ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002124 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002125 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002126 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002127}
2128
Dan Gohman12fce772008-05-15 19:50:34 +00002129// getIndexedType - Returns the type of the element that would be extracted
2130// with an extractvalue instruction with the specified parameters.
2131//
2132// A null type is returned if the indices are invalid for the specified
2133// pointer type.
2134//
Chris Lattner229907c2011-07-18 04:54:35 +00002135Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002136 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002137 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002138 // We can't use CompositeType::indexValid(Index) here.
2139 // indexValid() always returns true for arrays because getelementptr allows
2140 // out-of-bounds indices. Since we don't allow those for extractvalue and
2141 // insertvalue we need to check array indexing manually.
2142 // Since the only other types we can index into are struct types it's just
2143 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002144 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002145 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002146 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002147 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002148 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002149 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002150 } else {
2151 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002152 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002153 }
2154
2155 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002156 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002157 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002158}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002159
2160//===----------------------------------------------------------------------===//
Cameron McInallycbde0d92018-11-13 18:15:47 +00002161// UnaryOperator Class
2162//===----------------------------------------------------------------------===//
2163
2164UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2165 Type *Ty, const Twine &Name,
2166 Instruction *InsertBefore)
2167 : UnaryInstruction(Ty, iType, S, InsertBefore) {
2168 Op<0>() = S;
2169 setName(Name);
2170 AssertOK();
2171}
2172
2173UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
2174 Type *Ty, const Twine &Name,
2175 BasicBlock *InsertAtEnd)
2176 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2177 Op<0>() = S;
2178 setName(Name);
2179 AssertOK();
2180}
2181
2182UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2183 const Twine &Name,
2184 Instruction *InsertBefore) {
2185 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2186}
2187
2188UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
2189 const Twine &Name,
2190 BasicBlock *InsertAtEnd) {
2191 UnaryOperator *Res = Create(Op, S, Name);
2192 InsertAtEnd->getInstList().push_back(Res);
2193 return Res;
2194}
2195
2196void UnaryOperator::AssertOK() {
2197 Value *LHS = getOperand(0);
2198 (void)LHS; // Silence warnings.
2199#ifndef NDEBUG
2200 switch (getOpcode()) {
2201 case FNeg:
2202 assert(getType() == LHS->getType() &&
2203 "Unary operation should return same type as operand!");
2204 assert(getType()->isFPOrFPVectorTy() &&
2205 "Tried to create a floating-point operation on a "
2206 "non-floating-point type!");
2207 break;
2208 default: llvm_unreachable("Invalid opcode provided");
2209 }
2210#endif
2211}
2212
2213//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002214// BinaryOperator Class
2215//===----------------------------------------------------------------------===//
2216
Chris Lattner2195fc42007-02-24 00:55:48 +00002217BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002218 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002219 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002220 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002221 OperandTraits<BinaryOperator>::op_begin(this),
2222 OperandTraits<BinaryOperator>::operands(this),
2223 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002224 Op<0>() = S1;
2225 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002226 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002227 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002228}
2229
Fangrui Songf78650a2018-07-30 19:41:25 +00002230BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002231 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002232 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002233 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002234 OperandTraits<BinaryOperator>::op_begin(this),
2235 OperandTraits<BinaryOperator>::operands(this),
2236 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002237 Op<0>() = S1;
2238 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002239 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002240 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002241}
2242
Craig Topper700892f2017-06-26 07:15:59 +00002243void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002244 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002245 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002246 assert(LHS->getType() == RHS->getType() &&
2247 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002248#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00002249 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002250 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002251 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002252 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002253 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002254 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002255 "Tried to create an integer operation on a non-integer type!");
2256 break;
2257 case FAdd: case FSub:
2258 case FMul:
2259 assert(getType() == LHS->getType() &&
2260 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002261 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002262 "Tried to create a floating-point operation on a "
2263 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002264 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002265 case UDiv:
2266 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002267 assert(getType() == LHS->getType() &&
2268 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002269 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002270 "Incorrect operand type (not integer) for S/UDIV");
2271 break;
2272 case FDiv:
2273 assert(getType() == LHS->getType() &&
2274 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002275 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002276 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002277 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002278 case URem:
2279 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002280 assert(getType() == LHS->getType() &&
2281 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002282 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002283 "Incorrect operand type (not integer) for S/UREM");
2284 break;
2285 case FRem:
2286 assert(getType() == LHS->getType() &&
2287 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002288 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002289 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002290 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002291 case Shl:
2292 case LShr:
2293 case AShr:
2294 assert(getType() == LHS->getType() &&
2295 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002296 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002297 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002298 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002299 case And: case Or:
2300 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002301 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002302 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002303 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002304 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002305 break;
Craig Topper700892f2017-06-26 07:15:59 +00002306 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002307 }
2308#endif
2309}
2310
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002311BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002312 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002313 Instruction *InsertBefore) {
2314 assert(S1->getType() == S2->getType() &&
2315 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002316 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002317}
2318
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002319BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002320 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002321 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002322 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002323 InsertAtEnd->getInstList().push_back(Res);
2324 return Res;
2325}
2326
Dan Gohman5476cfd2009-08-12 16:23:25 +00002327BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002328 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002329 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002330 return new BinaryOperator(Instruction::Sub,
2331 zero, Op,
2332 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002333}
2334
Dan Gohman5476cfd2009-08-12 16:23:25 +00002335BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002336 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002337 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002338 return new BinaryOperator(Instruction::Sub,
2339 zero, Op,
2340 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002341}
2342
Dan Gohman4ab44202009-12-18 02:58:50 +00002343BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2344 Instruction *InsertBefore) {
2345 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2346 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2347}
2348
2349BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2350 BasicBlock *InsertAtEnd) {
2351 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2352 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2353}
2354
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002355BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2356 Instruction *InsertBefore) {
2357 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2358 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2359}
2360
2361BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2362 BasicBlock *InsertAtEnd) {
2363 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2364 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2365}
2366
Dan Gohman5476cfd2009-08-12 16:23:25 +00002367BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002368 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002369 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002370 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002371 Op->getType(), Name, InsertBefore);
2372}
2373
Dan Gohman5476cfd2009-08-12 16:23:25 +00002374BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002375 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002376 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002377 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002378 Op->getType(), Name, InsertAtEnd);
2379}
2380
Dan Gohman5476cfd2009-08-12 16:23:25 +00002381BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002382 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002383 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002384 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002385 Op->getType(), Name, InsertBefore);
2386}
2387
Dan Gohman5476cfd2009-08-12 16:23:25 +00002388BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002389 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002390 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002391 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002392 Op->getType(), Name, InsertAtEnd);
2393}
2394
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002395// Exchange the two operands to this instruction. This instruction is safe to
2396// use on any binary instruction and does not modify the semantics of the
2397// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2398// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002399bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002400 if (!isCommutative())
2401 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002402 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002403 return false;
2404}
2405
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002406//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002407// FPMathOperator Class
2408//===----------------------------------------------------------------------===//
2409
Duncan Sands05f4df82012-04-16 16:28:59 +00002410float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002411 const MDNode *MD =
2412 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002413 if (!MD)
2414 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002415 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002416 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002417}
2418
Duncan Sands05f4df82012-04-16 16:28:59 +00002419//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002420// CastInst Class
2421//===----------------------------------------------------------------------===//
2422
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423// Just determine if this cast only deals with integral->integral conversion.
2424bool CastInst::isIntegerCast() const {
2425 switch (getOpcode()) {
2426 default: return false;
2427 case Instruction::ZExt:
2428 case Instruction::SExt:
2429 case Instruction::Trunc:
2430 return true;
2431 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002432 return getOperand(0)->getType()->isIntegerTy() &&
2433 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002435}
2436
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437bool CastInst::isLosslessCast() const {
2438 // Only BitCast can be lossless, exit fast if we're not BitCast
2439 if (getOpcode() != Instruction::BitCast)
2440 return false;
2441
2442 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002443 Type *SrcTy = getOperand(0)->getType();
2444 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445 if (SrcTy == DstTy)
2446 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002447
Reid Spencer8d9336d2006-12-31 05:26:44 +00002448 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002449 if (SrcTy->isPointerTy())
2450 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 return false; // Other types have no identity values
2452}
2453
2454/// This function determines if the CastInst does not require any bits to be
2455/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002456/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002458/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002459/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002460/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002461/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002462bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002463 Type *SrcTy,
2464 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002465 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002466 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002467 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 case Instruction::Trunc:
2469 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002470 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::FPTrunc:
2472 case Instruction::FPExt:
2473 case Instruction::UIToFP:
2474 case Instruction::SIToFP:
2475 case Instruction::FPToUI:
2476 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002477 case Instruction::AddrSpaceCast:
2478 // TODO: Target informations may give a more accurate answer here.
2479 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 case Instruction::BitCast:
2481 return true; // BitCast never modifies bits.
2482 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002483 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002484 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002486 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002487 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 }
2489}
2490
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002491bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002492 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002493}
2494
2495/// This function determines if a pair of casts can be eliminated and what
2496/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497/// instructions like this:
2498/// * %F = firstOpcode SrcTy %x to MidTy
2499/// * %S = secondOpcode MidTy %F to DstTy
2500/// The function returns a resultOpcode so these two casts can be replaced with:
2501/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002502/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503unsigned CastInst::isEliminableCastPair(
2504 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002505 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2506 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 // Define the 144 possibilities for these two cast instructions. The values
2508 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002509 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002510 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 // the following cast properties:
2512 //
2513 // Size Compare Source Destination
2514 // Operator Src ? Size Type Sign Type Sign
2515 // -------- ------------ ------------------- ---------------------
2516 // TRUNC > Integer Any Integral Any
2517 // ZEXT < Integral Unsigned Integer Any
2518 // SEXT < Integral Signed Integer Any
2519 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002520 // FPTOSI n/a FloatPt n/a Integral Signed
2521 // UITOFP n/a Integral Unsigned FloatPt n/a
2522 // SITOFP n/a Integral Signed FloatPt n/a
2523 // FPTRUNC > FloatPt n/a FloatPt n/a
2524 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525 // PTRTOINT n/a Pointer n/a Integral Unsigned
2526 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002527 // BITCAST = FirstClass n/a FirstClass n/a
2528 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002529 //
2530 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002531 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2532 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002533 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002534 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002535 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002536 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2539 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // T F F U S F F P I B A -+
2541 // R Z S P P I I T P 2 N T S |
2542 // U E E 2 2 2 2 R E I T C C +- secondOp
2543 // N X X U S F F N X N 2 V V |
2544 // C T T I I P P C T T P T T -+
2545 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002546 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002547 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2548 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2549 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2550 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2551 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002552 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002553 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002554 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2555 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2556 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2557 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002559
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002560 // TODO: This logic could be encoded into the table above and handled in the
2561 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002562 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002563 // merging. However, any pair of bitcasts are allowed.
2564 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2565 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2566 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002567
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002568 // Check if any of the casts convert scalars <-> vectors.
2569 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2570 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2571 if (!AreBothBitcasts)
2572 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573
2574 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2575 [secondOp-Instruction::CastOpsBegin];
2576 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002577 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002578 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002580 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002581 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002583 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002584 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002586 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002587 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002588 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002589 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002590 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 return firstOp;
2592 return 0;
2593 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002594 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002595 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002596 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597 return firstOp;
2598 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002599 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002600 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002601 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002602 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603 return secondOp;
2604 return 0;
2605 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002606 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002607 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002608 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 return secondOp;
2610 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002611 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002612 // Cannot simplify if address spaces are different!
2613 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2614 return 0;
2615
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002616 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002617 // We can still fold this without knowing the actual sizes as long we
2618 // know that the intermediate pointer is the largest possible
2619 // pointer size.
2620 // FIXME: Is this always true?
2621 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002622 return Instruction::BitCast;
2623
2624 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002625 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002626 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002627 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628 if (MidSize >= PtrSize)
2629 return Instruction::BitCast;
2630 return 0;
2631 }
2632 case 8: {
2633 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2634 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2635 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002636 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2637 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638 if (SrcSize == DstSize)
2639 return Instruction::BitCast;
2640 else if (SrcSize < DstSize)
2641 return firstOp;
2642 return secondOp;
2643 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002644 case 9:
2645 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002647 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002649 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002650 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002651 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002652 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2653 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654 if (SrcSize <= PtrSize && SrcSize == DstSize)
2655 return Instruction::BitCast;
2656 return 0;
2657 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002658 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002659 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2660 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2661 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2662 return Instruction::AddrSpaceCast;
2663 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002664 case 13:
2665 // FIXME: this state can be merged with (1), but the following assert
2666 // is useful to check the correcteness of the sequence due to semantic
2667 // change of bitcast.
2668 assert(
2669 SrcTy->isPtrOrPtrVectorTy() &&
2670 MidTy->isPtrOrPtrVectorTy() &&
2671 DstTy->isPtrOrPtrVectorTy() &&
2672 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2673 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2674 "Illegal addrspacecast, bitcast sequence!");
2675 // Allowed, use first cast's opcode
2676 return firstOp;
2677 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002678 // bitcast, addrspacecast -> addrspacecast if the element type of
2679 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002680 if (SrcTy->getScalarType()->getPointerElementType() ==
2681 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002682 return Instruction::AddrSpaceCast;
2683 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002684 case 15:
2685 // FIXME: this state can be merged with (1), but the following assert
2686 // is useful to check the correcteness of the sequence due to semantic
2687 // change of bitcast.
2688 assert(
2689 SrcTy->isIntOrIntVectorTy() &&
2690 MidTy->isPtrOrPtrVectorTy() &&
2691 DstTy->isPtrOrPtrVectorTy() &&
2692 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2693 "Illegal inttoptr, bitcast sequence!");
2694 // Allowed, use first cast's opcode
2695 return firstOp;
2696 case 16:
2697 // FIXME: this state can be merged with (2), but the following assert
2698 // is useful to check the correcteness of the sequence due to semantic
2699 // change of bitcast.
2700 assert(
2701 SrcTy->isPtrOrPtrVectorTy() &&
2702 MidTy->isPtrOrPtrVectorTy() &&
2703 DstTy->isIntOrIntVectorTy() &&
2704 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2705 "Illegal bitcast, ptrtoint sequence!");
2706 // Allowed, use second cast's opcode
2707 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002708 case 17:
2709 // (sitofp (zext x)) -> (uitofp x)
2710 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002711 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002712 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002714 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715 default:
Craig Topperc514b542012-02-05 22:14:15 +00002716 llvm_unreachable("Error in CastResults table!!!");
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::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002721 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002722 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723 // Construct and return the appropriate CastInst subclass
2724 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002725 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2726 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2727 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2728 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2729 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2730 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2731 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2732 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2733 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2734 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2735 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2736 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2737 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2738 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740}
2741
Chris Lattner229907c2011-07-18 04:54:35 +00002742CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002743 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002744 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745 // Construct and return the appropriate CastInst subclass
2746 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002747 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2748 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2749 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2750 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2751 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2752 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2753 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2754 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2755 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2756 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2757 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2758 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2759 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2760 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762}
2763
Fangrui Songf78650a2018-07-30 19:41:25 +00002764CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002765 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002766 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002767 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002768 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2769 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002770}
2771
Fangrui Songf78650a2018-07-30 19:41:25 +00002772CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002773 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002774 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002775 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002776 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2777 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002778}
2779
Fangrui Songf78650a2018-07-30 19:41:25 +00002780CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002781 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002782 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002783 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002784 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2785 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002786}
2787
Fangrui Songf78650a2018-07-30 19:41:25 +00002788CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002789 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002790 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002791 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002792 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2793 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002794}
2795
Chris Lattner229907c2011-07-18 04:54:35 +00002796CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002797 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002798 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002799 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002800 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2801 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002802}
2803
Chris Lattner229907c2011-07-18 04:54:35 +00002804CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002805 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002806 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002807 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002808 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2809 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002810}
2811
Chris Lattner229907c2011-07-18 04:54:35 +00002812CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002813 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002814 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002815 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2816 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2817 "Invalid cast");
2818 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002819 assert((!Ty->isVectorTy() ||
2820 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002821 "Invalid cast");
2822
Matt Arsenault065ced92013-07-31 00:17:33 +00002823 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002824 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002825
Matt Arsenault740980e2014-07-14 17:24:35 +00002826 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002827}
2828
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002829/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002830CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2831 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002832 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002833 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2834 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002835 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002836 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002837 assert((!Ty->isVectorTy() ||
2838 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002839 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002840
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002841 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002842 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002843
Matt Arsenault740980e2014-07-14 17:24:35 +00002844 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2845}
2846
2847CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2848 Value *S, Type *Ty,
2849 const Twine &Name,
2850 BasicBlock *InsertAtEnd) {
2851 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2852 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2853
2854 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2855 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2856
2857 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2858}
2859
2860CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2861 Value *S, Type *Ty,
2862 const Twine &Name,
2863 Instruction *InsertBefore) {
2864 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2865 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2866
2867 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002868 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2869
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002870 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002871}
2872
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002873CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2874 const Twine &Name,
2875 Instruction *InsertBefore) {
2876 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2877 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2878 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2879 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2880
2881 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2882}
2883
Matt Arsenault740980e2014-07-14 17:24:35 +00002884CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002885 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002886 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002887 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002888 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002889 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2890 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002891 Instruction::CastOps opcode =
2892 (SrcBits == DstBits ? Instruction::BitCast :
2893 (SrcBits > DstBits ? Instruction::Trunc :
2894 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002895 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002896}
2897
Fangrui Songf78650a2018-07-30 19:41:25 +00002898CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002899 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002900 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002901 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002902 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002903 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2904 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002905 Instruction::CastOps opcode =
2906 (SrcBits == DstBits ? Instruction::BitCast :
2907 (SrcBits > DstBits ? Instruction::Trunc :
2908 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002909 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002910}
2911
Fangrui Songf78650a2018-07-30 19:41:25 +00002912CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2913 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002914 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002915 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002916 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002917 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2918 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002919 Instruction::CastOps opcode =
2920 (SrcBits == DstBits ? Instruction::BitCast :
2921 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002922 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002923}
2924
Fangrui Songf78650a2018-07-30 19:41:25 +00002925CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2926 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002927 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002928 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002929 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002930 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2931 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002932 Instruction::CastOps opcode =
2933 (SrcBits == DstBits ? Instruction::BitCast :
2934 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002935 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002936}
2937
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002938// Check whether it is valid to call getCastOpcode for these types.
2939// This routine must be kept in sync with getCastOpcode.
2940bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2941 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2942 return false;
2943
2944 if (SrcTy == DestTy)
2945 return true;
2946
2947 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2948 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2949 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2950 // An element by element cast. Valid if casting the elements is valid.
2951 SrcTy = SrcVecTy->getElementType();
2952 DestTy = DestVecTy->getElementType();
2953 }
2954
2955 // Get the bit sizes, we'll need these
2956 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2957 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2958
2959 // Run through the possibilities ...
2960 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002961 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002962 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002963 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002964 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002965 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002966 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002967 // Casting from something else
2968 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002969 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002970 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2971 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002972 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002973 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002974 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002975 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002976 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002977 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002978 return false;
2979 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002980 if (DestTy->isVectorTy()) // Casting to vector
2981 return DestBits == SrcBits;
2982 if (DestTy->isPointerTy()) { // Casting to pointer
2983 if (SrcTy->isPointerTy()) // Casting from pointer
2984 return true;
2985 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002986 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002987 if (DestTy->isX86_MMXTy()) {
2988 if (SrcTy->isVectorTy())
2989 return DestBits == SrcBits; // 64-bit vector to MMX
2990 return false;
2991 } // Casting to something else
2992 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002993}
2994
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002995bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2996 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2997 return false;
2998
2999 if (SrcTy == DestTy)
3000 return true;
3001
3002 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3003 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
3004 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3005 // An element by element cast. Valid if casting the elements is valid.
3006 SrcTy = SrcVecTy->getElementType();
3007 DestTy = DestVecTy->getElementType();
3008 }
3009 }
3010 }
3011
3012 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
3013 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
3014 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
3015 }
3016 }
3017
3018 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3019 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
3020
3021 // Could still have vectors of pointers if the number of elements doesn't
3022 // match
3023 if (SrcBits == 0 || DestBits == 0)
3024 return false;
3025
3026 if (SrcBits != DestBits)
3027 return false;
3028
3029 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
3030 return false;
3031
3032 return true;
3033}
3034
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003035bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003036 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00003037 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003038 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3039 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00003040 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3041 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003042 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3043 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00003044 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3045 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003046
3047 return isBitCastable(SrcTy, DestTy);
3048}
3049
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003050// Provide a way to get a "cast" where the cast opcode is inferred from the
3051// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003052// logic in the castIsValid function below. This axiom should hold:
3053// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3054// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003056// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003058CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003059 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3060 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061
Duncan Sands55e50902008-01-06 10:12:28 +00003062 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3063 "Only first class types are castable!");
3064
Duncan Sandsa8514532011-05-18 07:13:41 +00003065 if (SrcTy == DestTy)
3066 return BitCast;
3067
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003068 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003069 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3070 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003071 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3072 // An element by element cast. Find the appropriate opcode based on the
3073 // element types.
3074 SrcTy = SrcVecTy->getElementType();
3075 DestTy = DestVecTy->getElementType();
3076 }
3077
3078 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003079 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3080 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003081
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003082 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003083 if (DestTy->isIntegerTy()) { // Casting to integral
3084 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085 if (DestBits < SrcBits)
3086 return Trunc; // int -> smaller int
3087 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003088 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089 return SExt; // signed -> SEXT
3090 else
3091 return ZExt; // unsigned -> ZEXT
3092 } else {
3093 return BitCast; // Same size, No-op cast
3094 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003095 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00003096 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003097 return FPToSI; // FP -> sint
3098 else
Fangrui Songf78650a2018-07-30 19:41:25 +00003099 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003100 } else if (SrcTy->isVectorTy()) {
3101 assert(DestBits == SrcBits &&
3102 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103 return BitCast; // Same size, no-op cast
3104 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003105 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106 "Casting from a value that is not first-class type");
3107 return PtrToInt; // ptr -> int
3108 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003109 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3110 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003111 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003112 return SIToFP; // sint -> FP
3113 else
3114 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003115 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003116 if (DestBits < SrcBits) {
3117 return FPTrunc; // FP -> smaller FP
3118 } else if (DestBits > SrcBits) {
3119 return FPExt; // FP -> larger FP
3120 } else {
3121 return BitCast; // same size, no-op cast
3122 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003123 } else if (SrcTy->isVectorTy()) {
3124 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003125 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003126 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003128 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003129 } else if (DestTy->isVectorTy()) {
3130 assert(DestBits == SrcBits &&
3131 "Illegal cast to vector (wrong type or size)");
3132 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003133 } else if (DestTy->isPointerTy()) {
3134 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003135 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3136 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003138 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003141 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003142 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003143 if (SrcTy->isVectorTy()) {
3144 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003145 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003146 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003147 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003148 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003149 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150}
3151
3152//===----------------------------------------------------------------------===//
3153// CastInst SubClass Constructors
3154//===----------------------------------------------------------------------===//
3155
3156/// Check that the construction parameters for a CastInst are correct. This
3157/// could be broken out into the separate constructors but it is useful to have
3158/// it in one place and to eliminate the redundant code for getting the sizes
3159/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00003160bool
Chris Lattner229907c2011-07-18 04:54:35 +00003161CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003163 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003164
Chris Lattner37bc78a2010-01-26 21:51:43 +00003165 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3166 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167 return false;
3168
3169 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003170 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3171 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172
Duncan Sands7f646562011-05-18 09:21:57 +00003173 // If these are vector types, get the lengths of the vectors (using zero for
3174 // scalar types means that checking that vector lengths match also checks that
3175 // scalars are not being converted to vectors or vectors to scalars).
3176 unsigned SrcLength = SrcTy->isVectorTy() ?
3177 cast<VectorType>(SrcTy)->getNumElements() : 0;
3178 unsigned DstLength = DstTy->isVectorTy() ?
3179 cast<VectorType>(DstTy)->getNumElements() : 0;
3180
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181 // Switch on the opcode provided
3182 switch (op) {
3183 default: return false; // This is an input error
3184 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003185 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3186 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003188 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3189 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00003190 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003191 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3192 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003194 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3195 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003197 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3198 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003200 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003201 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3202 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003205 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3206 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003208 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003209 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003210 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3211 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3212 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003213 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003215 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003216 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003217 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3218 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3219 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003220 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003221 case Instruction::BitCast: {
3222 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3223 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3224
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225 // BitCast implies a no-op cast of type only. No bits change.
3226 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003227 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228 return false;
3229
Alp Tokerf907b892013-12-05 05:44:44 +00003230 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003231 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003232 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003233 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3234
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003235 // If both are pointers then the address spaces must match.
3236 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3237 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003238
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003239 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003240 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3241 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3242 if (SrcVecTy && DstVecTy)
3243 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3244 if (SrcVecTy)
3245 return SrcVecTy->getNumElements() == 1;
3246 if (DstVecTy)
3247 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003248
3249 return true;
3250 }
3251 case Instruction::AddrSpaceCast: {
3252 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3253 if (!SrcPtrTy)
3254 return false;
3255
3256 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3257 if (!DstPtrTy)
3258 return false;
3259
3260 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3261 return false;
3262
3263 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3264 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3265 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3266
3267 return false;
3268 }
3269
3270 return true;
3271 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272 }
3273}
3274
3275TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003276 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003277) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003278 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003279}
3280
3281TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003282 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003283) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003284 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003285}
3286
3287ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003288 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003289) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003290 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003291}
3292
3293ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003294 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003295) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003296 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003297}
3298SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003299 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003300) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302}
3303
Jeff Cohencc08c832006-12-02 02:22:01 +00003304SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003305 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003306) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308}
3309
3310FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003311 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003312) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314}
3315
3316FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003317 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003318) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320}
3321
3322FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003323 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003324) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326}
3327
3328FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003329 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003330) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003332}
3333
3334UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003335 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003336) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003338}
3339
3340UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003341 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003342) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003344}
3345
3346SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003347 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003348) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003350}
3351
3352SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003353 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003354) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003356}
3357
3358FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003359 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003360) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003362}
3363
3364FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003365 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003366) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003368}
3369
3370FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003371 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003372) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003374}
3375
3376FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003377 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003378) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003380}
3381
3382PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003383 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003384) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003386}
3387
3388PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003389 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003390) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003392}
3393
3394IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003395 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003396) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003398}
3399
3400IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003401 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003402) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003404}
3405
3406BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003407 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003408) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003410}
3411
3412BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003413 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003414) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003416}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003417
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003418AddrSpaceCastInst::AddrSpaceCastInst(
3419 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3420) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3421 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3422}
3423
3424AddrSpaceCastInst::AddrSpaceCastInst(
3425 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3426) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3427 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3428}
3429
Chris Lattnerf16dc002006-09-17 19:29:56 +00003430//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003431// CmpInst Classes
3432//===----------------------------------------------------------------------===//
3433
Craig Topper1c3f2832015-12-15 06:11:33 +00003434CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Pateld1172a02018-11-07 00:00:42 +00003435 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3436 Instruction *FlagsSource)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003437 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003438 OperandTraits<CmpInst>::op_begin(this),
3439 OperandTraits<CmpInst>::operands(this),
3440 InsertBefore) {
Sanjay Pateld1172a02018-11-07 00:00:42 +00003441 Op<0>() = LHS;
3442 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003443 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003444 setName(Name);
Sanjay Pateld1172a02018-11-07 00:00:42 +00003445 if (FlagsSource)
3446 copyIRFlags(FlagsSource);
Reid Spencerd9436b62006-11-20 01:22:35 +00003447}
Gabor Greiff6caff662008-05-10 08:32:32 +00003448
Craig Topper1c3f2832015-12-15 06:11:33 +00003449CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3450 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003451 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003452 OperandTraits<CmpInst>::op_begin(this),
3453 OperandTraits<CmpInst>::operands(this),
3454 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003455 Op<0>() = LHS;
3456 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003457 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003458 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003459}
3460
3461CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003462CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003463 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003464 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003465 if (InsertBefore)
3466 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3467 S1, S2, Name);
3468 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003469 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003470 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003471 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003472
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003473 if (InsertBefore)
3474 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3475 S1, S2, Name);
3476 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003477 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003478 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003479}
3480
3481CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003482CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003483 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003484 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003485 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3486 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003487 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003488 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3489 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003490}
3491
3492void CmpInst::swapOperands() {
3493 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3494 IC->swapOperands();
3495 else
3496 cast<FCmpInst>(this)->swapOperands();
3497}
3498
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003499bool CmpInst::isCommutative() const {
3500 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003501 return IC->isCommutative();
3502 return cast<FCmpInst>(this)->isCommutative();
3503}
3504
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003505bool CmpInst::isEquality() const {
3506 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003507 return IC->isEquality();
3508 return cast<FCmpInst>(this)->isEquality();
3509}
3510
Dan Gohman4e724382008-05-31 02:47:54 +00003511CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003512 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003513 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003514 case ICMP_EQ: return ICMP_NE;
3515 case ICMP_NE: return ICMP_EQ;
3516 case ICMP_UGT: return ICMP_ULE;
3517 case ICMP_ULT: return ICMP_UGE;
3518 case ICMP_UGE: return ICMP_ULT;
3519 case ICMP_ULE: return ICMP_UGT;
3520 case ICMP_SGT: return ICMP_SLE;
3521 case ICMP_SLT: return ICMP_SGE;
3522 case ICMP_SGE: return ICMP_SLT;
3523 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003524
Dan Gohman4e724382008-05-31 02:47:54 +00003525 case FCMP_OEQ: return FCMP_UNE;
3526 case FCMP_ONE: return FCMP_UEQ;
3527 case FCMP_OGT: return FCMP_ULE;
3528 case FCMP_OLT: return FCMP_UGE;
3529 case FCMP_OGE: return FCMP_ULT;
3530 case FCMP_OLE: return FCMP_UGT;
3531 case FCMP_UEQ: return FCMP_ONE;
3532 case FCMP_UNE: return FCMP_OEQ;
3533 case FCMP_UGT: return FCMP_OLE;
3534 case FCMP_ULT: return FCMP_OGE;
3535 case FCMP_UGE: return FCMP_OLT;
3536 case FCMP_ULE: return FCMP_OGT;
3537 case FCMP_ORD: return FCMP_UNO;
3538 case FCMP_UNO: return FCMP_ORD;
3539 case FCMP_TRUE: return FCMP_FALSE;
3540 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003541 }
3542}
3543
Tim Northoverde3aea0412016-08-17 20:25:25 +00003544StringRef CmpInst::getPredicateName(Predicate Pred) {
3545 switch (Pred) {
3546 default: return "unknown";
3547 case FCmpInst::FCMP_FALSE: return "false";
3548 case FCmpInst::FCMP_OEQ: return "oeq";
3549 case FCmpInst::FCMP_OGT: return "ogt";
3550 case FCmpInst::FCMP_OGE: return "oge";
3551 case FCmpInst::FCMP_OLT: return "olt";
3552 case FCmpInst::FCMP_OLE: return "ole";
3553 case FCmpInst::FCMP_ONE: return "one";
3554 case FCmpInst::FCMP_ORD: return "ord";
3555 case FCmpInst::FCMP_UNO: return "uno";
3556 case FCmpInst::FCMP_UEQ: return "ueq";
3557 case FCmpInst::FCMP_UGT: return "ugt";
3558 case FCmpInst::FCMP_UGE: return "uge";
3559 case FCmpInst::FCMP_ULT: return "ult";
3560 case FCmpInst::FCMP_ULE: return "ule";
3561 case FCmpInst::FCMP_UNE: return "une";
3562 case FCmpInst::FCMP_TRUE: return "true";
3563 case ICmpInst::ICMP_EQ: return "eq";
3564 case ICmpInst::ICMP_NE: return "ne";
3565 case ICmpInst::ICMP_SGT: return "sgt";
3566 case ICmpInst::ICMP_SGE: return "sge";
3567 case ICmpInst::ICMP_SLT: return "slt";
3568 case ICmpInst::ICMP_SLE: return "sle";
3569 case ICmpInst::ICMP_UGT: return "ugt";
3570 case ICmpInst::ICMP_UGE: return "uge";
3571 case ICmpInst::ICMP_ULT: return "ult";
3572 case ICmpInst::ICMP_ULE: return "ule";
3573 }
3574}
3575
Reid Spencer266e42b2006-12-23 06:05:41 +00003576ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3577 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003578 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003579 case ICMP_EQ: case ICMP_NE:
3580 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003581 return pred;
3582 case ICMP_UGT: return ICMP_SGT;
3583 case ICMP_ULT: return ICMP_SLT;
3584 case ICMP_UGE: return ICMP_SGE;
3585 case ICMP_ULE: return ICMP_SLE;
3586 }
3587}
3588
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003589ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3590 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003591 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003592 case ICMP_EQ: case ICMP_NE:
3593 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003594 return pred;
3595 case ICMP_SGT: return ICMP_UGT;
3596 case ICMP_SLT: return ICMP_ULT;
3597 case ICMP_SGE: return ICMP_UGE;
3598 case ICMP_SLE: return ICMP_ULE;
3599 }
3600}
3601
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003602CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3603 switch (pred) {
3604 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3605 case ICMP_SGT: return ICMP_SGE;
3606 case ICMP_SLT: return ICMP_SLE;
3607 case ICMP_SGE: return ICMP_SGT;
3608 case ICMP_SLE: return ICMP_SLT;
3609 case ICMP_UGT: return ICMP_UGE;
3610 case ICMP_ULT: return ICMP_ULE;
3611 case ICMP_UGE: return ICMP_UGT;
3612 case ICMP_ULE: return ICMP_ULT;
3613
3614 case FCMP_OGT: return FCMP_OGE;
3615 case FCMP_OLT: return FCMP_OLE;
3616 case FCMP_OGE: return FCMP_OGT;
3617 case FCMP_OLE: return FCMP_OLT;
3618 case FCMP_UGT: return FCMP_UGE;
3619 case FCMP_ULT: return FCMP_ULE;
3620 case FCMP_UGE: return FCMP_UGT;
3621 case FCMP_ULE: return FCMP_ULT;
3622 }
3623}
3624
Dan Gohman4e724382008-05-31 02:47:54 +00003625CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003626 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003627 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003628 case ICMP_EQ: case ICMP_NE:
3629 return pred;
3630 case ICMP_SGT: return ICMP_SLT;
3631 case ICMP_SLT: return ICMP_SGT;
3632 case ICMP_SGE: return ICMP_SLE;
3633 case ICMP_SLE: return ICMP_SGE;
3634 case ICMP_UGT: return ICMP_ULT;
3635 case ICMP_ULT: return ICMP_UGT;
3636 case ICMP_UGE: return ICMP_ULE;
3637 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003638
Reid Spencerd9436b62006-11-20 01:22:35 +00003639 case FCMP_FALSE: case FCMP_TRUE:
3640 case FCMP_OEQ: case FCMP_ONE:
3641 case FCMP_UEQ: case FCMP_UNE:
3642 case FCMP_ORD: case FCMP_UNO:
3643 return pred;
3644 case FCMP_OGT: return FCMP_OLT;
3645 case FCMP_OLT: return FCMP_OGT;
3646 case FCMP_OGE: return FCMP_OLE;
3647 case FCMP_OLE: return FCMP_OGE;
3648 case FCMP_UGT: return FCMP_ULT;
3649 case FCMP_ULT: return FCMP_UGT;
3650 case FCMP_UGE: return FCMP_ULE;
3651 case FCMP_ULE: return FCMP_UGE;
3652 }
3653}
3654
Max Kazantsevb299ade2018-02-07 11:16:29 +00003655CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3656 switch (pred) {
3657 case ICMP_SGT: return ICMP_SGE;
3658 case ICMP_SLT: return ICMP_SLE;
3659 case ICMP_UGT: return ICMP_UGE;
3660 case ICMP_ULT: return ICMP_ULE;
3661 case FCMP_OGT: return FCMP_OGE;
3662 case FCMP_OLT: return FCMP_OLE;
3663 case FCMP_UGT: return FCMP_UGE;
3664 case FCMP_ULT: return FCMP_ULE;
3665 default: return pred;
3666 }
3667}
3668
Sanjoy Das6e78b172015-10-22 19:57:34 +00003669CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3670 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3671
3672 switch (pred) {
3673 default:
3674 llvm_unreachable("Unknown predicate!");
3675 case CmpInst::ICMP_ULT:
3676 return CmpInst::ICMP_SLT;
3677 case CmpInst::ICMP_ULE:
3678 return CmpInst::ICMP_SLE;
3679 case CmpInst::ICMP_UGT:
3680 return CmpInst::ICMP_SGT;
3681 case CmpInst::ICMP_UGE:
3682 return CmpInst::ICMP_SGE;
3683 }
3684}
3685
Craig Topper1c3f2832015-12-15 06:11:33 +00003686bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003687 switch (predicate) {
3688 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003689 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003690 case ICmpInst::ICMP_UGE: return true;
3691 }
3692}
3693
Craig Topper1c3f2832015-12-15 06:11:33 +00003694bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003695 switch (predicate) {
3696 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003697 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003698 case ICmpInst::ICMP_SGE: return true;
3699 }
3700}
3701
Craig Topper1c3f2832015-12-15 06:11:33 +00003702bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003703 switch (predicate) {
3704 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003705 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3706 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003707 case FCmpInst::FCMP_ORD: return true;
3708 }
3709}
Fangrui Songf78650a2018-07-30 19:41:25 +00003710
Craig Topper1c3f2832015-12-15 06:11:33 +00003711bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003712 switch (predicate) {
3713 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003714 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3715 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003716 case FCmpInst::FCMP_UNO: return true;
3717 }
3718}
3719
Craig Topper1c3f2832015-12-15 06:11:33 +00003720bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003721 switch(predicate) {
3722 default: return false;
3723 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3724 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3725 }
3726}
3727
Craig Topper1c3f2832015-12-15 06:11:33 +00003728bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003729 switch(predicate) {
3730 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3731 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3732 default: return false;
3733 }
3734}
3735
Chad Rosier99bc4802016-04-21 16:18:02 +00003736bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003737 // If the predicates match, then we know the first condition implies the
3738 // second is true.
3739 if (Pred1 == Pred2)
3740 return true;
3741
3742 switch (Pred1) {
3743 default:
3744 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003745 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003746 // 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 +00003747 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3748 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003749 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3750 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3751 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3752 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3753 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3754 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3755 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3756 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003757 }
3758 return false;
3759}
3760
Chad Rosier99bc4802016-04-21 16:18:02 +00003761bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003762 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003763}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003764
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003765//===----------------------------------------------------------------------===//
3766// SwitchInst Implementation
3767//===----------------------------------------------------------------------===//
3768
Chris Lattnerbaf00152010-11-17 05:41:46 +00003769void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3770 assert(Value && Default && NumReserved);
3771 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003772 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003773 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003774
Pete Coopereb31b682015-05-21 22:48:54 +00003775 Op<0>() = Value;
3776 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003777}
3778
Chris Lattner2195fc42007-02-24 00:55:48 +00003779/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3780/// switch on and a default destination. The number of additional cases can
3781/// be specified here to make memory allocation more efficient. This
3782/// constructor can also autoinsert before another instruction.
3783SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3784 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003785 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3786 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003787 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003788}
3789
3790/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3791/// switch on and a default destination. The number of additional cases can
3792/// be specified here to make memory allocation more efficient. This
3793/// constructor also autoinserts at the end of the specified BasicBlock.
3794SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3795 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003796 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3797 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003798 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003799}
3800
Misha Brukmanb1c93172005-04-21 23:48:37 +00003801SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003802 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003803 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003804 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003805 Use *OL = getOperandList();
3806 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003807 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003808 OL[i] = InOL[i];
3809 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003810 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003811 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003812}
3813
3814/// addCase - Add an entry to the switch instruction...
3815///
Chris Lattner47ac1872005-02-24 05:32:09 +00003816void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003817 unsigned NewCaseIdx = getNumCases();
3818 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003819 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003820 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003821 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003822 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003823 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003824 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003825 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003826 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003827}
3828
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003829/// removeCase - This method removes the specified case and its successor
3830/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003831SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3832 unsigned idx = I->getCaseIndex();
3833
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003834 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003835
3836 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003837 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003838
Jay Foad14277722011-02-01 09:22:34 +00003839 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003840 if (2 + (idx + 1) * 2 != NumOps) {
3841 OL[2 + idx * 2] = OL[NumOps - 2];
3842 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003843 }
3844
3845 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003846 OL[NumOps-2].set(nullptr);
3847 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003848 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003849
3850 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003851}
3852
Jay Foade98f29d2011-04-01 08:00:58 +00003853/// growOperands - grow operands - This grows the operand list in response
3854/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003855///
Jay Foade98f29d2011-04-01 08:00:58 +00003856void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003857 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003858 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003859
3860 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003861 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003862}
3863
Chris Lattner3ed871f2009-10-27 19:13:16 +00003864//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003865// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003866//===----------------------------------------------------------------------===//
3867
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003868void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003869 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003870 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003871 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003872 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003873 allocHungoffUses(ReservedSpace);
3874
Pete Coopereb31b682015-05-21 22:48:54 +00003875 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003876}
3877
3878
Jay Foade98f29d2011-04-01 08:00:58 +00003879/// growOperands - grow operands - This grows the operand list in response
3880/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003881///
Jay Foade98f29d2011-04-01 08:00:58 +00003882void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003883 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003884 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003885
Chris Lattner3ed871f2009-10-27 19:13:16 +00003886 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003887 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003888}
3889
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003890IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3891 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003892 : Instruction(Type::getVoidTy(Address->getContext()),
3893 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003894 init(Address, NumCases);
3895}
3896
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003897IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3898 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003899 : Instruction(Type::getVoidTy(Address->getContext()),
3900 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003901 init(Address, NumCases);
3902}
3903
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003904IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003905 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3906 nullptr, IBI.getNumOperands()) {
Pete Cooper3fc30402015-06-10 22:38:46 +00003907 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003908 Use *OL = getOperandList();
3909 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003910 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3911 OL[i] = InOL[i];
3912 SubclassOptionalData = IBI.SubclassOptionalData;
3913}
3914
Chris Lattner3ed871f2009-10-27 19:13:16 +00003915/// addDestination - Add a destination.
3916///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003917void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003918 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003919 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003920 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003921 // Initialize some new operands.
3922 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003923 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003924 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003925}
3926
3927/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003928/// indirectbr instruction.
3929void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003930 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003931
Chris Lattner3ed871f2009-10-27 19:13:16 +00003932 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003933 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003934
3935 // Replace this value with the last one.
3936 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003937
Chris Lattner3ed871f2009-10-27 19:13:16 +00003938 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003939 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003940 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003941}
3942
Chris Lattner3ed871f2009-10-27 19:13:16 +00003943//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003944// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003945//===----------------------------------------------------------------------===//
3946
Chris Lattnerf22be932004-10-15 23:52:53 +00003947// Define these methods here so vtables don't get emitted into every translation
3948// unit that uses these classes.
3949
Pete Cooper75403d72015-06-24 20:22:23 +00003950GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003951 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003952}
3953
Cameron McInallycbde0d92018-11-13 18:15:47 +00003954UnaryOperator *UnaryOperator::cloneImpl() const {
3955 return Create(getOpcode(), Op<0>());
3956}
3957
Pete Cooper75403d72015-06-24 20:22:23 +00003958BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003959 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003960}
3961
Pete Cooper75403d72015-06-24 20:22:23 +00003962FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003963 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003964}
3965
Pete Cooper75403d72015-06-24 20:22:23 +00003966ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003967 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003968}
3969
Pete Cooper75403d72015-06-24 20:22:23 +00003970ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003971 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003972}
3973
Pete Cooper75403d72015-06-24 20:22:23 +00003974InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003975 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003976}
3977
Pete Cooper75403d72015-06-24 20:22:23 +00003978AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003979 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003980 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003981 (Value *)getOperand(0), getAlignment());
3982 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003983 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003984 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003985}
3986
Pete Cooper75403d72015-06-24 20:22:23 +00003987LoadInst *LoadInst::cloneImpl() const {
James Y Knight84c1dbd2019-01-14 21:37:53 +00003988 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003989 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003990}
3991
Pete Cooper75403d72015-06-24 20:22:23 +00003992StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003993 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003994 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003995
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003996}
3997
Pete Cooper75403d72015-06-24 20:22:23 +00003998AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003999 AtomicCmpXchgInst *Result =
4000 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00004001 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00004002 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004003 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00004004 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004005 return Result;
4006}
4007
Pete Cooper75403d72015-06-24 20:22:23 +00004008AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004009 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00004010 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
4011 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004012 Result->setVolatile(isVolatile());
4013 return Result;
4014}
4015
Pete Cooper75403d72015-06-24 20:22:23 +00004016FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00004017 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00004018}
4019
Pete Cooper75403d72015-06-24 20:22:23 +00004020TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004021 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004022}
4023
Pete Cooper75403d72015-06-24 20:22:23 +00004024ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004025 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004026}
4027
Pete Cooper75403d72015-06-24 20:22:23 +00004028SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004029 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004030}
4031
Pete Cooper75403d72015-06-24 20:22:23 +00004032FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004033 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004034}
4035
Pete Cooper75403d72015-06-24 20:22:23 +00004036FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004037 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004038}
4039
Pete Cooper75403d72015-06-24 20:22:23 +00004040UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004041 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004042}
4043
Pete Cooper75403d72015-06-24 20:22:23 +00004044SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004045 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004046}
4047
Pete Cooper75403d72015-06-24 20:22:23 +00004048FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004049 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004050}
4051
Pete Cooper75403d72015-06-24 20:22:23 +00004052FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004053 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004054}
4055
Pete Cooper75403d72015-06-24 20:22:23 +00004056PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004057 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004058}
4059
Pete Cooper75403d72015-06-24 20:22:23 +00004060IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004061 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004062}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004063
Pete Cooper75403d72015-06-24 20:22:23 +00004064BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004065 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004066}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00004067
Pete Cooper75403d72015-06-24 20:22:23 +00004068AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004069 return new AddrSpaceCastInst(getOperand(0), getType());
4070}
4071
Pete Cooper75403d72015-06-24 20:22:23 +00004072CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004073 if (hasOperandBundles()) {
4074 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4075 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4076 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004077 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004078}
4079
Pete Cooper75403d72015-06-24 20:22:23 +00004080SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004081 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004082}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004083
Pete Cooper75403d72015-06-24 20:22:23 +00004084VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004085 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004086}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004087
Pete Cooper75403d72015-06-24 20:22:23 +00004088ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004089 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004090}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004091
Pete Cooper75403d72015-06-24 20:22:23 +00004092InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004093 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004094}
4095
Pete Cooper75403d72015-06-24 20:22:23 +00004096ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004097 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004098}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004099
Pete Cooper75403d72015-06-24 20:22:23 +00004100PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004101
Pete Cooper75403d72015-06-24 20:22:23 +00004102LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004103 return new LandingPadInst(*this);
4104}
4105
Pete Cooper75403d72015-06-24 20:22:23 +00004106ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004107 return new(getNumOperands()) ReturnInst(*this);
4108}
4109
Pete Cooper75403d72015-06-24 20:22:23 +00004110BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004111 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004112}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004113
Pete Cooper75403d72015-06-24 20:22:23 +00004114SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004115
Pete Cooper75403d72015-06-24 20:22:23 +00004116IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004117 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004118}
4119
Pete Cooper75403d72015-06-24 20:22:23 +00004120InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004121 if (hasOperandBundles()) {
4122 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4123 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4124 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004125 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004126}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004127
Craig Topper784929d2019-02-08 20:48:56 +00004128CallBrInst *CallBrInst::cloneImpl() const {
4129 if (hasOperandBundles()) {
4130 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4131 return new (getNumOperands(), DescriptorBytes) CallBrInst(*this);
4132 }
4133 return new (getNumOperands()) CallBrInst(*this);
4134}
4135
Pete Cooper75403d72015-06-24 20:22:23 +00004136ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004137
David Majnemer654e1302015-07-31 17:58:14 +00004138CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4139 return new (getNumOperands()) CleanupReturnInst(*this);
4140}
4141
David Majnemer654e1302015-07-31 17:58:14 +00004142CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004143 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004144}
4145
David Majnemer8a1c45d2015-12-12 05:38:55 +00004146CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4147 return new CatchSwitchInst(*this);
4148}
4149
4150FuncletPadInst *FuncletPadInst::cloneImpl() const {
4151 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004152}
4153
Pete Cooper75403d72015-06-24 20:22:23 +00004154UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004155 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004156 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004157}