blob: 32db918dab973f74138da2ad01348a58a1ddf0bc [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001//===- Instructions.cpp - Implement the LLVM instructions -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000017#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-06-26 06:17:00 +000048// AllocaInst Class
49//===----------------------------------------------------------------------===//
50
51Optional<uint64_t>
52AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
53 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
54 if (isArrayAllocation()) {
55 auto C = dyn_cast<ConstantInt>(getArraySize());
56 if (!C)
57 return None;
58 Size *= C->getZExtValue();
59 }
60 return Size;
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000064// CallSite Class
65//===----------------------------------------------------------------------===//
66
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000067User::op_iterator CallSite::getCallee() const {
68 Instruction *II(getInstruction());
69 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000070 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000071 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000072}
73
Gordon Henriksen14a55692007-12-10 02:14:30 +000074//===----------------------------------------------------------------------===//
75// TerminatorInst Class
76//===----------------------------------------------------------------------===//
77
Reid Kleckner45a13e12017-05-11 21:26:55 +000078unsigned TerminatorInst::getNumSuccessors() const {
79 switch (getOpcode()) {
80#define HANDLE_TERM_INST(N, OPC, CLASS) \
81 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000082 return static_cast<const CLASS *>(this)->getNumSuccessors();
Reid Kleckner45a13e12017-05-11 21:26:55 +000083#include "llvm/IR/Instruction.def"
84 default:
85 break;
86 }
87 llvm_unreachable("not a terminator");
88}
89
90BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
91 switch (getOpcode()) {
92#define HANDLE_TERM_INST(N, OPC, CLASS) \
93 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000094 return static_cast<const CLASS *>(this)->getSuccessor(idx);
Reid Kleckner45a13e12017-05-11 21:26:55 +000095#include "llvm/IR/Instruction.def"
96 default:
97 break;
98 }
99 llvm_unreachable("not a terminator");
100}
101
102void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
103 switch (getOpcode()) {
104#define HANDLE_TERM_INST(N, OPC, CLASS) \
105 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +0000106 return static_cast<CLASS *>(this)->setSuccessor(idx, B);
Reid Kleckner45a13e12017-05-11 21:26:55 +0000107#include "llvm/IR/Instruction.def"
108 default:
109 break;
110 }
111 llvm_unreachable("not a terminator");
112}
113
Gabor Greiff6caff662008-05-10 08:32:32 +0000114//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000115// SelectInst Class
116//===----------------------------------------------------------------------===//
117
118/// areInvalidOperands - Return a string if the specified operands are invalid
119/// for a select operation, otherwise return null.
120const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
121 if (Op1->getType() != Op2->getType())
122 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000123
124 if (Op1->getType()->isTokenTy())
125 return "select values cannot have token type";
126
Chris Lattner229907c2011-07-18 04:54:35 +0000127 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000128 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000129 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000130 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000131 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000132 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000133 return "selected values for vector select must be vectors";
134 if (ET->getNumElements() != VT->getNumElements())
135 return "vector select requires selected vectors to have "
136 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000137 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000138 return "select condition must be i1 or <n x i1>";
139 }
Craig Topperc6207612014-04-09 06:08:46 +0000140 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000141}
142
Chris Lattner88107952008-12-29 00:12:50 +0000143//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144// PHINode Class
145//===----------------------------------------------------------------------===//
146
147PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000148 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
149 ReservedSpace(PN.getNumOperands()) {
150 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000151 std::copy(PN.op_begin(), PN.op_end(), op_begin());
152 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000153 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154}
155
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156// removeIncomingValue - Remove an incoming value. This is useful if a
157// predecessor basic block is deleted.
158Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000159 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160
161 // Move everything after this operand down.
162 //
163 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000164 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000165 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000166 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
167 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000168
169 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000170 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000171 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000172
173 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000174 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000176 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177 eraseFromParent();
178 }
179 return Removed;
180}
181
Jay Foade98f29d2011-04-01 08:00:58 +0000182/// growOperands - grow operands - This grows the operand list in response
183/// to a push_back style of operation. This grows the number of ops by 1.5
184/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000185///
Jay Foade98f29d2011-04-01 08:00:58 +0000186void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000187 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000188 unsigned NumOps = e + e / 2;
189 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
190
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000192 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000193}
194
Nate Begemanb3923212005-08-04 23:24:19 +0000195/// hasConstantValue - If the specified PHI node always merges together the same
196/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000197Value *PHINode::hasConstantValue() const {
198 // Exploit the fact that phi nodes always have at least one entry.
199 Value *ConstantValue = getIncomingValue(0);
200 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000201 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
202 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000203 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000204 // The case where the first value is this PHI.
205 ConstantValue = getIncomingValue(i);
206 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000207 if (ConstantValue == this)
208 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000209 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000210}
211
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000212/// hasConstantOrUndefValue - Whether the specified PHI node always merges
213/// together the same value, assuming that undefs result in the same value as
214/// non-undefs.
215/// Unlike \ref hasConstantValue, this does not return a value because the
216/// unique non-undef incoming value need not dominate the PHI node.
217bool PHINode::hasConstantOrUndefValue() const {
218 Value *ConstantValue = nullptr;
219 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
220 Value *Incoming = getIncomingValue(i);
221 if (Incoming != this && !isa<UndefValue>(Incoming)) {
222 if (ConstantValue && ConstantValue != Incoming)
223 return false;
224 ConstantValue = Incoming;
225 }
226 }
227 return true;
228}
229
Bill Wendlingfae14752011-08-12 20:24:12 +0000230//===----------------------------------------------------------------------===//
231// LandingPadInst Implementation
232//===----------------------------------------------------------------------===//
233
David Majnemer7fddecc2015-06-17 20:52:32 +0000234LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
235 const Twine &NameStr, Instruction *InsertBefore)
236 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
237 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000238}
239
David Majnemer7fddecc2015-06-17 20:52:32 +0000240LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
241 const Twine &NameStr, BasicBlock *InsertAtEnd)
242 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
243 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000244}
245
246LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000247 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
248 LP.getNumOperands()),
249 ReservedSpace(LP.getNumOperands()) {
250 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000251 Use *OL = getOperandList();
252 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000253 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
254 OL[I] = InOL[I];
255
256 setCleanup(LP.isCleanup());
257}
258
David Majnemer7fddecc2015-06-17 20:52:32 +0000259LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000260 const Twine &NameStr,
261 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000262 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000263}
264
David Majnemer7fddecc2015-06-17 20:52:32 +0000265LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000266 const Twine &NameStr,
267 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000268 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000269}
270
David Majnemer7fddecc2015-06-17 20:52:32 +0000271void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000272 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000273 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000274 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000275 setName(NameStr);
276 setCleanup(false);
277}
278
279/// growOperands - grow operands - This grows the operand list in response to a
280/// push_back style of operation. This grows the number of ops by 2 times.
281void LandingPadInst::growOperands(unsigned Size) {
282 unsigned e = getNumOperands();
283 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000284 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000285 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000286}
287
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000288void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000289 unsigned OpNo = getNumOperands();
290 growOperands(1);
291 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000292 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000293 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000294}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295
296//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297// CallInst Implementation
298//===----------------------------------------------------------------------===//
299
David Blaikie348de692015-04-23 21:36:23 +0000300void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000301 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000302 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000303 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
304 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000305 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306
Jay Foad5bd375a2011-07-15 08:37:34 +0000307#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 assert((Args.size() == FTy->getNumParams() ||
309 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000310 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000311
312 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000313 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000315 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000316#endif
317
318 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000319
320 auto It = populateBundleOperandInfos(Bundles, Args.size());
321 (void)It;
322 assert(It + 1 == op_end() && "Should add up!");
323
Jay Foad5bd375a2011-07-15 08:37:34 +0000324 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325}
326
Jay Foad5bd375a2011-07-15 08:37:34 +0000327void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000328 FTy =
329 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000330 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000331 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000332
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000333 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000334
335 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000338CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
339 : CallBase<CallInst>(
340 cast<FunctionType>(
341 cast<PointerType>(Func->getType())->getElementType())
342 ->getReturnType(),
343 Instruction::Call,
344 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1,
345 InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000346 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347}
348
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000349CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
350 : CallBase<CallInst>(
351 cast<FunctionType>(
352 cast<PointerType>(Func->getType())->getElementType())
353 ->getReturnType(),
354 Instruction::Call,
355 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000356 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357}
358
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359CallInst::CallInst(const CallInst &CI)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000360 : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
361 OperandTraits<CallBase<CallInst>>::op_end(this) -
362 CI.getNumOperands(),
363 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000364 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000365 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000366
Jay Foad5bd375a2011-07-15 08:37:34 +0000367 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000368 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
369 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000370 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371}
372
Sanjoy Das2d161452015-11-18 06:23:38 +0000373CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
374 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000375 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000376
377 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
378 InsertPt);
379 NewCI->setTailCallKind(CI->getTailCallKind());
380 NewCI->setCallingConv(CI->getCallingConv());
381 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000382 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000383 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000384 return NewCI;
385}
386
Hal Finkele87ad542016-07-10 23:01:32 +0000387
Reid Klecknera0b45f42017-05-03 18:17:31 +0000388
Hal Finkele87ad542016-07-10 23:01:32 +0000389
Eric Christopher901b1a72008-05-16 20:39:43 +0000390
Amaury Secheta65a2372016-06-15 05:14:29 +0000391
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000392
Reid Klecknera0b45f42017-05-03 18:17:31 +0000393
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000394
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000395
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000396/// IsConstantOne - Return true only if val is constant int 1
397static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000398 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000399 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
400 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000401}
402
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000403static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000404 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000405 Type *AllocTy, Value *AllocSize,
406 Value *ArraySize,
407 ArrayRef<OperandBundleDef> OpB,
408 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410 "createMalloc needs either InsertBefore or InsertAtEnd");
411
Fangrui Songf78650a2018-07-30 19:41:25 +0000412 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000413 // bitcast (i8* malloc(typeSize)) to type*
414 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000415 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000416 if (!ArraySize)
417 ArraySize = ConstantInt::get(IntPtrTy, 1);
418 else if (ArraySize->getType() != IntPtrTy) {
419 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000420 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
421 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000422 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000423 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
424 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000425 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000426
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000427 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000428 if (IsConstantOne(AllocSize)) {
429 AllocSize = ArraySize; // Operand * 1 = Operand
430 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
431 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
432 false /*ZExt*/);
433 // Malloc arg is constant product of type size and array size
434 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
435 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436 // Multiply type size by the array size...
437 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
439 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000440 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000441 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
442 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000444 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000447 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000448 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
449 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000450 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000451 Value *MallocFunc = MallocF;
452 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000453 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000454 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000455 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000456 CallInst *MCall = nullptr;
457 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000458 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000459 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
460 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000461 Result = MCall;
462 if (Result->getType() != AllocPtrType)
463 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000464 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000466 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000467 Result = MCall;
468 if (Result->getType() != AllocPtrType) {
469 InsertAtEnd->getInstList().push_back(MCall);
470 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000472 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000473 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000475 if (Function *F = dyn_cast<Function>(MallocFunc)) {
476 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000477 if (!F->returnDoesNotAlias())
478 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000479 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000480 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000481
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000482 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000483}
484
485/// CreateMalloc - Generate the IR for a call to malloc:
486/// 1. Compute the malloc call's argument as the specified type's size,
487/// possibly multiplied by the array size if the array size is not
488/// constant 1.
489/// 2. Call malloc with that argument.
490/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000491Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000492 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000493 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000494 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000495 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000496 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000497 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000498}
David Majnemerfadc6db2016-04-29 08:07:22 +0000499Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
500 Type *IntPtrTy, Type *AllocTy,
501 Value *AllocSize, Value *ArraySize,
502 ArrayRef<OperandBundleDef> OpB,
503 Function *MallocF,
504 const Twine &Name) {
505 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
506 ArraySize, OpB, MallocF, Name);
507}
508
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509/// CreateMalloc - Generate the IR for a call to malloc:
510/// 1. Compute the malloc call's argument as the specified type's size,
511/// possibly multiplied by the array size if the array size is not
512/// constant 1.
513/// 2. Call malloc with that argument.
514/// 3. Bitcast the result of the malloc call to the specified type.
515/// Note: This function does not add the bitcast to the basic block, that is the
516/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000517Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000518 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000519 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000520 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000521 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000522 ArraySize, None, MallocF, Name);
523}
524Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
525 Type *IntPtrTy, Type *AllocTy,
526 Value *AllocSize, Value *ArraySize,
527 ArrayRef<OperandBundleDef> OpB,
528 Function *MallocF, const Twine &Name) {
529 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
530 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000531}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000532
David Majnemerfadc6db2016-04-29 08:07:22 +0000533static Instruction *createFree(Value *Source,
534 ArrayRef<OperandBundleDef> Bundles,
535 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000536 BasicBlock *InsertAtEnd) {
537 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
538 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000539 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000540 "Can not free something of nonpointer type!");
541
Ana Pazosb3596022016-02-03 21:34:39 +0000542 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
543 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000544
Chris Lattner229907c2011-07-18 04:54:35 +0000545 Type *VoidTy = Type::getVoidTy(M->getContext());
546 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000547 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000548 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000549 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000550 Value *PtrCast = Source;
551 if (InsertBefore) {
552 if (Source->getType() != IntPtrTy)
553 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000554 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000555 } else {
556 if (Source->getType() != IntPtrTy)
557 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000558 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000559 }
560 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000561 if (Function *F = dyn_cast<Function>(FreeFunc))
562 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000563
564 return Result;
565}
566
567/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000568Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000569 return createFree(Source, None, InsertBefore, nullptr);
570}
571Instruction *CallInst::CreateFree(Value *Source,
572 ArrayRef<OperandBundleDef> Bundles,
573 Instruction *InsertBefore) {
574 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000575}
576
577/// CreateFree - Generate the IR for a call to the builtin free function.
578/// Note: This function does not add the call to the basic block, that is the
579/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000580Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000581 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
582 assert(FreeCall && "CreateFree did not create a CallInst");
583 return FreeCall;
584}
585Instruction *CallInst::CreateFree(Value *Source,
586 ArrayRef<OperandBundleDef> Bundles,
587 BasicBlock *InsertAtEnd) {
588 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000589 assert(FreeCall && "CreateFree did not create a CallInst");
590 return FreeCall;
591}
592
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593//===----------------------------------------------------------------------===//
594// InvokeInst Implementation
595//===----------------------------------------------------------------------===//
596
David Blaikie3e807092015-05-13 18:35:26 +0000597void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
598 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000599 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000600 const Twine &NameStr) {
601 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000602
Sanjoy Das9303c242015-09-24 19:14:18 +0000603 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
604 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000605 Op<-3>() = Fn;
606 Op<-2>() = IfNormal;
607 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000608
609#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000610 assert(((Args.size() == FTy->getNumParams()) ||
611 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000612 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000613
Jay Foad5bd375a2011-07-15 08:37:34 +0000614 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000615 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000616 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000617 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000618#endif
619
620 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000621
622 auto It = populateBundleOperandInfos(Bundles, Args.size());
623 (void)It;
624 assert(It + 3 == op_end() && "Should add up!");
625
Jay Foad5bd375a2011-07-15 08:37:34 +0000626 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627}
628
Misha Brukmanb1c93172005-04-21 23:48:37 +0000629InvokeInst::InvokeInst(const InvokeInst &II)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000630 : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
631 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
632 II.getNumOperands(),
633 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000634 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000635 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000636 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
637 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000638 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639}
640
Sanjoy Das2d161452015-11-18 06:23:38 +0000641InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
642 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000643 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000644
645 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
646 II->getUnwindDest(), Args, OpB,
647 II->getName(), InsertPt);
648 NewII->setCallingConv(II->getCallingConv());
649 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000650 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000651 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000652 return NewII;
653}
654
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000655
Bill Wendlingfae14752011-08-12 20:24:12 +0000656LandingPadInst *InvokeInst::getLandingPadInst() const {
657 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
658}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000659
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660//===----------------------------------------------------------------------===//
661// ReturnInst Implementation
662//===----------------------------------------------------------------------===//
663
Chris Lattner2195fc42007-02-24 00:55:48 +0000664ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000665 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000666 OperandTraits<ReturnInst>::op_end(this) -
667 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000668 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000669 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000670 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000671 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000672}
673
Owen Anderson55f1c092009-08-13 21:58:54 +0000674ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
675 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000676 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
677 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000678 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000679 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000680}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000681
Owen Anderson55f1c092009-08-13 21:58:54 +0000682ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
683 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000684 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
685 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000686 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000687 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000688}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000689
Owen Anderson55f1c092009-08-13 21:58:54 +0000690ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
691 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000692 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000693}
694
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000696// ResumeInst Implementation
697//===----------------------------------------------------------------------===//
698
699ResumeInst::ResumeInst(const ResumeInst &RI)
700 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
701 OperandTraits<ResumeInst>::op_begin(this), 1) {
702 Op<0>() = RI.Op<0>();
703}
704
705ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
706 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
707 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
708 Op<0>() = Exn;
709}
710
711ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
712 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
713 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
714 Op<0>() = Exn;
715}
716
Bill Wendlingf891bf82011-07-31 06:30:59 +0000717//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000718// CleanupReturnInst Implementation
719//===----------------------------------------------------------------------===//
720
721CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
722 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
723 OperandTraits<CleanupReturnInst>::op_end(this) -
724 CRI.getNumOperands(),
725 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000726 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000727 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000728 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000729 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000730}
731
David Majnemer8a1c45d2015-12-12 05:38:55 +0000732void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000733 if (UnwindBB)
734 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000735
David Majnemer8a1c45d2015-12-12 05:38:55 +0000736 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000737 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000738 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000739}
740
David Majnemer8a1c45d2015-12-12 05:38:55 +0000741CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
742 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000743 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
744 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000745 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
746 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000747 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000748}
749
David Majnemer8a1c45d2015-12-12 05:38:55 +0000750CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
751 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000752 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
753 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000754 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
755 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000756 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000757}
758
David Majnemer654e1302015-07-31 17:58:14 +0000759//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000760// CatchReturnInst Implementation
761//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000762void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000763 Op<0>() = CatchPad;
764 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000765}
David Majnemer654e1302015-07-31 17:58:14 +0000766
767CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
768 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000769 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
770 Op<0>() = CRI.Op<0>();
771 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000772}
773
David Majnemer8a1c45d2015-12-12 05:38:55 +0000774CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000775 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000776 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000777 OperandTraits<CatchReturnInst>::op_begin(this), 2,
778 InsertBefore) {
779 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000780}
781
David Majnemer8a1c45d2015-12-12 05:38:55 +0000782CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000783 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000784 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000785 OperandTraits<CatchReturnInst>::op_begin(this), 2,
786 InsertAtEnd) {
787 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000788}
789
David Majnemer654e1302015-07-31 17:58:14 +0000790//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000791// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000792//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000793
794CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
795 unsigned NumReservedValues,
796 const Twine &NameStr,
797 Instruction *InsertBefore)
798 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
799 InsertBefore) {
800 if (UnwindDest)
801 ++NumReservedValues;
802 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000803 setName(NameStr);
804}
805
David Majnemer8a1c45d2015-12-12 05:38:55 +0000806CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
807 unsigned NumReservedValues,
808 const Twine &NameStr, BasicBlock *InsertAtEnd)
809 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000810 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000811 if (UnwindDest)
812 ++NumReservedValues;
813 init(ParentPad, UnwindDest, NumReservedValues + 1);
814 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000815}
816
David Majnemer8a1c45d2015-12-12 05:38:55 +0000817CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
818 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
819 CSI.getNumOperands()) {
820 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
821 setNumHungOffUseOperands(ReservedSpace);
822 Use *OL = getOperandList();
823 const Use *InOL = CSI.getOperandList();
824 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
825 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000826}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000827
828void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
829 unsigned NumReservedValues) {
830 assert(ParentPad && NumReservedValues);
831
832 ReservedSpace = NumReservedValues;
833 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
834 allocHungoffUses(ReservedSpace);
835
836 Op<0>() = ParentPad;
837 if (UnwindDest) {
838 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
839 setUnwindDest(UnwindDest);
840 }
841}
842
843/// growOperands - grow operands - This grows the operand list in response to a
844/// push_back style of operation. This grows the number of ops by 2 times.
845void CatchSwitchInst::growOperands(unsigned Size) {
846 unsigned NumOperands = getNumOperands();
847 assert(NumOperands >= 1);
848 if (ReservedSpace >= NumOperands + Size)
849 return;
850 ReservedSpace = (NumOperands + Size / 2) * 2;
851 growHungoffUses(ReservedSpace);
852}
853
854void CatchSwitchInst::addHandler(BasicBlock *Handler) {
855 unsigned OpNo = getNumOperands();
856 growOperands(1);
857 assert(OpNo < ReservedSpace && "Growing didn't work!");
858 setNumHungOffUseOperands(getNumOperands() + 1);
859 getOperandList()[OpNo] = Handler;
860}
861
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000862void CatchSwitchInst::removeHandler(handler_iterator HI) {
863 // Move all subsequent handlers up one.
864 Use *EndDst = op_end() - 1;
865 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
866 *CurDst = *(CurDst + 1);
867 // Null out the last handler use.
868 *EndDst = nullptr;
869
870 setNumHungOffUseOperands(getNumOperands() - 1);
871}
872
David Majnemer8a1c45d2015-12-12 05:38:55 +0000873//===----------------------------------------------------------------------===//
874// FuncletPadInst Implementation
875//===----------------------------------------------------------------------===//
876void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
877 const Twine &NameStr) {
878 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
879 std::copy(Args.begin(), Args.end(), op_begin());
880 setParentPad(ParentPad);
881 setName(NameStr);
882}
883
884FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
885 : Instruction(FPI.getType(), FPI.getOpcode(),
886 OperandTraits<FuncletPadInst>::op_end(this) -
887 FPI.getNumOperands(),
888 FPI.getNumOperands()) {
889 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
890 setParentPad(FPI.getParentPad());
891}
892
893FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
894 ArrayRef<Value *> Args, unsigned Values,
895 const Twine &NameStr, Instruction *InsertBefore)
896 : Instruction(ParentPad->getType(), Op,
897 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
898 InsertBefore) {
899 init(ParentPad, Args, NameStr);
900}
901
902FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
903 ArrayRef<Value *> Args, unsigned Values,
904 const Twine &NameStr, BasicBlock *InsertAtEnd)
905 : Instruction(ParentPad->getType(), Op,
906 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
907 InsertAtEnd) {
908 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000909}
910
911//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000912// UnreachableInst Implementation
913//===----------------------------------------------------------------------===//
914
Fangrui Songf78650a2018-07-30 19:41:25 +0000915UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +0000916 Instruction *InsertBefore)
917 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000918 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000919}
Owen Anderson55f1c092009-08-13 21:58:54 +0000920UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
921 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000922 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000923}
924
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000925//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926// BranchInst Implementation
927//===----------------------------------------------------------------------===//
928
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929void BranchInst::AssertOK() {
930 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000931 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933}
934
Chris Lattner2195fc42007-02-24 00:55:48 +0000935BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000936 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000937 OperandTraits<BranchInst>::op_end(this) - 1,
938 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000939 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000940 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000941}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000942
Chris Lattner2195fc42007-02-24 00:55:48 +0000943BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
944 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000945 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000946 OperandTraits<BranchInst>::op_end(this) - 3,
947 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000948 Op<-1>() = IfTrue;
949 Op<-2>() = IfFalse;
950 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000951#ifndef NDEBUG
952 AssertOK();
953#endif
954}
955
956BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000957 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000958 OperandTraits<BranchInst>::op_end(this) - 1,
959 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000960 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000961 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000962}
963
964BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
965 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000966 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000967 OperandTraits<BranchInst>::op_end(this) - 3,
968 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000969 Op<-1>() = IfTrue;
970 Op<-2>() = IfFalse;
971 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000972#ifndef NDEBUG
973 AssertOK();
974#endif
975}
976
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000978 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
980 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000981 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000982 if (BI.getNumOperands() != 1) {
983 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000984 Op<-3>() = BI.Op<-3>();
985 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000987 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988}
989
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000990void BranchInst::swapSuccessors() {
991 assert(isConditional() &&
992 "Cannot swap successors of an unconditional branch");
993 Op<-1>().swap(Op<-2>());
994
995 // Update profile metadata if present and it matches our structural
996 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +0000997 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000998}
999
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001001// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002//===----------------------------------------------------------------------===//
1003
Owen Andersonb6b25302009-07-14 23:09:55 +00001004static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001006 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001007 else {
1008 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001009 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001010 assert(Amt->getType()->isIntegerTy() &&
1011 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001012 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001013 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001014}
1015
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001016AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001017 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001018 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001019
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001020AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001021 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001022 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001023
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001024AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001025 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001026 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1027
1028AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1029 const Twine &Name, BasicBlock *InsertAtEnd)
1030 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1031
1032AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1033 unsigned Align, const Twine &Name,
1034 Instruction *InsertBefore)
1035 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1036 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1037 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001038 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001039 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001040 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041}
1042
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001043AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1044 unsigned Align, const Twine &Name,
1045 BasicBlock *InsertAtEnd)
1046 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1047 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001048 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001049 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001050 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001051 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001052}
1053
Victor Hernandez8acf2952009-10-23 21:09:37 +00001054void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001055 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001056 assert(Align <= MaximumAlignment &&
1057 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001058 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1059 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001060 assert(getAlignment() == Align && "Alignment representation error!");
1061}
1062
Victor Hernandez8acf2952009-10-23 21:09:37 +00001063bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001064 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001065 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001066 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067}
1068
Chris Lattner8b291e62008-11-26 02:54:17 +00001069/// isStaticAlloca - Return true if this alloca is in the entry block of the
1070/// function and is a constant size. If so, the code generator will fold it
1071/// into the prolog/epilog code, so it is basically free.
1072bool AllocaInst::isStaticAlloca() const {
1073 // Must be constant size.
1074 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001075
Chris Lattner8b291e62008-11-26 02:54:17 +00001076 // Must be in the entry block.
1077 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001078 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001079}
1080
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001082// LoadInst Implementation
1083//===----------------------------------------------------------------------===//
1084
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001085void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001086 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001087 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001088 assert(!(isAtomic() && getAlignment() == 0) &&
1089 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090}
1091
Daniel Dunbar4975db62009-07-25 04:41:11 +00001092LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001093 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001094
Daniel Dunbar4975db62009-07-25 04:41:11 +00001095LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001096 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001097
David Blaikie0c28fd72015-05-20 21:46:30 +00001098LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001099 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001100 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001101
1102LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1103 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001104 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001105
David Blaikieb7a029872015-04-17 19:56:21 +00001106LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001107 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001108 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001109 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001111LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001112 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001113 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001114 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001115
David Blaikie15d9a4c2015-04-06 20:59:48 +00001116LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001117 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001118 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001119 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001120 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001121 setVolatile(isVolatile);
1122 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001123 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001124 AssertOK();
1125 setName(Name);
1126}
1127
Fangrui Songf78650a2018-07-30 19:41:25 +00001128LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001129 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001130 SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001132 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001133 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001134 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001135 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001136 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001137 AssertOK();
1138 setName(Name);
1139}
1140
Daniel Dunbar27096822009-08-11 18:11:15 +00001141LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1142 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1143 Load, Ptr, InsertBef) {
1144 setVolatile(false);
1145 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001146 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001147 AssertOK();
1148 if (Name && Name[0]) setName(Name);
1149}
1150
1151LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1152 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1153 Load, Ptr, InsertAE) {
1154 setVolatile(false);
1155 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001156 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001157 AssertOK();
1158 if (Name && Name[0]) setName(Name);
1159}
1160
David Blaikie0c28fd72015-05-20 21:46:30 +00001161LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001162 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001163 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1164 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001165 setVolatile(isVolatile);
1166 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001167 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001168 AssertOK();
1169 if (Name && Name[0]) setName(Name);
1170}
1171
1172LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1173 BasicBlock *InsertAE)
1174 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1175 Load, Ptr, InsertAE) {
1176 setVolatile(isVolatile);
1177 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001178 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001179 AssertOK();
1180 if (Name && Name[0]) setName(Name);
1181}
1182
Christopher Lamb84485702007-04-22 19:24:39 +00001183void LoadInst::setAlignment(unsigned Align) {
1184 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001185 assert(Align <= MaximumAlignment &&
1186 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001187 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001188 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001189 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001190}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191
1192//===----------------------------------------------------------------------===//
1193// StoreInst Implementation
1194//===----------------------------------------------------------------------===//
1195
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001196void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001197 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001198 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001199 "Ptr must have pointer type!");
1200 assert(getOperand(0)->getType() ==
1201 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001202 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001203 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001204 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001205}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001207StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001208 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001209
1210StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001211 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001212
Misha Brukmanb1c93172005-04-21 23:48:37 +00001213StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001215 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001216
1217StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001218 BasicBlock *InsertAtEnd)
1219 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1220
1221StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1222 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001223 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001224 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001225
1226StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1227 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001228 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001229 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001230
Misha Brukmanb1c93172005-04-21 23:48:37 +00001231StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001232 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001233 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001234 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001235 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001236 OperandTraits<StoreInst>::op_begin(this),
1237 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001238 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 Op<0>() = val;
1240 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001241 setVolatile(isVolatile);
1242 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001243 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001244 AssertOK();
1245}
1246
1247StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001248 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001249 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001250 BasicBlock *InsertAtEnd)
1251 : Instruction(Type::getVoidTy(val->getContext()), Store,
1252 OperandTraits<StoreInst>::op_begin(this),
1253 OperandTraits<StoreInst>::operands(this),
1254 InsertAtEnd) {
1255 Op<0>() = val;
1256 Op<1>() = addr;
1257 setVolatile(isVolatile);
1258 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001259 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001260 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001261}
1262
Christopher Lamb84485702007-04-22 19:24:39 +00001263void StoreInst::setAlignment(unsigned Align) {
1264 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001265 assert(Align <= MaximumAlignment &&
1266 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001267 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001268 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001269 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001270}
1271
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001272//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001273// AtomicCmpXchgInst Implementation
1274//===----------------------------------------------------------------------===//
1275
1276void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001277 AtomicOrdering SuccessOrdering,
1278 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001279 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001280 Op<0>() = Ptr;
1281 Op<1>() = Cmp;
1282 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001283 setSuccessOrdering(SuccessOrdering);
1284 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001285 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001286
1287 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1288 "All operands must be non-null!");
1289 assert(getOperand(0)->getType()->isPointerTy() &&
1290 "Ptr must have pointer type!");
1291 assert(getOperand(1)->getType() ==
1292 cast<PointerType>(getOperand(0)->getType())->getElementType()
1293 && "Ptr must be a pointer to Cmp type!");
1294 assert(getOperand(2)->getType() ==
1295 cast<PointerType>(getOperand(0)->getType())->getElementType()
1296 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001297 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001298 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001299 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001300 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001301 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1302 "AtomicCmpXchg failure argument shall be no stronger than the success "
1303 "argument");
1304 assert(FailureOrdering != AtomicOrdering::Release &&
1305 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001306 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001307}
1308
1309AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001310 AtomicOrdering SuccessOrdering,
1311 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001312 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001313 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001314 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001315 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001316 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1317 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001318 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001319}
1320
1321AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001322 AtomicOrdering SuccessOrdering,
1323 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001324 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001325 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001326 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001327 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001328 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1329 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001330 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001331}
Tim Northover420a2162014-06-13 14:24:07 +00001332
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001333//===----------------------------------------------------------------------===//
1334// AtomicRMWInst Implementation
1335//===----------------------------------------------------------------------===//
1336
1337void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1338 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001339 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001340 Op<0>() = Ptr;
1341 Op<1>() = Val;
1342 setOperation(Operation);
1343 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001344 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001345
1346 assert(getOperand(0) && getOperand(1) &&
1347 "All operands must be non-null!");
1348 assert(getOperand(0)->getType()->isPointerTy() &&
1349 "Ptr must have pointer type!");
1350 assert(getOperand(1)->getType() ==
1351 cast<PointerType>(getOperand(0)->getType())->getElementType()
1352 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001353 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001354 "AtomicRMW instructions must be atomic!");
1355}
1356
1357AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1358 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001359 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001360 Instruction *InsertBefore)
1361 : Instruction(Val->getType(), AtomicRMW,
1362 OperandTraits<AtomicRMWInst>::op_begin(this),
1363 OperandTraits<AtomicRMWInst>::operands(this),
1364 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001365 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001366}
1367
1368AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1369 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001370 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001371 BasicBlock *InsertAtEnd)
1372 : Instruction(Val->getType(), AtomicRMW,
1373 OperandTraits<AtomicRMWInst>::op_begin(this),
1374 OperandTraits<AtomicRMWInst>::operands(this),
1375 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001376 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001377}
1378
1379//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001380// FenceInst Implementation
1381//===----------------------------------------------------------------------===//
1382
Fangrui Songf78650a2018-07-30 19:41:25 +00001383FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001384 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001385 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001386 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001387 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001388 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001389}
1390
Fangrui Songf78650a2018-07-30 19:41:25 +00001391FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001392 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001393 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001394 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001395 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001396 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001397}
1398
1399//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001400// GetElementPtrInst Implementation
1401//===----------------------------------------------------------------------===//
1402
Jay Foadd1b78492011-07-25 09:48:08 +00001403void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001404 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001405 assert(getNumOperands() == 1 + IdxList.size() &&
1406 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001407 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001408 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001409 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001410}
1411
Gabor Greiff6caff662008-05-10 08:32:32 +00001412GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001413 : Instruction(GEPI.getType(), GetElementPtr,
1414 OperandTraits<GetElementPtrInst>::op_end(this) -
1415 GEPI.getNumOperands(),
1416 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001417 SourceElementType(GEPI.SourceElementType),
1418 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001419 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001420 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001421}
1422
Chris Lattner6090a422009-03-09 04:46:40 +00001423/// getIndexedType - Returns the type of the element that would be accessed with
1424/// a gep instruction with the specified parameters.
1425///
1426/// The Idxs pointer should point to a continuous piece of memory containing the
1427/// indices, either as Value* or uint64_t.
1428///
1429/// A null type is returned if the indices are invalid for the specified
1430/// pointer type.
1431///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001432template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001433static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001434 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001435 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001436 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001437
Chris Lattner6090a422009-03-09 04:46:40 +00001438 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001439 // it cannot be 'stepped over'.
1440 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001441 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001442
Dan Gohman1ecaf452008-05-31 00:58:22 +00001443 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001444 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001445 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001446 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001447 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001448 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001449 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001450 }
Craig Topperc6207612014-04-09 06:08:46 +00001451 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001452}
1453
David Blaikied288fb82015-03-30 21:41:43 +00001454Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1455 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001456}
1457
David Blaikied288fb82015-03-30 21:41:43 +00001458Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001459 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001460 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001461}
1462
David Blaikied288fb82015-03-30 21:41:43 +00001463Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1464 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001465}
1466
Chris Lattner45f15572007-04-14 00:12:57 +00001467/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1468/// zeros. If so, the result pointer and the first operand have the same
1469/// value, just potentially different types.
1470bool GetElementPtrInst::hasAllZeroIndices() const {
1471 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1472 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1473 if (!CI->isZero()) return false;
1474 } else {
1475 return false;
1476 }
1477 }
1478 return true;
1479}
1480
Chris Lattner27058292007-04-27 20:35:56 +00001481/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1482/// constant integers. If so, the result pointer and the first operand have
1483/// a constant offset between them.
1484bool GetElementPtrInst::hasAllConstantIndices() const {
1485 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1486 if (!isa<ConstantInt>(getOperand(i)))
1487 return false;
1488 }
1489 return true;
1490}
1491
Dan Gohman1b849082009-09-07 23:54:19 +00001492void GetElementPtrInst::setIsInBounds(bool B) {
1493 cast<GEPOperator>(this)->setIsInBounds(B);
1494}
Chris Lattner45f15572007-04-14 00:12:57 +00001495
Nick Lewycky28a5f252009-09-27 21:33:04 +00001496bool GetElementPtrInst::isInBounds() const {
1497 return cast<GEPOperator>(this)->isInBounds();
1498}
1499
Chandler Carruth1e140532012-12-11 10:29:10 +00001500bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1501 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001502 // Delegate to the generic GEPOperator implementation.
1503 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001504}
1505
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001506//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001507// ExtractElementInst Implementation
1508//===----------------------------------------------------------------------===//
1509
1510ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001511 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001512 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001513 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001514 ExtractElement,
1515 OperandTraits<ExtractElementInst>::op_begin(this),
1516 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001517 assert(isValidOperands(Val, Index) &&
1518 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001519 Op<0>() = Val;
1520 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001521 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001522}
1523
1524ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001525 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001526 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001527 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001528 ExtractElement,
1529 OperandTraits<ExtractElementInst>::op_begin(this),
1530 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001531 assert(isValidOperands(Val, Index) &&
1532 "Invalid extractelement instruction operands!");
1533
Gabor Greif2d3024d2008-05-26 21:33:52 +00001534 Op<0>() = Val;
1535 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001536 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001537}
1538
Chris Lattner54865b32006-04-08 04:05:48 +00001539bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001540 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001541 return false;
1542 return true;
1543}
1544
Robert Bocchino23004482006-01-10 19:05:34 +00001545//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001546// InsertElementInst Implementation
1547//===----------------------------------------------------------------------===//
1548
Chris Lattner54865b32006-04-08 04:05:48 +00001549InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001550 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001552 : Instruction(Vec->getType(), InsertElement,
1553 OperandTraits<InsertElementInst>::op_begin(this),
1554 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001555 assert(isValidOperands(Vec, Elt, Index) &&
1556 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001557 Op<0>() = Vec;
1558 Op<1>() = Elt;
1559 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001560 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001561}
1562
Chris Lattner54865b32006-04-08 04:05:48 +00001563InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001564 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001565 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001566 : Instruction(Vec->getType(), InsertElement,
1567 OperandTraits<InsertElementInst>::op_begin(this),
1568 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001569 assert(isValidOperands(Vec, Elt, Index) &&
1570 "Invalid insertelement instruction operands!");
1571
Gabor Greif2d3024d2008-05-26 21:33:52 +00001572 Op<0>() = Vec;
1573 Op<1>() = Elt;
1574 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001575 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001576}
1577
Fangrui Songf78650a2018-07-30 19:41:25 +00001578bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001579 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001580 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001581 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001582
Reid Spencerd84d35b2007-02-15 02:26:10 +00001583 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001584 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001585
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001586 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001587 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001588 return true;
1589}
1590
Robert Bocchinoca27f032006-01-17 20:07:22 +00001591//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001592// ShuffleVectorInst Implementation
1593//===----------------------------------------------------------------------===//
1594
1595ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001596 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001597 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001598: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001599 cast<VectorType>(Mask->getType())->getNumElements()),
1600 ShuffleVector,
1601 OperandTraits<ShuffleVectorInst>::op_begin(this),
1602 OperandTraits<ShuffleVectorInst>::operands(this),
1603 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001604 assert(isValidOperands(V1, V2, Mask) &&
1605 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001606 Op<0>() = V1;
1607 Op<1>() = V2;
1608 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001609 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001610}
1611
1612ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001613 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001614 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001615: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1616 cast<VectorType>(Mask->getType())->getNumElements()),
1617 ShuffleVector,
1618 OperandTraits<ShuffleVectorInst>::op_begin(this),
1619 OperandTraits<ShuffleVectorInst>::operands(this),
1620 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001621 assert(isValidOperands(V1, V2, Mask) &&
1622 "Invalid shuffle vector instruction operands!");
1623
Gabor Greif2d3024d2008-05-26 21:33:52 +00001624 Op<0>() = V1;
1625 Op<1>() = V2;
1626 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001627 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001628}
1629
Mon P Wang25f01062008-11-10 04:46:22 +00001630bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001631 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001632 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001633 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001634 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001635
Chris Lattner1dcb6542012-01-25 23:49:49 +00001636 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001637 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001638 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001639 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001640
1641 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001642 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1643 return true;
1644
Sanjay Patel8bd52282017-04-19 16:22:19 +00001645 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001646 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001647 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001648 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001649 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001650 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001651 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001652 return false;
1653 }
1654 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001655 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001656 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001657
Sanjay Patel8bd52282017-04-19 16:22:19 +00001658 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001659 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1660 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1661 if (CDS->getElementAsInteger(i) >= V1Size*2)
1662 return false;
1663 return true;
1664 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001665
Chris Lattner1dcb6542012-01-25 23:49:49 +00001666 // The bitcode reader can create a place holder for a forward reference
1667 // used as the shuffle mask. When this occurs, the shuffle mask will
1668 // fall into this case and fail. To avoid this error, do this bit of
1669 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001670 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001671 if (CE->getOpcode() == Instruction::UserOp1)
1672 return true;
1673
1674 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001675}
1676
Sanjay Patel2ca33602018-06-19 18:44:00 +00001677int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001678 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001679 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001680 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001681 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001682 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001683 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001684 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001685}
1686
Sanjay Patel2ca33602018-06-19 18:44:00 +00001687void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001688 SmallVectorImpl<int> &Result) {
1689 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001690
Sanjay Patel8bd52282017-04-19 16:22:19 +00001691 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001692 for (unsigned i = 0; i != NumElts; ++i)
1693 Result.push_back(CDS->getElementAsInteger(i));
1694 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001695 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001696 for (unsigned i = 0; i != NumElts; ++i) {
1697 Constant *C = Mask->getAggregateElement(i);
1698 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001699 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001700 }
1701}
1702
Sanjay Patel2ca33602018-06-19 18:44:00 +00001703bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1704 assert(!Mask.empty() && "Shuffle mask must contain elements");
1705 bool UsesLHS = false;
1706 bool UsesRHS = false;
1707 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1708 if (Mask[i] == -1)
1709 continue;
1710 assert(Mask[i] >= 0 && Mask[i] < (NumElts * 2) &&
1711 "Out-of-bounds shuffle mask element");
1712 UsesLHS |= (Mask[i] < NumElts);
1713 UsesRHS |= (Mask[i] >= NumElts);
1714 if (UsesLHS && UsesRHS)
1715 return false;
1716 }
1717 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1718 return true;
1719}
1720
1721bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1722 if (!isSingleSourceMask(Mask))
1723 return false;
1724 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1725 if (Mask[i] == -1)
1726 continue;
1727 if (Mask[i] != i && Mask[i] != (NumElts + i))
1728 return false;
1729 }
1730 return true;
1731}
1732
1733bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1734 if (!isSingleSourceMask(Mask))
1735 return false;
1736 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1737 if (Mask[i] == -1)
1738 continue;
1739 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1740 return false;
1741 }
1742 return true;
1743}
1744
1745bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1746 if (!isSingleSourceMask(Mask))
1747 return false;
1748 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1749 if (Mask[i] == -1)
1750 continue;
1751 if (Mask[i] != 0 && Mask[i] != NumElts)
1752 return false;
1753 }
1754 return true;
1755}
1756
1757bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1758 // Select is differentiated from identity. It requires using both sources.
1759 if (isSingleSourceMask(Mask))
1760 return false;
1761 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1762 if (Mask[i] == -1)
1763 continue;
1764 if (Mask[i] != i && Mask[i] != (NumElts + i))
1765 return false;
1766 }
1767 return true;
1768}
1769
1770bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1771 // Example masks that will return true:
1772 // v1 = <a, b, c, d>
1773 // v2 = <e, f, g, h>
1774 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1775 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1776
1777 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1778 int NumElts = Mask.size();
1779 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1780 return false;
1781
1782 // 2. The first element of the mask must be either a 0 or a 1.
1783 if (Mask[0] != 0 && Mask[0] != 1)
1784 return false;
1785
1786 // 3. The difference between the first 2 elements must be equal to the
1787 // number of elements in the mask.
1788 if ((Mask[1] - Mask[0]) != NumElts)
1789 return false;
1790
1791 // 4. The difference between consecutive even-numbered and odd-numbered
1792 // elements must be equal to 2.
1793 for (int i = 2; i < NumElts; ++i) {
1794 int MaskEltVal = Mask[i];
1795 if (MaskEltVal == -1)
1796 return false;
1797 int MaskEltPrevVal = Mask[i - 2];
1798 if (MaskEltVal - MaskEltPrevVal != 2)
1799 return false;
1800 }
1801 return true;
1802}
1803
1804
Dan Gohman12fce772008-05-15 19:50:34 +00001805//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001806// InsertValueInst Class
1807//===----------------------------------------------------------------------===//
1808
Fangrui Songf78650a2018-07-30 19:41:25 +00001809void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00001810 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001811 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001812
1813 // There's no fundamental reason why we require at least one index
1814 // (other than weirdness with &*IdxBegin being invalid; see
1815 // getelementptr's init routine for example). But there's no
1816 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001817 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001818
1819 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001820 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001821 Op<0>() = Agg;
1822 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001823
Jay Foad57aa6362011-07-13 10:26:04 +00001824 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001825 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001826}
1827
1828InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001829 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001830 OperandTraits<InsertValueInst>::op_begin(this), 2),
1831 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001832 Op<0>() = IVI.getOperand(0);
1833 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001834 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001835}
1836
1837//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001838// ExtractValueInst Class
1839//===----------------------------------------------------------------------===//
1840
Jay Foad57aa6362011-07-13 10:26:04 +00001841void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001842 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001843
Jay Foad57aa6362011-07-13 10:26:04 +00001844 // There's no fundamental reason why we require at least one index.
1845 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001846 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001847
Jay Foad57aa6362011-07-13 10:26:04 +00001848 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001849 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001850}
1851
1852ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001853 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001854 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001855 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001856}
1857
Dan Gohman12fce772008-05-15 19:50:34 +00001858// getIndexedType - Returns the type of the element that would be extracted
1859// with an extractvalue instruction with the specified parameters.
1860//
1861// A null type is returned if the indices are invalid for the specified
1862// pointer type.
1863//
Chris Lattner229907c2011-07-18 04:54:35 +00001864Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001865 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001866 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001867 // We can't use CompositeType::indexValid(Index) here.
1868 // indexValid() always returns true for arrays because getelementptr allows
1869 // out-of-bounds indices. Since we don't allow those for extractvalue and
1870 // insertvalue we need to check array indexing manually.
1871 // Since the only other types we can index into are struct types it's just
1872 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001873 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001874 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001875 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001876 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001877 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001878 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001879 } else {
1880 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001881 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001882 }
1883
1884 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001885 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001886 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001887}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001888
1889//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001890// BinaryOperator Class
1891//===----------------------------------------------------------------------===//
1892
Chris Lattner2195fc42007-02-24 00:55:48 +00001893BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001894 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001895 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001896 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001897 OperandTraits<BinaryOperator>::op_begin(this),
1898 OperandTraits<BinaryOperator>::operands(this),
1899 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001900 Op<0>() = S1;
1901 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001902 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001903 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001904}
1905
Fangrui Songf78650a2018-07-30 19:41:25 +00001906BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001907 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001908 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001909 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001910 OperandTraits<BinaryOperator>::op_begin(this),
1911 OperandTraits<BinaryOperator>::operands(this),
1912 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001913 Op<0>() = S1;
1914 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001915 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001916 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001917}
1918
Craig Topper700892f2017-06-26 07:15:59 +00001919void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001920 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001921 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001922 assert(LHS->getType() == RHS->getType() &&
1923 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00001925 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001926 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001927 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001928 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001929 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001930 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001931 "Tried to create an integer operation on a non-integer type!");
1932 break;
1933 case FAdd: case FSub:
1934 case FMul:
1935 assert(getType() == LHS->getType() &&
1936 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001937 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001938 "Tried to create a floating-point operation on a "
1939 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00001941 case UDiv:
1942 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001943 assert(getType() == LHS->getType() &&
1944 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001945 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001946 "Incorrect operand type (not integer) for S/UDIV");
1947 break;
1948 case FDiv:
1949 assert(getType() == LHS->getType() &&
1950 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001951 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001952 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001953 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00001954 case URem:
1955 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001956 assert(getType() == LHS->getType() &&
1957 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001958 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001959 "Incorrect operand type (not integer) for S/UREM");
1960 break;
1961 case FRem:
1962 assert(getType() == LHS->getType() &&
1963 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001964 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001965 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001966 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001967 case Shl:
1968 case LShr:
1969 case AShr:
1970 assert(getType() == LHS->getType() &&
1971 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001972 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001973 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001974 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001975 case And: case Or:
1976 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001977 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001978 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001979 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001980 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001981 break;
Craig Topper700892f2017-06-26 07:15:59 +00001982 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001983 }
1984#endif
1985}
1986
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001987BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001988 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001989 Instruction *InsertBefore) {
1990 assert(S1->getType() == S2->getType() &&
1991 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001992 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001993}
1994
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001995BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001996 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001997 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001999 InsertAtEnd->getInstList().push_back(Res);
2000 return Res;
2001}
2002
Dan Gohman5476cfd2009-08-12 16:23:25 +00002003BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002004 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002005 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002006 return new BinaryOperator(Instruction::Sub,
2007 zero, Op,
2008 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002009}
2010
Dan Gohman5476cfd2009-08-12 16:23:25 +00002011BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002012 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002013 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002014 return new BinaryOperator(Instruction::Sub,
2015 zero, Op,
2016 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002017}
2018
Dan Gohman4ab44202009-12-18 02:58:50 +00002019BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2020 Instruction *InsertBefore) {
2021 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2022 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2023}
2024
2025BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2026 BasicBlock *InsertAtEnd) {
2027 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2028 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2029}
2030
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002031BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2032 Instruction *InsertBefore) {
2033 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2034 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2035}
2036
2037BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2038 BasicBlock *InsertAtEnd) {
2039 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2040 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2041}
2042
Dan Gohman5476cfd2009-08-12 16:23:25 +00002043BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002044 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002045 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002046 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002047 Op->getType(), Name, InsertBefore);
2048}
2049
Dan Gohman5476cfd2009-08-12 16:23:25 +00002050BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002051 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002052 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002053 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002054 Op->getType(), Name, InsertAtEnd);
2055}
2056
Dan Gohman5476cfd2009-08-12 16:23:25 +00002057BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002058 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002059 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002060 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002061 Op->getType(), Name, InsertBefore);
2062}
2063
Dan Gohman5476cfd2009-08-12 16:23:25 +00002064BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002065 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002066 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002067 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002068 Op->getType(), Name, InsertAtEnd);
2069}
2070
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002071// isConstantAllOnes - Helper function for several functions below
2072static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002073 if (const Constant *C = dyn_cast<Constant>(V))
2074 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002075 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076}
2077
Owen Andersonbb2501b2009-07-13 22:18:28 +00002078bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002079 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2080 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002081 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002082 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083 return false;
2084}
2085
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002086bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002087 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2088 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002089 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002090 if (!IgnoreZeroSign)
2091 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2092 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2093 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002094 return false;
2095}
2096
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002097bool BinaryOperator::isNot(const Value *V) {
2098 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2099 return (Bop->getOpcode() == Instruction::Xor &&
2100 (isConstantAllOnes(Bop->getOperand(1)) ||
2101 isConstantAllOnes(Bop->getOperand(0))));
2102 return false;
2103}
2104
Chris Lattner2c7d1772005-04-24 07:28:37 +00002105Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002106 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002107}
2108
Chris Lattner2c7d1772005-04-24 07:28:37 +00002109const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2110 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002111}
2112
Dan Gohmana5b96452009-06-04 22:49:04 +00002113Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002114 return cast<BinaryOperator>(BinOp)->getOperand(1);
2115}
2116
2117const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2118 return getFNegArgument(const_cast<Value*>(BinOp));
2119}
2120
Chris Lattner2c7d1772005-04-24 07:28:37 +00002121Value *BinaryOperator::getNotArgument(Value *BinOp) {
2122 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2123 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2124 Value *Op0 = BO->getOperand(0);
2125 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 if (isConstantAllOnes(Op0)) return Op1;
2127
2128 assert(isConstantAllOnes(Op1));
2129 return Op0;
2130}
2131
Chris Lattner2c7d1772005-04-24 07:28:37 +00002132const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2133 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134}
2135
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002136// Exchange the two operands to this instruction. This instruction is safe to
2137// use on any binary instruction and does not modify the semantics of the
2138// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2139// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002140bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002141 if (!isCommutative())
2142 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002143 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002144 return false;
2145}
2146
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002147//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002148// FPMathOperator Class
2149//===----------------------------------------------------------------------===//
2150
Duncan Sands05f4df82012-04-16 16:28:59 +00002151float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002152 const MDNode *MD =
2153 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002154 if (!MD)
2155 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002156 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002157 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002158}
2159
Duncan Sands05f4df82012-04-16 16:28:59 +00002160//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002161// CastInst Class
2162//===----------------------------------------------------------------------===//
2163
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164// Just determine if this cast only deals with integral->integral conversion.
2165bool CastInst::isIntegerCast() const {
2166 switch (getOpcode()) {
2167 default: return false;
2168 case Instruction::ZExt:
2169 case Instruction::SExt:
2170 case Instruction::Trunc:
2171 return true;
2172 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002173 return getOperand(0)->getType()->isIntegerTy() &&
2174 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002176}
2177
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178bool CastInst::isLosslessCast() const {
2179 // Only BitCast can be lossless, exit fast if we're not BitCast
2180 if (getOpcode() != Instruction::BitCast)
2181 return false;
2182
2183 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002184 Type *SrcTy = getOperand(0)->getType();
2185 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186 if (SrcTy == DstTy)
2187 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002188
Reid Spencer8d9336d2006-12-31 05:26:44 +00002189 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002190 if (SrcTy->isPointerTy())
2191 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 return false; // Other types have no identity values
2193}
2194
2195/// This function determines if the CastInst does not require any bits to be
2196/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002197/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002199/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002200/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002201/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002202/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002203bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002204 Type *SrcTy,
2205 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002206 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002207 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002208 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 case Instruction::Trunc:
2210 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002211 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 case Instruction::FPTrunc:
2213 case Instruction::FPExt:
2214 case Instruction::UIToFP:
2215 case Instruction::SIToFP:
2216 case Instruction::FPToUI:
2217 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002218 case Instruction::AddrSpaceCast:
2219 // TODO: Target informations may give a more accurate answer here.
2220 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221 case Instruction::BitCast:
2222 return true; // BitCast never modifies bits.
2223 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002224 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002225 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002227 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002228 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229 }
2230}
2231
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002232bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002233 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002234}
2235
2236/// This function determines if a pair of casts can be eliminated and what
2237/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238/// instructions like this:
2239/// * %F = firstOpcode SrcTy %x to MidTy
2240/// * %S = secondOpcode MidTy %F to DstTy
2241/// The function returns a resultOpcode so these two casts can be replaced with:
2242/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002243/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244unsigned CastInst::isEliminableCastPair(
2245 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002246 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2247 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 // Define the 144 possibilities for these two cast instructions. The values
2249 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002250 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002251 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 // the following cast properties:
2253 //
2254 // Size Compare Source Destination
2255 // Operator Src ? Size Type Sign Type Sign
2256 // -------- ------------ ------------------- ---------------------
2257 // TRUNC > Integer Any Integral Any
2258 // ZEXT < Integral Unsigned Integer Any
2259 // SEXT < Integral Signed Integer Any
2260 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002261 // FPTOSI n/a FloatPt n/a Integral Signed
2262 // UITOFP n/a Integral Unsigned FloatPt n/a
2263 // SITOFP n/a Integral Signed FloatPt n/a
2264 // FPTRUNC > FloatPt n/a FloatPt n/a
2265 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266 // PTRTOINT n/a Pointer n/a Integral Unsigned
2267 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002268 // BITCAST = FirstClass n/a FirstClass n/a
2269 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002270 //
2271 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002272 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2273 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002274 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002275 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002276 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002277 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002278 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2280 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002281 // T F F U S F F P I B A -+
2282 // R Z S P P I I T P 2 N T S |
2283 // U E E 2 2 2 2 R E I T C C +- secondOp
2284 // N X X U S F F N X N 2 V V |
2285 // C T T I I P P C T T P T T -+
2286 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002287 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002288 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2289 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2290 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2291 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2292 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002293 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002294 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002295 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2296 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2297 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2298 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002300
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002301 // TODO: This logic could be encoded into the table above and handled in the
2302 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002303 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002304 // merging. However, any pair of bitcasts are allowed.
2305 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2306 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2307 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002308
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002309 // Check if any of the casts convert scalars <-> vectors.
2310 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2311 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2312 if (!AreBothBitcasts)
2313 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314
2315 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2316 [secondOp-Instruction::CastOpsBegin];
2317 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002318 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002319 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002321 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002322 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002324 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002325 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002327 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002328 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002329 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002330 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002331 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 return firstOp;
2333 return 0;
2334 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002335 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002336 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002337 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338 return firstOp;
2339 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002340 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002341 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002342 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002343 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 return secondOp;
2345 return 0;
2346 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002347 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002348 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002349 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350 return secondOp;
2351 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002352 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002353 // Cannot simplify if address spaces are different!
2354 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2355 return 0;
2356
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002357 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002358 // We can still fold this without knowing the actual sizes as long we
2359 // know that the intermediate pointer is the largest possible
2360 // pointer size.
2361 // FIXME: Is this always true?
2362 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002363 return Instruction::BitCast;
2364
2365 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002366 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002367 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002368 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 if (MidSize >= PtrSize)
2370 return Instruction::BitCast;
2371 return 0;
2372 }
2373 case 8: {
2374 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2375 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2376 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002377 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2378 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 if (SrcSize == DstSize)
2380 return Instruction::BitCast;
2381 else if (SrcSize < DstSize)
2382 return firstOp;
2383 return secondOp;
2384 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002385 case 9:
2386 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002388 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002390 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002391 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002392 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002393 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2394 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395 if (SrcSize <= PtrSize && SrcSize == DstSize)
2396 return Instruction::BitCast;
2397 return 0;
2398 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002399 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002400 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2401 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2402 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2403 return Instruction::AddrSpaceCast;
2404 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 case 13:
2406 // FIXME: this state can be merged with (1), but the following assert
2407 // is useful to check the correcteness of the sequence due to semantic
2408 // change of bitcast.
2409 assert(
2410 SrcTy->isPtrOrPtrVectorTy() &&
2411 MidTy->isPtrOrPtrVectorTy() &&
2412 DstTy->isPtrOrPtrVectorTy() &&
2413 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2414 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2415 "Illegal addrspacecast, bitcast sequence!");
2416 // Allowed, use first cast's opcode
2417 return firstOp;
2418 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002419 // bitcast, addrspacecast -> addrspacecast if the element type of
2420 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002421 if (SrcTy->getScalarType()->getPointerElementType() ==
2422 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002423 return Instruction::AddrSpaceCast;
2424 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 case 15:
2426 // FIXME: this state can be merged with (1), but the following assert
2427 // is useful to check the correcteness of the sequence due to semantic
2428 // change of bitcast.
2429 assert(
2430 SrcTy->isIntOrIntVectorTy() &&
2431 MidTy->isPtrOrPtrVectorTy() &&
2432 DstTy->isPtrOrPtrVectorTy() &&
2433 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2434 "Illegal inttoptr, bitcast sequence!");
2435 // Allowed, use first cast's opcode
2436 return firstOp;
2437 case 16:
2438 // FIXME: this state can be merged with (2), but the following assert
2439 // is useful to check the correcteness of the sequence due to semantic
2440 // change of bitcast.
2441 assert(
2442 SrcTy->isPtrOrPtrVectorTy() &&
2443 MidTy->isPtrOrPtrVectorTy() &&
2444 DstTy->isIntOrIntVectorTy() &&
2445 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2446 "Illegal bitcast, ptrtoint sequence!");
2447 // Allowed, use second cast's opcode
2448 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002449 case 17:
2450 // (sitofp (zext x)) -> (uitofp x)
2451 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002452 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002453 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002455 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 default:
Craig Topperc514b542012-02-05 22:14:15 +00002457 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459}
2460
Fangrui Songf78650a2018-07-30 19:41:25 +00002461CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002462 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002463 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 // Construct and return the appropriate CastInst subclass
2465 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002466 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2467 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2468 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2469 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2470 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2471 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2472 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2473 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2474 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2475 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2476 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2477 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2478 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2479 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481}
2482
Chris Lattner229907c2011-07-18 04:54:35 +00002483CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002484 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002485 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 // Construct and return the appropriate CastInst subclass
2487 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2489 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2490 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2491 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2492 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2493 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2494 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2495 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2496 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2497 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2498 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2499 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2500 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2501 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
Fangrui Songf78650a2018-07-30 19:41:25 +00002505CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002507 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002508 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002509 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2510 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002511}
2512
Fangrui Songf78650a2018-07-30 19:41:25 +00002513CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002514 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002515 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002516 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002517 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2518 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002519}
2520
Fangrui Songf78650a2018-07-30 19:41:25 +00002521CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002522 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002523 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002524 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002525 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2526 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002527}
2528
Fangrui Songf78650a2018-07-30 19:41:25 +00002529CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002530 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002531 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002532 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002533 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2534 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002535}
2536
Chris Lattner229907c2011-07-18 04:54:35 +00002537CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002538 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002539 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002540 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002541 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2542 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002543}
2544
Chris Lattner229907c2011-07-18 04:54:35 +00002545CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002546 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002547 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002548 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002549 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2550 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002551}
2552
Chris Lattner229907c2011-07-18 04:54:35 +00002553CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002554 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002555 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002556 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2557 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2558 "Invalid cast");
2559 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002560 assert((!Ty->isVectorTy() ||
2561 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002562 "Invalid cast");
2563
Matt Arsenault065ced92013-07-31 00:17:33 +00002564 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002565 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002566
Matt Arsenault740980e2014-07-14 17:24:35 +00002567 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002568}
2569
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002570/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002571CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2572 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002573 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002574 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2575 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002576 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002577 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002578 assert((!Ty->isVectorTy() ||
2579 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002580 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002581
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002582 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002583 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002584
Matt Arsenault740980e2014-07-14 17:24:35 +00002585 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2586}
2587
2588CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2589 Value *S, Type *Ty,
2590 const Twine &Name,
2591 BasicBlock *InsertAtEnd) {
2592 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2593 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2594
2595 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2596 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2597
2598 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2599}
2600
2601CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2602 Value *S, Type *Ty,
2603 const Twine &Name,
2604 Instruction *InsertBefore) {
2605 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2606 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2607
2608 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002609 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2610
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002611 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002612}
2613
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002614CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2615 const Twine &Name,
2616 Instruction *InsertBefore) {
2617 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2618 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2619 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2620 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2621
2622 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2623}
2624
Matt Arsenault740980e2014-07-14 17:24:35 +00002625CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002626 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002627 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002628 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002629 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002630 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2631 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002632 Instruction::CastOps opcode =
2633 (SrcBits == DstBits ? Instruction::BitCast :
2634 (SrcBits > DstBits ? Instruction::Trunc :
2635 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002636 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002637}
2638
Fangrui Songf78650a2018-07-30 19:41:25 +00002639CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002641 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002642 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002643 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002644 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2645 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002646 Instruction::CastOps opcode =
2647 (SrcBits == DstBits ? Instruction::BitCast :
2648 (SrcBits > DstBits ? Instruction::Trunc :
2649 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002650 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002651}
2652
Fangrui Songf78650a2018-07-30 19:41:25 +00002653CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2654 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002655 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002656 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002657 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002658 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2659 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002660 Instruction::CastOps opcode =
2661 (SrcBits == DstBits ? Instruction::BitCast :
2662 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002663 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002664}
2665
Fangrui Songf78650a2018-07-30 19:41:25 +00002666CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2667 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002668 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002669 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002670 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002671 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2672 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002673 Instruction::CastOps opcode =
2674 (SrcBits == DstBits ? Instruction::BitCast :
2675 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002676 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002677}
2678
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002679// Check whether it is valid to call getCastOpcode for these types.
2680// This routine must be kept in sync with getCastOpcode.
2681bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2682 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2683 return false;
2684
2685 if (SrcTy == DestTy)
2686 return true;
2687
2688 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2689 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2690 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2691 // An element by element cast. Valid if casting the elements is valid.
2692 SrcTy = SrcVecTy->getElementType();
2693 DestTy = DestVecTy->getElementType();
2694 }
2695
2696 // Get the bit sizes, we'll need these
2697 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2698 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2699
2700 // Run through the possibilities ...
2701 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002702 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002703 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002704 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002705 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002706 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002707 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002708 // Casting from something else
2709 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002710 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002711 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2712 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002713 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002714 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002715 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002716 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002717 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002718 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002719 return false;
2720 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002721 if (DestTy->isVectorTy()) // Casting to vector
2722 return DestBits == SrcBits;
2723 if (DestTy->isPointerTy()) { // Casting to pointer
2724 if (SrcTy->isPointerTy()) // Casting from pointer
2725 return true;
2726 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002727 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002728 if (DestTy->isX86_MMXTy()) {
2729 if (SrcTy->isVectorTy())
2730 return DestBits == SrcBits; // 64-bit vector to MMX
2731 return false;
2732 } // Casting to something else
2733 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002734}
2735
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002736bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2737 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2738 return false;
2739
2740 if (SrcTy == DestTy)
2741 return true;
2742
2743 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2744 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2745 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2746 // An element by element cast. Valid if casting the elements is valid.
2747 SrcTy = SrcVecTy->getElementType();
2748 DestTy = DestVecTy->getElementType();
2749 }
2750 }
2751 }
2752
2753 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2754 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2755 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2756 }
2757 }
2758
2759 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2760 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2761
2762 // Could still have vectors of pointers if the number of elements doesn't
2763 // match
2764 if (SrcBits == 0 || DestBits == 0)
2765 return false;
2766
2767 if (SrcBits != DestBits)
2768 return false;
2769
2770 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2771 return false;
2772
2773 return true;
2774}
2775
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002776bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002777 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002778 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002779 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2780 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002781 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2782 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002783 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2784 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002785 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2786 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002787
2788 return isBitCastable(SrcTy, DestTy);
2789}
2790
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002791// Provide a way to get a "cast" where the cast opcode is inferred from the
2792// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002793// logic in the castIsValid function below. This axiom should hold:
2794// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2795// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002797// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002798Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002799CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002800 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2801 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002802
Duncan Sands55e50902008-01-06 10:12:28 +00002803 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2804 "Only first class types are castable!");
2805
Duncan Sandsa8514532011-05-18 07:13:41 +00002806 if (SrcTy == DestTy)
2807 return BitCast;
2808
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002809 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002810 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2811 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002812 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2813 // An element by element cast. Find the appropriate opcode based on the
2814 // element types.
2815 SrcTy = SrcVecTy->getElementType();
2816 DestTy = DestVecTy->getElementType();
2817 }
2818
2819 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002820 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2821 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002822
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002824 if (DestTy->isIntegerTy()) { // Casting to integral
2825 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826 if (DestBits < SrcBits)
2827 return Trunc; // int -> smaller int
2828 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002829 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830 return SExt; // signed -> SEXT
2831 else
2832 return ZExt; // unsigned -> ZEXT
2833 } else {
2834 return BitCast; // Same size, No-op cast
2835 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002836 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00002837 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002838 return FPToSI; // FP -> sint
2839 else
Fangrui Songf78650a2018-07-30 19:41:25 +00002840 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002841 } else if (SrcTy->isVectorTy()) {
2842 assert(DestBits == SrcBits &&
2843 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002844 return BitCast; // Same size, no-op cast
2845 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002846 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847 "Casting from a value that is not first-class type");
2848 return PtrToInt; // ptr -> int
2849 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002850 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2851 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002852 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853 return SIToFP; // sint -> FP
2854 else
2855 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002856 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002857 if (DestBits < SrcBits) {
2858 return FPTrunc; // FP -> smaller FP
2859 } else if (DestBits > SrcBits) {
2860 return FPExt; // FP -> larger FP
2861 } else {
2862 return BitCast; // same size, no-op cast
2863 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002864 } else if (SrcTy->isVectorTy()) {
2865 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002866 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002867 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002868 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002869 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002870 } else if (DestTy->isVectorTy()) {
2871 assert(DestBits == SrcBits &&
2872 "Illegal cast to vector (wrong type or size)");
2873 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002874 } else if (DestTy->isPointerTy()) {
2875 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002876 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2877 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002878 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002879 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002880 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002881 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002882 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002883 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002884 if (SrcTy->isVectorTy()) {
2885 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002886 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002887 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002888 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002890 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891}
2892
2893//===----------------------------------------------------------------------===//
2894// CastInst SubClass Constructors
2895//===----------------------------------------------------------------------===//
2896
2897/// Check that the construction parameters for a CastInst are correct. This
2898/// could be broken out into the separate constructors but it is useful to have
2899/// it in one place and to eliminate the redundant code for getting the sizes
2900/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00002901bool
Chris Lattner229907c2011-07-18 04:54:35 +00002902CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002904 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002905
Chris Lattner37bc78a2010-01-26 21:51:43 +00002906 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2907 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002908 return false;
2909
2910 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002911 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2912 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913
Duncan Sands7f646562011-05-18 09:21:57 +00002914 // If these are vector types, get the lengths of the vectors (using zero for
2915 // scalar types means that checking that vector lengths match also checks that
2916 // scalars are not being converted to vectors or vectors to scalars).
2917 unsigned SrcLength = SrcTy->isVectorTy() ?
2918 cast<VectorType>(SrcTy)->getNumElements() : 0;
2919 unsigned DstLength = DstTy->isVectorTy() ?
2920 cast<VectorType>(DstTy)->getNumElements() : 0;
2921
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922 // Switch on the opcode provided
2923 switch (op) {
2924 default: return false; // This is an input error
2925 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002926 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2927 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002929 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2930 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00002931 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002932 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2933 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002934 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002935 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2936 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002938 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2939 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002940 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002942 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2943 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002944 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002946 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2947 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002948 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002949 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002950 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002951 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2952 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2953 return false;
Craig Topper95d23472017-07-09 07:04:00 +00002954 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002956 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002957 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002958 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2959 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2960 return false;
Craig Topper95d23472017-07-09 07:04:00 +00002961 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002962 case Instruction::BitCast: {
2963 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2964 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2965
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002966 // BitCast implies a no-op cast of type only. No bits change.
2967 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002968 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969 return false;
2970
Alp Tokerf907b892013-12-05 05:44:44 +00002971 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002972 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002973 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002974 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2975
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002976 // If both are pointers then the address spaces must match.
2977 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2978 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002979
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002980 // A vector of pointers must have the same number of elements.
2981 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2982 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2983 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2984
2985 return false;
2986 }
2987
2988 return true;
2989 }
2990 case Instruction::AddrSpaceCast: {
2991 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2992 if (!SrcPtrTy)
2993 return false;
2994
2995 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2996 if (!DstPtrTy)
2997 return false;
2998
2999 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3000 return false;
3001
3002 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3003 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3004 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3005
3006 return false;
3007 }
3008
3009 return true;
3010 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011 }
3012}
3013
3014TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003015 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003017 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003018}
3019
3020TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003021 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003022) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003023 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024}
3025
3026ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003027 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003028) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003029 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030}
3031
3032ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003033 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003034) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003035 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036}
3037SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003038 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003039) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003040 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041}
3042
Jeff Cohencc08c832006-12-02 02:22:01 +00003043SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003044 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003045) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003046 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047}
3048
3049FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003050 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003051) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003052 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053}
3054
3055FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003056 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003057) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003058 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059}
3060
3061FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003062 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003063) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003064 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003065}
3066
3067FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003068 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003069) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003070 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003071}
3072
3073UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003074 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003075) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003076 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077}
3078
3079UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003080 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003081) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003082 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083}
3084
3085SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003086 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003087) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003088 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089}
3090
3091SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003092 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003093) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003094 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095}
3096
3097FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003098 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003099) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003100 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101}
3102
3103FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003104 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003105) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003106 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003107}
3108
3109FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003110 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003111) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003112 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113}
3114
3115FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003116 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003117) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003118 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119}
3120
3121PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003122 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003123) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003124 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125}
3126
3127PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003128 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003129) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003130 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131}
3132
3133IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003134 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003135) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003136 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137}
3138
3139IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003140 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003141) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143}
3144
3145BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003146 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003147) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003148 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149}
3150
3151BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003152 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003153) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003154 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003156
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003157AddrSpaceCastInst::AddrSpaceCastInst(
3158 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3159) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3160 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3161}
3162
3163AddrSpaceCastInst::AddrSpaceCastInst(
3164 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3165) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3166 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3167}
3168
Chris Lattnerf16dc002006-09-17 19:29:56 +00003169//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003170// CmpInst Classes
3171//===----------------------------------------------------------------------===//
3172
Craig Topper1c3f2832015-12-15 06:11:33 +00003173CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3174 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003175 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003176 OperandTraits<CmpInst>::op_begin(this),
3177 OperandTraits<CmpInst>::operands(this),
3178 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003179 Op<0>() = LHS;
3180 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003181 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003182 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003183}
Gabor Greiff6caff662008-05-10 08:32:32 +00003184
Craig Topper1c3f2832015-12-15 06:11:33 +00003185CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3186 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003187 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003188 OperandTraits<CmpInst>::op_begin(this),
3189 OperandTraits<CmpInst>::operands(this),
3190 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003191 Op<0>() = LHS;
3192 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003193 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003194 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003195}
3196
3197CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003198CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003199 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003200 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003201 if (InsertBefore)
3202 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3203 S1, S2, Name);
3204 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003205 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003206 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003207 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003208
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003209 if (InsertBefore)
3210 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3211 S1, S2, Name);
3212 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003213 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003214 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003215}
3216
3217CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003218CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003219 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003220 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3222 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003223 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3225 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003226}
3227
3228void CmpInst::swapOperands() {
3229 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3230 IC->swapOperands();
3231 else
3232 cast<FCmpInst>(this)->swapOperands();
3233}
3234
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003235bool CmpInst::isCommutative() const {
3236 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003237 return IC->isCommutative();
3238 return cast<FCmpInst>(this)->isCommutative();
3239}
3240
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003241bool CmpInst::isEquality() const {
3242 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003243 return IC->isEquality();
3244 return cast<FCmpInst>(this)->isEquality();
3245}
3246
Dan Gohman4e724382008-05-31 02:47:54 +00003247CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003248 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003249 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003250 case ICMP_EQ: return ICMP_NE;
3251 case ICMP_NE: return ICMP_EQ;
3252 case ICMP_UGT: return ICMP_ULE;
3253 case ICMP_ULT: return ICMP_UGE;
3254 case ICMP_UGE: return ICMP_ULT;
3255 case ICMP_ULE: return ICMP_UGT;
3256 case ICMP_SGT: return ICMP_SLE;
3257 case ICMP_SLT: return ICMP_SGE;
3258 case ICMP_SGE: return ICMP_SLT;
3259 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003260
Dan Gohman4e724382008-05-31 02:47:54 +00003261 case FCMP_OEQ: return FCMP_UNE;
3262 case FCMP_ONE: return FCMP_UEQ;
3263 case FCMP_OGT: return FCMP_ULE;
3264 case FCMP_OLT: return FCMP_UGE;
3265 case FCMP_OGE: return FCMP_ULT;
3266 case FCMP_OLE: return FCMP_UGT;
3267 case FCMP_UEQ: return FCMP_ONE;
3268 case FCMP_UNE: return FCMP_OEQ;
3269 case FCMP_UGT: return FCMP_OLE;
3270 case FCMP_ULT: return FCMP_OGE;
3271 case FCMP_UGE: return FCMP_OLT;
3272 case FCMP_ULE: return FCMP_OGT;
3273 case FCMP_ORD: return FCMP_UNO;
3274 case FCMP_UNO: return FCMP_ORD;
3275 case FCMP_TRUE: return FCMP_FALSE;
3276 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003277 }
3278}
3279
Tim Northoverde3aea0412016-08-17 20:25:25 +00003280StringRef CmpInst::getPredicateName(Predicate Pred) {
3281 switch (Pred) {
3282 default: return "unknown";
3283 case FCmpInst::FCMP_FALSE: return "false";
3284 case FCmpInst::FCMP_OEQ: return "oeq";
3285 case FCmpInst::FCMP_OGT: return "ogt";
3286 case FCmpInst::FCMP_OGE: return "oge";
3287 case FCmpInst::FCMP_OLT: return "olt";
3288 case FCmpInst::FCMP_OLE: return "ole";
3289 case FCmpInst::FCMP_ONE: return "one";
3290 case FCmpInst::FCMP_ORD: return "ord";
3291 case FCmpInst::FCMP_UNO: return "uno";
3292 case FCmpInst::FCMP_UEQ: return "ueq";
3293 case FCmpInst::FCMP_UGT: return "ugt";
3294 case FCmpInst::FCMP_UGE: return "uge";
3295 case FCmpInst::FCMP_ULT: return "ult";
3296 case FCmpInst::FCMP_ULE: return "ule";
3297 case FCmpInst::FCMP_UNE: return "une";
3298 case FCmpInst::FCMP_TRUE: return "true";
3299 case ICmpInst::ICMP_EQ: return "eq";
3300 case ICmpInst::ICMP_NE: return "ne";
3301 case ICmpInst::ICMP_SGT: return "sgt";
3302 case ICmpInst::ICMP_SGE: return "sge";
3303 case ICmpInst::ICMP_SLT: return "slt";
3304 case ICmpInst::ICMP_SLE: return "sle";
3305 case ICmpInst::ICMP_UGT: return "ugt";
3306 case ICmpInst::ICMP_UGE: return "uge";
3307 case ICmpInst::ICMP_ULT: return "ult";
3308 case ICmpInst::ICMP_ULE: return "ule";
3309 }
3310}
3311
Reid Spencer266e42b2006-12-23 06:05:41 +00003312ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3313 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003314 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003315 case ICMP_EQ: case ICMP_NE:
3316 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003317 return pred;
3318 case ICMP_UGT: return ICMP_SGT;
3319 case ICMP_ULT: return ICMP_SLT;
3320 case ICMP_UGE: return ICMP_SGE;
3321 case ICMP_ULE: return ICMP_SLE;
3322 }
3323}
3324
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003325ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3326 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003327 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003328 case ICMP_EQ: case ICMP_NE:
3329 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003330 return pred;
3331 case ICMP_SGT: return ICMP_UGT;
3332 case ICMP_SLT: return ICMP_ULT;
3333 case ICMP_SGE: return ICMP_UGE;
3334 case ICMP_SLE: return ICMP_ULE;
3335 }
3336}
3337
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003338CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3339 switch (pred) {
3340 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3341 case ICMP_SGT: return ICMP_SGE;
3342 case ICMP_SLT: return ICMP_SLE;
3343 case ICMP_SGE: return ICMP_SGT;
3344 case ICMP_SLE: return ICMP_SLT;
3345 case ICMP_UGT: return ICMP_UGE;
3346 case ICMP_ULT: return ICMP_ULE;
3347 case ICMP_UGE: return ICMP_UGT;
3348 case ICMP_ULE: return ICMP_ULT;
3349
3350 case FCMP_OGT: return FCMP_OGE;
3351 case FCMP_OLT: return FCMP_OLE;
3352 case FCMP_OGE: return FCMP_OGT;
3353 case FCMP_OLE: return FCMP_OLT;
3354 case FCMP_UGT: return FCMP_UGE;
3355 case FCMP_ULT: return FCMP_ULE;
3356 case FCMP_UGE: return FCMP_UGT;
3357 case FCMP_ULE: return FCMP_ULT;
3358 }
3359}
3360
Dan Gohman4e724382008-05-31 02:47:54 +00003361CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003362 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003363 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003364 case ICMP_EQ: case ICMP_NE:
3365 return pred;
3366 case ICMP_SGT: return ICMP_SLT;
3367 case ICMP_SLT: return ICMP_SGT;
3368 case ICMP_SGE: return ICMP_SLE;
3369 case ICMP_SLE: return ICMP_SGE;
3370 case ICMP_UGT: return ICMP_ULT;
3371 case ICMP_ULT: return ICMP_UGT;
3372 case ICMP_UGE: return ICMP_ULE;
3373 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003374
Reid Spencerd9436b62006-11-20 01:22:35 +00003375 case FCMP_FALSE: case FCMP_TRUE:
3376 case FCMP_OEQ: case FCMP_ONE:
3377 case FCMP_UEQ: case FCMP_UNE:
3378 case FCMP_ORD: case FCMP_UNO:
3379 return pred;
3380 case FCMP_OGT: return FCMP_OLT;
3381 case FCMP_OLT: return FCMP_OGT;
3382 case FCMP_OGE: return FCMP_OLE;
3383 case FCMP_OLE: return FCMP_OGE;
3384 case FCMP_UGT: return FCMP_ULT;
3385 case FCMP_ULT: return FCMP_UGT;
3386 case FCMP_UGE: return FCMP_ULE;
3387 case FCMP_ULE: return FCMP_UGE;
3388 }
3389}
3390
Max Kazantsevb299ade2018-02-07 11:16:29 +00003391CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3392 switch (pred) {
3393 case ICMP_SGT: return ICMP_SGE;
3394 case ICMP_SLT: return ICMP_SLE;
3395 case ICMP_UGT: return ICMP_UGE;
3396 case ICMP_ULT: return ICMP_ULE;
3397 case FCMP_OGT: return FCMP_OGE;
3398 case FCMP_OLT: return FCMP_OLE;
3399 case FCMP_UGT: return FCMP_UGE;
3400 case FCMP_ULT: return FCMP_ULE;
3401 default: return pred;
3402 }
3403}
3404
Sanjoy Das6e78b172015-10-22 19:57:34 +00003405CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3406 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3407
3408 switch (pred) {
3409 default:
3410 llvm_unreachable("Unknown predicate!");
3411 case CmpInst::ICMP_ULT:
3412 return CmpInst::ICMP_SLT;
3413 case CmpInst::ICMP_ULE:
3414 return CmpInst::ICMP_SLE;
3415 case CmpInst::ICMP_UGT:
3416 return CmpInst::ICMP_SGT;
3417 case CmpInst::ICMP_UGE:
3418 return CmpInst::ICMP_SGE;
3419 }
3420}
3421
Craig Topper1c3f2832015-12-15 06:11:33 +00003422bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003423 switch (predicate) {
3424 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003425 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003426 case ICmpInst::ICMP_UGE: return true;
3427 }
3428}
3429
Craig Topper1c3f2832015-12-15 06:11:33 +00003430bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003431 switch (predicate) {
3432 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003433 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003434 case ICmpInst::ICMP_SGE: return true;
3435 }
3436}
3437
Craig Topper1c3f2832015-12-15 06:11:33 +00003438bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003439 switch (predicate) {
3440 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003441 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3442 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003443 case FCmpInst::FCMP_ORD: return true;
3444 }
3445}
Fangrui Songf78650a2018-07-30 19:41:25 +00003446
Craig Topper1c3f2832015-12-15 06:11:33 +00003447bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003448 switch (predicate) {
3449 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003450 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3451 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003452 case FCmpInst::FCMP_UNO: return true;
3453 }
3454}
3455
Craig Topper1c3f2832015-12-15 06:11:33 +00003456bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003457 switch(predicate) {
3458 default: return false;
3459 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3460 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3461 }
3462}
3463
Craig Topper1c3f2832015-12-15 06:11:33 +00003464bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003465 switch(predicate) {
3466 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3467 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3468 default: return false;
3469 }
3470}
3471
Chad Rosier99bc4802016-04-21 16:18:02 +00003472bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003473 // If the predicates match, then we know the first condition implies the
3474 // second is true.
3475 if (Pred1 == Pred2)
3476 return true;
3477
3478 switch (Pred1) {
3479 default:
3480 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003481 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003482 // 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 +00003483 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3484 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003485 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3486 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3487 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3488 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3489 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3490 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3491 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3492 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003493 }
3494 return false;
3495}
3496
Chad Rosier99bc4802016-04-21 16:18:02 +00003497bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003498 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003499}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003500
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003501//===----------------------------------------------------------------------===//
3502// SwitchInst Implementation
3503//===----------------------------------------------------------------------===//
3504
Chris Lattnerbaf00152010-11-17 05:41:46 +00003505void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3506 assert(Value && Default && NumReserved);
3507 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003508 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003509 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003510
Pete Coopereb31b682015-05-21 22:48:54 +00003511 Op<0>() = Value;
3512 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003513}
3514
Chris Lattner2195fc42007-02-24 00:55:48 +00003515/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3516/// switch on and a default destination. The number of additional cases can
3517/// be specified here to make memory allocation more efficient. This
3518/// constructor can also autoinsert before another instruction.
3519SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3520 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003521 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003522 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003523 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003524}
3525
3526/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3527/// switch on and a default destination. The number of additional cases can
3528/// be specified here to make memory allocation more efficient. This
3529/// constructor also autoinserts at the end of the specified BasicBlock.
3530SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3531 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003532 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003533 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003534 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003535}
3536
Misha Brukmanb1c93172005-04-21 23:48:37 +00003537SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003538 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003539 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003540 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003541 Use *OL = getOperandList();
3542 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003543 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003544 OL[i] = InOL[i];
3545 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003546 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003547 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003548}
3549
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003550
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003551/// addCase - Add an entry to the switch instruction...
3552///
Chris Lattner47ac1872005-02-24 05:32:09 +00003553void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003554 unsigned NewCaseIdx = getNumCases();
3555 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003556 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003557 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003558 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003559 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003560 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003561 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003562 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003563 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003564}
3565
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003566/// removeCase - This method removes the specified case and its successor
3567/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003568SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3569 unsigned idx = I->getCaseIndex();
3570
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003571 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003572
3573 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003574 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003575
Jay Foad14277722011-02-01 09:22:34 +00003576 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003577 if (2 + (idx + 1) * 2 != NumOps) {
3578 OL[2 + idx * 2] = OL[NumOps - 2];
3579 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003580 }
3581
3582 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003583 OL[NumOps-2].set(nullptr);
3584 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003585 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003586
3587 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003588}
3589
Jay Foade98f29d2011-04-01 08:00:58 +00003590/// growOperands - grow operands - This grows the operand list in response
3591/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003592///
Jay Foade98f29d2011-04-01 08:00:58 +00003593void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003594 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003595 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003596
3597 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003598 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003599}
3600
Chris Lattner3ed871f2009-10-27 19:13:16 +00003601//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003602// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003603//===----------------------------------------------------------------------===//
3604
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003605void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003606 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003607 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003608 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003609 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003610 allocHungoffUses(ReservedSpace);
3611
Pete Coopereb31b682015-05-21 22:48:54 +00003612 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003613}
3614
3615
Jay Foade98f29d2011-04-01 08:00:58 +00003616/// growOperands - grow operands - This grows the operand list in response
3617/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003618///
Jay Foade98f29d2011-04-01 08:00:58 +00003619void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003620 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003621 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003622
Chris Lattner3ed871f2009-10-27 19:13:16 +00003623 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003624 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003625}
3626
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003627IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3628 Instruction *InsertBefore)
3629: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003630 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003631 init(Address, NumCases);
3632}
3633
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003634IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3635 BasicBlock *InsertAtEnd)
3636: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003637 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003638 init(Address, NumCases);
3639}
3640
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003641IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003642 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3643 nullptr, IBI.getNumOperands()) {
3644 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003645 Use *OL = getOperandList();
3646 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003647 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3648 OL[i] = InOL[i];
3649 SubclassOptionalData = IBI.SubclassOptionalData;
3650}
3651
Chris Lattner3ed871f2009-10-27 19:13:16 +00003652/// addDestination - Add a destination.
3653///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003654void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003655 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003656 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003657 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003658 // Initialize some new operands.
3659 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003660 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003661 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003662}
3663
3664/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003665/// indirectbr instruction.
3666void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003667 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003668
Chris Lattner3ed871f2009-10-27 19:13:16 +00003669 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003670 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003671
3672 // Replace this value with the last one.
3673 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003674
Chris Lattner3ed871f2009-10-27 19:13:16 +00003675 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003676 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003677 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003678}
3679
Chris Lattner3ed871f2009-10-27 19:13:16 +00003680//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003681// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003682//===----------------------------------------------------------------------===//
3683
Chris Lattnerf22be932004-10-15 23:52:53 +00003684// Define these methods here so vtables don't get emitted into every translation
3685// unit that uses these classes.
3686
Pete Cooper75403d72015-06-24 20:22:23 +00003687GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003688 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003689}
3690
Pete Cooper75403d72015-06-24 20:22:23 +00003691BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003692 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003693}
3694
Pete Cooper75403d72015-06-24 20:22:23 +00003695FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003696 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003697}
3698
Pete Cooper75403d72015-06-24 20:22:23 +00003699ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003700 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003701}
3702
Pete Cooper75403d72015-06-24 20:22:23 +00003703ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003704 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003705}
3706
Pete Cooper75403d72015-06-24 20:22:23 +00003707InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003708 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003709}
3710
Pete Cooper75403d72015-06-24 20:22:23 +00003711AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003712 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003713 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003714 (Value *)getOperand(0), getAlignment());
3715 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003716 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003717 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003718}
3719
Pete Cooper75403d72015-06-24 20:22:23 +00003720LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003721 return new LoadInst(getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003722 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003723}
3724
Pete Cooper75403d72015-06-24 20:22:23 +00003725StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003726 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003727 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003728
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003729}
3730
Pete Cooper75403d72015-06-24 20:22:23 +00003731AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003732 AtomicCmpXchgInst *Result =
3733 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003734 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003735 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003736 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003737 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003738 return Result;
3739}
3740
Pete Cooper75403d72015-06-24 20:22:23 +00003741AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003742 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003743 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3744 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003745 Result->setVolatile(isVolatile());
3746 return Result;
3747}
3748
Pete Cooper75403d72015-06-24 20:22:23 +00003749FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003750 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003751}
3752
Pete Cooper75403d72015-06-24 20:22:23 +00003753TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003754 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003755}
3756
Pete Cooper75403d72015-06-24 20:22:23 +00003757ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003758 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003759}
3760
Pete Cooper75403d72015-06-24 20:22:23 +00003761SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003762 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003763}
3764
Pete Cooper75403d72015-06-24 20:22:23 +00003765FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003766 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003767}
3768
Pete Cooper75403d72015-06-24 20:22:23 +00003769FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003770 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003771}
3772
Pete Cooper75403d72015-06-24 20:22:23 +00003773UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003774 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003775}
3776
Pete Cooper75403d72015-06-24 20:22:23 +00003777SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003778 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003779}
3780
Pete Cooper75403d72015-06-24 20:22:23 +00003781FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003782 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003783}
3784
Pete Cooper75403d72015-06-24 20:22:23 +00003785FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003786 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003787}
3788
Pete Cooper75403d72015-06-24 20:22:23 +00003789PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003790 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003791}
3792
Pete Cooper75403d72015-06-24 20:22:23 +00003793IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003794 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003795}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003796
Pete Cooper75403d72015-06-24 20:22:23 +00003797BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003798 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003799}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003800
Pete Cooper75403d72015-06-24 20:22:23 +00003801AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003802 return new AddrSpaceCastInst(getOperand(0), getType());
3803}
3804
Pete Cooper75403d72015-06-24 20:22:23 +00003805CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003806 if (hasOperandBundles()) {
3807 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3808 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3809 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003810 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003811}
3812
Pete Cooper75403d72015-06-24 20:22:23 +00003813SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003814 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003815}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003816
Pete Cooper75403d72015-06-24 20:22:23 +00003817VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003818 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003819}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003820
Pete Cooper75403d72015-06-24 20:22:23 +00003821ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003822 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003823}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003824
Pete Cooper75403d72015-06-24 20:22:23 +00003825InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003826 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003830 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003831}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003832
Pete Cooper75403d72015-06-24 20:22:23 +00003833PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003834
Pete Cooper75403d72015-06-24 20:22:23 +00003835LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003836 return new LandingPadInst(*this);
3837}
3838
Pete Cooper75403d72015-06-24 20:22:23 +00003839ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003840 return new(getNumOperands()) ReturnInst(*this);
3841}
3842
Pete Cooper75403d72015-06-24 20:22:23 +00003843BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003844 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003845}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003846
Pete Cooper75403d72015-06-24 20:22:23 +00003847SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003848
Pete Cooper75403d72015-06-24 20:22:23 +00003849IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003850 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003851}
3852
Pete Cooper75403d72015-06-24 20:22:23 +00003853InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003854 if (hasOperandBundles()) {
3855 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3856 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3857 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003858 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003859}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003860
Pete Cooper75403d72015-06-24 20:22:23 +00003861ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003862
David Majnemer654e1302015-07-31 17:58:14 +00003863CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3864 return new (getNumOperands()) CleanupReturnInst(*this);
3865}
3866
David Majnemer654e1302015-07-31 17:58:14 +00003867CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003868 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003869}
3870
David Majnemer8a1c45d2015-12-12 05:38:55 +00003871CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3872 return new CatchSwitchInst(*this);
3873}
3874
3875FuncletPadInst *FuncletPadInst::cloneImpl() const {
3876 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003880 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003881 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003882}