blob: 49f9fe837082ef902c814ef3c0832cb3eea4c077 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +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 Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
90PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000091 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
92 ReservedSpace(PN.getNumOperands()) {
93 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000094 std::copy(PN.op_begin(), PN.op_end(), op_begin());
95 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000096 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099// removeIncomingValue - Remove an incoming value. This is useful if a
100// predecessor basic block is deleted.
101Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000102 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000103
104 // Move everything after this operand down.
105 //
106 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000107 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
110 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111
112 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000113 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000114 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000117 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000119 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 eraseFromParent();
121 }
122 return Removed;
123}
124
Jay Foade98f29d2011-04-01 08:00:58 +0000125/// growOperands - grow operands - This grows the operand list in response
126/// to a push_back style of operation. This grows the number of ops by 1.5
127/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128///
Jay Foade98f29d2011-04-01 08:00:58 +0000129void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000130 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000131 unsigned NumOps = e + e / 2;
132 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
133
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000134 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000135 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136}
137
Nate Begemanb3923212005-08-04 23:24:19 +0000138/// hasConstantValue - If the specified PHI node always merges together the same
139/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000140Value *PHINode::hasConstantValue() const {
141 // Exploit the fact that phi nodes always have at least one entry.
142 Value *ConstantValue = getIncomingValue(0);
143 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000144 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
145 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000146 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000147 // The case where the first value is this PHI.
148 ConstantValue = getIncomingValue(i);
149 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000150 if (ConstantValue == this)
151 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000152 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000153}
154
Bill Wendlingfae14752011-08-12 20:24:12 +0000155//===----------------------------------------------------------------------===//
156// LandingPadInst Implementation
157//===----------------------------------------------------------------------===//
158
David Majnemer7fddecc2015-06-17 20:52:32 +0000159LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
160 const Twine &NameStr, Instruction *InsertBefore)
161 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
162 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000163}
164
David Majnemer7fddecc2015-06-17 20:52:32 +0000165LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
166 const Twine &NameStr, BasicBlock *InsertAtEnd)
167 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
168 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000169}
170
171LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000172 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
173 LP.getNumOperands()),
174 ReservedSpace(LP.getNumOperands()) {
175 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000176 Use *OL = getOperandList();
177 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000178 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
179 OL[I] = InOL[I];
180
181 setCleanup(LP.isCleanup());
182}
183
David Majnemer7fddecc2015-06-17 20:52:32 +0000184LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000185 const Twine &NameStr,
186 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000187 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000188}
189
David Majnemer7fddecc2015-06-17 20:52:32 +0000190LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000191 const Twine &NameStr,
192 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000193 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000194}
195
David Majnemer7fddecc2015-06-17 20:52:32 +0000196void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000197 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000198 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000199 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000200 setName(NameStr);
201 setCleanup(false);
202}
203
204/// growOperands - grow operands - This grows the operand list in response to a
205/// push_back style of operation. This grows the number of ops by 2 times.
206void LandingPadInst::growOperands(unsigned Size) {
207 unsigned e = getNumOperands();
208 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000209 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000210 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000211}
212
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000213void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000214 unsigned OpNo = getNumOperands();
215 growOperands(1);
216 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000217 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000218 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000219}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220
221//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222// CallInst Implementation
223//===----------------------------------------------------------------------===//
224
Gordon Henriksen14a55692007-12-10 02:14:30 +0000225CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226}
227
David Blaikie348de692015-04-23 21:36:23 +0000228void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000229 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000230 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000231 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
232 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000233 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234
Jay Foad5bd375a2011-07-15 08:37:34 +0000235#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000236 assert((Args.size() == FTy->getNumParams() ||
237 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000238 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000239
240 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000241 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000242 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000243 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000244#endif
245
246 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000247
248 auto It = populateBundleOperandInfos(Bundles, Args.size());
249 (void)It;
250 assert(It + 1 == op_end() && "Should add up!");
251
Jay Foad5bd375a2011-07-15 08:37:34 +0000252 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253}
254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000256 FTy =
257 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000258 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000259 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000262
263 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264}
265
Daniel Dunbar4975db62009-07-25 04:41:11 +0000266CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 Instruction *InsertBefore)
268 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
269 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000270 Instruction::Call,
271 OperandTraits<CallInst>::op_end(this) - 1,
272 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274}
275
Daniel Dunbar4975db62009-07-25 04:41:11 +0000276CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 BasicBlock *InsertAtEnd)
278 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
279 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000280 Instruction::Call,
281 OperandTraits<CallInst>::op_end(this) - 1,
282 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000283 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000287 : Instruction(CI.getType(), Instruction::Call,
288 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
289 CI.getNumOperands()),
290 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000291 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000292 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000293
Jay Foad5bd375a2011-07-15 08:37:34 +0000294 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000295 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
296 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000297 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000300void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000301 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000302 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000303 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000304}
305
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000306void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
307 AttributeSet PAL = getAttributes();
308 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
309 setAttributes(PAL);
310}
311
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000312void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000313 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000314 AttrBuilder B(attr);
315 LLVMContext &Context = getContext();
316 PAL = PAL.removeAttributes(Context, i,
317 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000319}
320
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000321void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
322 AttributeSet PAL = getAttributes();
323 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
324 setAttributes(PAL);
325}
326
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000327void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
328 AttributeSet PAL = getAttributes();
329 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
330 setAttributes(PAL);
331}
332
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000333bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000334 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000335 return true;
336 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000337 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000338 return false;
339}
340
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000341/// IsConstantOne - Return true only if val is constant int 1
342static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000343 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000344 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
345 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000346}
347
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000348static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000349 BasicBlock *InsertAtEnd, Type *IntPtrTy,
350 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000351 Value *ArraySize, Function *MallocF,
352 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000353 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000354 "createMalloc needs either InsertBefore or InsertAtEnd");
355
356 // malloc(type) becomes:
357 // bitcast (i8* malloc(typeSize)) to type*
358 // malloc(type, arraySize) becomes:
359 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000360 if (!ArraySize)
361 ArraySize = ConstantInt::get(IntPtrTy, 1);
362 else if (ArraySize->getType() != IntPtrTy) {
363 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000364 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
365 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000366 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000367 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
368 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000369 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000370
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000371 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000372 if (IsConstantOne(AllocSize)) {
373 AllocSize = ArraySize; // Operand * 1 = Operand
374 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
375 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
376 false /*ZExt*/);
377 // Malloc arg is constant product of type size and array size
378 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
379 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380 // Multiply type size by the array size...
381 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000382 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
383 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000384 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000385 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
386 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000388 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391 // Create the call to Malloc.
392 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
393 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000394 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000395 Value *MallocFunc = MallocF;
396 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000397 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000398 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000399 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000400 CallInst *MCall = nullptr;
401 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000402 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000403 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000404 Result = MCall;
405 if (Result->getType() != AllocPtrType)
406 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000407 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000408 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000409 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000410 Result = MCall;
411 if (Result->getType() != AllocPtrType) {
412 InsertAtEnd->getInstList().push_back(MCall);
413 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000414 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000415 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000416 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000417 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000418 if (Function *F = dyn_cast<Function>(MallocFunc)) {
419 MCall->setCallingConv(F->getCallingConv());
420 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
421 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000422 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000424 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425}
426
427/// CreateMalloc - Generate the IR for a call to malloc:
428/// 1. Compute the malloc call's argument as the specified type's size,
429/// possibly multiplied by the array size if the array size is not
430/// constant 1.
431/// 2. Call malloc with that argument.
432/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000433Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000434 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000435 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000436 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000437 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000438 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000439 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000440}
441
442/// CreateMalloc - Generate the IR for a call to malloc:
443/// 1. Compute the malloc call's argument as the specified type's size,
444/// possibly multiplied by the array size if the array size is not
445/// constant 1.
446/// 2. Call malloc with that argument.
447/// 3. Bitcast the result of the malloc call to the specified type.
448/// Note: This function does not add the bitcast to the basic block, that is the
449/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000450Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000451 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 Value *AllocSize, Value *ArraySize,
453 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000454 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000455 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000456}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000457
Victor Hernandeze2971492009-10-24 04:23:03 +0000458static Instruction* createFree(Value* Source, Instruction *InsertBefore,
459 BasicBlock *InsertAtEnd) {
460 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
461 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000462 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000463 "Can not free something of nonpointer type!");
464
465 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
466 Module* M = BB->getParent()->getParent();
467
Chris Lattner229907c2011-07-18 04:54:35 +0000468 Type *VoidTy = Type::getVoidTy(M->getContext());
469 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000470 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000471 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000472 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000473 Value *PtrCast = Source;
474 if (InsertBefore) {
475 if (Source->getType() != IntPtrTy)
476 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
477 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
478 } else {
479 if (Source->getType() != IntPtrTy)
480 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
481 Result = CallInst::Create(FreeFunc, PtrCast, "");
482 }
483 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000484 if (Function *F = dyn_cast<Function>(FreeFunc))
485 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000486
487 return Result;
488}
489
490/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000491Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000492 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000493}
494
495/// CreateFree - Generate the IR for a call to the builtin free function.
496/// Note: This function does not add the call to the basic block, that is the
497/// responsibility of the caller.
498Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000499 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000500 assert(FreeCall && "CreateFree did not create a CallInst");
501 return FreeCall;
502}
503
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000504//===----------------------------------------------------------------------===//
505// InvokeInst Implementation
506//===----------------------------------------------------------------------===//
507
David Blaikie3e807092015-05-13 18:35:26 +0000508void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
509 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000510 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000511 const Twine &NameStr) {
512 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000513
Sanjoy Das9303c242015-09-24 19:14:18 +0000514 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
515 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000516 Op<-3>() = Fn;
517 Op<-2>() = IfNormal;
518 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000519
520#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000521 assert(((Args.size() == FTy->getNumParams()) ||
522 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000523 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000524
Jay Foad5bd375a2011-07-15 08:37:34 +0000525 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000526 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000527 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000528 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000529#endif
530
531 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000532
533 auto It = populateBundleOperandInfos(Bundles, Args.size());
534 (void)It;
535 assert(It + 3 == op_end() && "Should add up!");
536
Jay Foad5bd375a2011-07-15 08:37:34 +0000537 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538}
539
Misha Brukmanb1c93172005-04-21 23:48:37 +0000540InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000541 : TerminatorInst(II.getType(), Instruction::Invoke,
542 OperandTraits<InvokeInst>::op_end(this) -
543 II.getNumOperands(),
544 II.getNumOperands()),
545 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000546 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000547 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000548 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
549 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000550 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551}
552
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000553BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
554 return getSuccessor(idx);
555}
556unsigned InvokeInst::getNumSuccessorsV() const {
557 return getNumSuccessors();
558}
559void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
560 return setSuccessor(idx, B);
561}
562
Michael Gottesman41748d72013-06-27 00:25:01 +0000563bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000564 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000565 return true;
566 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000567 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000568 return false;
569}
570
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000571bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000572 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000573 return true;
574 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000575 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000576 return false;
577}
578
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000579void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000580 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000581 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000582 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000583}
584
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000585void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000586 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000587 AttrBuilder B(attr);
588 PAL = PAL.removeAttributes(getContext(), i,
589 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000590 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000591}
592
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000593void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
594 AttributeSet PAL = getAttributes();
595 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
596 setAttributes(PAL);
597}
598
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000599void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
600 AttributeSet PAL = getAttributes();
601 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
602 setAttributes(PAL);
603}
604
Bill Wendlingfae14752011-08-12 20:24:12 +0000605LandingPadInst *InvokeInst::getLandingPadInst() const {
606 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
607}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000608
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609//===----------------------------------------------------------------------===//
610// ReturnInst Implementation
611//===----------------------------------------------------------------------===//
612
Chris Lattner2195fc42007-02-24 00:55:48 +0000613ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000614 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000615 OperandTraits<ReturnInst>::op_end(this) -
616 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000617 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000618 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000619 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000620 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000621}
622
Owen Anderson55f1c092009-08-13 21:58:54 +0000623ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
624 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000625 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
626 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000627 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000629}
Owen Anderson55f1c092009-08-13 21:58:54 +0000630ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
631 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000632 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
633 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000634 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000635 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
Owen Anderson55f1c092009-08-13 21:58:54 +0000637ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
638 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000639 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000640}
641
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642unsigned ReturnInst::getNumSuccessorsV() const {
643 return getNumSuccessors();
644}
645
Devang Patelae682fb2008-02-26 17:56:20 +0000646/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
647/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000649 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000650}
651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000653 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654}
655
Devang Patel59643e52008-02-23 00:35:18 +0000656ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000657}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000659//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000660// ResumeInst Implementation
661//===----------------------------------------------------------------------===//
662
663ResumeInst::ResumeInst(const ResumeInst &RI)
664 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
665 OperandTraits<ResumeInst>::op_begin(this), 1) {
666 Op<0>() = RI.Op<0>();
667}
668
669ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
670 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
671 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
672 Op<0>() = Exn;
673}
674
675ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
676 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
677 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
678 Op<0>() = Exn;
679}
680
681unsigned ResumeInst::getNumSuccessorsV() const {
682 return getNumSuccessors();
683}
684
685void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
686 llvm_unreachable("ResumeInst has no successors!");
687}
688
689BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
690 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000691}
692
693//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000694// CleanupEndPadInst Implementation
695//===----------------------------------------------------------------------===//
696
697CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
698 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
699 OperandTraits<CleanupEndPadInst>::op_end(this) -
700 CEPI.getNumOperands(),
701 CEPI.getNumOperands()) {
702 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
703 setCleanupPad(CEPI.getCleanupPad());
704 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
705 setUnwindDest(UnwindDest);
706}
707
708void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
709 setCleanupPad(CleanupPad);
710 if (UnwindBB) {
711 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
712 setUnwindDest(UnwindBB);
713 }
714}
715
716CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
717 BasicBlock *UnwindBB, unsigned Values,
718 Instruction *InsertBefore)
719 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
720 Instruction::CleanupEndPad,
721 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
722 Values, InsertBefore) {
723 init(CleanupPad, UnwindBB);
724}
725
726CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
727 BasicBlock *UnwindBB, unsigned Values,
728 BasicBlock *InsertAtEnd)
729 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
730 Instruction::CleanupEndPad,
731 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
732 Values, InsertAtEnd) {
733 init(CleanupPad, UnwindBB);
734}
735
736BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
737 assert(Idx == 0);
738 return getUnwindDest();
739}
740unsigned CleanupEndPadInst::getNumSuccessorsV() const {
741 return getNumSuccessors();
742}
743void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
744 assert(Idx == 0);
745 setUnwindDest(B);
746}
747
748//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000749// CleanupReturnInst Implementation
750//===----------------------------------------------------------------------===//
751
752CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
753 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
754 OperandTraits<CleanupReturnInst>::op_end(this) -
755 CRI.getNumOperands(),
756 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000757 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000758 Op<-1>() = CRI.Op<-1>();
759 if (CRI.hasUnwindDest())
760 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000761}
762
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000763void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000764 if (UnwindBB)
765 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000766
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000767 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000768 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000769 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000770}
771
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000772CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000773 BasicBlock *UnwindBB, unsigned Values,
774 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000775 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
776 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000777 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
778 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000779 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000780}
781
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000782CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000783 BasicBlock *UnwindBB, unsigned Values,
784 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000785 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
786 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000787 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
788 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000789 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000790}
791
792BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
793 assert(Idx == 0);
794 return getUnwindDest();
795}
796unsigned CleanupReturnInst::getNumSuccessorsV() const {
797 return getNumSuccessors();
798}
799void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
800 assert(Idx == 0);
801 setUnwindDest(B);
802}
803
804//===----------------------------------------------------------------------===//
805// CatchEndPadInst Implementation
806//===----------------------------------------------------------------------===//
807
808CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
809 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
810 OperandTraits<CatchEndPadInst>::op_end(this) -
811 CRI.getNumOperands(),
812 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000813 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000814 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
815 setUnwindDest(UnwindDest);
816}
817
818void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000819 if (UnwindBB) {
820 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
821 setUnwindDest(UnwindBB);
822 }
823}
824
825CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000826 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000827 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
828 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
829 Values, InsertBefore) {
830 init(UnwindBB);
831}
832
833CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000834 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000835 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
836 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
837 Values, InsertAtEnd) {
838 init(UnwindBB);
839}
840
841BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
842 assert(Idx == 0);
843 return getUnwindDest();
844}
845unsigned CatchEndPadInst::getNumSuccessorsV() const {
846 return getNumSuccessors();
847}
848void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
849 assert(Idx == 0);
850 setUnwindDest(B);
851}
852
853//===----------------------------------------------------------------------===//
854// CatchReturnInst Implementation
855//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000856void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
857 Op<0>() = CatchPad;
858 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000859}
David Majnemer654e1302015-07-31 17:58:14 +0000860
861CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
862 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000863 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
864 Op<0>() = CRI.Op<0>();
865 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000866}
867
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000868CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000869 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000870 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000871 OperandTraits<CatchReturnInst>::op_begin(this), 2,
872 InsertBefore) {
873 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000874}
875
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000876CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000877 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000878 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000879 OperandTraits<CatchReturnInst>::op_begin(this), 2,
880 InsertAtEnd) {
881 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000882}
883
884BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000885 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000886 return getSuccessor();
887}
888unsigned CatchReturnInst::getNumSuccessorsV() const {
889 return getNumSuccessors();
890}
891void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000892 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000893 setSuccessor(B);
894}
895
896//===----------------------------------------------------------------------===//
897// CatchPadInst Implementation
898//===----------------------------------------------------------------------===//
899void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000900 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000901 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
902 Op<-2>() = IfNormal;
903 Op<-1>() = IfException;
904 std::copy(Args.begin(), Args.end(), op_begin());
905 setName(NameStr);
906}
907
908CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
909 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
910 OperandTraits<CatchPadInst>::op_end(this) -
911 CPI.getNumOperands(),
912 CPI.getNumOperands()) {
913 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
914}
915
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000916CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
917 ArrayRef<Value *> Args, unsigned Values,
918 const Twine &NameStr, Instruction *InsertBefore)
919 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
920 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000921 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
922 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000923 init(IfNormal, IfException, Args, NameStr);
924}
925
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000926CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
927 ArrayRef<Value *> Args, unsigned Values,
928 const Twine &NameStr, BasicBlock *InsertAtEnd)
929 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
930 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000931 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
932 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000933 init(IfNormal, IfException, Args, NameStr);
934}
935
936BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
937 return getSuccessor(Idx);
938}
939unsigned CatchPadInst::getNumSuccessorsV() const {
940 return getNumSuccessors();
941}
942void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
943 return setSuccessor(Idx, B);
944}
945
946//===----------------------------------------------------------------------===//
947// TerminatePadInst Implementation
948//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000949void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000950 if (BB)
951 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
952 if (BB)
953 Op<-1>() = BB;
954 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +0000955}
956
957TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
958 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
959 OperandTraits<TerminatePadInst>::op_end(this) -
960 TPI.getNumOperands(),
961 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000962 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000963 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
964}
965
966TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000967 ArrayRef<Value *> Args, unsigned Values,
968 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000969 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
970 OperandTraits<TerminatePadInst>::op_end(this) - Values,
971 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000972 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000973}
974
975TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000976 ArrayRef<Value *> Args, unsigned Values,
977 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000978 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
979 OperandTraits<TerminatePadInst>::op_end(this) - Values,
980 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000981 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000982}
983
984BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
985 assert(Idx == 0);
986 return getUnwindDest();
987}
988unsigned TerminatePadInst::getNumSuccessorsV() const {
989 return getNumSuccessors();
990}
991void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
992 assert(Idx == 0);
993 return setUnwindDest(B);
994}
995
996//===----------------------------------------------------------------------===//
997// CleanupPadInst Implementation
998//===----------------------------------------------------------------------===//
999void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1000 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1001 std::copy(Args.begin(), Args.end(), op_begin());
1002 setName(NameStr);
1003}
1004
1005CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1006 : Instruction(CPI.getType(), Instruction::CleanupPad,
1007 OperandTraits<CleanupPadInst>::op_end(this) -
1008 CPI.getNumOperands(),
1009 CPI.getNumOperands()) {
1010 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
1011}
1012
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001013CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001014 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001015 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001016 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1017 Args.size(), InsertBefore) {
1018 init(Args, NameStr);
1019}
1020
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001021CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001022 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001023 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001024 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1025 Args.size(), InsertAtEnd) {
1026 init(Args, NameStr);
1027}
1028
1029//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001030// UnreachableInst Implementation
1031//===----------------------------------------------------------------------===//
1032
Owen Anderson55f1c092009-08-13 21:58:54 +00001033UnreachableInst::UnreachableInst(LLVMContext &Context,
1034 Instruction *InsertBefore)
1035 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001036 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001037}
Owen Anderson55f1c092009-08-13 21:58:54 +00001038UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1039 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001040 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001041}
1042
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043unsigned UnreachableInst::getNumSuccessorsV() const {
1044 return getNumSuccessors();
1045}
1046
1047void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001048 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001049}
1050
1051BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001052 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001053}
1054
1055//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001056// BranchInst Implementation
1057//===----------------------------------------------------------------------===//
1058
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059void BranchInst::AssertOK() {
1060 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001061 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063}
1064
Chris Lattner2195fc42007-02-24 00:55:48 +00001065BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001066 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001067 OperandTraits<BranchInst>::op_end(this) - 1,
1068 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001069 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001070 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001071}
1072BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1073 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001074 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001075 OperandTraits<BranchInst>::op_end(this) - 3,
1076 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001077 Op<-1>() = IfTrue;
1078 Op<-2>() = IfFalse;
1079 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001080#ifndef NDEBUG
1081 AssertOK();
1082#endif
1083}
1084
1085BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001086 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001087 OperandTraits<BranchInst>::op_end(this) - 1,
1088 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001089 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001090 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001091}
1092
1093BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1094 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001095 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001096 OperandTraits<BranchInst>::op_end(this) - 3,
1097 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001098 Op<-1>() = IfTrue;
1099 Op<-2>() = IfFalse;
1100 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001101#ifndef NDEBUG
1102 AssertOK();
1103#endif
1104}
1105
1106
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001108 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001109 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1110 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001111 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001112 if (BI.getNumOperands() != 1) {
1113 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001114 Op<-3>() = BI.Op<-3>();
1115 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001116 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001117 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118}
1119
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001120void BranchInst::swapSuccessors() {
1121 assert(isConditional() &&
1122 "Cannot swap successors of an unconditional branch");
1123 Op<-1>().swap(Op<-2>());
1124
1125 // Update profile metadata if present and it matches our structural
1126 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001127 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001128 if (!ProfileData || ProfileData->getNumOperands() != 3)
1129 return;
1130
1131 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001132 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1133 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001134 setMetadata(LLVMContext::MD_prof,
1135 MDNode::get(ProfileData->getContext(), Ops));
1136}
1137
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001138BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1139 return getSuccessor(idx);
1140}
1141unsigned BranchInst::getNumSuccessorsV() const {
1142 return getNumSuccessors();
1143}
1144void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1145 setSuccessor(idx, B);
1146}
1147
1148
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001150// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001151//===----------------------------------------------------------------------===//
1152
Owen Andersonb6b25302009-07-14 23:09:55 +00001153static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001154 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001155 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001156 else {
1157 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001158 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001159 assert(Amt->getType()->isIntegerTy() &&
1160 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001161 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001162 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163}
1164
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001165AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1166 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001167
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001168AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1169 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001170
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001171AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001172 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001173 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001174
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001175AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001176 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001177 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001178
Chris Lattner229907c2011-07-18 04:54:35 +00001179AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001180 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001181 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1182 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1183 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001184 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001185 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001186 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187}
1188
Chris Lattner229907c2011-07-18 04:54:35 +00001189AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001190 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001191 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1192 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1193 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001194 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001195 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001196 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001197}
1198
Gordon Henriksen14a55692007-12-10 02:14:30 +00001199// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001200AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001201}
1202
Victor Hernandez8acf2952009-10-23 21:09:37 +00001203void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001204 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001205 assert(Align <= MaximumAlignment &&
1206 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001207 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1208 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001209 assert(getAlignment() == Align && "Alignment representation error!");
1210}
1211
Victor Hernandez8acf2952009-10-23 21:09:37 +00001212bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001213 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001214 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001215 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216}
1217
Chris Lattner8b291e62008-11-26 02:54:17 +00001218/// isStaticAlloca - Return true if this alloca is in the entry block of the
1219/// function and is a constant size. If so, the code generator will fold it
1220/// into the prolog/epilog code, so it is basically free.
1221bool AllocaInst::isStaticAlloca() const {
1222 // Must be constant size.
1223 if (!isa<ConstantInt>(getArraySize())) return false;
1224
1225 // Must be in the entry block.
1226 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001227 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001228}
1229
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001230//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231// LoadInst Implementation
1232//===----------------------------------------------------------------------===//
1233
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001234void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001235 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001237 assert(!(isAtomic() && getAlignment() == 0) &&
1238 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001239}
1240
Daniel Dunbar4975db62009-07-25 04:41:11 +00001241LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001242 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001243
Daniel Dunbar4975db62009-07-25 04:41:11 +00001244LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001245 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001246
David Blaikie0c28fd72015-05-20 21:46:30 +00001247LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001248 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001249 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001250
1251LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1252 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001253 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001254
David Blaikieb7a029872015-04-17 19:56:21 +00001255LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001256 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001257 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001258 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001259
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001260LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001261 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001262 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001263}
1264
David Blaikie15d9a4c2015-04-06 20:59:48 +00001265LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001266 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001267 SynchronizationScope SynchScope, Instruction *InsertBef)
1268 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001269 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001270 setVolatile(isVolatile);
1271 setAlignment(Align);
1272 setAtomic(Order, SynchScope);
1273 AssertOK();
1274 setName(Name);
1275}
1276
1277LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1278 unsigned Align, AtomicOrdering Order,
1279 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001280 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001282 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001283 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001284 setAlignment(Align);
1285 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001286 AssertOK();
1287 setName(Name);
1288}
1289
Daniel Dunbar27096822009-08-11 18:11:15 +00001290LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1291 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1292 Load, Ptr, InsertBef) {
1293 setVolatile(false);
1294 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001295 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001296 AssertOK();
1297 if (Name && Name[0]) setName(Name);
1298}
1299
1300LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1301 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1302 Load, Ptr, InsertAE) {
1303 setVolatile(false);
1304 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001305 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001306 AssertOK();
1307 if (Name && Name[0]) setName(Name);
1308}
1309
David Blaikie0c28fd72015-05-20 21:46:30 +00001310LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001311 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001312 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1313 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001314 setVolatile(isVolatile);
1315 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001316 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001317 AssertOK();
1318 if (Name && Name[0]) setName(Name);
1319}
1320
1321LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1322 BasicBlock *InsertAE)
1323 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1324 Load, Ptr, InsertAE) {
1325 setVolatile(isVolatile);
1326 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001327 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001328 AssertOK();
1329 if (Name && Name[0]) setName(Name);
1330}
1331
Christopher Lamb84485702007-04-22 19:24:39 +00001332void LoadInst::setAlignment(unsigned Align) {
1333 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001334 assert(Align <= MaximumAlignment &&
1335 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001336 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001337 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001338 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001339}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001340
1341//===----------------------------------------------------------------------===//
1342// StoreInst Implementation
1343//===----------------------------------------------------------------------===//
1344
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001345void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001346 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001347 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001348 "Ptr must have pointer type!");
1349 assert(getOperand(0)->getType() ==
1350 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001351 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001352 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001353 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001354}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001356StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001357 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001358
1359StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001360 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001361
Misha Brukmanb1c93172005-04-21 23:48:37 +00001362StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001363 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001364 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001365
1366StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001367 BasicBlock *InsertAtEnd)
1368 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1369
1370StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1371 Instruction *InsertBefore)
1372 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1373 InsertBefore) {}
1374
1375StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1376 BasicBlock *InsertAtEnd)
1377 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1378 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001379
Misha Brukmanb1c93172005-04-21 23:48:37 +00001380StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001381 unsigned Align, AtomicOrdering Order,
1382 SynchronizationScope SynchScope,
1383 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001384 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001385 OperandTraits<StoreInst>::op_begin(this),
1386 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001387 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001388 Op<0>() = val;
1389 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001390 setVolatile(isVolatile);
1391 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001392 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001393 AssertOK();
1394}
1395
1396StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001397 unsigned Align, AtomicOrdering Order,
1398 SynchronizationScope SynchScope,
1399 BasicBlock *InsertAtEnd)
1400 : Instruction(Type::getVoidTy(val->getContext()), Store,
1401 OperandTraits<StoreInst>::op_begin(this),
1402 OperandTraits<StoreInst>::operands(this),
1403 InsertAtEnd) {
1404 Op<0>() = val;
1405 Op<1>() = addr;
1406 setVolatile(isVolatile);
1407 setAlignment(Align);
1408 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001409 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001410}
1411
Christopher Lamb84485702007-04-22 19:24:39 +00001412void StoreInst::setAlignment(unsigned Align) {
1413 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001414 assert(Align <= MaximumAlignment &&
1415 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001416 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001417 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001418 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001419}
1420
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001421//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001422// AtomicCmpXchgInst Implementation
1423//===----------------------------------------------------------------------===//
1424
1425void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001426 AtomicOrdering SuccessOrdering,
1427 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001428 SynchronizationScope SynchScope) {
1429 Op<0>() = Ptr;
1430 Op<1>() = Cmp;
1431 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001432 setSuccessOrdering(SuccessOrdering);
1433 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001434 setSynchScope(SynchScope);
1435
1436 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1437 "All operands must be non-null!");
1438 assert(getOperand(0)->getType()->isPointerTy() &&
1439 "Ptr must have pointer type!");
1440 assert(getOperand(1)->getType() ==
1441 cast<PointerType>(getOperand(0)->getType())->getElementType()
1442 && "Ptr must be a pointer to Cmp type!");
1443 assert(getOperand(2)->getType() ==
1444 cast<PointerType>(getOperand(0)->getType())->getElementType()
1445 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001446 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001447 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001448 assert(FailureOrdering != NotAtomic &&
1449 "AtomicCmpXchg instructions must be atomic!");
1450 assert(SuccessOrdering >= FailureOrdering &&
1451 "AtomicCmpXchg success ordering must be at least as strong as fail");
1452 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1453 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001454}
1455
1456AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001457 AtomicOrdering SuccessOrdering,
1458 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001459 SynchronizationScope SynchScope,
1460 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001461 : Instruction(
1462 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1463 nullptr),
1464 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1465 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001466 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001467}
1468
1469AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001470 AtomicOrdering SuccessOrdering,
1471 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001472 SynchronizationScope SynchScope,
1473 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001474 : Instruction(
1475 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1476 nullptr),
1477 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1478 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001479 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001480}
Tim Northover420a2162014-06-13 14:24:07 +00001481
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001482//===----------------------------------------------------------------------===//
1483// AtomicRMWInst Implementation
1484//===----------------------------------------------------------------------===//
1485
1486void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1487 AtomicOrdering Ordering,
1488 SynchronizationScope SynchScope) {
1489 Op<0>() = Ptr;
1490 Op<1>() = Val;
1491 setOperation(Operation);
1492 setOrdering(Ordering);
1493 setSynchScope(SynchScope);
1494
1495 assert(getOperand(0) && getOperand(1) &&
1496 "All operands must be non-null!");
1497 assert(getOperand(0)->getType()->isPointerTy() &&
1498 "Ptr must have pointer type!");
1499 assert(getOperand(1)->getType() ==
1500 cast<PointerType>(getOperand(0)->getType())->getElementType()
1501 && "Ptr must be a pointer to Val type!");
1502 assert(Ordering != NotAtomic &&
1503 "AtomicRMW instructions must be atomic!");
1504}
1505
1506AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1507 AtomicOrdering Ordering,
1508 SynchronizationScope SynchScope,
1509 Instruction *InsertBefore)
1510 : Instruction(Val->getType(), AtomicRMW,
1511 OperandTraits<AtomicRMWInst>::op_begin(this),
1512 OperandTraits<AtomicRMWInst>::operands(this),
1513 InsertBefore) {
1514 Init(Operation, Ptr, Val, Ordering, SynchScope);
1515}
1516
1517AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1518 AtomicOrdering Ordering,
1519 SynchronizationScope SynchScope,
1520 BasicBlock *InsertAtEnd)
1521 : Instruction(Val->getType(), AtomicRMW,
1522 OperandTraits<AtomicRMWInst>::op_begin(this),
1523 OperandTraits<AtomicRMWInst>::operands(this),
1524 InsertAtEnd) {
1525 Init(Operation, Ptr, Val, Ordering, SynchScope);
1526}
1527
1528//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001529// FenceInst Implementation
1530//===----------------------------------------------------------------------===//
1531
1532FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1533 SynchronizationScope SynchScope,
1534 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001535 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001536 setOrdering(Ordering);
1537 setSynchScope(SynchScope);
1538}
1539
1540FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1541 SynchronizationScope SynchScope,
1542 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001543 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001544 setOrdering(Ordering);
1545 setSynchScope(SynchScope);
1546}
1547
1548//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001549// GetElementPtrInst Implementation
1550//===----------------------------------------------------------------------===//
1551
Jay Foadd1b78492011-07-25 09:48:08 +00001552void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001553 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001554 assert(getNumOperands() == 1 + IdxList.size() &&
1555 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001556 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001557 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001558 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001559}
1560
Gabor Greiff6caff662008-05-10 08:32:32 +00001561GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001562 : Instruction(GEPI.getType(), GetElementPtr,
1563 OperandTraits<GetElementPtrInst>::op_end(this) -
1564 GEPI.getNumOperands(),
1565 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001566 SourceElementType(GEPI.SourceElementType),
1567 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001568 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001569 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001570}
1571
Chris Lattner6090a422009-03-09 04:46:40 +00001572/// getIndexedType - Returns the type of the element that would be accessed with
1573/// a gep instruction with the specified parameters.
1574///
1575/// The Idxs pointer should point to a continuous piece of memory containing the
1576/// indices, either as Value* or uint64_t.
1577///
1578/// A null type is returned if the indices are invalid for the specified
1579/// pointer type.
1580///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001581template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001582static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001583 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001584 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001585 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001586
Chris Lattner6090a422009-03-09 04:46:40 +00001587 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001588 // it cannot be 'stepped over'.
1589 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001590 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001591
Dan Gohman1ecaf452008-05-31 00:58:22 +00001592 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001593 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001594 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001595 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001596 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001597 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001598 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001599 }
Craig Topperc6207612014-04-09 06:08:46 +00001600 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601}
1602
David Blaikied288fb82015-03-30 21:41:43 +00001603Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1604 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001605}
1606
David Blaikied288fb82015-03-30 21:41:43 +00001607Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001608 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001609 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001610}
1611
David Blaikied288fb82015-03-30 21:41:43 +00001612Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1613 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001614}
1615
Chris Lattner45f15572007-04-14 00:12:57 +00001616/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1617/// zeros. If so, the result pointer and the first operand have the same
1618/// value, just potentially different types.
1619bool GetElementPtrInst::hasAllZeroIndices() const {
1620 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1621 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1622 if (!CI->isZero()) return false;
1623 } else {
1624 return false;
1625 }
1626 }
1627 return true;
1628}
1629
Chris Lattner27058292007-04-27 20:35:56 +00001630/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1631/// constant integers. If so, the result pointer and the first operand have
1632/// a constant offset between them.
1633bool GetElementPtrInst::hasAllConstantIndices() const {
1634 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1635 if (!isa<ConstantInt>(getOperand(i)))
1636 return false;
1637 }
1638 return true;
1639}
1640
Dan Gohman1b849082009-09-07 23:54:19 +00001641void GetElementPtrInst::setIsInBounds(bool B) {
1642 cast<GEPOperator>(this)->setIsInBounds(B);
1643}
Chris Lattner45f15572007-04-14 00:12:57 +00001644
Nick Lewycky28a5f252009-09-27 21:33:04 +00001645bool GetElementPtrInst::isInBounds() const {
1646 return cast<GEPOperator>(this)->isInBounds();
1647}
1648
Chandler Carruth1e140532012-12-11 10:29:10 +00001649bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1650 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001651 // Delegate to the generic GEPOperator implementation.
1652 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001653}
1654
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001656// ExtractElementInst Implementation
1657//===----------------------------------------------------------------------===//
1658
1659ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001660 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001661 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001662 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001663 ExtractElement,
1664 OperandTraits<ExtractElementInst>::op_begin(this),
1665 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001666 assert(isValidOperands(Val, Index) &&
1667 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001668 Op<0>() = Val;
1669 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001670 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001671}
1672
1673ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001674 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001675 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001676 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001677 ExtractElement,
1678 OperandTraits<ExtractElementInst>::op_begin(this),
1679 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001680 assert(isValidOperands(Val, Index) &&
1681 "Invalid extractelement instruction operands!");
1682
Gabor Greif2d3024d2008-05-26 21:33:52 +00001683 Op<0>() = Val;
1684 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001685 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001686}
1687
Chris Lattner65511ff2006-10-05 06:24:58 +00001688
Chris Lattner54865b32006-04-08 04:05:48 +00001689bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001690 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001691 return false;
1692 return true;
1693}
1694
1695
Robert Bocchino23004482006-01-10 19:05:34 +00001696//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001697// InsertElementInst Implementation
1698//===----------------------------------------------------------------------===//
1699
Chris Lattner54865b32006-04-08 04:05:48 +00001700InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001701 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001702 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001703 : Instruction(Vec->getType(), InsertElement,
1704 OperandTraits<InsertElementInst>::op_begin(this),
1705 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001706 assert(isValidOperands(Vec, Elt, Index) &&
1707 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001708 Op<0>() = Vec;
1709 Op<1>() = Elt;
1710 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001711 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001712}
1713
Chris Lattner54865b32006-04-08 04:05:48 +00001714InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001715 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001716 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001717 : Instruction(Vec->getType(), InsertElement,
1718 OperandTraits<InsertElementInst>::op_begin(this),
1719 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001720 assert(isValidOperands(Vec, Elt, Index) &&
1721 "Invalid insertelement instruction operands!");
1722
Gabor Greif2d3024d2008-05-26 21:33:52 +00001723 Op<0>() = Vec;
1724 Op<1>() = Elt;
1725 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001726 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001727}
1728
Chris Lattner54865b32006-04-08 04:05:48 +00001729bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1730 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001731 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001732 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001733
Reid Spencerd84d35b2007-02-15 02:26:10 +00001734 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001735 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001736
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001737 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001738 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001739 return true;
1740}
1741
1742
Robert Bocchinoca27f032006-01-17 20:07:22 +00001743//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001744// ShuffleVectorInst Implementation
1745//===----------------------------------------------------------------------===//
1746
1747ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001748 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001750: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001751 cast<VectorType>(Mask->getType())->getNumElements()),
1752 ShuffleVector,
1753 OperandTraits<ShuffleVectorInst>::op_begin(this),
1754 OperandTraits<ShuffleVectorInst>::operands(this),
1755 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001756 assert(isValidOperands(V1, V2, Mask) &&
1757 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001758 Op<0>() = V1;
1759 Op<1>() = V2;
1760 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001761 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001762}
1763
1764ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001765 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001766 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001767: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1768 cast<VectorType>(Mask->getType())->getNumElements()),
1769 ShuffleVector,
1770 OperandTraits<ShuffleVectorInst>::op_begin(this),
1771 OperandTraits<ShuffleVectorInst>::operands(this),
1772 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001773 assert(isValidOperands(V1, V2, Mask) &&
1774 "Invalid shuffle vector instruction operands!");
1775
Gabor Greif2d3024d2008-05-26 21:33:52 +00001776 Op<0>() = V1;
1777 Op<1>() = V2;
1778 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001779 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001780}
1781
Mon P Wang25f01062008-11-10 04:46:22 +00001782bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001783 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001784 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001785 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001786 return false;
1787
Chris Lattner1dcb6542012-01-25 23:49:49 +00001788 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001789 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001790 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001791 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001792
1793 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001794 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1795 return true;
1796
Nate Begeman60a31c32010-08-13 00:16:46 +00001797 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001798 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001799 for (Value *Op : MV->operands()) {
1800 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001801 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001802 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001803 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001804 return false;
1805 }
1806 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001807 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001808 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001809
1810 if (const ConstantDataSequential *CDS =
1811 dyn_cast<ConstantDataSequential>(Mask)) {
1812 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1813 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1814 if (CDS->getElementAsInteger(i) >= V1Size*2)
1815 return false;
1816 return true;
1817 }
1818
1819 // The bitcode reader can create a place holder for a forward reference
1820 // used as the shuffle mask. When this occurs, the shuffle mask will
1821 // fall into this case and fail. To avoid this error, do this bit of
1822 // ugliness to allow such a mask pass.
1823 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1824 if (CE->getOpcode() == Instruction::UserOp1)
1825 return true;
1826
1827 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001828}
1829
Chris Lattnerf724e342008-03-02 05:28:33 +00001830/// getMaskValue - Return the index from the shuffle mask for the specified
1831/// output result. This is either -1 if the element is undef or a number less
1832/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001833int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1834 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1835 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001836 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001837 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001838 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001839 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001840 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001841}
1842
Chris Lattner1dcb6542012-01-25 23:49:49 +00001843/// getShuffleMask - Return the full mask for this instruction, where each
1844/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001845void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1846 SmallVectorImpl<int> &Result) {
1847 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001848
Chris Lattnercf129702012-01-26 02:51:13 +00001849 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001850 for (unsigned i = 0; i != NumElts; ++i)
1851 Result.push_back(CDS->getElementAsInteger(i));
1852 return;
1853 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001854 for (unsigned i = 0; i != NumElts; ++i) {
1855 Constant *C = Mask->getAggregateElement(i);
1856 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001857 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001858 }
1859}
1860
1861
Dan Gohman12fce772008-05-15 19:50:34 +00001862//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001863// InsertValueInst Class
1864//===----------------------------------------------------------------------===//
1865
Jay Foad57aa6362011-07-13 10:26:04 +00001866void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1867 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001868 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001869
1870 // There's no fundamental reason why we require at least one index
1871 // (other than weirdness with &*IdxBegin being invalid; see
1872 // getelementptr's init routine for example). But there's no
1873 // present need to support it.
1874 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1875
1876 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001877 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001878 Op<0>() = Agg;
1879 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001880
Jay Foad57aa6362011-07-13 10:26:04 +00001881 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001882 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001883}
1884
1885InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001886 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001887 OperandTraits<InsertValueInst>::op_begin(this), 2),
1888 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001889 Op<0>() = IVI.getOperand(0);
1890 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001891 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001892}
1893
1894//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001895// ExtractValueInst Class
1896//===----------------------------------------------------------------------===//
1897
Jay Foad57aa6362011-07-13 10:26:04 +00001898void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001899 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001900
Jay Foad57aa6362011-07-13 10:26:04 +00001901 // There's no fundamental reason why we require at least one index.
1902 // But there's no present need to support it.
1903 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001904
Jay Foad57aa6362011-07-13 10:26:04 +00001905 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001906 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001907}
1908
1909ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001910 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001911 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001912 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001913}
1914
Dan Gohman12fce772008-05-15 19:50:34 +00001915// getIndexedType - Returns the type of the element that would be extracted
1916// with an extractvalue instruction with the specified parameters.
1917//
1918// A null type is returned if the indices are invalid for the specified
1919// pointer type.
1920//
Chris Lattner229907c2011-07-18 04:54:35 +00001921Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001922 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001923 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001924 // We can't use CompositeType::indexValid(Index) here.
1925 // indexValid() always returns true for arrays because getelementptr allows
1926 // out-of-bounds indices. Since we don't allow those for extractvalue and
1927 // insertvalue we need to check array indexing manually.
1928 // Since the only other types we can index into are struct types it's just
1929 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001930 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001931 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001932 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001933 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001934 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001935 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001936 } else {
1937 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001938 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001939 }
1940
1941 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001942 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001943 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001944}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001945
1946//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001947// BinaryOperator Class
1948//===----------------------------------------------------------------------===//
1949
Chris Lattner2195fc42007-02-24 00:55:48 +00001950BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001951 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001952 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001953 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001954 OperandTraits<BinaryOperator>::op_begin(this),
1955 OperandTraits<BinaryOperator>::operands(this),
1956 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001957 Op<0>() = S1;
1958 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001959 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001960 setName(Name);
1961}
1962
1963BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001964 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001965 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001966 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001967 OperandTraits<BinaryOperator>::op_begin(this),
1968 OperandTraits<BinaryOperator>::operands(this),
1969 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001970 Op<0>() = S1;
1971 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001972 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001973 setName(Name);
1974}
1975
1976
1977void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001978 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001979 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001980 assert(LHS->getType() == RHS->getType() &&
1981 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001982#ifndef NDEBUG
1983 switch (iType) {
1984 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001985 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001986 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001988 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001989 "Tried to create an integer operation on a non-integer type!");
1990 break;
1991 case FAdd: case FSub:
1992 case FMul:
1993 assert(getType() == LHS->getType() &&
1994 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001995 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001996 "Tried to create a floating-point operation on a "
1997 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001998 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001999 case UDiv:
2000 case SDiv:
2001 assert(getType() == LHS->getType() &&
2002 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002003 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002004 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002005 "Incorrect operand type (not integer) for S/UDIV");
2006 break;
2007 case FDiv:
2008 assert(getType() == LHS->getType() &&
2009 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002011 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002012 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002013 case URem:
2014 case SRem:
2015 assert(getType() == LHS->getType() &&
2016 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002017 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002018 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002019 "Incorrect operand type (not integer) for S/UREM");
2020 break;
2021 case FRem:
2022 assert(getType() == LHS->getType() &&
2023 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002024 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002025 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002026 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002027 case Shl:
2028 case LShr:
2029 case AShr:
2030 assert(getType() == LHS->getType() &&
2031 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002032 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002033 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002034 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002035 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002036 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002037 case And: case Or:
2038 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002039 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002040 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002041 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002042 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002043 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002044 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002045 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002046 default:
2047 break;
2048 }
2049#endif
2050}
2051
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002053 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002054 Instruction *InsertBefore) {
2055 assert(S1->getType() == S2->getType() &&
2056 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002057 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002058}
2059
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002060BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002061 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002062 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002064 InsertAtEnd->getInstList().push_back(Res);
2065 return Res;
2066}
2067
Dan Gohman5476cfd2009-08-12 16:23:25 +00002068BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002069 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002070 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002071 return new BinaryOperator(Instruction::Sub,
2072 zero, Op,
2073 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002074}
2075
Dan Gohman5476cfd2009-08-12 16:23:25 +00002076BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002077 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002078 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002079 return new BinaryOperator(Instruction::Sub,
2080 zero, Op,
2081 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002082}
2083
Dan Gohman4ab44202009-12-18 02:58:50 +00002084BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2085 Instruction *InsertBefore) {
2086 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2087 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2088}
2089
2090BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2091 BasicBlock *InsertAtEnd) {
2092 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2093 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2094}
2095
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002096BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2097 Instruction *InsertBefore) {
2098 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2099 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2100}
2101
2102BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2103 BasicBlock *InsertAtEnd) {
2104 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2105 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2106}
2107
Dan Gohman5476cfd2009-08-12 16:23:25 +00002108BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002109 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002110 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002111 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002112 Op->getType(), Name, InsertBefore);
2113}
2114
Dan Gohman5476cfd2009-08-12 16:23:25 +00002115BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002116 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002117 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002118 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002119 Op->getType(), Name, InsertAtEnd);
2120}
2121
Dan Gohman5476cfd2009-08-12 16:23:25 +00002122BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002123 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002124 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002125 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 Op->getType(), Name, InsertBefore);
2127}
2128
Dan Gohman5476cfd2009-08-12 16:23:25 +00002129BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002130 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002131 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002132 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002133 Op->getType(), Name, InsertAtEnd);
2134}
2135
2136
2137// isConstantAllOnes - Helper function for several functions below
2138static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002139 if (const Constant *C = dyn_cast<Constant>(V))
2140 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002141 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002142}
2143
Owen Andersonbb2501b2009-07-13 22:18:28 +00002144bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002145 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2146 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002147 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2148 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002149 return false;
2150}
2151
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002152bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002153 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2154 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002155 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2156 if (!IgnoreZeroSign)
2157 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2158 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2159 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002160 return false;
2161}
2162
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002163bool BinaryOperator::isNot(const Value *V) {
2164 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2165 return (Bop->getOpcode() == Instruction::Xor &&
2166 (isConstantAllOnes(Bop->getOperand(1)) ||
2167 isConstantAllOnes(Bop->getOperand(0))));
2168 return false;
2169}
2170
Chris Lattner2c7d1772005-04-24 07:28:37 +00002171Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002172 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173}
2174
Chris Lattner2c7d1772005-04-24 07:28:37 +00002175const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2176 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177}
2178
Dan Gohmana5b96452009-06-04 22:49:04 +00002179Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002180 return cast<BinaryOperator>(BinOp)->getOperand(1);
2181}
2182
2183const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2184 return getFNegArgument(const_cast<Value*>(BinOp));
2185}
2186
Chris Lattner2c7d1772005-04-24 07:28:37 +00002187Value *BinaryOperator::getNotArgument(Value *BinOp) {
2188 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2189 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2190 Value *Op0 = BO->getOperand(0);
2191 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002192 if (isConstantAllOnes(Op0)) return Op1;
2193
2194 assert(isConstantAllOnes(Op1));
2195 return Op0;
2196}
2197
Chris Lattner2c7d1772005-04-24 07:28:37 +00002198const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2199 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002200}
2201
2202
2203// swapOperands - Exchange the two operands to this instruction. This
2204// instruction is safe to use on any binary instruction and does not
2205// modify the semantics of the instruction. If the instruction is
2206// order dependent (SetLT f.e.) the opcode is changed.
2207//
2208bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002209 if (!isCommutative())
2210 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002211 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002212 return false;
2213}
2214
Dan Gohman1b849082009-09-07 23:54:19 +00002215void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2216 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2217}
2218
2219void BinaryOperator::setHasNoSignedWrap(bool b) {
2220 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2221}
2222
2223void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002224 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002225}
2226
Nick Lewycky28a5f252009-09-27 21:33:04 +00002227bool BinaryOperator::hasNoUnsignedWrap() const {
2228 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2229}
2230
2231bool BinaryOperator::hasNoSignedWrap() const {
2232 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2233}
2234
2235bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002236 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002237}
2238
Sanjay Patela982d992014-09-03 01:06:50 +00002239void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002240 // Copy the wrapping flags.
2241 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2242 setHasNoSignedWrap(OB->hasNoSignedWrap());
2243 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2244 }
2245
2246 // Copy the exact flag.
2247 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2248 setIsExact(PE->isExact());
2249
2250 // Copy the fast-math flags.
2251 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002252 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002253}
2254
Sanjay Patela982d992014-09-03 01:06:50 +00002255void BinaryOperator::andIRFlags(const Value *V) {
2256 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2257 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2258 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2259 }
2260
2261 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2262 setIsExact(isExact() & PE->isExact());
2263
2264 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2265 FastMathFlags FM = getFastMathFlags();
2266 FM &= FP->getFastMathFlags();
2267 copyFastMathFlags(FM);
2268 }
2269}
2270
2271
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002272//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002273// FPMathOperator Class
2274//===----------------------------------------------------------------------===//
2275
2276/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2277/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002278/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002279float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002280 const MDNode *MD =
2281 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002282 if (!MD)
2283 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002284 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002285 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002286}
2287
2288
2289//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002290// CastInst Class
2291//===----------------------------------------------------------------------===//
2292
David Blaikie3a15e142011-12-01 08:00:17 +00002293void CastInst::anchor() {}
2294
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295// Just determine if this cast only deals with integral->integral conversion.
2296bool CastInst::isIntegerCast() const {
2297 switch (getOpcode()) {
2298 default: return false;
2299 case Instruction::ZExt:
2300 case Instruction::SExt:
2301 case Instruction::Trunc:
2302 return true;
2303 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002304 return getOperand(0)->getType()->isIntegerTy() &&
2305 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002307}
2308
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309bool CastInst::isLosslessCast() const {
2310 // Only BitCast can be lossless, exit fast if we're not BitCast
2311 if (getOpcode() != Instruction::BitCast)
2312 return false;
2313
2314 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002315 Type* SrcTy = getOperand(0)->getType();
2316 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 if (SrcTy == DstTy)
2318 return true;
2319
Reid Spencer8d9336d2006-12-31 05:26:44 +00002320 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002321 if (SrcTy->isPointerTy())
2322 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 return false; // Other types have no identity values
2324}
2325
2326/// This function determines if the CastInst does not require any bits to be
2327/// changed in order to effect the cast. Essentially, it identifies cases where
2328/// no code gen is necessary for the cast, hence the name no-op cast. For
2329/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002330/// # bitcast i32* %x to i8*
2331/// # bitcast <2 x i32> %x to <4 x i16>
2332/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002333/// @brief Determine if the described cast is a no-op.
2334bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002335 Type *SrcTy,
2336 Type *DestTy,
2337 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002338 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002339 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 case Instruction::Trunc:
2341 case Instruction::ZExt:
2342 case Instruction::SExt:
2343 case Instruction::FPTrunc:
2344 case Instruction::FPExt:
2345 case Instruction::UIToFP:
2346 case Instruction::SIToFP:
2347 case Instruction::FPToUI:
2348 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002349 case Instruction::AddrSpaceCast:
2350 // TODO: Target informations may give a more accurate answer here.
2351 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 case Instruction::BitCast:
2353 return true; // BitCast never modifies bits.
2354 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002355 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002356 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002358 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002359 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 }
2361}
2362
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002363/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002364bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002365 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2366}
2367
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002368bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002369 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002370 if (getOpcode() == Instruction::PtrToInt)
2371 PtrOpTy = getOperand(0)->getType();
2372 else if (getOpcode() == Instruction::IntToPtr)
2373 PtrOpTy = getType();
2374
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002375 Type *IntPtrTy =
2376 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002377
2378 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2379}
2380
2381/// This function determines if a pair of casts can be eliminated and what
2382/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383/// instructions like this:
2384/// * %F = firstOpcode SrcTy %x to MidTy
2385/// * %S = secondOpcode MidTy %F to DstTy
2386/// The function returns a resultOpcode so these two casts can be replaced with:
2387/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2388/// If no such cast is permited, the function returns 0.
2389unsigned CastInst::isEliminableCastPair(
2390 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002391 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2392 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 // Define the 144 possibilities for these two cast instructions. The values
2394 // in this matrix determine what to do in a given situation and select the
2395 // case in the switch below. The rows correspond to firstOp, the columns
2396 // correspond to secondOp. In looking at the table below, keep in mind
2397 // the following cast properties:
2398 //
2399 // Size Compare Source Destination
2400 // Operator Src ? Size Type Sign Type Sign
2401 // -------- ------------ ------------------- ---------------------
2402 // TRUNC > Integer Any Integral Any
2403 // ZEXT < Integral Unsigned Integer Any
2404 // SEXT < Integral Signed Integer Any
2405 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002406 // FPTOSI n/a FloatPt n/a Integral Signed
2407 // UITOFP n/a Integral Unsigned FloatPt n/a
2408 // SITOFP n/a Integral Signed FloatPt n/a
2409 // FPTRUNC > FloatPt n/a FloatPt n/a
2410 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 // PTRTOINT n/a Pointer n/a Integral Unsigned
2412 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002413 // BITCAST = FirstClass n/a FirstClass n/a
2414 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002415 //
2416 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002417 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2418 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002419 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002420 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002421 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002422 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002423 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2425 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002426 // T F F U S F F P I B A -+
2427 // R Z S P P I I T P 2 N T S |
2428 // U E E 2 2 2 2 R E I T C C +- secondOp
2429 // N X X U S F F N X N 2 V V |
2430 // C T T I I P P C T T P T T -+
2431 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002432 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002433 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2434 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2435 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2436 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2437 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002438 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2440 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2441 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2442 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2443 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445
Chris Lattner25eea4d2010-07-12 01:19:22 +00002446 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002447 // merging. However, bitcast of A->B->A are allowed.
2448 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2449 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2450 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2451
2452 // Check if any of the bitcasts convert scalars<->vectors.
2453 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2454 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2455 // Unless we are bitcasing to the original type, disallow optimizations.
2456 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457
2458 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2459 [secondOp-Instruction::CastOpsBegin];
2460 switch (ElimCase) {
2461 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 return 0;
2464 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002465 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466 return firstOp;
2467 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 return secondOp;
2470 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002471 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002472 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002473 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002474 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475 return firstOp;
2476 return 0;
2477 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002478 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002479 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002480 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 return firstOp;
2482 return 0;
2483 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002484 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002485 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 return secondOp;
2488 return 0;
2489 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002490 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002491 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 return secondOp;
2494 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002495 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 // Cannot simplify if address spaces are different!
2497 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2498 return 0;
2499
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002500 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002501 // We can still fold this without knowing the actual sizes as long we
2502 // know that the intermediate pointer is the largest possible
2503 // pointer size.
2504 // FIXME: Is this always true?
2505 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002506 return Instruction::BitCast;
2507
2508 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002509 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002510 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002511 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 if (MidSize >= PtrSize)
2513 return Instruction::BitCast;
2514 return 0;
2515 }
2516 case 8: {
2517 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2518 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2519 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002520 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2521 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 if (SrcSize == DstSize)
2523 return Instruction::BitCast;
2524 else if (SrcSize < DstSize)
2525 return firstOp;
2526 return secondOp;
2527 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002528 case 9:
2529 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 return Instruction::ZExt;
2531 case 10:
2532 // fpext followed by ftrunc is allowed if the bit size returned to is
2533 // the same as the original, in which case its just a bitcast
2534 if (SrcTy == DstTy)
2535 return Instruction::BitCast;
2536 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002537 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002539 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002540 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002541 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002542 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2543 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 if (SrcSize <= PtrSize && SrcSize == DstSize)
2545 return Instruction::BitCast;
2546 return 0;
2547 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002548 case 12: {
2549 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2550 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2551 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2552 return Instruction::AddrSpaceCast;
2553 return Instruction::BitCast;
2554 }
2555 case 13:
2556 // FIXME: this state can be merged with (1), but the following assert
2557 // is useful to check the correcteness of the sequence due to semantic
2558 // change of bitcast.
2559 assert(
2560 SrcTy->isPtrOrPtrVectorTy() &&
2561 MidTy->isPtrOrPtrVectorTy() &&
2562 DstTy->isPtrOrPtrVectorTy() &&
2563 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2564 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2565 "Illegal addrspacecast, bitcast sequence!");
2566 // Allowed, use first cast's opcode
2567 return firstOp;
2568 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002569 // bitcast, addrspacecast -> addrspacecast if the element type of
2570 // bitcast's source is the same as that of addrspacecast's destination.
2571 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2572 return Instruction::AddrSpaceCast;
2573 return 0;
2574
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002575 case 15:
2576 // FIXME: this state can be merged with (1), but the following assert
2577 // is useful to check the correcteness of the sequence due to semantic
2578 // change of bitcast.
2579 assert(
2580 SrcTy->isIntOrIntVectorTy() &&
2581 MidTy->isPtrOrPtrVectorTy() &&
2582 DstTy->isPtrOrPtrVectorTy() &&
2583 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2584 "Illegal inttoptr, bitcast sequence!");
2585 // Allowed, use first cast's opcode
2586 return firstOp;
2587 case 16:
2588 // FIXME: this state can be merged with (2), but the following assert
2589 // is useful to check the correcteness of the sequence due to semantic
2590 // change of bitcast.
2591 assert(
2592 SrcTy->isPtrOrPtrVectorTy() &&
2593 MidTy->isPtrOrPtrVectorTy() &&
2594 DstTy->isIntOrIntVectorTy() &&
2595 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2596 "Illegal bitcast, ptrtoint sequence!");
2597 // Allowed, use second cast's opcode
2598 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002599 case 17:
2600 // (sitofp (zext x)) -> (uitofp x)
2601 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002603 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002605 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606 default:
Craig Topperc514b542012-02-05 22:14:15 +00002607 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609}
2610
Chris Lattner229907c2011-07-18 04:54:35 +00002611CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002613 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614 // Construct and return the appropriate CastInst subclass
2615 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002616 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2617 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2618 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2619 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2620 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2621 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2622 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2623 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2624 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2625 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2626 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2627 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2628 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2629 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631}
2632
Chris Lattner229907c2011-07-18 04:54:35 +00002633CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002634 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002635 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636 // Construct and return the appropriate CastInst subclass
2637 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002638 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2639 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2640 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2641 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2642 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2643 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2644 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2645 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2646 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2647 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2648 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2649 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2650 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2651 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653}
2654
Chris Lattner229907c2011-07-18 04:54:35 +00002655CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002657 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002658 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002659 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2660 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002661}
2662
Chris Lattner229907c2011-07-18 04:54:35 +00002663CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002665 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002666 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002667 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2668 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002669}
2670
Chris Lattner229907c2011-07-18 04:54:35 +00002671CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002672 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002673 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002674 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002675 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2676 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002677}
2678
Chris Lattner229907c2011-07-18 04:54:35 +00002679CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002680 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002681 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002682 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002683 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2684 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002685}
2686
Chris Lattner229907c2011-07-18 04:54:35 +00002687CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002688 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002689 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002690 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002691 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2692 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002693}
2694
Chris Lattner229907c2011-07-18 04:54:35 +00002695CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002696 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002697 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002698 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002699 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2700 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002701}
2702
Chris Lattner229907c2011-07-18 04:54:35 +00002703CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002704 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002705 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002706 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2707 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2708 "Invalid cast");
2709 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002710 assert((!Ty->isVectorTy() ||
2711 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002712 "Invalid cast");
2713
Matt Arsenault065ced92013-07-31 00:17:33 +00002714 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002715 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002716
Matt Arsenault740980e2014-07-14 17:24:35 +00002717 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002718}
2719
2720/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002721CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2722 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002723 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002724 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2725 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002726 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002727 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002728 assert((!Ty->isVectorTy() ||
2729 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002730 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002731
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002732 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002733 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002734
Matt Arsenault740980e2014-07-14 17:24:35 +00002735 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2736}
2737
2738CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2739 Value *S, Type *Ty,
2740 const Twine &Name,
2741 BasicBlock *InsertAtEnd) {
2742 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2743 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2744
2745 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2746 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2747
2748 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2749}
2750
2751CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2752 Value *S, Type *Ty,
2753 const Twine &Name,
2754 Instruction *InsertBefore) {
2755 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2756 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2757
2758 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002759 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2760
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002761 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002762}
2763
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002764CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2765 const Twine &Name,
2766 Instruction *InsertBefore) {
2767 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2768 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2769 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2770 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2771
2772 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2773}
2774
Matt Arsenault740980e2014-07-14 17:24:35 +00002775CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002776 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002777 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002778 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002779 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002780 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2781 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002782 Instruction::CastOps opcode =
2783 (SrcBits == DstBits ? Instruction::BitCast :
2784 (SrcBits > DstBits ? Instruction::Trunc :
2785 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002786 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002787}
2788
Chris Lattner229907c2011-07-18 04:54:35 +00002789CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002790 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002791 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002792 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002793 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002794 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2795 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002796 Instruction::CastOps opcode =
2797 (SrcBits == DstBits ? Instruction::BitCast :
2798 (SrcBits > DstBits ? Instruction::Trunc :
2799 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002800 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002801}
2802
Chris Lattner229907c2011-07-18 04:54:35 +00002803CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002804 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002805 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002806 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002807 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002808 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2809 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002810 Instruction::CastOps opcode =
2811 (SrcBits == DstBits ? Instruction::BitCast :
2812 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002813 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002814}
2815
Chris Lattner229907c2011-07-18 04:54:35 +00002816CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002817 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002818 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002819 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002820 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002821 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2822 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002823 Instruction::CastOps opcode =
2824 (SrcBits == DstBits ? Instruction::BitCast :
2825 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002826 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002827}
2828
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002829// Check whether it is valid to call getCastOpcode for these types.
2830// This routine must be kept in sync with getCastOpcode.
2831bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2832 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2833 return false;
2834
2835 if (SrcTy == DestTy)
2836 return true;
2837
2838 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2839 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2840 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2841 // An element by element cast. Valid if casting the elements is valid.
2842 SrcTy = SrcVecTy->getElementType();
2843 DestTy = DestVecTy->getElementType();
2844 }
2845
2846 // Get the bit sizes, we'll need these
2847 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2848 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2849
2850 // Run through the possibilities ...
2851 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002852 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002853 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002854 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002855 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002856 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002857 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002858 // Casting from something else
2859 return SrcTy->isPointerTy();
2860 }
2861 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2862 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002863 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002864 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002865 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002866 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002867 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002868 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002869 return false;
2870 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002871 if (DestTy->isVectorTy()) // Casting to vector
2872 return DestBits == SrcBits;
2873 if (DestTy->isPointerTy()) { // Casting to pointer
2874 if (SrcTy->isPointerTy()) // Casting from pointer
2875 return true;
2876 return SrcTy->isIntegerTy(); // Casting from integral
2877 }
2878 if (DestTy->isX86_MMXTy()) {
2879 if (SrcTy->isVectorTy())
2880 return DestBits == SrcBits; // 64-bit vector to MMX
2881 return false;
2882 } // Casting to something else
2883 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002884}
2885
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002886bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2887 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2888 return false;
2889
2890 if (SrcTy == DestTy)
2891 return true;
2892
2893 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2894 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2895 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2896 // An element by element cast. Valid if casting the elements is valid.
2897 SrcTy = SrcVecTy->getElementType();
2898 DestTy = DestVecTy->getElementType();
2899 }
2900 }
2901 }
2902
2903 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2904 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2905 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2906 }
2907 }
2908
2909 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2910 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2911
2912 // Could still have vectors of pointers if the number of elements doesn't
2913 // match
2914 if (SrcBits == 0 || DestBits == 0)
2915 return false;
2916
2917 if (SrcBits != DestBits)
2918 return false;
2919
2920 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2921 return false;
2922
2923 return true;
2924}
2925
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002926bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002927 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002928 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2929 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002930 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002931 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2932 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002933 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002934
2935 return isBitCastable(SrcTy, DestTy);
2936}
2937
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002938// Provide a way to get a "cast" where the cast opcode is inferred from the
2939// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002940// logic in the castIsValid function below. This axiom should hold:
2941// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2942// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002944// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002946CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002947 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2948 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949
Duncan Sands55e50902008-01-06 10:12:28 +00002950 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2951 "Only first class types are castable!");
2952
Duncan Sandsa8514532011-05-18 07:13:41 +00002953 if (SrcTy == DestTy)
2954 return BitCast;
2955
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002956 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002957 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2958 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002959 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2960 // An element by element cast. Find the appropriate opcode based on the
2961 // element types.
2962 SrcTy = SrcVecTy->getElementType();
2963 DestTy = DestVecTy->getElementType();
2964 }
2965
2966 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002967 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2968 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002969
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002970 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002971 if (DestTy->isIntegerTy()) { // Casting to integral
2972 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973 if (DestBits < SrcBits)
2974 return Trunc; // int -> smaller int
2975 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002976 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977 return SExt; // signed -> SEXT
2978 else
2979 return ZExt; // unsigned -> ZEXT
2980 } else {
2981 return BitCast; // Same size, No-op cast
2982 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002983 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002984 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002985 return FPToSI; // FP -> sint
2986 else
2987 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002988 } else if (SrcTy->isVectorTy()) {
2989 assert(DestBits == SrcBits &&
2990 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 return BitCast; // Same size, no-op cast
2992 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002993 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002994 "Casting from a value that is not first-class type");
2995 return PtrToInt; // ptr -> int
2996 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002997 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2998 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002999 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000 return SIToFP; // sint -> FP
3001 else
3002 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003003 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003004 if (DestBits < SrcBits) {
3005 return FPTrunc; // FP -> smaller FP
3006 } else if (DestBits > SrcBits) {
3007 return FPExt; // FP -> larger FP
3008 } else {
3009 return BitCast; // same size, no-op cast
3010 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003011 } else if (SrcTy->isVectorTy()) {
3012 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003013 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003014 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003015 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003016 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003017 } else if (DestTy->isVectorTy()) {
3018 assert(DestBits == SrcBits &&
3019 "Illegal cast to vector (wrong type or size)");
3020 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003021 } else if (DestTy->isPointerTy()) {
3022 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003023 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3024 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003025 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003026 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003028 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003029 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003030 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003031 if (SrcTy->isVectorTy()) {
3032 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003033 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003034 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003035 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003037 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038}
3039
3040//===----------------------------------------------------------------------===//
3041// CastInst SubClass Constructors
3042//===----------------------------------------------------------------------===//
3043
3044/// Check that the construction parameters for a CastInst are correct. This
3045/// could be broken out into the separate constructors but it is useful to have
3046/// it in one place and to eliminate the redundant code for getting the sizes
3047/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003048bool
Chris Lattner229907c2011-07-18 04:54:35 +00003049CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050
3051 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003052 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003053
Chris Lattner37bc78a2010-01-26 21:51:43 +00003054 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3055 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 return false;
3057
3058 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003059 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3060 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061
Duncan Sands7f646562011-05-18 09:21:57 +00003062 // If these are vector types, get the lengths of the vectors (using zero for
3063 // scalar types means that checking that vector lengths match also checks that
3064 // scalars are not being converted to vectors or vectors to scalars).
3065 unsigned SrcLength = SrcTy->isVectorTy() ?
3066 cast<VectorType>(SrcTy)->getNumElements() : 0;
3067 unsigned DstLength = DstTy->isVectorTy() ?
3068 cast<VectorType>(DstTy)->getNumElements() : 0;
3069
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003070 // Switch on the opcode provided
3071 switch (op) {
3072 default: return false; // This is an input error
3073 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003074 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3075 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003077 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3078 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003079 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003080 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3081 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003082 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003083 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3084 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003086 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3087 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003088 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003090 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3091 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003092 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003093 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003094 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3095 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003097 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003098 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003099 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3100 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3101 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003102 return SrcTy->getScalarType()->isPointerTy() &&
3103 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003104 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003105 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003106 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003107 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3108 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3109 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003110 return SrcTy->getScalarType()->isIntegerTy() &&
3111 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003112 case Instruction::BitCast: {
3113 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3114 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3115
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003116 // BitCast implies a no-op cast of type only. No bits change.
3117 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003118 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119 return false;
3120
Alp Tokerf907b892013-12-05 05:44:44 +00003121 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003122 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003123 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003124 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3125
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003126 // If both are pointers then the address spaces must match.
3127 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3128 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003129
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003130 // A vector of pointers must have the same number of elements.
3131 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3132 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3133 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3134
3135 return false;
3136 }
3137
3138 return true;
3139 }
3140 case Instruction::AddrSpaceCast: {
3141 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3142 if (!SrcPtrTy)
3143 return false;
3144
3145 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3146 if (!DstPtrTy)
3147 return false;
3148
3149 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3150 return false;
3151
3152 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3153 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3154 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3155
3156 return false;
3157 }
3158
3159 return true;
3160 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161 }
3162}
3163
3164TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003188 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003190 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191}
3192
Jeff Cohencc08c832006-12-02 02:22:01 +00003193SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003194 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003196 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197}
3198
3199FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003200 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003202 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203}
3204
3205FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003206 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003208 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209}
3210
3211FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215}
3216
3217FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003218 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221}
3222
3223UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003224 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003227}
3228
3229UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003230 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233}
3234
3235SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003236 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003238 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239}
3240
3241SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003242 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003244 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245}
3246
3247FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003248 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003249) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003250 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251}
3252
3253FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003254 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003255) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003256 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257}
3258
3259FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003260 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003261) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003262 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003263}
3264
3265FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003266 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003267) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003268 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003269}
3270
3271PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003272 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003273) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003274 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003275}
3276
3277PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003278 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003279) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003280 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003281}
3282
3283IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003284 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003285) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003286 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003287}
3288
3289IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003290 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003291) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003292 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003293}
3294
3295BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003296 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003297) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003298 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003299}
3300
3301BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003302 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003303) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003304 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003305}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003306
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003307AddrSpaceCastInst::AddrSpaceCastInst(
3308 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3309) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3310 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3311}
3312
3313AddrSpaceCastInst::AddrSpaceCastInst(
3314 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3315) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3316 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3317}
3318
Chris Lattnerf16dc002006-09-17 19:29:56 +00003319//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003320// CmpInst Classes
3321//===----------------------------------------------------------------------===//
3322
Craig Topper3186c012012-09-23 02:12:10 +00003323void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003324
Chris Lattner229907c2011-07-18 04:54:35 +00003325CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003326 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003327 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003328 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003329 OperandTraits<CmpInst>::op_begin(this),
3330 OperandTraits<CmpInst>::operands(this),
3331 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003332 Op<0>() = LHS;
3333 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003334 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003335 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003336}
Gabor Greiff6caff662008-05-10 08:32:32 +00003337
Chris Lattner229907c2011-07-18 04:54:35 +00003338CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003339 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003340 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003341 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003342 OperandTraits<CmpInst>::op_begin(this),
3343 OperandTraits<CmpInst>::operands(this),
3344 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003345 Op<0>() = LHS;
3346 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003347 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003348 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003349}
3350
3351CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003352CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003353 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003354 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003355 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003356 if (InsertBefore)
3357 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3358 S1, S2, Name);
3359 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003360 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003361 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003362 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003363
3364 if (InsertBefore)
3365 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3366 S1, S2, Name);
3367 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003368 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003370}
3371
3372CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003373CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003374 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003375 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003376 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3377 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003378 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003379 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3380 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003381}
3382
3383void CmpInst::swapOperands() {
3384 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3385 IC->swapOperands();
3386 else
3387 cast<FCmpInst>(this)->swapOperands();
3388}
3389
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003390bool CmpInst::isCommutative() const {
3391 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003392 return IC->isCommutative();
3393 return cast<FCmpInst>(this)->isCommutative();
3394}
3395
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003396bool CmpInst::isEquality() const {
3397 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003398 return IC->isEquality();
3399 return cast<FCmpInst>(this)->isEquality();
3400}
3401
3402
Dan Gohman4e724382008-05-31 02:47:54 +00003403CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003404 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003405 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003406 case ICMP_EQ: return ICMP_NE;
3407 case ICMP_NE: return ICMP_EQ;
3408 case ICMP_UGT: return ICMP_ULE;
3409 case ICMP_ULT: return ICMP_UGE;
3410 case ICMP_UGE: return ICMP_ULT;
3411 case ICMP_ULE: return ICMP_UGT;
3412 case ICMP_SGT: return ICMP_SLE;
3413 case ICMP_SLT: return ICMP_SGE;
3414 case ICMP_SGE: return ICMP_SLT;
3415 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003416
Dan Gohman4e724382008-05-31 02:47:54 +00003417 case FCMP_OEQ: return FCMP_UNE;
3418 case FCMP_ONE: return FCMP_UEQ;
3419 case FCMP_OGT: return FCMP_ULE;
3420 case FCMP_OLT: return FCMP_UGE;
3421 case FCMP_OGE: return FCMP_ULT;
3422 case FCMP_OLE: return FCMP_UGT;
3423 case FCMP_UEQ: return FCMP_ONE;
3424 case FCMP_UNE: return FCMP_OEQ;
3425 case FCMP_UGT: return FCMP_OLE;
3426 case FCMP_ULT: return FCMP_OGE;
3427 case FCMP_UGE: return FCMP_OLT;
3428 case FCMP_ULE: return FCMP_OGT;
3429 case FCMP_ORD: return FCMP_UNO;
3430 case FCMP_UNO: return FCMP_ORD;
3431 case FCMP_TRUE: return FCMP_FALSE;
3432 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003433 }
3434}
3435
Reid Spencer266e42b2006-12-23 06:05:41 +00003436ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3437 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003438 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003439 case ICMP_EQ: case ICMP_NE:
3440 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3441 return pred;
3442 case ICMP_UGT: return ICMP_SGT;
3443 case ICMP_ULT: return ICMP_SLT;
3444 case ICMP_UGE: return ICMP_SGE;
3445 case ICMP_ULE: return ICMP_SLE;
3446 }
3447}
3448
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003449ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3450 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003451 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003452 case ICMP_EQ: case ICMP_NE:
3453 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3454 return pred;
3455 case ICMP_SGT: return ICMP_UGT;
3456 case ICMP_SLT: return ICMP_ULT;
3457 case ICMP_SGE: return ICMP_UGE;
3458 case ICMP_SLE: return ICMP_ULE;
3459 }
3460}
3461
Reid Spencer0286bc12007-02-28 22:00:54 +00003462/// Initialize a set of values that all satisfy the condition with C.
3463///
3464ConstantRange
3465ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3466 APInt Lower(C);
3467 APInt Upper(C);
3468 uint32_t BitWidth = C.getBitWidth();
3469 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003470 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003471 case ICmpInst::ICMP_EQ: ++Upper; break;
3472 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003473 case ICmpInst::ICMP_ULT:
3474 Lower = APInt::getMinValue(BitWidth);
3475 // Check for an empty-set condition.
3476 if (Lower == Upper)
3477 return ConstantRange(BitWidth, /*isFullSet=*/false);
3478 break;
3479 case ICmpInst::ICMP_SLT:
3480 Lower = APInt::getSignedMinValue(BitWidth);
3481 // Check for an empty-set condition.
3482 if (Lower == Upper)
3483 return ConstantRange(BitWidth, /*isFullSet=*/false);
3484 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003485 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003486 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003487 // Check for an empty-set condition.
3488 if (Lower == Upper)
3489 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003490 break;
3491 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003492 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003493 // Check for an empty-set condition.
3494 if (Lower == Upper)
3495 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003496 break;
3497 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003498 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003499 // Check for a full-set condition.
3500 if (Lower == Upper)
3501 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003502 break;
3503 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003504 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003505 // Check for a full-set condition.
3506 if (Lower == Upper)
3507 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003508 break;
3509 case ICmpInst::ICMP_UGE:
3510 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003511 // Check for a full-set condition.
3512 if (Lower == Upper)
3513 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003514 break;
3515 case ICmpInst::ICMP_SGE:
3516 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003517 // Check for a full-set condition.
3518 if (Lower == Upper)
3519 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003520 break;
3521 }
3522 return ConstantRange(Lower, Upper);
3523}
3524
Dan Gohman4e724382008-05-31 02:47:54 +00003525CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003526 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003527 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003528 case ICMP_EQ: case ICMP_NE:
3529 return pred;
3530 case ICMP_SGT: return ICMP_SLT;
3531 case ICMP_SLT: return ICMP_SGT;
3532 case ICMP_SGE: return ICMP_SLE;
3533 case ICMP_SLE: return ICMP_SGE;
3534 case ICMP_UGT: return ICMP_ULT;
3535 case ICMP_ULT: return ICMP_UGT;
3536 case ICMP_UGE: return ICMP_ULE;
3537 case ICMP_ULE: return ICMP_UGE;
3538
Reid Spencerd9436b62006-11-20 01:22:35 +00003539 case FCMP_FALSE: case FCMP_TRUE:
3540 case FCMP_OEQ: case FCMP_ONE:
3541 case FCMP_UEQ: case FCMP_UNE:
3542 case FCMP_ORD: case FCMP_UNO:
3543 return pred;
3544 case FCMP_OGT: return FCMP_OLT;
3545 case FCMP_OLT: return FCMP_OGT;
3546 case FCMP_OGE: return FCMP_OLE;
3547 case FCMP_OLE: return FCMP_OGE;
3548 case FCMP_UGT: return FCMP_ULT;
3549 case FCMP_ULT: return FCMP_UGT;
3550 case FCMP_UGE: return FCMP_ULE;
3551 case FCMP_ULE: return FCMP_UGE;
3552 }
3553}
3554
Reid Spencer266e42b2006-12-23 06:05:41 +00003555bool CmpInst::isUnsigned(unsigned short predicate) {
3556 switch (predicate) {
3557 default: return false;
3558 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3559 case ICmpInst::ICMP_UGE: return true;
3560 }
3561}
3562
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003563bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003564 switch (predicate) {
3565 default: return false;
3566 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3567 case ICmpInst::ICMP_SGE: return true;
3568 }
3569}
3570
3571bool CmpInst::isOrdered(unsigned short predicate) {
3572 switch (predicate) {
3573 default: return false;
3574 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3575 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3576 case FCmpInst::FCMP_ORD: return true;
3577 }
3578}
3579
3580bool CmpInst::isUnordered(unsigned short predicate) {
3581 switch (predicate) {
3582 default: return false;
3583 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3584 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3585 case FCmpInst::FCMP_UNO: return true;
3586 }
3587}
3588
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003589bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3590 switch(predicate) {
3591 default: return false;
3592 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3593 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3594 }
3595}
3596
3597bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3598 switch(predicate) {
3599 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3600 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3601 default: return false;
3602 }
3603}
3604
3605
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003606//===----------------------------------------------------------------------===//
3607// SwitchInst Implementation
3608//===----------------------------------------------------------------------===//
3609
Chris Lattnerbaf00152010-11-17 05:41:46 +00003610void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3611 assert(Value && Default && NumReserved);
3612 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003613 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003614 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003615
Pete Coopereb31b682015-05-21 22:48:54 +00003616 Op<0>() = Value;
3617 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003618}
3619
Chris Lattner2195fc42007-02-24 00:55:48 +00003620/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3621/// switch on and a default destination. The number of additional cases can
3622/// be specified here to make memory allocation more efficient. This
3623/// constructor can also autoinsert before another instruction.
3624SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3625 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003626 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003627 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003628 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003629}
3630
3631/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3632/// switch on and a default destination. The number of additional cases can
3633/// be specified here to make memory allocation more efficient. This
3634/// constructor also autoinserts at the end of the specified BasicBlock.
3635SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3636 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003637 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003638 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003639 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003640}
3641
Misha Brukmanb1c93172005-04-21 23:48:37 +00003642SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003643 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003644 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003645 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003646 Use *OL = getOperandList();
3647 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003648 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003649 OL[i] = InOL[i];
3650 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003651 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003652 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003653}
3654
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003655
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003656/// addCase - Add an entry to the switch instruction...
3657///
Chris Lattner47ac1872005-02-24 05:32:09 +00003658void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003659 unsigned NewCaseIdx = getNumCases();
3660 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003661 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003662 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003663 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003664 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003665 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003666 CaseIt Case(this, NewCaseIdx);
3667 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003668 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003669}
3670
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003671/// removeCase - This method removes the specified case and its successor
3672/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003673void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003674 unsigned idx = i.getCaseIndex();
3675
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003676 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003677
3678 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003679 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003680
Jay Foad14277722011-02-01 09:22:34 +00003681 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003682 if (2 + (idx + 1) * 2 != NumOps) {
3683 OL[2 + idx * 2] = OL[NumOps - 2];
3684 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003685 }
3686
3687 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003688 OL[NumOps-2].set(nullptr);
3689 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003690 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003691}
3692
Jay Foade98f29d2011-04-01 08:00:58 +00003693/// growOperands - grow operands - This grows the operand list in response
3694/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003695///
Jay Foade98f29d2011-04-01 08:00:58 +00003696void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003697 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003698 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003699
3700 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003701 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003702}
3703
3704
3705BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3706 return getSuccessor(idx);
3707}
3708unsigned SwitchInst::getNumSuccessorsV() const {
3709 return getNumSuccessors();
3710}
3711void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3712 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003713}
Chris Lattnerf22be932004-10-15 23:52:53 +00003714
Chris Lattner3ed871f2009-10-27 19:13:16 +00003715//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003716// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003717//===----------------------------------------------------------------------===//
3718
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003719void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003720 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003721 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003722 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003723 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003724 allocHungoffUses(ReservedSpace);
3725
Pete Coopereb31b682015-05-21 22:48:54 +00003726 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003727}
3728
3729
Jay Foade98f29d2011-04-01 08:00:58 +00003730/// growOperands - grow operands - This grows the operand list in response
3731/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003732///
Jay Foade98f29d2011-04-01 08:00:58 +00003733void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003734 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003735 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003736
3737 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003738 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739}
3740
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003741IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3742 Instruction *InsertBefore)
3743: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003744 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003745 init(Address, NumCases);
3746}
3747
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003748IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3749 BasicBlock *InsertAtEnd)
3750: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003751 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003752 init(Address, NumCases);
3753}
3754
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003755IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003756 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3757 nullptr, IBI.getNumOperands()) {
3758 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003759 Use *OL = getOperandList();
3760 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003761 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3762 OL[i] = InOL[i];
3763 SubclassOptionalData = IBI.SubclassOptionalData;
3764}
3765
Chris Lattner3ed871f2009-10-27 19:13:16 +00003766/// addDestination - Add a destination.
3767///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003768void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003769 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003770 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003771 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772 // Initialize some new operands.
3773 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003774 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003775 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003776}
3777
3778/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003779/// indirectbr instruction.
3780void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003781 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3782
3783 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003784 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003785
3786 // Replace this value with the last one.
3787 OL[idx+1] = OL[NumOps-1];
3788
3789 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003790 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003791 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003792}
3793
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003794BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003795 return getSuccessor(idx);
3796}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003797unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798 return getNumSuccessors();
3799}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003800void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003801 setSuccessor(idx, B);
3802}
3803
3804//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003805// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003806//===----------------------------------------------------------------------===//
3807
Chris Lattnerf22be932004-10-15 23:52:53 +00003808// Define these methods here so vtables don't get emitted into every translation
3809// unit that uses these classes.
3810
Pete Cooper75403d72015-06-24 20:22:23 +00003811GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003812 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003813}
3814
Pete Cooper75403d72015-06-24 20:22:23 +00003815BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003816 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003817}
3818
Pete Cooper75403d72015-06-24 20:22:23 +00003819FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003820 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003821}
3822
Pete Cooper75403d72015-06-24 20:22:23 +00003823ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003824 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003825}
3826
Pete Cooper75403d72015-06-24 20:22:23 +00003827ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003828 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003829}
3830
Pete Cooper75403d72015-06-24 20:22:23 +00003831InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003832 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003833}
3834
Pete Cooper75403d72015-06-24 20:22:23 +00003835AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003836 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3837 (Value *)getOperand(0), getAlignment());
3838 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3839 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003840}
3841
Pete Cooper75403d72015-06-24 20:22:23 +00003842LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003843 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3844 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003845}
3846
Pete Cooper75403d72015-06-24 20:22:23 +00003847StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003848 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003849 getAlignment(), getOrdering(), getSynchScope());
3850
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003851}
3852
Pete Cooper75403d72015-06-24 20:22:23 +00003853AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003854 AtomicCmpXchgInst *Result =
3855 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003856 getSuccessOrdering(), getFailureOrdering(),
3857 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003858 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003859 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003860 return Result;
3861}
3862
Pete Cooper75403d72015-06-24 20:22:23 +00003863AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003864 AtomicRMWInst *Result =
3865 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3866 getOrdering(), getSynchScope());
3867 Result->setVolatile(isVolatile());
3868 return Result;
3869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003872 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3873}
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003897}
3898
Pete Cooper75403d72015-06-24 20:22:23 +00003899SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003900 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003901}
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003904 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905}
3906
Pete Cooper75403d72015-06-24 20:22:23 +00003907FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003908 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909}
3910
Pete Cooper75403d72015-06-24 20:22:23 +00003911PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003912 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003913}
3914
Pete Cooper75403d72015-06-24 20:22:23 +00003915IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003916 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003917}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003918
Pete Cooper75403d72015-06-24 20:22:23 +00003919BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003920 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003921}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003922
Pete Cooper75403d72015-06-24 20:22:23 +00003923AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003924 return new AddrSpaceCastInst(getOperand(0), getType());
3925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003928 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003929}
3930
Pete Cooper75403d72015-06-24 20:22:23 +00003931SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003932 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003933}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003934
Pete Cooper75403d72015-06-24 20:22:23 +00003935VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003936 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003937}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003938
Pete Cooper75403d72015-06-24 20:22:23 +00003939ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003940 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003941}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003942
Pete Cooper75403d72015-06-24 20:22:23 +00003943InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003944 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945}
3946
Pete Cooper75403d72015-06-24 20:22:23 +00003947ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003948 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003949}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003950
Pete Cooper75403d72015-06-24 20:22:23 +00003951PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003952
Pete Cooper75403d72015-06-24 20:22:23 +00003953LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003954 return new LandingPadInst(*this);
3955}
3956
Pete Cooper75403d72015-06-24 20:22:23 +00003957ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003958 return new(getNumOperands()) ReturnInst(*this);
3959}
3960
Pete Cooper75403d72015-06-24 20:22:23 +00003961BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003962 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003963}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003964
Pete Cooper75403d72015-06-24 20:22:23 +00003965SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003966
Pete Cooper75403d72015-06-24 20:22:23 +00003967IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003968 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003969}
3970
Pete Cooper75403d72015-06-24 20:22:23 +00003971InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003972 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003973}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003974
Pete Cooper75403d72015-06-24 20:22:23 +00003975ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003976
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003977CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
3978 return new (getNumOperands()) CleanupEndPadInst(*this);
3979}
3980
David Majnemer654e1302015-07-31 17:58:14 +00003981CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3982 return new (getNumOperands()) CleanupReturnInst(*this);
3983}
3984
3985CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
3986 return new (getNumOperands()) CatchEndPadInst(*this);
3987}
3988
3989CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003990 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003991}
3992
3993CatchPadInst *CatchPadInst::cloneImpl() const {
3994 return new (getNumOperands()) CatchPadInst(*this);
3995}
3996
3997TerminatePadInst *TerminatePadInst::cloneImpl() const {
3998 return new (getNumOperands()) TerminatePadInst(*this);
3999}
4000
4001CleanupPadInst *CleanupPadInst::cloneImpl() const {
4002 return new (getNumOperands()) CleanupPadInst(*this);
4003}
4004
Pete Cooper75403d72015-06-24 20:22:23 +00004005UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004006 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004007 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004008}