blob: 855101b4ac8b593721c50c1eb94dee8dc3735059 [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;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000566
567 // Operand bundles override attributes on the called function, but don't
568 // override attributes directly present on the invoke instruction.
569 if (isFnAttrDisallowedByOpBundle(A))
570 return false;
571
Bill Wendling375eb1f2012-10-09 00:28:54 +0000572 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000573 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000574 return false;
575}
576
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000577bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000578 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000579 return true;
580 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000581 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000582 return false;
583}
584
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000585void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000586 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000587 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000588 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000589}
590
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000591void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000592 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000593 AttrBuilder B(attr);
594 PAL = PAL.removeAttributes(getContext(), i,
595 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000596 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000597}
598
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000599void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
600 AttributeSet PAL = getAttributes();
601 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
602 setAttributes(PAL);
603}
604
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000605void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
606 AttributeSet PAL = getAttributes();
607 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
608 setAttributes(PAL);
609}
610
Bill Wendlingfae14752011-08-12 20:24:12 +0000611LandingPadInst *InvokeInst::getLandingPadInst() const {
612 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
613}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000614
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000615//===----------------------------------------------------------------------===//
616// ReturnInst Implementation
617//===----------------------------------------------------------------------===//
618
Chris Lattner2195fc42007-02-24 00:55:48 +0000619ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000620 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 OperandTraits<ReturnInst>::op_end(this) -
622 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000623 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000624 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000625 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000626 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000627}
628
Owen Anderson55f1c092009-08-13 21:58:54 +0000629ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
630 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
632 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000633 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000634 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
Owen Anderson55f1c092009-08-13 21:58:54 +0000636ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
637 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
639 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000640 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000641 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000642}
Owen Anderson55f1c092009-08-13 21:58:54 +0000643ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
644 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000645 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000646}
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648unsigned ReturnInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
650}
651
Devang Patelae682fb2008-02-26 17:56:20 +0000652/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
653/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000655 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656}
657
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000659 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660}
661
Devang Patel59643e52008-02-23 00:35:18 +0000662ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000663}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000666// ResumeInst Implementation
667//===----------------------------------------------------------------------===//
668
669ResumeInst::ResumeInst(const ResumeInst &RI)
670 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
671 OperandTraits<ResumeInst>::op_begin(this), 1) {
672 Op<0>() = RI.Op<0>();
673}
674
675ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
676 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
677 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
678 Op<0>() = Exn;
679}
680
681ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
682 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
683 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
684 Op<0>() = Exn;
685}
686
687unsigned ResumeInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690
691void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
692 llvm_unreachable("ResumeInst has no successors!");
693}
694
695BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
696 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000697}
698
699//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000700// CleanupEndPadInst Implementation
701//===----------------------------------------------------------------------===//
702
703CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
704 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
705 OperandTraits<CleanupEndPadInst>::op_end(this) -
706 CEPI.getNumOperands(),
707 CEPI.getNumOperands()) {
708 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
709 setCleanupPad(CEPI.getCleanupPad());
710 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
711 setUnwindDest(UnwindDest);
712}
713
714void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
715 setCleanupPad(CleanupPad);
716 if (UnwindBB) {
717 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
718 setUnwindDest(UnwindBB);
719 }
720}
721
722CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
723 BasicBlock *UnwindBB, unsigned Values,
724 Instruction *InsertBefore)
725 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
726 Instruction::CleanupEndPad,
727 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
728 Values, InsertBefore) {
729 init(CleanupPad, UnwindBB);
730}
731
732CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
733 BasicBlock *UnwindBB, unsigned Values,
734 BasicBlock *InsertAtEnd)
735 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
736 Instruction::CleanupEndPad,
737 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
738 Values, InsertAtEnd) {
739 init(CleanupPad, UnwindBB);
740}
741
742BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
743 assert(Idx == 0);
744 return getUnwindDest();
745}
746unsigned CleanupEndPadInst::getNumSuccessorsV() const {
747 return getNumSuccessors();
748}
749void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
750 assert(Idx == 0);
751 setUnwindDest(B);
752}
753
754//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000755// CleanupReturnInst Implementation
756//===----------------------------------------------------------------------===//
757
758CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
759 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
760 OperandTraits<CleanupReturnInst>::op_end(this) -
761 CRI.getNumOperands(),
762 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000763 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000764 Op<-1>() = CRI.Op<-1>();
765 if (CRI.hasUnwindDest())
766 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000767}
768
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000769void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000770 if (UnwindBB)
771 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000772
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000773 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000774 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000775 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000776}
777
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000778CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000779 BasicBlock *UnwindBB, unsigned Values,
780 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000781 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
782 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000783 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
784 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000785 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000786}
787
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000788CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000789 BasicBlock *UnwindBB, unsigned Values,
790 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000791 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
792 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000793 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
794 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000795 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000796}
797
798BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
799 assert(Idx == 0);
800 return getUnwindDest();
801}
802unsigned CleanupReturnInst::getNumSuccessorsV() const {
803 return getNumSuccessors();
804}
805void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
806 assert(Idx == 0);
807 setUnwindDest(B);
808}
809
810//===----------------------------------------------------------------------===//
811// CatchEndPadInst Implementation
812//===----------------------------------------------------------------------===//
813
814CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
815 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
816 OperandTraits<CatchEndPadInst>::op_end(this) -
817 CRI.getNumOperands(),
818 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000819 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000820 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
821 setUnwindDest(UnwindDest);
822}
823
824void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000825 if (UnwindBB) {
826 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
827 setUnwindDest(UnwindBB);
828 }
829}
830
831CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000832 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000833 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
834 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
835 Values, InsertBefore) {
836 init(UnwindBB);
837}
838
839CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000840 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000841 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
842 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
843 Values, InsertAtEnd) {
844 init(UnwindBB);
845}
846
847BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
848 assert(Idx == 0);
849 return getUnwindDest();
850}
851unsigned CatchEndPadInst::getNumSuccessorsV() const {
852 return getNumSuccessors();
853}
854void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
855 assert(Idx == 0);
856 setUnwindDest(B);
857}
858
859//===----------------------------------------------------------------------===//
860// CatchReturnInst Implementation
861//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000862void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
863 Op<0>() = CatchPad;
864 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000865}
David Majnemer654e1302015-07-31 17:58:14 +0000866
867CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
868 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000869 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
870 Op<0>() = CRI.Op<0>();
871 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000872}
873
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000874CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000875 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000876 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000877 OperandTraits<CatchReturnInst>::op_begin(this), 2,
878 InsertBefore) {
879 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000880}
881
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000882CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000883 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000884 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000885 OperandTraits<CatchReturnInst>::op_begin(this), 2,
886 InsertAtEnd) {
887 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000888}
889
890BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000891 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000892 return getSuccessor();
893}
894unsigned CatchReturnInst::getNumSuccessorsV() const {
895 return getNumSuccessors();
896}
897void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000898 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000899 setSuccessor(B);
900}
901
902//===----------------------------------------------------------------------===//
903// CatchPadInst Implementation
904//===----------------------------------------------------------------------===//
905void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000906 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000907 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
908 Op<-2>() = IfNormal;
909 Op<-1>() = IfException;
910 std::copy(Args.begin(), Args.end(), op_begin());
911 setName(NameStr);
912}
913
914CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
915 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
916 OperandTraits<CatchPadInst>::op_end(this) -
917 CPI.getNumOperands(),
918 CPI.getNumOperands()) {
919 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
920}
921
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000922CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
923 ArrayRef<Value *> Args, unsigned Values,
924 const Twine &NameStr, Instruction *InsertBefore)
925 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
926 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000927 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
928 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000929 init(IfNormal, IfException, Args, NameStr);
930}
931
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000932CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
933 ArrayRef<Value *> Args, unsigned Values,
934 const Twine &NameStr, BasicBlock *InsertAtEnd)
935 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
936 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000937 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
938 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000939 init(IfNormal, IfException, Args, NameStr);
940}
941
942BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
943 return getSuccessor(Idx);
944}
945unsigned CatchPadInst::getNumSuccessorsV() const {
946 return getNumSuccessors();
947}
948void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
949 return setSuccessor(Idx, B);
950}
951
952//===----------------------------------------------------------------------===//
953// TerminatePadInst Implementation
954//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000955void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000956 if (BB)
957 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
958 if (BB)
959 Op<-1>() = BB;
960 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +0000961}
962
963TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
964 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
965 OperandTraits<TerminatePadInst>::op_end(this) -
966 TPI.getNumOperands(),
967 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000968 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000969 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
970}
971
972TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000973 ArrayRef<Value *> Args, unsigned Values,
974 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000975 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
976 OperandTraits<TerminatePadInst>::op_end(this) - Values,
977 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000978 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000979}
980
981TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000982 ArrayRef<Value *> Args, unsigned Values,
983 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000984 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
985 OperandTraits<TerminatePadInst>::op_end(this) - Values,
986 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000987 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000988}
989
990BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
991 assert(Idx == 0);
992 return getUnwindDest();
993}
994unsigned TerminatePadInst::getNumSuccessorsV() const {
995 return getNumSuccessors();
996}
997void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
998 assert(Idx == 0);
999 return setUnwindDest(B);
1000}
1001
1002//===----------------------------------------------------------------------===//
1003// CleanupPadInst Implementation
1004//===----------------------------------------------------------------------===//
1005void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1006 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1007 std::copy(Args.begin(), Args.end(), op_begin());
1008 setName(NameStr);
1009}
1010
1011CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1012 : Instruction(CPI.getType(), Instruction::CleanupPad,
1013 OperandTraits<CleanupPadInst>::op_end(this) -
1014 CPI.getNumOperands(),
1015 CPI.getNumOperands()) {
1016 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
1017}
1018
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001019CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001020 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001021 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001022 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1023 Args.size(), InsertBefore) {
1024 init(Args, NameStr);
1025}
1026
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001027CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001028 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001029 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001030 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1031 Args.size(), InsertAtEnd) {
1032 init(Args, NameStr);
1033}
1034
1035//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001036// UnreachableInst Implementation
1037//===----------------------------------------------------------------------===//
1038
Owen Anderson55f1c092009-08-13 21:58:54 +00001039UnreachableInst::UnreachableInst(LLVMContext &Context,
1040 Instruction *InsertBefore)
1041 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001042 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001043}
Owen Anderson55f1c092009-08-13 21:58:54 +00001044UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1045 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001046 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001047}
1048
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001049unsigned UnreachableInst::getNumSuccessorsV() const {
1050 return getNumSuccessors();
1051}
1052
1053void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001054 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001055}
1056
1057BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001058 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001059}
1060
1061//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062// BranchInst Implementation
1063//===----------------------------------------------------------------------===//
1064
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001065void BranchInst::AssertOK() {
1066 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001067 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069}
1070
Chris Lattner2195fc42007-02-24 00:55:48 +00001071BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001072 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001073 OperandTraits<BranchInst>::op_end(this) - 1,
1074 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001075 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001076 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001077}
1078BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1079 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001080 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001081 OperandTraits<BranchInst>::op_end(this) - 3,
1082 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001083 Op<-1>() = IfTrue;
1084 Op<-2>() = IfFalse;
1085 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001086#ifndef NDEBUG
1087 AssertOK();
1088#endif
1089}
1090
1091BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001092 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001093 OperandTraits<BranchInst>::op_end(this) - 1,
1094 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001095 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001096 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001097}
1098
1099BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1100 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001101 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 OperandTraits<BranchInst>::op_end(this) - 3,
1103 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001104 Op<-1>() = IfTrue;
1105 Op<-2>() = IfFalse;
1106 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001107#ifndef NDEBUG
1108 AssertOK();
1109#endif
1110}
1111
1112
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001114 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1116 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001117 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001118 if (BI.getNumOperands() != 1) {
1119 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001120 Op<-3>() = BI.Op<-3>();
1121 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001123 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001124}
1125
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001126void BranchInst::swapSuccessors() {
1127 assert(isConditional() &&
1128 "Cannot swap successors of an unconditional branch");
1129 Op<-1>().swap(Op<-2>());
1130
1131 // Update profile metadata if present and it matches our structural
1132 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001133 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001134 if (!ProfileData || ProfileData->getNumOperands() != 3)
1135 return;
1136
1137 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001138 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1139 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001140 setMetadata(LLVMContext::MD_prof,
1141 MDNode::get(ProfileData->getContext(), Ops));
1142}
1143
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001144BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1145 return getSuccessor(idx);
1146}
1147unsigned BranchInst::getNumSuccessorsV() const {
1148 return getNumSuccessors();
1149}
1150void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1151 setSuccessor(idx, B);
1152}
1153
1154
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001156// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001157//===----------------------------------------------------------------------===//
1158
Owen Andersonb6b25302009-07-14 23:09:55 +00001159static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001160 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001161 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001162 else {
1163 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001164 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001165 assert(Amt->getType()->isIntegerTy() &&
1166 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001167 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001168 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001169}
1170
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001171AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1172 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001173
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001174AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1175 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001176
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001177AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001178 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001179 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001180
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001181AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001182 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001183 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001184
Chris Lattner229907c2011-07-18 04:54:35 +00001185AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001186 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001187 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1188 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1189 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001190 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001191 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001192 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193}
1194
Chris Lattner229907c2011-07-18 04:54:35 +00001195AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001196 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001197 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1198 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1199 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001200 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001201 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001202 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001203}
1204
Gordon Henriksen14a55692007-12-10 02:14:30 +00001205// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001206AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001207}
1208
Victor Hernandez8acf2952009-10-23 21:09:37 +00001209void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001210 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001211 assert(Align <= MaximumAlignment &&
1212 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001213 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1214 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001215 assert(getAlignment() == Align && "Alignment representation error!");
1216}
1217
Victor Hernandez8acf2952009-10-23 21:09:37 +00001218bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001219 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001220 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001221 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001222}
1223
Chris Lattner8b291e62008-11-26 02:54:17 +00001224/// isStaticAlloca - Return true if this alloca is in the entry block of the
1225/// function and is a constant size. If so, the code generator will fold it
1226/// into the prolog/epilog code, so it is basically free.
1227bool AllocaInst::isStaticAlloca() const {
1228 // Must be constant size.
1229 if (!isa<ConstantInt>(getArraySize())) return false;
1230
1231 // Must be in the entry block.
1232 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001233 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001234}
1235
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001237// LoadInst Implementation
1238//===----------------------------------------------------------------------===//
1239
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001240void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001241 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001242 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001243 assert(!(isAtomic() && getAlignment() == 0) &&
1244 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001245}
1246
Daniel Dunbar4975db62009-07-25 04:41:11 +00001247LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001248 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001249
Daniel Dunbar4975db62009-07-25 04:41:11 +00001250LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001251 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252
David Blaikie0c28fd72015-05-20 21:46:30 +00001253LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001254 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001255 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001256
1257LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1258 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001259 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001260
David Blaikieb7a029872015-04-17 19:56:21 +00001261LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001262 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001263 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001264 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001266LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001267 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001268 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001269}
1270
David Blaikie15d9a4c2015-04-06 20:59:48 +00001271LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001272 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001273 SynchronizationScope SynchScope, Instruction *InsertBef)
1274 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001275 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001276 setVolatile(isVolatile);
1277 setAlignment(Align);
1278 setAtomic(Order, SynchScope);
1279 AssertOK();
1280 setName(Name);
1281}
1282
1283LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1284 unsigned Align, AtomicOrdering Order,
1285 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001286 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001287 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001288 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001289 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001290 setAlignment(Align);
1291 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001292 AssertOK();
1293 setName(Name);
1294}
1295
Daniel Dunbar27096822009-08-11 18:11:15 +00001296LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1297 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1298 Load, Ptr, InsertBef) {
1299 setVolatile(false);
1300 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001301 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001302 AssertOK();
1303 if (Name && Name[0]) setName(Name);
1304}
1305
1306LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1307 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1308 Load, Ptr, InsertAE) {
1309 setVolatile(false);
1310 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001311 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001312 AssertOK();
1313 if (Name && Name[0]) setName(Name);
1314}
1315
David Blaikie0c28fd72015-05-20 21:46:30 +00001316LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001317 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001318 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1319 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001320 setVolatile(isVolatile);
1321 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001322 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001323 AssertOK();
1324 if (Name && Name[0]) setName(Name);
1325}
1326
1327LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1328 BasicBlock *InsertAE)
1329 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1330 Load, Ptr, InsertAE) {
1331 setVolatile(isVolatile);
1332 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001333 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001334 AssertOK();
1335 if (Name && Name[0]) setName(Name);
1336}
1337
Christopher Lamb84485702007-04-22 19:24:39 +00001338void LoadInst::setAlignment(unsigned Align) {
1339 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001340 assert(Align <= MaximumAlignment &&
1341 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001342 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001343 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001344 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001345}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001346
1347//===----------------------------------------------------------------------===//
1348// StoreInst Implementation
1349//===----------------------------------------------------------------------===//
1350
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001351void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001352 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001353 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001354 "Ptr must have pointer type!");
1355 assert(getOperand(0)->getType() ==
1356 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001357 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001358 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001359 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001360}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001361
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001362StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001363 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001364
1365StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001366 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001367
Misha Brukmanb1c93172005-04-21 23:48:37 +00001368StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001369 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001370 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001371
1372StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001373 BasicBlock *InsertAtEnd)
1374 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1375
1376StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1377 Instruction *InsertBefore)
1378 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1379 InsertBefore) {}
1380
1381StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1382 BasicBlock *InsertAtEnd)
1383 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1384 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001385
Misha Brukmanb1c93172005-04-21 23:48:37 +00001386StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001387 unsigned Align, AtomicOrdering Order,
1388 SynchronizationScope SynchScope,
1389 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001390 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001391 OperandTraits<StoreInst>::op_begin(this),
1392 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001393 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001394 Op<0>() = val;
1395 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001396 setVolatile(isVolatile);
1397 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001398 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001399 AssertOK();
1400}
1401
1402StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001403 unsigned Align, AtomicOrdering Order,
1404 SynchronizationScope SynchScope,
1405 BasicBlock *InsertAtEnd)
1406 : Instruction(Type::getVoidTy(val->getContext()), Store,
1407 OperandTraits<StoreInst>::op_begin(this),
1408 OperandTraits<StoreInst>::operands(this),
1409 InsertAtEnd) {
1410 Op<0>() = val;
1411 Op<1>() = addr;
1412 setVolatile(isVolatile);
1413 setAlignment(Align);
1414 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001415 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001416}
1417
Christopher Lamb84485702007-04-22 19:24:39 +00001418void StoreInst::setAlignment(unsigned Align) {
1419 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001420 assert(Align <= MaximumAlignment &&
1421 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001422 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001423 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001424 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001425}
1426
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001427//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001428// AtomicCmpXchgInst Implementation
1429//===----------------------------------------------------------------------===//
1430
1431void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001432 AtomicOrdering SuccessOrdering,
1433 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001434 SynchronizationScope SynchScope) {
1435 Op<0>() = Ptr;
1436 Op<1>() = Cmp;
1437 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001438 setSuccessOrdering(SuccessOrdering);
1439 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001440 setSynchScope(SynchScope);
1441
1442 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1443 "All operands must be non-null!");
1444 assert(getOperand(0)->getType()->isPointerTy() &&
1445 "Ptr must have pointer type!");
1446 assert(getOperand(1)->getType() ==
1447 cast<PointerType>(getOperand(0)->getType())->getElementType()
1448 && "Ptr must be a pointer to Cmp type!");
1449 assert(getOperand(2)->getType() ==
1450 cast<PointerType>(getOperand(0)->getType())->getElementType()
1451 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001452 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001453 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001454 assert(FailureOrdering != NotAtomic &&
1455 "AtomicCmpXchg instructions must be atomic!");
1456 assert(SuccessOrdering >= FailureOrdering &&
1457 "AtomicCmpXchg success ordering must be at least as strong as fail");
1458 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1459 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001460}
1461
1462AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001463 AtomicOrdering SuccessOrdering,
1464 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001465 SynchronizationScope SynchScope,
1466 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001467 : Instruction(
1468 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1469 nullptr),
1470 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1471 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001472 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001473}
1474
1475AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001476 AtomicOrdering SuccessOrdering,
1477 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001478 SynchronizationScope SynchScope,
1479 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001480 : Instruction(
1481 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1482 nullptr),
1483 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1484 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001485 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001486}
Tim Northover420a2162014-06-13 14:24:07 +00001487
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001488//===----------------------------------------------------------------------===//
1489// AtomicRMWInst Implementation
1490//===----------------------------------------------------------------------===//
1491
1492void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1493 AtomicOrdering Ordering,
1494 SynchronizationScope SynchScope) {
1495 Op<0>() = Ptr;
1496 Op<1>() = Val;
1497 setOperation(Operation);
1498 setOrdering(Ordering);
1499 setSynchScope(SynchScope);
1500
1501 assert(getOperand(0) && getOperand(1) &&
1502 "All operands must be non-null!");
1503 assert(getOperand(0)->getType()->isPointerTy() &&
1504 "Ptr must have pointer type!");
1505 assert(getOperand(1)->getType() ==
1506 cast<PointerType>(getOperand(0)->getType())->getElementType()
1507 && "Ptr must be a pointer to Val type!");
1508 assert(Ordering != NotAtomic &&
1509 "AtomicRMW instructions must be atomic!");
1510}
1511
1512AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1513 AtomicOrdering Ordering,
1514 SynchronizationScope SynchScope,
1515 Instruction *InsertBefore)
1516 : Instruction(Val->getType(), AtomicRMW,
1517 OperandTraits<AtomicRMWInst>::op_begin(this),
1518 OperandTraits<AtomicRMWInst>::operands(this),
1519 InsertBefore) {
1520 Init(Operation, Ptr, Val, Ordering, SynchScope);
1521}
1522
1523AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1524 AtomicOrdering Ordering,
1525 SynchronizationScope SynchScope,
1526 BasicBlock *InsertAtEnd)
1527 : Instruction(Val->getType(), AtomicRMW,
1528 OperandTraits<AtomicRMWInst>::op_begin(this),
1529 OperandTraits<AtomicRMWInst>::operands(this),
1530 InsertAtEnd) {
1531 Init(Operation, Ptr, Val, Ordering, SynchScope);
1532}
1533
1534//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001535// FenceInst Implementation
1536//===----------------------------------------------------------------------===//
1537
1538FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1539 SynchronizationScope SynchScope,
1540 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001541 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001542 setOrdering(Ordering);
1543 setSynchScope(SynchScope);
1544}
1545
1546FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1547 SynchronizationScope SynchScope,
1548 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001549 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001550 setOrdering(Ordering);
1551 setSynchScope(SynchScope);
1552}
1553
1554//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555// GetElementPtrInst Implementation
1556//===----------------------------------------------------------------------===//
1557
Jay Foadd1b78492011-07-25 09:48:08 +00001558void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001559 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001560 assert(getNumOperands() == 1 + IdxList.size() &&
1561 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001562 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001563 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001564 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001565}
1566
Gabor Greiff6caff662008-05-10 08:32:32 +00001567GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001568 : Instruction(GEPI.getType(), GetElementPtr,
1569 OperandTraits<GetElementPtrInst>::op_end(this) -
1570 GEPI.getNumOperands(),
1571 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001572 SourceElementType(GEPI.SourceElementType),
1573 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001574 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001575 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001576}
1577
Chris Lattner6090a422009-03-09 04:46:40 +00001578/// getIndexedType - Returns the type of the element that would be accessed with
1579/// a gep instruction with the specified parameters.
1580///
1581/// The Idxs pointer should point to a continuous piece of memory containing the
1582/// indices, either as Value* or uint64_t.
1583///
1584/// A null type is returned if the indices are invalid for the specified
1585/// pointer type.
1586///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001587template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001588static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001589 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001590 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001591 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001592
Chris Lattner6090a422009-03-09 04:46:40 +00001593 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001594 // it cannot be 'stepped over'.
1595 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001596 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001597
Dan Gohman1ecaf452008-05-31 00:58:22 +00001598 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001599 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001600 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001601 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001602 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001603 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001604 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001605 }
Craig Topperc6207612014-04-09 06:08:46 +00001606 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001607}
1608
David Blaikied288fb82015-03-30 21:41:43 +00001609Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1610 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001611}
1612
David Blaikied288fb82015-03-30 21:41:43 +00001613Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001614 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001615 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001616}
1617
David Blaikied288fb82015-03-30 21:41:43 +00001618Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1619 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001620}
1621
Chris Lattner45f15572007-04-14 00:12:57 +00001622/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1623/// zeros. If so, the result pointer and the first operand have the same
1624/// value, just potentially different types.
1625bool GetElementPtrInst::hasAllZeroIndices() const {
1626 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1627 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1628 if (!CI->isZero()) return false;
1629 } else {
1630 return false;
1631 }
1632 }
1633 return true;
1634}
1635
Chris Lattner27058292007-04-27 20:35:56 +00001636/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1637/// constant integers. If so, the result pointer and the first operand have
1638/// a constant offset between them.
1639bool GetElementPtrInst::hasAllConstantIndices() const {
1640 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1641 if (!isa<ConstantInt>(getOperand(i)))
1642 return false;
1643 }
1644 return true;
1645}
1646
Dan Gohman1b849082009-09-07 23:54:19 +00001647void GetElementPtrInst::setIsInBounds(bool B) {
1648 cast<GEPOperator>(this)->setIsInBounds(B);
1649}
Chris Lattner45f15572007-04-14 00:12:57 +00001650
Nick Lewycky28a5f252009-09-27 21:33:04 +00001651bool GetElementPtrInst::isInBounds() const {
1652 return cast<GEPOperator>(this)->isInBounds();
1653}
1654
Chandler Carruth1e140532012-12-11 10:29:10 +00001655bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1656 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001657 // Delegate to the generic GEPOperator implementation.
1658 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001659}
1660
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001662// ExtractElementInst Implementation
1663//===----------------------------------------------------------------------===//
1664
1665ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001666 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001667 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001668 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001669 ExtractElement,
1670 OperandTraits<ExtractElementInst>::op_begin(this),
1671 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001672 assert(isValidOperands(Val, Index) &&
1673 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001674 Op<0>() = Val;
1675 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001676 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001677}
1678
1679ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001680 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001681 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001682 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001683 ExtractElement,
1684 OperandTraits<ExtractElementInst>::op_begin(this),
1685 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001686 assert(isValidOperands(Val, Index) &&
1687 "Invalid extractelement instruction operands!");
1688
Gabor Greif2d3024d2008-05-26 21:33:52 +00001689 Op<0>() = Val;
1690 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001691 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001692}
1693
Chris Lattner65511ff2006-10-05 06:24:58 +00001694
Chris Lattner54865b32006-04-08 04:05:48 +00001695bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001696 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001697 return false;
1698 return true;
1699}
1700
1701
Robert Bocchino23004482006-01-10 19:05:34 +00001702//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001703// InsertElementInst Implementation
1704//===----------------------------------------------------------------------===//
1705
Chris Lattner54865b32006-04-08 04:05:48 +00001706InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001707 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001708 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001709 : Instruction(Vec->getType(), InsertElement,
1710 OperandTraits<InsertElementInst>::op_begin(this),
1711 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001712 assert(isValidOperands(Vec, Elt, Index) &&
1713 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001714 Op<0>() = Vec;
1715 Op<1>() = Elt;
1716 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001717 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001718}
1719
Chris Lattner54865b32006-04-08 04:05:48 +00001720InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001721 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001722 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001723 : Instruction(Vec->getType(), InsertElement,
1724 OperandTraits<InsertElementInst>::op_begin(this),
1725 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001726 assert(isValidOperands(Vec, Elt, Index) &&
1727 "Invalid insertelement instruction operands!");
1728
Gabor Greif2d3024d2008-05-26 21:33:52 +00001729 Op<0>() = Vec;
1730 Op<1>() = Elt;
1731 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001732 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001733}
1734
Chris Lattner54865b32006-04-08 04:05:48 +00001735bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1736 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001737 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001738 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001739
Reid Spencerd84d35b2007-02-15 02:26:10 +00001740 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001741 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001742
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001743 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001744 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001745 return true;
1746}
1747
1748
Robert Bocchinoca27f032006-01-17 20:07:22 +00001749//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001750// ShuffleVectorInst Implementation
1751//===----------------------------------------------------------------------===//
1752
1753ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001754 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001755 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001756: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001757 cast<VectorType>(Mask->getType())->getNumElements()),
1758 ShuffleVector,
1759 OperandTraits<ShuffleVectorInst>::op_begin(this),
1760 OperandTraits<ShuffleVectorInst>::operands(this),
1761 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001762 assert(isValidOperands(V1, V2, Mask) &&
1763 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001764 Op<0>() = V1;
1765 Op<1>() = V2;
1766 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001767 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001768}
1769
1770ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001771 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001772 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001773: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1774 cast<VectorType>(Mask->getType())->getNumElements()),
1775 ShuffleVector,
1776 OperandTraits<ShuffleVectorInst>::op_begin(this),
1777 OperandTraits<ShuffleVectorInst>::operands(this),
1778 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001779 assert(isValidOperands(V1, V2, Mask) &&
1780 "Invalid shuffle vector instruction operands!");
1781
Gabor Greif2d3024d2008-05-26 21:33:52 +00001782 Op<0>() = V1;
1783 Op<1>() = V2;
1784 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001785 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001786}
1787
Mon P Wang25f01062008-11-10 04:46:22 +00001788bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001789 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001790 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001791 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001792 return false;
1793
Chris Lattner1dcb6542012-01-25 23:49:49 +00001794 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001795 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001796 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001797 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001798
1799 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001800 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1801 return true;
1802
Nate Begeman60a31c32010-08-13 00:16:46 +00001803 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001804 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001805 for (Value *Op : MV->operands()) {
1806 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001807 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001808 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001809 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001810 return false;
1811 }
1812 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001813 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001814 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001815
1816 if (const ConstantDataSequential *CDS =
1817 dyn_cast<ConstantDataSequential>(Mask)) {
1818 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1819 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1820 if (CDS->getElementAsInteger(i) >= V1Size*2)
1821 return false;
1822 return true;
1823 }
1824
1825 // The bitcode reader can create a place holder for a forward reference
1826 // used as the shuffle mask. When this occurs, the shuffle mask will
1827 // fall into this case and fail. To avoid this error, do this bit of
1828 // ugliness to allow such a mask pass.
1829 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1830 if (CE->getOpcode() == Instruction::UserOp1)
1831 return true;
1832
1833 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001834}
1835
Chris Lattnerf724e342008-03-02 05:28:33 +00001836/// getMaskValue - Return the index from the shuffle mask for the specified
1837/// output result. This is either -1 if the element is undef or a number less
1838/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001839int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1840 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1841 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001842 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001843 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001844 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001845 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001846 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001847}
1848
Chris Lattner1dcb6542012-01-25 23:49:49 +00001849/// getShuffleMask - Return the full mask for this instruction, where each
1850/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001851void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1852 SmallVectorImpl<int> &Result) {
1853 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001854
Chris Lattnercf129702012-01-26 02:51:13 +00001855 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001856 for (unsigned i = 0; i != NumElts; ++i)
1857 Result.push_back(CDS->getElementAsInteger(i));
1858 return;
1859 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001860 for (unsigned i = 0; i != NumElts; ++i) {
1861 Constant *C = Mask->getAggregateElement(i);
1862 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001863 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001864 }
1865}
1866
1867
Dan Gohman12fce772008-05-15 19:50:34 +00001868//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001869// InsertValueInst Class
1870//===----------------------------------------------------------------------===//
1871
Jay Foad57aa6362011-07-13 10:26:04 +00001872void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1873 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001874 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001875
1876 // There's no fundamental reason why we require at least one index
1877 // (other than weirdness with &*IdxBegin being invalid; see
1878 // getelementptr's init routine for example). But there's no
1879 // present need to support it.
1880 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1881
1882 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001883 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001884 Op<0>() = Agg;
1885 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001886
Jay Foad57aa6362011-07-13 10:26:04 +00001887 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001888 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001889}
1890
1891InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001892 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001893 OperandTraits<InsertValueInst>::op_begin(this), 2),
1894 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001895 Op<0>() = IVI.getOperand(0);
1896 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001897 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001898}
1899
1900//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001901// ExtractValueInst Class
1902//===----------------------------------------------------------------------===//
1903
Jay Foad57aa6362011-07-13 10:26:04 +00001904void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001905 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001906
Jay Foad57aa6362011-07-13 10:26:04 +00001907 // There's no fundamental reason why we require at least one index.
1908 // But there's no present need to support it.
1909 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001910
Jay Foad57aa6362011-07-13 10:26:04 +00001911 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001912 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001913}
1914
1915ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001916 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001917 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001918 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001919}
1920
Dan Gohman12fce772008-05-15 19:50:34 +00001921// getIndexedType - Returns the type of the element that would be extracted
1922// with an extractvalue instruction with the specified parameters.
1923//
1924// A null type is returned if the indices are invalid for the specified
1925// pointer type.
1926//
Chris Lattner229907c2011-07-18 04:54:35 +00001927Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001928 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001929 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001930 // We can't use CompositeType::indexValid(Index) here.
1931 // indexValid() always returns true for arrays because getelementptr allows
1932 // out-of-bounds indices. Since we don't allow those for extractvalue and
1933 // insertvalue we need to check array indexing manually.
1934 // Since the only other types we can index into are struct types it's just
1935 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001936 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001937 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001938 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001939 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001940 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001941 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001942 } else {
1943 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001944 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001945 }
1946
1947 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001948 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001949 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001950}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001951
1952//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001953// BinaryOperator Class
1954//===----------------------------------------------------------------------===//
1955
Chris Lattner2195fc42007-02-24 00:55:48 +00001956BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001957 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001958 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001959 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001960 OperandTraits<BinaryOperator>::op_begin(this),
1961 OperandTraits<BinaryOperator>::operands(this),
1962 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001963 Op<0>() = S1;
1964 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001965 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001966 setName(Name);
1967}
1968
1969BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001970 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001971 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001972 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001973 OperandTraits<BinaryOperator>::op_begin(this),
1974 OperandTraits<BinaryOperator>::operands(this),
1975 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001976 Op<0>() = S1;
1977 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001978 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001979 setName(Name);
1980}
1981
1982
1983void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001984 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001985 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001986 assert(LHS->getType() == RHS->getType() &&
1987 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001988#ifndef NDEBUG
1989 switch (iType) {
1990 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001991 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001992 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001993 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001994 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001995 "Tried to create an integer operation on a non-integer type!");
1996 break;
1997 case FAdd: case FSub:
1998 case FMul:
1999 assert(getType() == LHS->getType() &&
2000 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002001 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002002 "Tried to create a floating-point operation on a "
2003 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002004 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002005 case UDiv:
2006 case SDiv:
2007 assert(getType() == LHS->getType() &&
2008 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002009 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002011 "Incorrect operand type (not integer) for S/UDIV");
2012 break;
2013 case FDiv:
2014 assert(getType() == LHS->getType() &&
2015 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002016 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002017 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002018 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002019 case URem:
2020 case SRem:
2021 assert(getType() == LHS->getType() &&
2022 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002023 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002024 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002025 "Incorrect operand type (not integer) for S/UREM");
2026 break;
2027 case FRem:
2028 assert(getType() == LHS->getType() &&
2029 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002030 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002031 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002032 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002033 case Shl:
2034 case LShr:
2035 case AShr:
2036 assert(getType() == LHS->getType() &&
2037 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002038 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002039 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002040 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002041 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002042 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002043 case And: case Or:
2044 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002045 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002046 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002047 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002048 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002049 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002050 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002052 default:
2053 break;
2054 }
2055#endif
2056}
2057
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002058BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002059 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002060 Instruction *InsertBefore) {
2061 assert(S1->getType() == S2->getType() &&
2062 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002063 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002064}
2065
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002067 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002068 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002069 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002070 InsertAtEnd->getInstList().push_back(Res);
2071 return Res;
2072}
2073
Dan Gohman5476cfd2009-08-12 16:23:25 +00002074BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002075 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002076 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002077 return new BinaryOperator(Instruction::Sub,
2078 zero, Op,
2079 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002080}
2081
Dan Gohman5476cfd2009-08-12 16:23:25 +00002082BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002084 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002085 return new BinaryOperator(Instruction::Sub,
2086 zero, Op,
2087 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002088}
2089
Dan Gohman4ab44202009-12-18 02:58:50 +00002090BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2091 Instruction *InsertBefore) {
2092 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2093 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2094}
2095
2096BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2097 BasicBlock *InsertAtEnd) {
2098 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2099 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2100}
2101
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002102BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2103 Instruction *InsertBefore) {
2104 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2105 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2106}
2107
2108BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2109 BasicBlock *InsertAtEnd) {
2110 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2111 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2112}
2113
Dan Gohman5476cfd2009-08-12 16:23:25 +00002114BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002115 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002116 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002117 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002118 Op->getType(), Name, InsertBefore);
2119}
2120
Dan Gohman5476cfd2009-08-12 16:23:25 +00002121BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002122 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002123 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002124 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002125 Op->getType(), Name, InsertAtEnd);
2126}
2127
Dan Gohman5476cfd2009-08-12 16:23:25 +00002128BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002130 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002131 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002132 Op->getType(), Name, InsertBefore);
2133}
2134
Dan Gohman5476cfd2009-08-12 16:23:25 +00002135BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002136 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002137 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002138 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002139 Op->getType(), Name, InsertAtEnd);
2140}
2141
2142
2143// isConstantAllOnes - Helper function for several functions below
2144static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002145 if (const Constant *C = dyn_cast<Constant>(V))
2146 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002147 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002148}
2149
Owen Andersonbb2501b2009-07-13 22:18:28 +00002150bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002151 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2152 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002153 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2154 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002155 return false;
2156}
2157
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002158bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002159 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2160 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002161 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2162 if (!IgnoreZeroSign)
2163 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2164 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2165 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002166 return false;
2167}
2168
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169bool BinaryOperator::isNot(const Value *V) {
2170 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2171 return (Bop->getOpcode() == Instruction::Xor &&
2172 (isConstantAllOnes(Bop->getOperand(1)) ||
2173 isConstantAllOnes(Bop->getOperand(0))));
2174 return false;
2175}
2176
Chris Lattner2c7d1772005-04-24 07:28:37 +00002177Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002178 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002179}
2180
Chris Lattner2c7d1772005-04-24 07:28:37 +00002181const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2182 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002183}
2184
Dan Gohmana5b96452009-06-04 22:49:04 +00002185Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002186 return cast<BinaryOperator>(BinOp)->getOperand(1);
2187}
2188
2189const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2190 return getFNegArgument(const_cast<Value*>(BinOp));
2191}
2192
Chris Lattner2c7d1772005-04-24 07:28:37 +00002193Value *BinaryOperator::getNotArgument(Value *BinOp) {
2194 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2195 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2196 Value *Op0 = BO->getOperand(0);
2197 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198 if (isConstantAllOnes(Op0)) return Op1;
2199
2200 assert(isConstantAllOnes(Op1));
2201 return Op0;
2202}
2203
Chris Lattner2c7d1772005-04-24 07:28:37 +00002204const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2205 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002206}
2207
2208
2209// swapOperands - Exchange the two operands to this instruction. This
2210// instruction is safe to use on any binary instruction and does not
2211// modify the semantics of the instruction. If the instruction is
2212// order dependent (SetLT f.e.) the opcode is changed.
2213//
2214bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002215 if (!isCommutative())
2216 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002217 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002218 return false;
2219}
2220
Dan Gohman1b849082009-09-07 23:54:19 +00002221void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2222 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2223}
2224
2225void BinaryOperator::setHasNoSignedWrap(bool b) {
2226 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2227}
2228
2229void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002230 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002231}
2232
Nick Lewycky28a5f252009-09-27 21:33:04 +00002233bool BinaryOperator::hasNoUnsignedWrap() const {
2234 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2235}
2236
2237bool BinaryOperator::hasNoSignedWrap() const {
2238 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2239}
2240
2241bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002242 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002243}
2244
Sanjay Patela982d992014-09-03 01:06:50 +00002245void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002246 // Copy the wrapping flags.
2247 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2248 setHasNoSignedWrap(OB->hasNoSignedWrap());
2249 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2250 }
2251
2252 // Copy the exact flag.
2253 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2254 setIsExact(PE->isExact());
2255
2256 // Copy the fast-math flags.
2257 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002258 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002259}
2260
Sanjay Patela982d992014-09-03 01:06:50 +00002261void BinaryOperator::andIRFlags(const Value *V) {
2262 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2263 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2264 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2265 }
2266
2267 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2268 setIsExact(isExact() & PE->isExact());
2269
2270 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2271 FastMathFlags FM = getFastMathFlags();
2272 FM &= FP->getFastMathFlags();
2273 copyFastMathFlags(FM);
2274 }
2275}
2276
2277
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002278//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002279// FPMathOperator Class
2280//===----------------------------------------------------------------------===//
2281
2282/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2283/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002284/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002285float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002286 const MDNode *MD =
2287 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002288 if (!MD)
2289 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002290 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002291 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002292}
2293
2294
2295//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002296// CastInst Class
2297//===----------------------------------------------------------------------===//
2298
David Blaikie3a15e142011-12-01 08:00:17 +00002299void CastInst::anchor() {}
2300
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301// Just determine if this cast only deals with integral->integral conversion.
2302bool CastInst::isIntegerCast() const {
2303 switch (getOpcode()) {
2304 default: return false;
2305 case Instruction::ZExt:
2306 case Instruction::SExt:
2307 case Instruction::Trunc:
2308 return true;
2309 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002310 return getOperand(0)->getType()->isIntegerTy() &&
2311 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002313}
2314
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315bool CastInst::isLosslessCast() const {
2316 // Only BitCast can be lossless, exit fast if we're not BitCast
2317 if (getOpcode() != Instruction::BitCast)
2318 return false;
2319
2320 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002321 Type* SrcTy = getOperand(0)->getType();
2322 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 if (SrcTy == DstTy)
2324 return true;
2325
Reid Spencer8d9336d2006-12-31 05:26:44 +00002326 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002327 if (SrcTy->isPointerTy())
2328 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 return false; // Other types have no identity values
2330}
2331
2332/// This function determines if the CastInst does not require any bits to be
2333/// changed in order to effect the cast. Essentially, it identifies cases where
2334/// no code gen is necessary for the cast, hence the name no-op cast. For
2335/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002336/// # bitcast i32* %x to i8*
2337/// # bitcast <2 x i32> %x to <4 x i16>
2338/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002339/// @brief Determine if the described cast is a no-op.
2340bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002341 Type *SrcTy,
2342 Type *DestTy,
2343 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002344 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002345 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 case Instruction::Trunc:
2347 case Instruction::ZExt:
2348 case Instruction::SExt:
2349 case Instruction::FPTrunc:
2350 case Instruction::FPExt:
2351 case Instruction::UIToFP:
2352 case Instruction::SIToFP:
2353 case Instruction::FPToUI:
2354 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002355 case Instruction::AddrSpaceCast:
2356 // TODO: Target informations may give a more accurate answer here.
2357 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 case Instruction::BitCast:
2359 return true; // BitCast never modifies bits.
2360 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002361 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002362 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002364 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002365 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366 }
2367}
2368
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002369/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002370bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002371 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2372}
2373
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002374bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002375 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002376 if (getOpcode() == Instruction::PtrToInt)
2377 PtrOpTy = getOperand(0)->getType();
2378 else if (getOpcode() == Instruction::IntToPtr)
2379 PtrOpTy = getType();
2380
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002381 Type *IntPtrTy =
2382 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002383
2384 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2385}
2386
2387/// This function determines if a pair of casts can be eliminated and what
2388/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389/// instructions like this:
2390/// * %F = firstOpcode SrcTy %x to MidTy
2391/// * %S = secondOpcode MidTy %F to DstTy
2392/// The function returns a resultOpcode so these two casts can be replaced with:
2393/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2394/// If no such cast is permited, the function returns 0.
2395unsigned CastInst::isEliminableCastPair(
2396 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002397 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2398 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 // Define the 144 possibilities for these two cast instructions. The values
2400 // in this matrix determine what to do in a given situation and select the
2401 // case in the switch below. The rows correspond to firstOp, the columns
2402 // correspond to secondOp. In looking at the table below, keep in mind
2403 // the following cast properties:
2404 //
2405 // Size Compare Source Destination
2406 // Operator Src ? Size Type Sign Type Sign
2407 // -------- ------------ ------------------- ---------------------
2408 // TRUNC > Integer Any Integral Any
2409 // ZEXT < Integral Unsigned Integer Any
2410 // SEXT < Integral Signed Integer Any
2411 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002412 // FPTOSI n/a FloatPt n/a Integral Signed
2413 // UITOFP n/a Integral Unsigned FloatPt n/a
2414 // SITOFP n/a Integral Signed FloatPt n/a
2415 // FPTRUNC > FloatPt n/a FloatPt n/a
2416 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 // PTRTOINT n/a Pointer n/a Integral Unsigned
2418 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002419 // BITCAST = FirstClass n/a FirstClass n/a
2420 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002421 //
2422 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002423 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2424 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002426 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002427 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002428 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002429 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2431 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002432 // T F F U S F F P I B A -+
2433 // R Z S P P I I T P 2 N T S |
2434 // U E E 2 2 2 2 R E I T C C +- secondOp
2435 // N X X U S F F N X N 2 V V |
2436 // C T T I I P P C T T P T T -+
2437 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002438 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2440 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2441 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2442 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2443 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002444 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2446 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2447 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2448 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2449 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002451
Chris Lattner25eea4d2010-07-12 01:19:22 +00002452 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002453 // merging. However, bitcast of A->B->A are allowed.
2454 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2455 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2456 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2457
2458 // Check if any of the bitcasts convert scalars<->vectors.
2459 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2460 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2461 // Unless we are bitcasing to the original type, disallow optimizations.
2462 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463
2464 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2465 [secondOp-Instruction::CastOpsBegin];
2466 switch (ElimCase) {
2467 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 return 0;
2470 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002471 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 return firstOp;
2473 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002474 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475 return secondOp;
2476 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002477 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002478 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002479 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002480 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 return firstOp;
2482 return 0;
2483 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002484 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002485 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 return firstOp;
2488 return 0;
2489 case 5:
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 an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 return secondOp;
2494 return 0;
2495 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002497 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002498 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 return secondOp;
2500 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002501 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002502 // Cannot simplify if address spaces are different!
2503 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2504 return 0;
2505
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002506 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002507 // We can still fold this without knowing the actual sizes as long we
2508 // know that the intermediate pointer is the largest possible
2509 // pointer size.
2510 // FIXME: Is this always true?
2511 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002512 return Instruction::BitCast;
2513
2514 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002515 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002516 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002517 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 if (MidSize >= PtrSize)
2519 return Instruction::BitCast;
2520 return 0;
2521 }
2522 case 8: {
2523 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2524 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2525 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002526 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2527 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 if (SrcSize == DstSize)
2529 return Instruction::BitCast;
2530 else if (SrcSize < DstSize)
2531 return firstOp;
2532 return secondOp;
2533 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 case 9:
2535 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 return Instruction::ZExt;
2537 case 10:
2538 // fpext followed by ftrunc is allowed if the bit size returned to is
2539 // the same as the original, in which case its just a bitcast
2540 if (SrcTy == DstTy)
2541 return Instruction::BitCast;
2542 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002543 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002545 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002546 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002547 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002548 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2549 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 if (SrcSize <= PtrSize && SrcSize == DstSize)
2551 return Instruction::BitCast;
2552 return 0;
2553 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002554 case 12: {
2555 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2556 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2557 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2558 return Instruction::AddrSpaceCast;
2559 return Instruction::BitCast;
2560 }
2561 case 13:
2562 // FIXME: this state can be merged with (1), but the following assert
2563 // is useful to check the correcteness of the sequence due to semantic
2564 // change of bitcast.
2565 assert(
2566 SrcTy->isPtrOrPtrVectorTy() &&
2567 MidTy->isPtrOrPtrVectorTy() &&
2568 DstTy->isPtrOrPtrVectorTy() &&
2569 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2570 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2571 "Illegal addrspacecast, bitcast sequence!");
2572 // Allowed, use first cast's opcode
2573 return firstOp;
2574 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002575 // bitcast, addrspacecast -> addrspacecast if the element type of
2576 // bitcast's source is the same as that of addrspacecast's destination.
2577 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2578 return Instruction::AddrSpaceCast;
2579 return 0;
2580
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002581 case 15:
2582 // FIXME: this state can be merged with (1), but the following assert
2583 // is useful to check the correcteness of the sequence due to semantic
2584 // change of bitcast.
2585 assert(
2586 SrcTy->isIntOrIntVectorTy() &&
2587 MidTy->isPtrOrPtrVectorTy() &&
2588 DstTy->isPtrOrPtrVectorTy() &&
2589 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2590 "Illegal inttoptr, bitcast sequence!");
2591 // Allowed, use first cast's opcode
2592 return firstOp;
2593 case 16:
2594 // FIXME: this state can be merged with (2), but the following assert
2595 // is useful to check the correcteness of the sequence due to semantic
2596 // change of bitcast.
2597 assert(
2598 SrcTy->isPtrOrPtrVectorTy() &&
2599 MidTy->isPtrOrPtrVectorTy() &&
2600 DstTy->isIntOrIntVectorTy() &&
2601 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2602 "Illegal bitcast, ptrtoint sequence!");
2603 // Allowed, use second cast's opcode
2604 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002605 case 17:
2606 // (sitofp (zext x)) -> (uitofp x)
2607 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002609 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002611 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 default:
Craig Topperc514b542012-02-05 22:14:15 +00002613 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615}
2616
Chris Lattner229907c2011-07-18 04:54:35 +00002617CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002618 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002619 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620 // Construct and return the appropriate CastInst subclass
2621 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002622 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2623 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2624 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2625 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2626 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2627 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2628 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2629 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2630 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2631 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2632 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2633 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2634 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2635 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637}
2638
Chris Lattner229907c2011-07-18 04:54:35 +00002639CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002641 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642 // Construct and return the appropriate CastInst subclass
2643 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002644 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2645 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2646 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2647 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2648 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2649 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2650 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2651 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2652 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2653 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2654 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2655 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2656 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2657 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659}
2660
Chris Lattner229907c2011-07-18 04:54:35 +00002661CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002662 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002663 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002664 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002665 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2666 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002667}
2668
Chris Lattner229907c2011-07-18 04:54:35 +00002669CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002670 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002671 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002672 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002673 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2674 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002675}
2676
Chris Lattner229907c2011-07-18 04:54:35 +00002677CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002678 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002679 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002680 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002681 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2682 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002683}
2684
Chris Lattner229907c2011-07-18 04:54:35 +00002685CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002686 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002687 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002688 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002689 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2690 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002691}
2692
Chris Lattner229907c2011-07-18 04:54:35 +00002693CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002694 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002695 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002696 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002697 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2698 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002699}
2700
Chris Lattner229907c2011-07-18 04:54:35 +00002701CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002702 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002703 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002704 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002705 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2706 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002707}
2708
Chris Lattner229907c2011-07-18 04:54:35 +00002709CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002710 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002711 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002712 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2713 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2714 "Invalid cast");
2715 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002716 assert((!Ty->isVectorTy() ||
2717 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002718 "Invalid cast");
2719
Matt Arsenault065ced92013-07-31 00:17:33 +00002720 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002721 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002722
Matt Arsenault740980e2014-07-14 17:24:35 +00002723 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002724}
2725
2726/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002727CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2728 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002729 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002730 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2731 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002732 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002733 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002734 assert((!Ty->isVectorTy() ||
2735 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002736 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002737
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002738 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002739 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002740
Matt Arsenault740980e2014-07-14 17:24:35 +00002741 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2742}
2743
2744CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2745 Value *S, Type *Ty,
2746 const Twine &Name,
2747 BasicBlock *InsertAtEnd) {
2748 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2749 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2750
2751 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2752 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2753
2754 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2755}
2756
2757CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2758 Value *S, Type *Ty,
2759 const Twine &Name,
2760 Instruction *InsertBefore) {
2761 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2762 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2763
2764 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002765 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2766
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002767 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002768}
2769
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002770CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2771 const Twine &Name,
2772 Instruction *InsertBefore) {
2773 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2774 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2775 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2776 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2777
2778 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2779}
2780
Matt Arsenault740980e2014-07-14 17:24:35 +00002781CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002782 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002783 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002784 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002785 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002786 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2787 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002788 Instruction::CastOps opcode =
2789 (SrcBits == DstBits ? Instruction::BitCast :
2790 (SrcBits > DstBits ? Instruction::Trunc :
2791 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002792 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002793}
2794
Chris Lattner229907c2011-07-18 04:54:35 +00002795CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002796 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002797 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002798 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002799 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002800 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2801 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002802 Instruction::CastOps opcode =
2803 (SrcBits == DstBits ? Instruction::BitCast :
2804 (SrcBits > DstBits ? Instruction::Trunc :
2805 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002806 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002807}
2808
Chris Lattner229907c2011-07-18 04:54:35 +00002809CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002810 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002811 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002812 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002813 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002814 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2815 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002816 Instruction::CastOps opcode =
2817 (SrcBits == DstBits ? Instruction::BitCast :
2818 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002819 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002820}
2821
Chris Lattner229907c2011-07-18 04:54:35 +00002822CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002823 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002824 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002825 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002826 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002827 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2828 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002829 Instruction::CastOps opcode =
2830 (SrcBits == DstBits ? Instruction::BitCast :
2831 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002832 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002833}
2834
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002835// Check whether it is valid to call getCastOpcode for these types.
2836// This routine must be kept in sync with getCastOpcode.
2837bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2838 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2839 return false;
2840
2841 if (SrcTy == DestTy)
2842 return true;
2843
2844 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2845 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2846 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2847 // An element by element cast. Valid if casting the elements is valid.
2848 SrcTy = SrcVecTy->getElementType();
2849 DestTy = DestVecTy->getElementType();
2850 }
2851
2852 // Get the bit sizes, we'll need these
2853 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2854 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2855
2856 // Run through the possibilities ...
2857 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002858 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002859 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002860 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002861 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002862 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002863 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002864 // Casting from something else
2865 return SrcTy->isPointerTy();
2866 }
2867 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2868 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002869 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002870 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002871 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002872 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002873 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002874 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002875 return false;
2876 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002877 if (DestTy->isVectorTy()) // Casting to vector
2878 return DestBits == SrcBits;
2879 if (DestTy->isPointerTy()) { // Casting to pointer
2880 if (SrcTy->isPointerTy()) // Casting from pointer
2881 return true;
2882 return SrcTy->isIntegerTy(); // Casting from integral
2883 }
2884 if (DestTy->isX86_MMXTy()) {
2885 if (SrcTy->isVectorTy())
2886 return DestBits == SrcBits; // 64-bit vector to MMX
2887 return false;
2888 } // Casting to something else
2889 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002890}
2891
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002892bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2893 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2894 return false;
2895
2896 if (SrcTy == DestTy)
2897 return true;
2898
2899 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2900 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2901 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2902 // An element by element cast. Valid if casting the elements is valid.
2903 SrcTy = SrcVecTy->getElementType();
2904 DestTy = DestVecTy->getElementType();
2905 }
2906 }
2907 }
2908
2909 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2910 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2911 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2912 }
2913 }
2914
2915 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2916 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2917
2918 // Could still have vectors of pointers if the number of elements doesn't
2919 // match
2920 if (SrcBits == 0 || DestBits == 0)
2921 return false;
2922
2923 if (SrcBits != DestBits)
2924 return false;
2925
2926 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2927 return false;
2928
2929 return true;
2930}
2931
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002932bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002933 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002934 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2935 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002936 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002937 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2938 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002939 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002940
2941 return isBitCastable(SrcTy, DestTy);
2942}
2943
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002944// Provide a way to get a "cast" where the cast opcode is inferred from the
2945// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002946// logic in the castIsValid function below. This axiom should hold:
2947// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2948// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002950// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002952CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002953 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2954 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955
Duncan Sands55e50902008-01-06 10:12:28 +00002956 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2957 "Only first class types are castable!");
2958
Duncan Sandsa8514532011-05-18 07:13:41 +00002959 if (SrcTy == DestTy)
2960 return BitCast;
2961
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002962 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002963 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2964 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002965 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2966 // An element by element cast. Find the appropriate opcode based on the
2967 // element types.
2968 SrcTy = SrcVecTy->getElementType();
2969 DestTy = DestVecTy->getElementType();
2970 }
2971
2972 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002973 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2974 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002975
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002976 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002977 if (DestTy->isIntegerTy()) { // Casting to integral
2978 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979 if (DestBits < SrcBits)
2980 return Trunc; // int -> smaller int
2981 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002982 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 return SExt; // signed -> SEXT
2984 else
2985 return ZExt; // unsigned -> ZEXT
2986 } else {
2987 return BitCast; // Same size, No-op cast
2988 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002989 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002990 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 return FPToSI; // FP -> sint
2992 else
2993 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002994 } else if (SrcTy->isVectorTy()) {
2995 assert(DestBits == SrcBits &&
2996 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997 return BitCast; // Same size, no-op cast
2998 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002999 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000 "Casting from a value that is not first-class type");
3001 return PtrToInt; // ptr -> int
3002 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003003 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3004 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003005 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006 return SIToFP; // sint -> FP
3007 else
3008 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003009 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003010 if (DestBits < SrcBits) {
3011 return FPTrunc; // FP -> smaller FP
3012 } else if (DestBits > SrcBits) {
3013 return FPExt; // FP -> larger FP
3014 } else {
3015 return BitCast; // same size, no-op cast
3016 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003017 } else if (SrcTy->isVectorTy()) {
3018 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003019 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003020 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003022 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003023 } else if (DestTy->isVectorTy()) {
3024 assert(DestBits == SrcBits &&
3025 "Illegal cast to vector (wrong type or size)");
3026 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003027 } else if (DestTy->isPointerTy()) {
3028 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003029 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3030 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003032 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003035 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003036 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003037 if (SrcTy->isVectorTy()) {
3038 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003039 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003040 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003041 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003043 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003044}
3045
3046//===----------------------------------------------------------------------===//
3047// CastInst SubClass Constructors
3048//===----------------------------------------------------------------------===//
3049
3050/// Check that the construction parameters for a CastInst are correct. This
3051/// could be broken out into the separate constructors but it is useful to have
3052/// it in one place and to eliminate the redundant code for getting the sizes
3053/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003054bool
Chris Lattner229907c2011-07-18 04:54:35 +00003055CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056
3057 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003058 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003059
Chris Lattner37bc78a2010-01-26 21:51:43 +00003060 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3061 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003062 return false;
3063
3064 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003065 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3066 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067
Duncan Sands7f646562011-05-18 09:21:57 +00003068 // If these are vector types, get the lengths of the vectors (using zero for
3069 // scalar types means that checking that vector lengths match also checks that
3070 // scalars are not being converted to vectors or vectors to scalars).
3071 unsigned SrcLength = SrcTy->isVectorTy() ?
3072 cast<VectorType>(SrcTy)->getNumElements() : 0;
3073 unsigned DstLength = DstTy->isVectorTy() ?
3074 cast<VectorType>(DstTy)->getNumElements() : 0;
3075
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076 // Switch on the opcode provided
3077 switch (op) {
3078 default: return false; // This is an input error
3079 case Instruction::Trunc:
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::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003083 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3084 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003086 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3087 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003088 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003089 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3090 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003091 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003092 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3093 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003094 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003096 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3097 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003098 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003099 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003100 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3101 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003102 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003103 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003104 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003105 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3106 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3107 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003108 return SrcTy->getScalarType()->isPointerTy() &&
3109 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003110 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003111 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003112 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003113 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3114 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3115 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003116 return SrcTy->getScalarType()->isIntegerTy() &&
3117 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003118 case Instruction::BitCast: {
3119 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3120 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3121
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003122 // BitCast implies a no-op cast of type only. No bits change.
3123 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003124 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125 return false;
3126
Alp Tokerf907b892013-12-05 05:44:44 +00003127 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003128 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003129 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003130 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3131
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003132 // If both are pointers then the address spaces must match.
3133 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3134 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003135
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003136 // A vector of pointers must have the same number of elements.
3137 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3138 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3139 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3140
3141 return false;
3142 }
3143
3144 return true;
3145 }
3146 case Instruction::AddrSpaceCast: {
3147 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3148 if (!SrcPtrTy)
3149 return false;
3150
3151 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3152 if (!DstPtrTy)
3153 return false;
3154
3155 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3156 return false;
3157
3158 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3159 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3160 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3161
3162 return false;
3163 }
3164
3165 return true;
3166 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167 }
3168}
3169
3170TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003190) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003194 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003196 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197}
3198
Jeff Cohencc08c832006-12-02 02:22:01 +00003199SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003200 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003202 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003208 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209}
3210
3211FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221}
3222
3223FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003224 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233}
3234
3235UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003236 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003238 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003244 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245}
3246
3247SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003248 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003249) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003250 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003255) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003256 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257}
3258
3259FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003260 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003261) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003262 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003267) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003268 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003269}
3270
3271FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003272 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003273) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003274 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003279) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003280 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003281}
3282
3283PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003284 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003285) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003286 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003291) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003292 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003293}
3294
3295IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003296 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003297) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003298 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003303) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003304 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003305}
3306
3307BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003308 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003309) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003310 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003311}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003312
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003313AddrSpaceCastInst::AddrSpaceCastInst(
3314 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3315) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3316 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3317}
3318
3319AddrSpaceCastInst::AddrSpaceCastInst(
3320 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3321) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3322 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3323}
3324
Chris Lattnerf16dc002006-09-17 19:29:56 +00003325//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003326// CmpInst Classes
3327//===----------------------------------------------------------------------===//
3328
Craig Topper3186c012012-09-23 02:12:10 +00003329void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003330
Chris Lattner229907c2011-07-18 04:54:35 +00003331CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003332 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003333 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003334 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003335 OperandTraits<CmpInst>::op_begin(this),
3336 OperandTraits<CmpInst>::operands(this),
3337 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003338 Op<0>() = LHS;
3339 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003340 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003341 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003342}
Gabor Greiff6caff662008-05-10 08:32:32 +00003343
Chris Lattner229907c2011-07-18 04:54:35 +00003344CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003345 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003346 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003347 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003348 OperandTraits<CmpInst>::op_begin(this),
3349 OperandTraits<CmpInst>::operands(this),
3350 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003351 Op<0>() = LHS;
3352 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003353 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003354 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003355}
3356
3357CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003358CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003359 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003360 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003361 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003362 if (InsertBefore)
3363 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3364 S1, S2, Name);
3365 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003366 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003367 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003368 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369
3370 if (InsertBefore)
3371 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3372 S1, S2, Name);
3373 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003374 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003375 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003376}
3377
3378CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003379CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003380 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003381 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003382 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3383 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003384 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003385 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3386 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003387}
3388
3389void CmpInst::swapOperands() {
3390 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3391 IC->swapOperands();
3392 else
3393 cast<FCmpInst>(this)->swapOperands();
3394}
3395
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003396bool CmpInst::isCommutative() const {
3397 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003398 return IC->isCommutative();
3399 return cast<FCmpInst>(this)->isCommutative();
3400}
3401
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003402bool CmpInst::isEquality() const {
3403 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003404 return IC->isEquality();
3405 return cast<FCmpInst>(this)->isEquality();
3406}
3407
3408
Dan Gohman4e724382008-05-31 02:47:54 +00003409CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003410 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003411 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003412 case ICMP_EQ: return ICMP_NE;
3413 case ICMP_NE: return ICMP_EQ;
3414 case ICMP_UGT: return ICMP_ULE;
3415 case ICMP_ULT: return ICMP_UGE;
3416 case ICMP_UGE: return ICMP_ULT;
3417 case ICMP_ULE: return ICMP_UGT;
3418 case ICMP_SGT: return ICMP_SLE;
3419 case ICMP_SLT: return ICMP_SGE;
3420 case ICMP_SGE: return ICMP_SLT;
3421 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003422
Dan Gohman4e724382008-05-31 02:47:54 +00003423 case FCMP_OEQ: return FCMP_UNE;
3424 case FCMP_ONE: return FCMP_UEQ;
3425 case FCMP_OGT: return FCMP_ULE;
3426 case FCMP_OLT: return FCMP_UGE;
3427 case FCMP_OGE: return FCMP_ULT;
3428 case FCMP_OLE: return FCMP_UGT;
3429 case FCMP_UEQ: return FCMP_ONE;
3430 case FCMP_UNE: return FCMP_OEQ;
3431 case FCMP_UGT: return FCMP_OLE;
3432 case FCMP_ULT: return FCMP_OGE;
3433 case FCMP_UGE: return FCMP_OLT;
3434 case FCMP_ULE: return FCMP_OGT;
3435 case FCMP_ORD: return FCMP_UNO;
3436 case FCMP_UNO: return FCMP_ORD;
3437 case FCMP_TRUE: return FCMP_FALSE;
3438 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003439 }
3440}
3441
Reid Spencer266e42b2006-12-23 06:05:41 +00003442ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3443 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003444 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003445 case ICMP_EQ: case ICMP_NE:
3446 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3447 return pred;
3448 case ICMP_UGT: return ICMP_SGT;
3449 case ICMP_ULT: return ICMP_SLT;
3450 case ICMP_UGE: return ICMP_SGE;
3451 case ICMP_ULE: return ICMP_SLE;
3452 }
3453}
3454
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003455ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3456 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003457 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003458 case ICMP_EQ: case ICMP_NE:
3459 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3460 return pred;
3461 case ICMP_SGT: return ICMP_UGT;
3462 case ICMP_SLT: return ICMP_ULT;
3463 case ICMP_SGE: return ICMP_UGE;
3464 case ICMP_SLE: return ICMP_ULE;
3465 }
3466}
3467
Reid Spencer0286bc12007-02-28 22:00:54 +00003468/// Initialize a set of values that all satisfy the condition with C.
3469///
3470ConstantRange
3471ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3472 APInt Lower(C);
3473 APInt Upper(C);
3474 uint32_t BitWidth = C.getBitWidth();
3475 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003476 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003477 case ICmpInst::ICMP_EQ: ++Upper; break;
3478 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003479 case ICmpInst::ICMP_ULT:
3480 Lower = APInt::getMinValue(BitWidth);
3481 // Check for an empty-set condition.
3482 if (Lower == Upper)
3483 return ConstantRange(BitWidth, /*isFullSet=*/false);
3484 break;
3485 case ICmpInst::ICMP_SLT:
3486 Lower = APInt::getSignedMinValue(BitWidth);
3487 // Check for an empty-set condition.
3488 if (Lower == Upper)
3489 return ConstantRange(BitWidth, /*isFullSet=*/false);
3490 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003491 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003492 ++Lower; Upper = APInt::getMinValue(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_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003498 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003499 // Check for an empty-set condition.
3500 if (Lower == Upper)
3501 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003502 break;
3503 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003504 Lower = APInt::getMinValue(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_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003510 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
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_UGE:
3516 Upper = APInt::getMinValue(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 case ICmpInst::ICMP_SGE:
3522 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003523 // Check for a full-set condition.
3524 if (Lower == Upper)
3525 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003526 break;
3527 }
3528 return ConstantRange(Lower, Upper);
3529}
3530
Dan Gohman4e724382008-05-31 02:47:54 +00003531CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003532 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003533 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003534 case ICMP_EQ: case ICMP_NE:
3535 return pred;
3536 case ICMP_SGT: return ICMP_SLT;
3537 case ICMP_SLT: return ICMP_SGT;
3538 case ICMP_SGE: return ICMP_SLE;
3539 case ICMP_SLE: return ICMP_SGE;
3540 case ICMP_UGT: return ICMP_ULT;
3541 case ICMP_ULT: return ICMP_UGT;
3542 case ICMP_UGE: return ICMP_ULE;
3543 case ICMP_ULE: return ICMP_UGE;
3544
Reid Spencerd9436b62006-11-20 01:22:35 +00003545 case FCMP_FALSE: case FCMP_TRUE:
3546 case FCMP_OEQ: case FCMP_ONE:
3547 case FCMP_UEQ: case FCMP_UNE:
3548 case FCMP_ORD: case FCMP_UNO:
3549 return pred;
3550 case FCMP_OGT: return FCMP_OLT;
3551 case FCMP_OLT: return FCMP_OGT;
3552 case FCMP_OGE: return FCMP_OLE;
3553 case FCMP_OLE: return FCMP_OGE;
3554 case FCMP_UGT: return FCMP_ULT;
3555 case FCMP_ULT: return FCMP_UGT;
3556 case FCMP_UGE: return FCMP_ULE;
3557 case FCMP_ULE: return FCMP_UGE;
3558 }
3559}
3560
Reid Spencer266e42b2006-12-23 06:05:41 +00003561bool CmpInst::isUnsigned(unsigned short predicate) {
3562 switch (predicate) {
3563 default: return false;
3564 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3565 case ICmpInst::ICMP_UGE: return true;
3566 }
3567}
3568
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003569bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003570 switch (predicate) {
3571 default: return false;
3572 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3573 case ICmpInst::ICMP_SGE: return true;
3574 }
3575}
3576
3577bool CmpInst::isOrdered(unsigned short predicate) {
3578 switch (predicate) {
3579 default: return false;
3580 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3581 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3582 case FCmpInst::FCMP_ORD: return true;
3583 }
3584}
3585
3586bool CmpInst::isUnordered(unsigned short predicate) {
3587 switch (predicate) {
3588 default: return false;
3589 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3590 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3591 case FCmpInst::FCMP_UNO: return true;
3592 }
3593}
3594
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003595bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3596 switch(predicate) {
3597 default: return false;
3598 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3599 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3600 }
3601}
3602
3603bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3604 switch(predicate) {
3605 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3606 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3607 default: return false;
3608 }
3609}
3610
3611
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003612//===----------------------------------------------------------------------===//
3613// SwitchInst Implementation
3614//===----------------------------------------------------------------------===//
3615
Chris Lattnerbaf00152010-11-17 05:41:46 +00003616void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3617 assert(Value && Default && NumReserved);
3618 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003619 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003620 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003621
Pete Coopereb31b682015-05-21 22:48:54 +00003622 Op<0>() = Value;
3623 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003624}
3625
Chris Lattner2195fc42007-02-24 00:55:48 +00003626/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3627/// switch on and a default destination. The number of additional cases can
3628/// be specified here to make memory allocation more efficient. This
3629/// constructor can also autoinsert before another instruction.
3630SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3631 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003632 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003633 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003634 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003635}
3636
3637/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3638/// switch on and a default destination. The number of additional cases can
3639/// be specified here to make memory allocation more efficient. This
3640/// constructor also autoinserts at the end of the specified BasicBlock.
3641SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3642 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003643 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003644 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003645 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003646}
3647
Misha Brukmanb1c93172005-04-21 23:48:37 +00003648SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003649 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003650 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003651 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003652 Use *OL = getOperandList();
3653 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003654 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003655 OL[i] = InOL[i];
3656 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003657 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003658 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003659}
3660
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003661
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003662/// addCase - Add an entry to the switch instruction...
3663///
Chris Lattner47ac1872005-02-24 05:32:09 +00003664void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003665 unsigned NewCaseIdx = getNumCases();
3666 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003667 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003668 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003669 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003670 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003671 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003672 CaseIt Case(this, NewCaseIdx);
3673 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003674 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003675}
3676
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003677/// removeCase - This method removes the specified case and its successor
3678/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003679void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003680 unsigned idx = i.getCaseIndex();
3681
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003682 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003683
3684 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003685 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003686
Jay Foad14277722011-02-01 09:22:34 +00003687 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003688 if (2 + (idx + 1) * 2 != NumOps) {
3689 OL[2 + idx * 2] = OL[NumOps - 2];
3690 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003691 }
3692
3693 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003694 OL[NumOps-2].set(nullptr);
3695 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003696 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003697}
3698
Jay Foade98f29d2011-04-01 08:00:58 +00003699/// growOperands - grow operands - This grows the operand list in response
3700/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003701///
Jay Foade98f29d2011-04-01 08:00:58 +00003702void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003703 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003704 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003705
3706 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003707 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003708}
3709
3710
3711BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3712 return getSuccessor(idx);
3713}
3714unsigned SwitchInst::getNumSuccessorsV() const {
3715 return getNumSuccessors();
3716}
3717void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3718 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003719}
Chris Lattnerf22be932004-10-15 23:52:53 +00003720
Chris Lattner3ed871f2009-10-27 19:13:16 +00003721//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003722// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003723//===----------------------------------------------------------------------===//
3724
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003725void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003726 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003727 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003728 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003729 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003730 allocHungoffUses(ReservedSpace);
3731
Pete Coopereb31b682015-05-21 22:48:54 +00003732 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003733}
3734
3735
Jay Foade98f29d2011-04-01 08:00:58 +00003736/// growOperands - grow operands - This grows the operand list in response
3737/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003738///
Jay Foade98f29d2011-04-01 08:00:58 +00003739void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003740 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003741 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742
3743 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003744 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003745}
3746
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003747IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3748 Instruction *InsertBefore)
3749: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003750 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751 init(Address, NumCases);
3752}
3753
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003754IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3755 BasicBlock *InsertAtEnd)
3756: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003757 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758 init(Address, NumCases);
3759}
3760
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003761IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003762 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3763 nullptr, IBI.getNumOperands()) {
3764 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003765 Use *OL = getOperandList();
3766 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003767 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3768 OL[i] = InOL[i];
3769 SubclassOptionalData = IBI.SubclassOptionalData;
3770}
3771
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772/// addDestination - Add a destination.
3773///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003774void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003775 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003776 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003777 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003778 // Initialize some new operands.
3779 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003780 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003781 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003782}
3783
3784/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003785/// indirectbr instruction.
3786void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003787 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3788
3789 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003790 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003791
3792 // Replace this value with the last one.
3793 OL[idx+1] = OL[NumOps-1];
3794
3795 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003796 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003797 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798}
3799
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003800BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003801 return getSuccessor(idx);
3802}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003803unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003804 return getNumSuccessors();
3805}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003806void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807 setSuccessor(idx, B);
3808}
3809
3810//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003811// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003812//===----------------------------------------------------------------------===//
3813
Chris Lattnerf22be932004-10-15 23:52:53 +00003814// Define these methods here so vtables don't get emitted into every translation
3815// unit that uses these classes.
3816
Pete Cooper75403d72015-06-24 20:22:23 +00003817GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003818 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003819}
3820
Pete Cooper75403d72015-06-24 20:22:23 +00003821BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003822 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003823}
3824
Pete Cooper75403d72015-06-24 20:22:23 +00003825FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003826 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003830 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003831}
3832
Pete Cooper75403d72015-06-24 20:22:23 +00003833ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003834 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003835}
3836
Pete Cooper75403d72015-06-24 20:22:23 +00003837InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003838 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003839}
3840
Pete Cooper75403d72015-06-24 20:22:23 +00003841AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003842 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3843 (Value *)getOperand(0), getAlignment());
3844 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3845 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003846}
3847
Pete Cooper75403d72015-06-24 20:22:23 +00003848LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003849 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3850 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003851}
3852
Pete Cooper75403d72015-06-24 20:22:23 +00003853StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003854 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003855 getAlignment(), getOrdering(), getSynchScope());
3856
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003857}
3858
Pete Cooper75403d72015-06-24 20:22:23 +00003859AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003860 AtomicCmpXchgInst *Result =
3861 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003862 getSuccessOrdering(), getFailureOrdering(),
3863 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003864 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003865 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003866 return Result;
3867}
3868
Pete Cooper75403d72015-06-24 20:22:23 +00003869AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003870 AtomicRMWInst *Result =
3871 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3872 getOrdering(), getSynchScope());
3873 Result->setVolatile(isVolatile());
3874 return Result;
3875}
3876
Pete Cooper75403d72015-06-24 20:22:23 +00003877FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003878 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3879}
3880
Pete Cooper75403d72015-06-24 20:22:23 +00003881TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003882 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003883}
3884
Pete Cooper75403d72015-06-24 20:22:23 +00003885ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003886 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003887}
3888
Pete Cooper75403d72015-06-24 20:22:23 +00003889SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003890 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003891}
3892
Pete Cooper75403d72015-06-24 20:22:23 +00003893FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003894 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003895}
3896
Pete Cooper75403d72015-06-24 20:22:23 +00003897FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003898 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003899}
3900
Pete Cooper75403d72015-06-24 20:22:23 +00003901UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003902 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003903}
3904
Pete Cooper75403d72015-06-24 20:22:23 +00003905SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003906 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003907}
3908
Pete Cooper75403d72015-06-24 20:22:23 +00003909FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003910 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003911}
3912
Pete Cooper75403d72015-06-24 20:22:23 +00003913FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003914 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003915}
3916
Pete Cooper75403d72015-06-24 20:22:23 +00003917PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003918 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919}
3920
Pete Cooper75403d72015-06-24 20:22:23 +00003921IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003922 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003923}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003924
Pete Cooper75403d72015-06-24 20:22:23 +00003925BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003926 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003927}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003928
Pete Cooper75403d72015-06-24 20:22:23 +00003929AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003930 return new AddrSpaceCastInst(getOperand(0), getType());
3931}
3932
Pete Cooper75403d72015-06-24 20:22:23 +00003933CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003934 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003938 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003939}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003940
Pete Cooper75403d72015-06-24 20:22:23 +00003941VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003942 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003943}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003944
Pete Cooper75403d72015-06-24 20:22:23 +00003945ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003946 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003947}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003948
Pete Cooper75403d72015-06-24 20:22:23 +00003949InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003950 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003954 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003955}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003956
Pete Cooper75403d72015-06-24 20:22:23 +00003957PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003958
Pete Cooper75403d72015-06-24 20:22:23 +00003959LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003960 return new LandingPadInst(*this);
3961}
3962
Pete Cooper75403d72015-06-24 20:22:23 +00003963ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003964 return new(getNumOperands()) ReturnInst(*this);
3965}
3966
Pete Cooper75403d72015-06-24 20:22:23 +00003967BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003968 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003969}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003970
Pete Cooper75403d72015-06-24 20:22:23 +00003971SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003972
Pete Cooper75403d72015-06-24 20:22:23 +00003973IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003974 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003975}
3976
Pete Cooper75403d72015-06-24 20:22:23 +00003977InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003979}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003980
Pete Cooper75403d72015-06-24 20:22:23 +00003981ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003982
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003983CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
3984 return new (getNumOperands()) CleanupEndPadInst(*this);
3985}
3986
David Majnemer654e1302015-07-31 17:58:14 +00003987CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3988 return new (getNumOperands()) CleanupReturnInst(*this);
3989}
3990
3991CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
3992 return new (getNumOperands()) CatchEndPadInst(*this);
3993}
3994
3995CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003996 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003997}
3998
3999CatchPadInst *CatchPadInst::cloneImpl() const {
4000 return new (getNumOperands()) CatchPadInst(*this);
4001}
4002
4003TerminatePadInst *TerminatePadInst::cloneImpl() const {
4004 return new (getNumOperands()) TerminatePadInst(*this);
4005}
4006
4007CleanupPadInst *CleanupPadInst::cloneImpl() const {
4008 return new (getNumOperands()) CleanupPadInst(*this);
4009}
4010
Pete Cooper75403d72015-06-24 20:22:23 +00004011UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004012 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004013 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004014}