blob: 7c3695b15310be8649f85fc9a39e130827c292a0 [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 {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000334 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
335
Bill Wendling749a43d2012-12-30 13:50:49 +0000336 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000337 return true;
338 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000339 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000340 return false;
341}
342
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000343bool CallInst::dataOperandHasImpliedAttr(unsigned i,
344 Attribute::AttrKind A) const {
345
346 // The attribute A can either be directly specified, if the operand in
347 // question is a call argument; or be indirectly implied by the kind of its
348 // containing operand bundle, if the operand is a bundle operand.
349
350 if (i < (getNumArgOperands() + 1))
351 return paramHasAttr(i, A);
352
353 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
354 "Must be either a call argument or an operand bundle!");
355 return getOperandBundleForOperand(i - 1).operandsHaveAttr(A);
356}
357
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000358/// IsConstantOne - Return true only if val is constant int 1
359static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000360 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000361 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
362 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000363}
364
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000365static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000366 BasicBlock *InsertAtEnd, Type *IntPtrTy,
367 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000368 Value *ArraySize, Function *MallocF,
369 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000370 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000371 "createMalloc needs either InsertBefore or InsertAtEnd");
372
373 // malloc(type) becomes:
374 // bitcast (i8* malloc(typeSize)) to type*
375 // malloc(type, arraySize) becomes:
376 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000377 if (!ArraySize)
378 ArraySize = ConstantInt::get(IntPtrTy, 1);
379 else if (ArraySize->getType() != IntPtrTy) {
380 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000381 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
382 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000383 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000384 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
385 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000386 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000388 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389 if (IsConstantOne(AllocSize)) {
390 AllocSize = ArraySize; // Operand * 1 = Operand
391 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
392 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
393 false /*ZExt*/);
394 // Malloc arg is constant product of type size and array size
395 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
396 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000397 // Multiply type size by the array size...
398 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000399 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
400 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000401 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000402 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
403 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406
Victor Hernandez788eaab2009-09-18 19:20:02 +0000407 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 // Create the call to Malloc.
409 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
410 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000411 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000412 Value *MallocFunc = MallocF;
413 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000414 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000415 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000416 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000417 CallInst *MCall = nullptr;
418 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000419 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000420 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000421 Result = MCall;
422 if (Result->getType() != AllocPtrType)
423 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000426 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000427 Result = MCall;
428 if (Result->getType() != AllocPtrType) {
429 InsertAtEnd->getInstList().push_back(MCall);
430 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000432 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000433 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000434 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000435 if (Function *F = dyn_cast<Function>(MallocFunc)) {
436 MCall->setCallingConv(F->getCallingConv());
437 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
438 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000439 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000440
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000441 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000442}
443
444/// CreateMalloc - Generate the IR for a call to malloc:
445/// 1. Compute the malloc call's argument as the specified type's size,
446/// possibly multiplied by the array size if the array size is not
447/// constant 1.
448/// 2. Call malloc with that argument.
449/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000450Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000451 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000453 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000454 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000455 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000456 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000457}
458
459/// CreateMalloc - Generate the IR for a call to malloc:
460/// 1. Compute the malloc call's argument as the specified type's size,
461/// possibly multiplied by the array size if the array size is not
462/// constant 1.
463/// 2. Call malloc with that argument.
464/// 3. Bitcast the result of the malloc call to the specified type.
465/// Note: This function does not add the bitcast to the basic block, that is the
466/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000467Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000468 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Value *AllocSize, Value *ArraySize,
470 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000471 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000472 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000473}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000474
Victor Hernandeze2971492009-10-24 04:23:03 +0000475static Instruction* createFree(Value* Source, Instruction *InsertBefore,
476 BasicBlock *InsertAtEnd) {
477 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
478 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000479 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000480 "Can not free something of nonpointer type!");
481
482 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
483 Module* M = BB->getParent()->getParent();
484
Chris Lattner229907c2011-07-18 04:54:35 +0000485 Type *VoidTy = Type::getVoidTy(M->getContext());
486 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000487 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000488 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000489 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000490 Value *PtrCast = Source;
491 if (InsertBefore) {
492 if (Source->getType() != IntPtrTy)
493 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
494 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
495 } else {
496 if (Source->getType() != IntPtrTy)
497 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
498 Result = CallInst::Create(FreeFunc, PtrCast, "");
499 }
500 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000501 if (Function *F = dyn_cast<Function>(FreeFunc))
502 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000503
504 return Result;
505}
506
507/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000508Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000509 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000510}
511
512/// CreateFree - Generate the IR for a call to the builtin free function.
513/// Note: This function does not add the call to the basic block, that is the
514/// responsibility of the caller.
515Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000516 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000517 assert(FreeCall && "CreateFree did not create a CallInst");
518 return FreeCall;
519}
520
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000521//===----------------------------------------------------------------------===//
522// InvokeInst Implementation
523//===----------------------------------------------------------------------===//
524
David Blaikie3e807092015-05-13 18:35:26 +0000525void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
526 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000527 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000528 const Twine &NameStr) {
529 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000530
Sanjoy Das9303c242015-09-24 19:14:18 +0000531 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
532 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000533 Op<-3>() = Fn;
534 Op<-2>() = IfNormal;
535 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000536
537#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000538 assert(((Args.size() == FTy->getNumParams()) ||
539 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000540 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000541
Jay Foad5bd375a2011-07-15 08:37:34 +0000542 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000543 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000544 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000545 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000546#endif
547
548 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000549
550 auto It = populateBundleOperandInfos(Bundles, Args.size());
551 (void)It;
552 assert(It + 3 == op_end() && "Should add up!");
553
Jay Foad5bd375a2011-07-15 08:37:34 +0000554 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000555}
556
Misha Brukmanb1c93172005-04-21 23:48:37 +0000557InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000558 : TerminatorInst(II.getType(), Instruction::Invoke,
559 OperandTraits<InvokeInst>::op_end(this) -
560 II.getNumOperands(),
561 II.getNumOperands()),
562 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000563 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000564 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000565 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
566 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000567 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000568}
569
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
571 return getSuccessor(idx);
572}
573unsigned InvokeInst::getNumSuccessorsV() const {
574 return getNumSuccessors();
575}
576void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
577 return setSuccessor(idx, B);
578}
579
Michael Gottesman41748d72013-06-27 00:25:01 +0000580bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000581 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000582 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000583
584 // Operand bundles override attributes on the called function, but don't
585 // override attributes directly present on the invoke instruction.
586 if (isFnAttrDisallowedByOpBundle(A))
587 return false;
588
Bill Wendling375eb1f2012-10-09 00:28:54 +0000589 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000590 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000591 return false;
592}
593
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000594bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000595 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
596
Bill Wendling749a43d2012-12-30 13:50:49 +0000597 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000598 return true;
599 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000600 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000601 return false;
602}
603
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000604bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
605 Attribute::AttrKind A) const {
606 // The attribute A can either be directly specified, if the operand in
607 // question is an invoke argument; or be indirectly implied by the kind of its
608 // containing operand bundle, if the operand is a bundle operand.
609
610 if (i < (getNumArgOperands() + 1))
611 return paramHasAttr(i, A);
612
613 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
614 "Must be either an invoke argument or an operand bundle!");
615 return getOperandBundleForOperand(i - 1).operandsHaveAttr(A);
616}
617
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000618void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000619 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000620 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000621 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000622}
623
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000624void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000625 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000626 AttrBuilder B(attr);
627 PAL = PAL.removeAttributes(getContext(), i,
628 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000629 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000630}
631
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000632void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
633 AttributeSet PAL = getAttributes();
634 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
635 setAttributes(PAL);
636}
637
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000638void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
639 AttributeSet PAL = getAttributes();
640 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
641 setAttributes(PAL);
642}
643
Bill Wendlingfae14752011-08-12 20:24:12 +0000644LandingPadInst *InvokeInst::getLandingPadInst() const {
645 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
646}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000647
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648//===----------------------------------------------------------------------===//
649// ReturnInst Implementation
650//===----------------------------------------------------------------------===//
651
Chris Lattner2195fc42007-02-24 00:55:48 +0000652ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000653 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000654 OperandTraits<ReturnInst>::op_end(this) -
655 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000656 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000657 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000658 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000659 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000660}
661
Owen Anderson55f1c092009-08-13 21:58:54 +0000662ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
663 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000664 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
665 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000666 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000667 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000668}
Owen Anderson55f1c092009-08-13 21:58:54 +0000669ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
670 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000671 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
672 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000673 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000674 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000675}
Owen Anderson55f1c092009-08-13 21:58:54 +0000676ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
677 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000678 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000679}
680
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000681unsigned ReturnInst::getNumSuccessorsV() const {
682 return getNumSuccessors();
683}
684
Devang Patelae682fb2008-02-26 17:56:20 +0000685/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
686/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000688 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689}
690
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000691BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000692 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693}
694
Devang Patel59643e52008-02-23 00:35:18 +0000695ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000696}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000699// ResumeInst Implementation
700//===----------------------------------------------------------------------===//
701
702ResumeInst::ResumeInst(const ResumeInst &RI)
703 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
704 OperandTraits<ResumeInst>::op_begin(this), 1) {
705 Op<0>() = RI.Op<0>();
706}
707
708ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
709 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
710 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
711 Op<0>() = Exn;
712}
713
714ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
715 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
716 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
717 Op<0>() = Exn;
718}
719
720unsigned ResumeInst::getNumSuccessorsV() const {
721 return getNumSuccessors();
722}
723
724void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
725 llvm_unreachable("ResumeInst has no successors!");
726}
727
728BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
729 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000730}
731
732//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000733// CleanupEndPadInst Implementation
734//===----------------------------------------------------------------------===//
735
736CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
737 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
738 OperandTraits<CleanupEndPadInst>::op_end(this) -
739 CEPI.getNumOperands(),
740 CEPI.getNumOperands()) {
741 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
742 setCleanupPad(CEPI.getCleanupPad());
743 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
744 setUnwindDest(UnwindDest);
745}
746
747void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
748 setCleanupPad(CleanupPad);
749 if (UnwindBB) {
750 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
751 setUnwindDest(UnwindBB);
752 }
753}
754
755CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
756 BasicBlock *UnwindBB, unsigned Values,
757 Instruction *InsertBefore)
758 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
759 Instruction::CleanupEndPad,
760 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
761 Values, InsertBefore) {
762 init(CleanupPad, UnwindBB);
763}
764
765CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
766 BasicBlock *UnwindBB, unsigned Values,
767 BasicBlock *InsertAtEnd)
768 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
769 Instruction::CleanupEndPad,
770 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
771 Values, InsertAtEnd) {
772 init(CleanupPad, UnwindBB);
773}
774
775BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
776 assert(Idx == 0);
777 return getUnwindDest();
778}
779unsigned CleanupEndPadInst::getNumSuccessorsV() const {
780 return getNumSuccessors();
781}
782void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
783 assert(Idx == 0);
784 setUnwindDest(B);
785}
786
787//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000788// CleanupReturnInst Implementation
789//===----------------------------------------------------------------------===//
790
791CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
792 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
793 OperandTraits<CleanupReturnInst>::op_end(this) -
794 CRI.getNumOperands(),
795 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000796 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000797 Op<-1>() = CRI.Op<-1>();
798 if (CRI.hasUnwindDest())
799 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000800}
801
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000802void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000803 if (UnwindBB)
804 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000805
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000806 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000807 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000808 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000809}
810
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000811CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000812 BasicBlock *UnwindBB, unsigned Values,
813 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000814 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
815 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000816 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
817 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000818 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000819}
820
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000821CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000822 BasicBlock *UnwindBB, unsigned Values,
823 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000824 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
825 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000826 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
827 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000828 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000829}
830
831BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
832 assert(Idx == 0);
833 return getUnwindDest();
834}
835unsigned CleanupReturnInst::getNumSuccessorsV() const {
836 return getNumSuccessors();
837}
838void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
839 assert(Idx == 0);
840 setUnwindDest(B);
841}
842
843//===----------------------------------------------------------------------===//
844// CatchEndPadInst Implementation
845//===----------------------------------------------------------------------===//
846
847CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
848 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
849 OperandTraits<CatchEndPadInst>::op_end(this) -
850 CRI.getNumOperands(),
851 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000852 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000853 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
854 setUnwindDest(UnwindDest);
855}
856
857void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000858 if (UnwindBB) {
859 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
860 setUnwindDest(UnwindBB);
861 }
862}
863
864CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000865 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000866 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
867 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
868 Values, InsertBefore) {
869 init(UnwindBB);
870}
871
872CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000873 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000874 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
875 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
876 Values, InsertAtEnd) {
877 init(UnwindBB);
878}
879
880BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
881 assert(Idx == 0);
882 return getUnwindDest();
883}
884unsigned CatchEndPadInst::getNumSuccessorsV() const {
885 return getNumSuccessors();
886}
887void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
888 assert(Idx == 0);
889 setUnwindDest(B);
890}
891
892//===----------------------------------------------------------------------===//
893// CatchReturnInst Implementation
894//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000895void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
896 Op<0>() = CatchPad;
897 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000898}
David Majnemer654e1302015-07-31 17:58:14 +0000899
900CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
901 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000902 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
903 Op<0>() = CRI.Op<0>();
904 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000905}
906
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000907CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000908 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000909 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000910 OperandTraits<CatchReturnInst>::op_begin(this), 2,
911 InsertBefore) {
912 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000913}
914
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000915CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000916 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000917 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000918 OperandTraits<CatchReturnInst>::op_begin(this), 2,
919 InsertAtEnd) {
920 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000921}
922
923BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000924 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000925 return getSuccessor();
926}
927unsigned CatchReturnInst::getNumSuccessorsV() const {
928 return getNumSuccessors();
929}
930void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000931 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000932 setSuccessor(B);
933}
934
935//===----------------------------------------------------------------------===//
936// CatchPadInst Implementation
937//===----------------------------------------------------------------------===//
938void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000939 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000940 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
941 Op<-2>() = IfNormal;
942 Op<-1>() = IfException;
943 std::copy(Args.begin(), Args.end(), op_begin());
944 setName(NameStr);
945}
946
947CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
948 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
949 OperandTraits<CatchPadInst>::op_end(this) -
950 CPI.getNumOperands(),
951 CPI.getNumOperands()) {
952 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
953}
954
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000955CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
956 ArrayRef<Value *> Args, unsigned Values,
957 const Twine &NameStr, Instruction *InsertBefore)
958 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
959 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000960 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
961 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000962 init(IfNormal, IfException, Args, NameStr);
963}
964
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000965CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
966 ArrayRef<Value *> Args, unsigned Values,
967 const Twine &NameStr, BasicBlock *InsertAtEnd)
968 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
969 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000970 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
971 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000972 init(IfNormal, IfException, Args, NameStr);
973}
974
975BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
976 return getSuccessor(Idx);
977}
978unsigned CatchPadInst::getNumSuccessorsV() const {
979 return getNumSuccessors();
980}
981void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
982 return setSuccessor(Idx, B);
983}
984
985//===----------------------------------------------------------------------===//
986// TerminatePadInst Implementation
987//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000988void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000989 if (BB)
990 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
991 if (BB)
992 Op<-1>() = BB;
993 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +0000994}
995
996TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
997 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
998 OperandTraits<TerminatePadInst>::op_end(this) -
999 TPI.getNumOperands(),
1000 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +00001001 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +00001002 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
1003}
1004
1005TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001006 ArrayRef<Value *> Args, unsigned Values,
1007 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001008 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1009 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1010 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001011 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001012}
1013
1014TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001015 ArrayRef<Value *> Args, unsigned Values,
1016 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001017 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1018 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1019 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001020 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001021}
1022
1023BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
1024 assert(Idx == 0);
1025 return getUnwindDest();
1026}
1027unsigned TerminatePadInst::getNumSuccessorsV() const {
1028 return getNumSuccessors();
1029}
1030void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1031 assert(Idx == 0);
1032 return setUnwindDest(B);
1033}
1034
1035//===----------------------------------------------------------------------===//
1036// CleanupPadInst Implementation
1037//===----------------------------------------------------------------------===//
1038void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1039 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1040 std::copy(Args.begin(), Args.end(), op_begin());
1041 setName(NameStr);
1042}
1043
1044CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1045 : Instruction(CPI.getType(), Instruction::CleanupPad,
1046 OperandTraits<CleanupPadInst>::op_end(this) -
1047 CPI.getNumOperands(),
1048 CPI.getNumOperands()) {
1049 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
1050}
1051
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001052CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001053 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001054 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001055 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1056 Args.size(), InsertBefore) {
1057 init(Args, NameStr);
1058}
1059
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001060CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001061 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001062 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001063 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1064 Args.size(), InsertAtEnd) {
1065 init(Args, NameStr);
1066}
1067
1068//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001069// UnreachableInst Implementation
1070//===----------------------------------------------------------------------===//
1071
Owen Anderson55f1c092009-08-13 21:58:54 +00001072UnreachableInst::UnreachableInst(LLVMContext &Context,
1073 Instruction *InsertBefore)
1074 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001075 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001076}
Owen Anderson55f1c092009-08-13 21:58:54 +00001077UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1078 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001079 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001080}
1081
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001082unsigned UnreachableInst::getNumSuccessorsV() const {
1083 return getNumSuccessors();
1084}
1085
1086void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001087 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001088}
1089
1090BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001091 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001092}
1093
1094//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001095// BranchInst Implementation
1096//===----------------------------------------------------------------------===//
1097
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001098void BranchInst::AssertOK() {
1099 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001100 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001101 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001102}
1103
Chris Lattner2195fc42007-02-24 00:55:48 +00001104BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001105 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001106 OperandTraits<BranchInst>::op_end(this) - 1,
1107 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001108 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001109 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001110}
1111BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1112 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001114 OperandTraits<BranchInst>::op_end(this) - 3,
1115 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001116 Op<-1>() = IfTrue;
1117 Op<-2>() = IfFalse;
1118 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001119#ifndef NDEBUG
1120 AssertOK();
1121#endif
1122}
1123
1124BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001125 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001126 OperandTraits<BranchInst>::op_end(this) - 1,
1127 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001128 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001129 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001130}
1131
1132BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1133 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001134 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 OperandTraits<BranchInst>::op_end(this) - 3,
1136 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001137 Op<-1>() = IfTrue;
1138 Op<-2>() = IfFalse;
1139 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001140#ifndef NDEBUG
1141 AssertOK();
1142#endif
1143}
1144
1145
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001146BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001147 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001148 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1149 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001150 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001151 if (BI.getNumOperands() != 1) {
1152 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001153 Op<-3>() = BI.Op<-3>();
1154 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001156 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001157}
1158
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001159void BranchInst::swapSuccessors() {
1160 assert(isConditional() &&
1161 "Cannot swap successors of an unconditional branch");
1162 Op<-1>().swap(Op<-2>());
1163
1164 // Update profile metadata if present and it matches our structural
1165 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001166 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001167 if (!ProfileData || ProfileData->getNumOperands() != 3)
1168 return;
1169
1170 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001171 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1172 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001173 setMetadata(LLVMContext::MD_prof,
1174 MDNode::get(ProfileData->getContext(), Ops));
1175}
1176
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001177BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1178 return getSuccessor(idx);
1179}
1180unsigned BranchInst::getNumSuccessorsV() const {
1181 return getNumSuccessors();
1182}
1183void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1184 setSuccessor(idx, B);
1185}
1186
1187
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001188//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001189// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190//===----------------------------------------------------------------------===//
1191
Owen Andersonb6b25302009-07-14 23:09:55 +00001192static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001194 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001195 else {
1196 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001197 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001198 assert(Amt->getType()->isIntegerTy() &&
1199 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001200 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001201 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202}
1203
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001204AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1205 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001206
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001207AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1208 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001209
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001210AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001211 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001212 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001213
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001214AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001215 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001216 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001217
Chris Lattner229907c2011-07-18 04:54:35 +00001218AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001219 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001220 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1221 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1222 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001223 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001224 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001225 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001226}
1227
Chris Lattner229907c2011-07-18 04:54:35 +00001228AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001229 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001230 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1231 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1232 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001233 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001234 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001235 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236}
1237
Gordon Henriksen14a55692007-12-10 02:14:30 +00001238// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001239AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001240}
1241
Victor Hernandez8acf2952009-10-23 21:09:37 +00001242void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001243 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001244 assert(Align <= MaximumAlignment &&
1245 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001246 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1247 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001248 assert(getAlignment() == Align && "Alignment representation error!");
1249}
1250
Victor Hernandez8acf2952009-10-23 21:09:37 +00001251bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001252 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001253 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001254 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001255}
1256
Chris Lattner8b291e62008-11-26 02:54:17 +00001257/// isStaticAlloca - Return true if this alloca is in the entry block of the
1258/// function and is a constant size. If so, the code generator will fold it
1259/// into the prolog/epilog code, so it is basically free.
1260bool AllocaInst::isStaticAlloca() const {
1261 // Must be constant size.
1262 if (!isa<ConstantInt>(getArraySize())) return false;
1263
1264 // Must be in the entry block.
1265 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001266 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001267}
1268
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001269//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001270// LoadInst Implementation
1271//===----------------------------------------------------------------------===//
1272
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001273void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001274 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001275 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001276 assert(!(isAtomic() && getAlignment() == 0) &&
1277 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001278}
1279
Daniel Dunbar4975db62009-07-25 04:41:11 +00001280LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001281 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001282
Daniel Dunbar4975db62009-07-25 04:41:11 +00001283LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001284 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285
David Blaikie0c28fd72015-05-20 21:46:30 +00001286LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001287 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001288 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001289
1290LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1291 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001292 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001293
David Blaikieb7a029872015-04-17 19:56:21 +00001294LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001295 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001296 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001297 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001299LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001300 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001301 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001302}
1303
David Blaikie15d9a4c2015-04-06 20:59:48 +00001304LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001305 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001306 SynchronizationScope SynchScope, Instruction *InsertBef)
1307 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001308 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001309 setVolatile(isVolatile);
1310 setAlignment(Align);
1311 setAtomic(Order, SynchScope);
1312 AssertOK();
1313 setName(Name);
1314}
1315
1316LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1317 unsigned Align, AtomicOrdering Order,
1318 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001320 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001321 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001322 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001323 setAlignment(Align);
1324 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001325 AssertOK();
1326 setName(Name);
1327}
1328
Daniel Dunbar27096822009-08-11 18:11:15 +00001329LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1330 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1331 Load, Ptr, InsertBef) {
1332 setVolatile(false);
1333 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001334 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001335 AssertOK();
1336 if (Name && Name[0]) setName(Name);
1337}
1338
1339LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1340 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1341 Load, Ptr, InsertAE) {
1342 setVolatile(false);
1343 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001344 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001345 AssertOK();
1346 if (Name && Name[0]) setName(Name);
1347}
1348
David Blaikie0c28fd72015-05-20 21:46:30 +00001349LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001350 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001351 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1352 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001353 setVolatile(isVolatile);
1354 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001355 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001356 AssertOK();
1357 if (Name && Name[0]) setName(Name);
1358}
1359
1360LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1361 BasicBlock *InsertAE)
1362 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1363 Load, Ptr, InsertAE) {
1364 setVolatile(isVolatile);
1365 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001366 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001367 AssertOK();
1368 if (Name && Name[0]) setName(Name);
1369}
1370
Christopher Lamb84485702007-04-22 19:24:39 +00001371void LoadInst::setAlignment(unsigned Align) {
1372 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001373 assert(Align <= MaximumAlignment &&
1374 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001375 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001376 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001377 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001378}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001379
1380//===----------------------------------------------------------------------===//
1381// StoreInst Implementation
1382//===----------------------------------------------------------------------===//
1383
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001384void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001385 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001386 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001387 "Ptr must have pointer type!");
1388 assert(getOperand(0)->getType() ==
1389 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001390 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001391 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001392 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001393}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001394
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001395StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001396 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001397
1398StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001399 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001400
Misha Brukmanb1c93172005-04-21 23:48:37 +00001401StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001402 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001403 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001404
1405StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001406 BasicBlock *InsertAtEnd)
1407 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1408
1409StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1410 Instruction *InsertBefore)
1411 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1412 InsertBefore) {}
1413
1414StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1415 BasicBlock *InsertAtEnd)
1416 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1417 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001418
Misha Brukmanb1c93172005-04-21 23:48:37 +00001419StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001420 unsigned Align, AtomicOrdering Order,
1421 SynchronizationScope SynchScope,
1422 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001423 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001424 OperandTraits<StoreInst>::op_begin(this),
1425 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001426 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001427 Op<0>() = val;
1428 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001429 setVolatile(isVolatile);
1430 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001431 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001432 AssertOK();
1433}
1434
1435StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001436 unsigned Align, AtomicOrdering Order,
1437 SynchronizationScope SynchScope,
1438 BasicBlock *InsertAtEnd)
1439 : Instruction(Type::getVoidTy(val->getContext()), Store,
1440 OperandTraits<StoreInst>::op_begin(this),
1441 OperandTraits<StoreInst>::operands(this),
1442 InsertAtEnd) {
1443 Op<0>() = val;
1444 Op<1>() = addr;
1445 setVolatile(isVolatile);
1446 setAlignment(Align);
1447 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001448 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001449}
1450
Christopher Lamb84485702007-04-22 19:24:39 +00001451void StoreInst::setAlignment(unsigned Align) {
1452 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001453 assert(Align <= MaximumAlignment &&
1454 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001455 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001456 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001457 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001458}
1459
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001460//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001461// AtomicCmpXchgInst Implementation
1462//===----------------------------------------------------------------------===//
1463
1464void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001465 AtomicOrdering SuccessOrdering,
1466 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001467 SynchronizationScope SynchScope) {
1468 Op<0>() = Ptr;
1469 Op<1>() = Cmp;
1470 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001471 setSuccessOrdering(SuccessOrdering);
1472 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001473 setSynchScope(SynchScope);
1474
1475 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1476 "All operands must be non-null!");
1477 assert(getOperand(0)->getType()->isPointerTy() &&
1478 "Ptr must have pointer type!");
1479 assert(getOperand(1)->getType() ==
1480 cast<PointerType>(getOperand(0)->getType())->getElementType()
1481 && "Ptr must be a pointer to Cmp type!");
1482 assert(getOperand(2)->getType() ==
1483 cast<PointerType>(getOperand(0)->getType())->getElementType()
1484 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001485 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001486 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001487 assert(FailureOrdering != NotAtomic &&
1488 "AtomicCmpXchg instructions must be atomic!");
1489 assert(SuccessOrdering >= FailureOrdering &&
1490 "AtomicCmpXchg success ordering must be at least as strong as fail");
1491 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1492 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001493}
1494
1495AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001496 AtomicOrdering SuccessOrdering,
1497 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001498 SynchronizationScope SynchScope,
1499 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001500 : Instruction(
1501 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1502 nullptr),
1503 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1504 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001505 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001506}
1507
1508AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001509 AtomicOrdering SuccessOrdering,
1510 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001511 SynchronizationScope SynchScope,
1512 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001513 : Instruction(
1514 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1515 nullptr),
1516 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1517 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001518 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001519}
Tim Northover420a2162014-06-13 14:24:07 +00001520
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001521//===----------------------------------------------------------------------===//
1522// AtomicRMWInst Implementation
1523//===----------------------------------------------------------------------===//
1524
1525void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1526 AtomicOrdering Ordering,
1527 SynchronizationScope SynchScope) {
1528 Op<0>() = Ptr;
1529 Op<1>() = Val;
1530 setOperation(Operation);
1531 setOrdering(Ordering);
1532 setSynchScope(SynchScope);
1533
1534 assert(getOperand(0) && getOperand(1) &&
1535 "All operands must be non-null!");
1536 assert(getOperand(0)->getType()->isPointerTy() &&
1537 "Ptr must have pointer type!");
1538 assert(getOperand(1)->getType() ==
1539 cast<PointerType>(getOperand(0)->getType())->getElementType()
1540 && "Ptr must be a pointer to Val type!");
1541 assert(Ordering != NotAtomic &&
1542 "AtomicRMW instructions must be atomic!");
1543}
1544
1545AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1546 AtomicOrdering Ordering,
1547 SynchronizationScope SynchScope,
1548 Instruction *InsertBefore)
1549 : Instruction(Val->getType(), AtomicRMW,
1550 OperandTraits<AtomicRMWInst>::op_begin(this),
1551 OperandTraits<AtomicRMWInst>::operands(this),
1552 InsertBefore) {
1553 Init(Operation, Ptr, Val, Ordering, SynchScope);
1554}
1555
1556AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1557 AtomicOrdering Ordering,
1558 SynchronizationScope SynchScope,
1559 BasicBlock *InsertAtEnd)
1560 : Instruction(Val->getType(), AtomicRMW,
1561 OperandTraits<AtomicRMWInst>::op_begin(this),
1562 OperandTraits<AtomicRMWInst>::operands(this),
1563 InsertAtEnd) {
1564 Init(Operation, Ptr, Val, Ordering, SynchScope);
1565}
1566
1567//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001568// FenceInst Implementation
1569//===----------------------------------------------------------------------===//
1570
1571FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1572 SynchronizationScope SynchScope,
1573 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001574 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001575 setOrdering(Ordering);
1576 setSynchScope(SynchScope);
1577}
1578
1579FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1580 SynchronizationScope SynchScope,
1581 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001582 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001583 setOrdering(Ordering);
1584 setSynchScope(SynchScope);
1585}
1586
1587//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001588// GetElementPtrInst Implementation
1589//===----------------------------------------------------------------------===//
1590
Jay Foadd1b78492011-07-25 09:48:08 +00001591void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001592 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001593 assert(getNumOperands() == 1 + IdxList.size() &&
1594 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001595 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001596 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001597 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001598}
1599
Gabor Greiff6caff662008-05-10 08:32:32 +00001600GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001601 : Instruction(GEPI.getType(), GetElementPtr,
1602 OperandTraits<GetElementPtrInst>::op_end(this) -
1603 GEPI.getNumOperands(),
1604 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001605 SourceElementType(GEPI.SourceElementType),
1606 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001607 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001608 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001609}
1610
Chris Lattner6090a422009-03-09 04:46:40 +00001611/// getIndexedType - Returns the type of the element that would be accessed with
1612/// a gep instruction with the specified parameters.
1613///
1614/// The Idxs pointer should point to a continuous piece of memory containing the
1615/// indices, either as Value* or uint64_t.
1616///
1617/// A null type is returned if the indices are invalid for the specified
1618/// pointer type.
1619///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001620template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001621static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001622 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001623 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001624 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001625
Chris Lattner6090a422009-03-09 04:46:40 +00001626 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001627 // it cannot be 'stepped over'.
1628 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001629 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001630
Dan Gohman1ecaf452008-05-31 00:58:22 +00001631 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001632 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001633 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001634 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001635 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001636 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001637 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001638 }
Craig Topperc6207612014-04-09 06:08:46 +00001639 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640}
1641
David Blaikied288fb82015-03-30 21:41:43 +00001642Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1643 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001644}
1645
David Blaikied288fb82015-03-30 21:41:43 +00001646Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001647 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001648 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001649}
1650
David Blaikied288fb82015-03-30 21:41:43 +00001651Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1652 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001653}
1654
Chris Lattner45f15572007-04-14 00:12:57 +00001655/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1656/// zeros. If so, the result pointer and the first operand have the same
1657/// value, just potentially different types.
1658bool GetElementPtrInst::hasAllZeroIndices() const {
1659 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1660 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1661 if (!CI->isZero()) return false;
1662 } else {
1663 return false;
1664 }
1665 }
1666 return true;
1667}
1668
Chris Lattner27058292007-04-27 20:35:56 +00001669/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1670/// constant integers. If so, the result pointer and the first operand have
1671/// a constant offset between them.
1672bool GetElementPtrInst::hasAllConstantIndices() const {
1673 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1674 if (!isa<ConstantInt>(getOperand(i)))
1675 return false;
1676 }
1677 return true;
1678}
1679
Dan Gohman1b849082009-09-07 23:54:19 +00001680void GetElementPtrInst::setIsInBounds(bool B) {
1681 cast<GEPOperator>(this)->setIsInBounds(B);
1682}
Chris Lattner45f15572007-04-14 00:12:57 +00001683
Nick Lewycky28a5f252009-09-27 21:33:04 +00001684bool GetElementPtrInst::isInBounds() const {
1685 return cast<GEPOperator>(this)->isInBounds();
1686}
1687
Chandler Carruth1e140532012-12-11 10:29:10 +00001688bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1689 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001690 // Delegate to the generic GEPOperator implementation.
1691 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001692}
1693
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001695// ExtractElementInst Implementation
1696//===----------------------------------------------------------------------===//
1697
1698ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001699 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001700 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001701 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001702 ExtractElement,
1703 OperandTraits<ExtractElementInst>::op_begin(this),
1704 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001705 assert(isValidOperands(Val, Index) &&
1706 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001707 Op<0>() = Val;
1708 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001709 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001710}
1711
1712ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001713 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001715 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001716 ExtractElement,
1717 OperandTraits<ExtractElementInst>::op_begin(this),
1718 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001719 assert(isValidOperands(Val, Index) &&
1720 "Invalid extractelement instruction operands!");
1721
Gabor Greif2d3024d2008-05-26 21:33:52 +00001722 Op<0>() = Val;
1723 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001724 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001725}
1726
Chris Lattner65511ff2006-10-05 06:24:58 +00001727
Chris Lattner54865b32006-04-08 04:05:48 +00001728bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001729 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001730 return false;
1731 return true;
1732}
1733
1734
Robert Bocchino23004482006-01-10 19:05:34 +00001735//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001736// InsertElementInst Implementation
1737//===----------------------------------------------------------------------===//
1738
Chris Lattner54865b32006-04-08 04:05:48 +00001739InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001740 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001741 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001742 : Instruction(Vec->getType(), InsertElement,
1743 OperandTraits<InsertElementInst>::op_begin(this),
1744 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001745 assert(isValidOperands(Vec, Elt, Index) &&
1746 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001747 Op<0>() = Vec;
1748 Op<1>() = Elt;
1749 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001750 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001751}
1752
Chris Lattner54865b32006-04-08 04:05:48 +00001753InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001754 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001755 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001756 : Instruction(Vec->getType(), InsertElement,
1757 OperandTraits<InsertElementInst>::op_begin(this),
1758 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001759 assert(isValidOperands(Vec, Elt, Index) &&
1760 "Invalid insertelement instruction operands!");
1761
Gabor Greif2d3024d2008-05-26 21:33:52 +00001762 Op<0>() = Vec;
1763 Op<1>() = Elt;
1764 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001765 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001766}
1767
Chris Lattner54865b32006-04-08 04:05:48 +00001768bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1769 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001770 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001771 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001772
Reid Spencerd84d35b2007-02-15 02:26:10 +00001773 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001774 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001775
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001776 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001777 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001778 return true;
1779}
1780
1781
Robert Bocchinoca27f032006-01-17 20:07:22 +00001782//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001783// ShuffleVectorInst Implementation
1784//===----------------------------------------------------------------------===//
1785
1786ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001787 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001788 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001789: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001790 cast<VectorType>(Mask->getType())->getNumElements()),
1791 ShuffleVector,
1792 OperandTraits<ShuffleVectorInst>::op_begin(this),
1793 OperandTraits<ShuffleVectorInst>::operands(this),
1794 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001795 assert(isValidOperands(V1, V2, Mask) &&
1796 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001797 Op<0>() = V1;
1798 Op<1>() = V2;
1799 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001800 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001801}
1802
1803ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001804 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001805 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001806: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1807 cast<VectorType>(Mask->getType())->getNumElements()),
1808 ShuffleVector,
1809 OperandTraits<ShuffleVectorInst>::op_begin(this),
1810 OperandTraits<ShuffleVectorInst>::operands(this),
1811 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001812 assert(isValidOperands(V1, V2, Mask) &&
1813 "Invalid shuffle vector instruction operands!");
1814
Gabor Greif2d3024d2008-05-26 21:33:52 +00001815 Op<0>() = V1;
1816 Op<1>() = V2;
1817 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001818 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001819}
1820
Mon P Wang25f01062008-11-10 04:46:22 +00001821bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001822 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001823 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001824 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001825 return false;
1826
Chris Lattner1dcb6542012-01-25 23:49:49 +00001827 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001828 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001829 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001830 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001831
1832 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001833 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1834 return true;
1835
Nate Begeman60a31c32010-08-13 00:16:46 +00001836 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001837 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001838 for (Value *Op : MV->operands()) {
1839 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001840 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001841 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001842 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001843 return false;
1844 }
1845 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001846 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001847 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001848
1849 if (const ConstantDataSequential *CDS =
1850 dyn_cast<ConstantDataSequential>(Mask)) {
1851 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1852 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1853 if (CDS->getElementAsInteger(i) >= V1Size*2)
1854 return false;
1855 return true;
1856 }
1857
1858 // The bitcode reader can create a place holder for a forward reference
1859 // used as the shuffle mask. When this occurs, the shuffle mask will
1860 // fall into this case and fail. To avoid this error, do this bit of
1861 // ugliness to allow such a mask pass.
1862 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1863 if (CE->getOpcode() == Instruction::UserOp1)
1864 return true;
1865
1866 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001867}
1868
Chris Lattnerf724e342008-03-02 05:28:33 +00001869/// getMaskValue - Return the index from the shuffle mask for the specified
1870/// output result. This is either -1 if the element is undef or a number less
1871/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001872int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1873 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1874 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001875 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001876 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001877 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001878 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001879 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001880}
1881
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882/// getShuffleMask - Return the full mask for this instruction, where each
1883/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001884void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1885 SmallVectorImpl<int> &Result) {
1886 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001887
Chris Lattnercf129702012-01-26 02:51:13 +00001888 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001889 for (unsigned i = 0; i != NumElts; ++i)
1890 Result.push_back(CDS->getElementAsInteger(i));
1891 return;
1892 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001893 for (unsigned i = 0; i != NumElts; ++i) {
1894 Constant *C = Mask->getAggregateElement(i);
1895 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001896 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001897 }
1898}
1899
1900
Dan Gohman12fce772008-05-15 19:50:34 +00001901//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001902// InsertValueInst Class
1903//===----------------------------------------------------------------------===//
1904
Jay Foad57aa6362011-07-13 10:26:04 +00001905void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1906 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001907 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001908
1909 // There's no fundamental reason why we require at least one index
1910 // (other than weirdness with &*IdxBegin being invalid; see
1911 // getelementptr's init routine for example). But there's no
1912 // present need to support it.
1913 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1914
1915 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001916 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001917 Op<0>() = Agg;
1918 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001919
Jay Foad57aa6362011-07-13 10:26:04 +00001920 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001921 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001922}
1923
1924InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001925 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001926 OperandTraits<InsertValueInst>::op_begin(this), 2),
1927 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001928 Op<0>() = IVI.getOperand(0);
1929 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001930 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001931}
1932
1933//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001934// ExtractValueInst Class
1935//===----------------------------------------------------------------------===//
1936
Jay Foad57aa6362011-07-13 10:26:04 +00001937void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001938 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001939
Jay Foad57aa6362011-07-13 10:26:04 +00001940 // There's no fundamental reason why we require at least one index.
1941 // But there's no present need to support it.
1942 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001943
Jay Foad57aa6362011-07-13 10:26:04 +00001944 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001945 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001946}
1947
1948ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001949 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001950 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001951 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001952}
1953
Dan Gohman12fce772008-05-15 19:50:34 +00001954// getIndexedType - Returns the type of the element that would be extracted
1955// with an extractvalue instruction with the specified parameters.
1956//
1957// A null type is returned if the indices are invalid for the specified
1958// pointer type.
1959//
Chris Lattner229907c2011-07-18 04:54:35 +00001960Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001961 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001962 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001963 // We can't use CompositeType::indexValid(Index) here.
1964 // indexValid() always returns true for arrays because getelementptr allows
1965 // out-of-bounds indices. Since we don't allow those for extractvalue and
1966 // insertvalue we need to check array indexing manually.
1967 // Since the only other types we can index into are struct types it's just
1968 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001969 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001970 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001971 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001972 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001973 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001974 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001975 } else {
1976 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001977 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001978 }
1979
1980 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001981 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001982 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001983}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001984
1985//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001986// BinaryOperator Class
1987//===----------------------------------------------------------------------===//
1988
Chris Lattner2195fc42007-02-24 00:55:48 +00001989BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001990 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001991 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001992 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001993 OperandTraits<BinaryOperator>::op_begin(this),
1994 OperandTraits<BinaryOperator>::operands(this),
1995 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001996 Op<0>() = S1;
1997 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001998 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001999 setName(Name);
2000}
2001
2002BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002003 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002004 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002005 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002006 OperandTraits<BinaryOperator>::op_begin(this),
2007 OperandTraits<BinaryOperator>::operands(this),
2008 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002009 Op<0>() = S1;
2010 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002011 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002012 setName(Name);
2013}
2014
2015
2016void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002017 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002018 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002019 assert(LHS->getType() == RHS->getType() &&
2020 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002021#ifndef NDEBUG
2022 switch (iType) {
2023 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002024 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002025 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002027 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002028 "Tried to create an integer operation on a non-integer type!");
2029 break;
2030 case FAdd: case FSub:
2031 case FMul:
2032 assert(getType() == LHS->getType() &&
2033 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002034 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002035 "Tried to create a floating-point operation on a "
2036 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002037 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002038 case UDiv:
2039 case SDiv:
2040 assert(getType() == LHS->getType() &&
2041 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002042 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002043 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002044 "Incorrect operand type (not integer) for S/UDIV");
2045 break;
2046 case FDiv:
2047 assert(getType() == LHS->getType() &&
2048 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002049 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002050 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002051 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002052 case URem:
2053 case SRem:
2054 assert(getType() == LHS->getType() &&
2055 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002056 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002057 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002058 "Incorrect operand type (not integer) for S/UREM");
2059 break;
2060 case FRem:
2061 assert(getType() == LHS->getType() &&
2062 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002063 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002064 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002065 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002066 case Shl:
2067 case LShr:
2068 case AShr:
2069 assert(getType() == LHS->getType() &&
2070 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002071 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002072 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002073 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002074 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002075 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076 case And: case Or:
2077 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002078 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002079 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002080 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002081 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002082 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002083 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002085 default:
2086 break;
2087 }
2088#endif
2089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002092 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002093 Instruction *InsertBefore) {
2094 assert(S1->getType() == S2->getType() &&
2095 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002096 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002097}
2098
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002099BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002100 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002101 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002102 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002103 InsertAtEnd->getInstList().push_back(Res);
2104 return Res;
2105}
2106
Dan Gohman5476cfd2009-08-12 16:23:25 +00002107BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002108 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002109 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002110 return new BinaryOperator(Instruction::Sub,
2111 zero, Op,
2112 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002113}
2114
Dan Gohman5476cfd2009-08-12 16:23:25 +00002115BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002116 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002117 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002118 return new BinaryOperator(Instruction::Sub,
2119 zero, Op,
2120 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002121}
2122
Dan Gohman4ab44202009-12-18 02:58:50 +00002123BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2124 Instruction *InsertBefore) {
2125 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2126 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2127}
2128
2129BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2130 BasicBlock *InsertAtEnd) {
2131 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2132 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2133}
2134
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002135BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2136 Instruction *InsertBefore) {
2137 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2138 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2139}
2140
2141BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2142 BasicBlock *InsertAtEnd) {
2143 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2144 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2145}
2146
Dan Gohman5476cfd2009-08-12 16:23:25 +00002147BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002148 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002149 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002150 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002151 Op->getType(), Name, InsertBefore);
2152}
2153
Dan Gohman5476cfd2009-08-12 16:23:25 +00002154BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002155 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002156 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002157 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002158 Op->getType(), Name, InsertAtEnd);
2159}
2160
Dan Gohman5476cfd2009-08-12 16:23:25 +00002161BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002162 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002163 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002164 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002165 Op->getType(), Name, InsertBefore);
2166}
2167
Dan Gohman5476cfd2009-08-12 16:23:25 +00002168BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002170 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002171 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002172 Op->getType(), Name, InsertAtEnd);
2173}
2174
2175
2176// isConstantAllOnes - Helper function for several functions below
2177static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002178 if (const Constant *C = dyn_cast<Constant>(V))
2179 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002180 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181}
2182
Owen Andersonbb2501b2009-07-13 22:18:28 +00002183bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002184 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2185 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002186 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2187 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002188 return false;
2189}
2190
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002191bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002192 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2193 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002194 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2195 if (!IgnoreZeroSign)
2196 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2197 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2198 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002199 return false;
2200}
2201
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002202bool BinaryOperator::isNot(const Value *V) {
2203 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2204 return (Bop->getOpcode() == Instruction::Xor &&
2205 (isConstantAllOnes(Bop->getOperand(1)) ||
2206 isConstantAllOnes(Bop->getOperand(0))));
2207 return false;
2208}
2209
Chris Lattner2c7d1772005-04-24 07:28:37 +00002210Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002211 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002212}
2213
Chris Lattner2c7d1772005-04-24 07:28:37 +00002214const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2215 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002216}
2217
Dan Gohmana5b96452009-06-04 22:49:04 +00002218Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002219 return cast<BinaryOperator>(BinOp)->getOperand(1);
2220}
2221
2222const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2223 return getFNegArgument(const_cast<Value*>(BinOp));
2224}
2225
Chris Lattner2c7d1772005-04-24 07:28:37 +00002226Value *BinaryOperator::getNotArgument(Value *BinOp) {
2227 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2228 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2229 Value *Op0 = BO->getOperand(0);
2230 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002231 if (isConstantAllOnes(Op0)) return Op1;
2232
2233 assert(isConstantAllOnes(Op1));
2234 return Op0;
2235}
2236
Chris Lattner2c7d1772005-04-24 07:28:37 +00002237const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2238 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002239}
2240
2241
2242// swapOperands - Exchange the two operands to this instruction. This
2243// instruction is safe to use on any binary instruction and does not
2244// modify the semantics of the instruction. If the instruction is
2245// order dependent (SetLT f.e.) the opcode is changed.
2246//
2247bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002248 if (!isCommutative())
2249 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002250 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002251 return false;
2252}
2253
Dan Gohman1b849082009-09-07 23:54:19 +00002254void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2255 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2256}
2257
2258void BinaryOperator::setHasNoSignedWrap(bool b) {
2259 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2260}
2261
2262void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002263 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002264}
2265
Nick Lewycky28a5f252009-09-27 21:33:04 +00002266bool BinaryOperator::hasNoUnsignedWrap() const {
2267 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2268}
2269
2270bool BinaryOperator::hasNoSignedWrap() const {
2271 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2272}
2273
2274bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002275 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002276}
2277
Sanjay Patela982d992014-09-03 01:06:50 +00002278void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002279 // Copy the wrapping flags.
2280 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2281 setHasNoSignedWrap(OB->hasNoSignedWrap());
2282 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2283 }
2284
2285 // Copy the exact flag.
2286 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2287 setIsExact(PE->isExact());
2288
2289 // Copy the fast-math flags.
2290 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002291 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002292}
2293
Sanjay Patela982d992014-09-03 01:06:50 +00002294void BinaryOperator::andIRFlags(const Value *V) {
2295 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2296 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2297 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2298 }
2299
2300 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2301 setIsExact(isExact() & PE->isExact());
2302
2303 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2304 FastMathFlags FM = getFastMathFlags();
2305 FM &= FP->getFastMathFlags();
2306 copyFastMathFlags(FM);
2307 }
2308}
2309
2310
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002311//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002312// FPMathOperator Class
2313//===----------------------------------------------------------------------===//
2314
2315/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2316/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002317/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002318float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002319 const MDNode *MD =
2320 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002321 if (!MD)
2322 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002323 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002324 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002325}
2326
2327
2328//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002329// CastInst Class
2330//===----------------------------------------------------------------------===//
2331
David Blaikie3a15e142011-12-01 08:00:17 +00002332void CastInst::anchor() {}
2333
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334// Just determine if this cast only deals with integral->integral conversion.
2335bool CastInst::isIntegerCast() const {
2336 switch (getOpcode()) {
2337 default: return false;
2338 case Instruction::ZExt:
2339 case Instruction::SExt:
2340 case Instruction::Trunc:
2341 return true;
2342 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002343 return getOperand(0)->getType()->isIntegerTy() &&
2344 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002346}
2347
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348bool CastInst::isLosslessCast() const {
2349 // Only BitCast can be lossless, exit fast if we're not BitCast
2350 if (getOpcode() != Instruction::BitCast)
2351 return false;
2352
2353 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002354 Type* SrcTy = getOperand(0)->getType();
2355 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 if (SrcTy == DstTy)
2357 return true;
2358
Reid Spencer8d9336d2006-12-31 05:26:44 +00002359 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002360 if (SrcTy->isPointerTy())
2361 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 return false; // Other types have no identity values
2363}
2364
2365/// This function determines if the CastInst does not require any bits to be
2366/// changed in order to effect the cast. Essentially, it identifies cases where
2367/// no code gen is necessary for the cast, hence the name no-op cast. For
2368/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002369/// # bitcast i32* %x to i8*
2370/// # bitcast <2 x i32> %x to <4 x i16>
2371/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002372/// @brief Determine if the described cast is a no-op.
2373bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002374 Type *SrcTy,
2375 Type *DestTy,
2376 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002377 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002378 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 case Instruction::Trunc:
2380 case Instruction::ZExt:
2381 case Instruction::SExt:
2382 case Instruction::FPTrunc:
2383 case Instruction::FPExt:
2384 case Instruction::UIToFP:
2385 case Instruction::SIToFP:
2386 case Instruction::FPToUI:
2387 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002388 case Instruction::AddrSpaceCast:
2389 // TODO: Target informations may give a more accurate answer here.
2390 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 case Instruction::BitCast:
2392 return true; // BitCast never modifies bits.
2393 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002394 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002395 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002397 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002398 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 }
2400}
2401
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002402/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002403bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002404 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2405}
2406
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002407bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002408 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002409 if (getOpcode() == Instruction::PtrToInt)
2410 PtrOpTy = getOperand(0)->getType();
2411 else if (getOpcode() == Instruction::IntToPtr)
2412 PtrOpTy = getType();
2413
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002414 Type *IntPtrTy =
2415 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002416
2417 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2418}
2419
2420/// This function determines if a pair of casts can be eliminated and what
2421/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422/// instructions like this:
2423/// * %F = firstOpcode SrcTy %x to MidTy
2424/// * %S = secondOpcode MidTy %F to DstTy
2425/// The function returns a resultOpcode so these two casts can be replaced with:
2426/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2427/// If no such cast is permited, the function returns 0.
2428unsigned CastInst::isEliminableCastPair(
2429 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002430 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2431 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432 // Define the 144 possibilities for these two cast instructions. The values
2433 // in this matrix determine what to do in a given situation and select the
2434 // case in the switch below. The rows correspond to firstOp, the columns
2435 // correspond to secondOp. In looking at the table below, keep in mind
2436 // the following cast properties:
2437 //
2438 // Size Compare Source Destination
2439 // Operator Src ? Size Type Sign Type Sign
2440 // -------- ------------ ------------------- ---------------------
2441 // TRUNC > Integer Any Integral Any
2442 // ZEXT < Integral Unsigned Integer Any
2443 // SEXT < Integral Signed Integer Any
2444 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445 // FPTOSI n/a FloatPt n/a Integral Signed
2446 // UITOFP n/a Integral Unsigned FloatPt n/a
2447 // SITOFP n/a Integral Signed FloatPt n/a
2448 // FPTRUNC > FloatPt n/a FloatPt n/a
2449 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 // PTRTOINT n/a Pointer n/a Integral Unsigned
2451 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002452 // BITCAST = FirstClass n/a FirstClass n/a
2453 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002454 //
2455 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002456 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2457 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002458 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002459 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002460 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002461 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2464 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002465 // T F F U S F F P I B A -+
2466 // R Z S P P I I T P 2 N T S |
2467 // U E E 2 2 2 2 R E I T C C +- secondOp
2468 // N X X U S F F N X N 2 V V |
2469 // C T T I I P P C T T P T T -+
2470 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002471 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002472 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2473 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2474 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2475 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2476 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002477 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002478 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2479 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2480 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2481 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2482 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002484
Chris Lattner25eea4d2010-07-12 01:19:22 +00002485 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002486 // merging. However, bitcast of A->B->A are allowed.
2487 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2488 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2489 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2490
2491 // Check if any of the bitcasts convert scalars<->vectors.
2492 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2493 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2494 // Unless we are bitcasing to the original type, disallow optimizations.
2495 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496
2497 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2498 [secondOp-Instruction::CastOpsBegin];
2499 switch (ElimCase) {
2500 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002501 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 return 0;
2503 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002504 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 return firstOp;
2506 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002507 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 return secondOp;
2509 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002510 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002511 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002512 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002513 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514 return firstOp;
2515 return 0;
2516 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002517 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002518 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002519 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520 return firstOp;
2521 return 0;
2522 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002523 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002524 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002525 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526 return secondOp;
2527 return 0;
2528 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002529 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002530 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002531 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 return secondOp;
2533 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002534 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002535 // Cannot simplify if address spaces are different!
2536 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2537 return 0;
2538
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002539 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // We can still fold this without knowing the actual sizes as long we
2541 // know that the intermediate pointer is the largest possible
2542 // pointer size.
2543 // FIXME: Is this always true?
2544 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002545 return Instruction::BitCast;
2546
2547 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002548 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002549 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002550 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551 if (MidSize >= PtrSize)
2552 return Instruction::BitCast;
2553 return 0;
2554 }
2555 case 8: {
2556 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2557 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2558 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002559 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2560 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 if (SrcSize == DstSize)
2562 return Instruction::BitCast;
2563 else if (SrcSize < DstSize)
2564 return firstOp;
2565 return secondOp;
2566 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002567 case 9:
2568 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 return Instruction::ZExt;
2570 case 10:
2571 // fpext followed by ftrunc is allowed if the bit size returned to is
2572 // the same as the original, in which case its just a bitcast
2573 if (SrcTy == DstTy)
2574 return Instruction::BitCast;
2575 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002576 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002578 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002579 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002580 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002581 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2582 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 if (SrcSize <= PtrSize && SrcSize == DstSize)
2584 return Instruction::BitCast;
2585 return 0;
2586 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002587 case 12: {
2588 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2589 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2590 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2591 return Instruction::AddrSpaceCast;
2592 return Instruction::BitCast;
2593 }
2594 case 13:
2595 // FIXME: this state can be merged with (1), but the following assert
2596 // is useful to check the correcteness of the sequence due to semantic
2597 // change of bitcast.
2598 assert(
2599 SrcTy->isPtrOrPtrVectorTy() &&
2600 MidTy->isPtrOrPtrVectorTy() &&
2601 DstTy->isPtrOrPtrVectorTy() &&
2602 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2603 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2604 "Illegal addrspacecast, bitcast sequence!");
2605 // Allowed, use first cast's opcode
2606 return firstOp;
2607 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002608 // bitcast, addrspacecast -> addrspacecast if the element type of
2609 // bitcast's source is the same as that of addrspacecast's destination.
2610 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2611 return Instruction::AddrSpaceCast;
2612 return 0;
2613
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002614 case 15:
2615 // FIXME: this state can be merged with (1), but the following assert
2616 // is useful to check the correcteness of the sequence due to semantic
2617 // change of bitcast.
2618 assert(
2619 SrcTy->isIntOrIntVectorTy() &&
2620 MidTy->isPtrOrPtrVectorTy() &&
2621 DstTy->isPtrOrPtrVectorTy() &&
2622 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2623 "Illegal inttoptr, bitcast sequence!");
2624 // Allowed, use first cast's opcode
2625 return firstOp;
2626 case 16:
2627 // FIXME: this state can be merged with (2), but the following assert
2628 // is useful to check the correcteness of the sequence due to semantic
2629 // change of bitcast.
2630 assert(
2631 SrcTy->isPtrOrPtrVectorTy() &&
2632 MidTy->isPtrOrPtrVectorTy() &&
2633 DstTy->isIntOrIntVectorTy() &&
2634 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2635 "Illegal bitcast, ptrtoint sequence!");
2636 // Allowed, use second cast's opcode
2637 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002638 case 17:
2639 // (sitofp (zext x)) -> (uitofp x)
2640 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002642 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002644 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645 default:
Craig Topperc514b542012-02-05 22:14:15 +00002646 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
Chris Lattner229907c2011-07-18 04:54:35 +00002650CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002652 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 // Construct and return the appropriate CastInst subclass
2654 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002655 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2656 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2657 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2658 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2659 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2660 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2661 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2662 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2663 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2664 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2665 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2666 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2667 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2668 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670}
2671
Chris Lattner229907c2011-07-18 04:54:35 +00002672CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002673 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002674 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675 // Construct and return the appropriate CastInst subclass
2676 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002677 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2678 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2679 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2680 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2681 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2682 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2683 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2684 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2685 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2686 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2687 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2688 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2689 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2690 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692}
2693
Chris Lattner229907c2011-07-18 04:54:35 +00002694CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002695 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002696 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002697 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002698 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2699 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002700}
2701
Chris Lattner229907c2011-07-18 04:54:35 +00002702CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002703 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002704 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002705 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002706 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2707 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002708}
2709
Chris Lattner229907c2011-07-18 04:54:35 +00002710CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002711 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002712 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002713 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002714 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2715 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002716}
2717
Chris Lattner229907c2011-07-18 04:54:35 +00002718CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002719 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002720 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002721 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002722 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2723 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002724}
2725
Chris Lattner229907c2011-07-18 04:54:35 +00002726CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002727 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002728 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002729 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002730 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2731 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002732}
2733
Chris Lattner229907c2011-07-18 04:54:35 +00002734CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002735 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002736 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002737 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002738 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2739 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002740}
2741
Chris Lattner229907c2011-07-18 04:54:35 +00002742CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002743 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002744 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002745 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2746 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2747 "Invalid cast");
2748 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002749 assert((!Ty->isVectorTy() ||
2750 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002751 "Invalid cast");
2752
Matt Arsenault065ced92013-07-31 00:17:33 +00002753 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002754 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002755
Matt Arsenault740980e2014-07-14 17:24:35 +00002756 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002757}
2758
2759/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002760CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2761 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002762 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002763 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2764 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002765 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002766 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002767 assert((!Ty->isVectorTy() ||
2768 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002769 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002770
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002771 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002772 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002773
Matt Arsenault740980e2014-07-14 17:24:35 +00002774 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2775}
2776
2777CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2778 Value *S, Type *Ty,
2779 const Twine &Name,
2780 BasicBlock *InsertAtEnd) {
2781 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2782 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2783
2784 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2785 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2786
2787 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2788}
2789
2790CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2791 Value *S, Type *Ty,
2792 const Twine &Name,
2793 Instruction *InsertBefore) {
2794 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2795 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2796
2797 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002798 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2799
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002800 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002801}
2802
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002803CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2804 const Twine &Name,
2805 Instruction *InsertBefore) {
2806 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2807 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2808 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2809 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2810
2811 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2812}
2813
Matt Arsenault740980e2014-07-14 17:24:35 +00002814CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002815 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002816 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002817 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002818 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002819 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2820 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002821 Instruction::CastOps opcode =
2822 (SrcBits == DstBits ? Instruction::BitCast :
2823 (SrcBits > DstBits ? Instruction::Trunc :
2824 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002825 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002826}
2827
Chris Lattner229907c2011-07-18 04:54:35 +00002828CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002829 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002830 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002831 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002832 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002833 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2834 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002835 Instruction::CastOps opcode =
2836 (SrcBits == DstBits ? Instruction::BitCast :
2837 (SrcBits > DstBits ? Instruction::Trunc :
2838 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002839 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002840}
2841
Chris Lattner229907c2011-07-18 04:54:35 +00002842CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002843 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002844 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002845 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002846 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002847 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2848 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002849 Instruction::CastOps opcode =
2850 (SrcBits == DstBits ? Instruction::BitCast :
2851 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002852 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002853}
2854
Chris Lattner229907c2011-07-18 04:54:35 +00002855CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002856 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002857 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002858 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002859 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002860 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2861 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002862 Instruction::CastOps opcode =
2863 (SrcBits == DstBits ? Instruction::BitCast :
2864 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002865 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002866}
2867
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002868// Check whether it is valid to call getCastOpcode for these types.
2869// This routine must be kept in sync with getCastOpcode.
2870bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2871 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2872 return false;
2873
2874 if (SrcTy == DestTy)
2875 return true;
2876
2877 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2878 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2879 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2880 // An element by element cast. Valid if casting the elements is valid.
2881 SrcTy = SrcVecTy->getElementType();
2882 DestTy = DestVecTy->getElementType();
2883 }
2884
2885 // Get the bit sizes, we'll need these
2886 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2887 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2888
2889 // Run through the possibilities ...
2890 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002891 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002892 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002893 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002894 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002895 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002896 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002897 // Casting from something else
2898 return SrcTy->isPointerTy();
2899 }
2900 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2901 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002902 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002903 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002904 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002905 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002906 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002907 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002908 return false;
2909 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002910 if (DestTy->isVectorTy()) // Casting to vector
2911 return DestBits == SrcBits;
2912 if (DestTy->isPointerTy()) { // Casting to pointer
2913 if (SrcTy->isPointerTy()) // Casting from pointer
2914 return true;
2915 return SrcTy->isIntegerTy(); // Casting from integral
2916 }
2917 if (DestTy->isX86_MMXTy()) {
2918 if (SrcTy->isVectorTy())
2919 return DestBits == SrcBits; // 64-bit vector to MMX
2920 return false;
2921 } // Casting to something else
2922 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002923}
2924
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002925bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2926 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2927 return false;
2928
2929 if (SrcTy == DestTy)
2930 return true;
2931
2932 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2933 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2934 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2935 // An element by element cast. Valid if casting the elements is valid.
2936 SrcTy = SrcVecTy->getElementType();
2937 DestTy = DestVecTy->getElementType();
2938 }
2939 }
2940 }
2941
2942 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2943 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2944 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2945 }
2946 }
2947
2948 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2949 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2950
2951 // Could still have vectors of pointers if the number of elements doesn't
2952 // match
2953 if (SrcBits == 0 || DestBits == 0)
2954 return false;
2955
2956 if (SrcBits != DestBits)
2957 return false;
2958
2959 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2960 return false;
2961
2962 return true;
2963}
2964
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002965bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002966 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002967 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2968 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002969 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002970 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2971 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002972 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002973
2974 return isBitCastable(SrcTy, DestTy);
2975}
2976
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002977// Provide a way to get a "cast" where the cast opcode is inferred from the
2978// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002979// logic in the castIsValid function below. This axiom should hold:
2980// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2981// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002982// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002983// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002985CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002986 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2987 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002988
Duncan Sands55e50902008-01-06 10:12:28 +00002989 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2990 "Only first class types are castable!");
2991
Duncan Sandsa8514532011-05-18 07:13:41 +00002992 if (SrcTy == DestTy)
2993 return BitCast;
2994
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002995 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002996 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2997 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002998 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2999 // An element by element cast. Find the appropriate opcode based on the
3000 // element types.
3001 SrcTy = SrcVecTy->getElementType();
3002 DestTy = DestVecTy->getElementType();
3003 }
3004
3005 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003006 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3007 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003008
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003009 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003010 if (DestTy->isIntegerTy()) { // Casting to integral
3011 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003012 if (DestBits < SrcBits)
3013 return Trunc; // int -> smaller int
3014 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003015 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016 return SExt; // signed -> SEXT
3017 else
3018 return ZExt; // unsigned -> ZEXT
3019 } else {
3020 return BitCast; // Same size, No-op cast
3021 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003022 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003023 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 return FPToSI; // FP -> sint
3025 else
3026 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003027 } else if (SrcTy->isVectorTy()) {
3028 assert(DestBits == SrcBits &&
3029 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030 return BitCast; // Same size, no-op cast
3031 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003032 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033 "Casting from a value that is not first-class type");
3034 return PtrToInt; // ptr -> int
3035 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003036 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3037 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003038 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003039 return SIToFP; // sint -> FP
3040 else
3041 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003042 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043 if (DestBits < SrcBits) {
3044 return FPTrunc; // FP -> smaller FP
3045 } else if (DestBits > SrcBits) {
3046 return FPExt; // FP -> larger FP
3047 } else {
3048 return BitCast; // same size, no-op cast
3049 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003050 } else if (SrcTy->isVectorTy()) {
3051 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003052 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003053 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003054 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003055 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003056 } else if (DestTy->isVectorTy()) {
3057 assert(DestBits == SrcBits &&
3058 "Illegal cast to vector (wrong type or size)");
3059 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003060 } else if (DestTy->isPointerTy()) {
3061 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003062 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3063 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003065 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003066 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003068 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003069 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003070 if (SrcTy->isVectorTy()) {
3071 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003072 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003073 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003074 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003076 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077}
3078
3079//===----------------------------------------------------------------------===//
3080// CastInst SubClass Constructors
3081//===----------------------------------------------------------------------===//
3082
3083/// Check that the construction parameters for a CastInst are correct. This
3084/// could be broken out into the separate constructors but it is useful to have
3085/// it in one place and to eliminate the redundant code for getting the sizes
3086/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003087bool
Chris Lattner229907c2011-07-18 04:54:35 +00003088CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089
3090 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003091 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003092
Chris Lattner37bc78a2010-01-26 21:51:43 +00003093 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3094 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 return false;
3096
3097 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003098 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3099 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100
Duncan Sands7f646562011-05-18 09:21:57 +00003101 // If these are vector types, get the lengths of the vectors (using zero for
3102 // scalar types means that checking that vector lengths match also checks that
3103 // scalars are not being converted to vectors or vectors to scalars).
3104 unsigned SrcLength = SrcTy->isVectorTy() ?
3105 cast<VectorType>(SrcTy)->getNumElements() : 0;
3106 unsigned DstLength = DstTy->isVectorTy() ?
3107 cast<VectorType>(DstTy)->getNumElements() : 0;
3108
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003109 // Switch on the opcode provided
3110 switch (op) {
3111 default: return false; // This is an input error
3112 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003113 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3114 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003115 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003116 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3117 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003118 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003119 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3120 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003121 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003122 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3123 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003124 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003125 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3126 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003129 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3130 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003133 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3134 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003136 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003137 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003138 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3139 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3140 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003141 return SrcTy->getScalarType()->isPointerTy() &&
3142 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003144 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003145 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003146 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3147 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3148 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003149 return SrcTy->getScalarType()->isIntegerTy() &&
3150 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003151 case Instruction::BitCast: {
3152 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3153 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3154
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155 // BitCast implies a no-op cast of type only. No bits change.
3156 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003157 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003158 return false;
3159
Alp Tokerf907b892013-12-05 05:44:44 +00003160 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003161 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003162 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003163 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3164
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003165 // If both are pointers then the address spaces must match.
3166 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3167 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003168
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003169 // A vector of pointers must have the same number of elements.
3170 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3171 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3172 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3173
3174 return false;
3175 }
3176
3177 return true;
3178 }
3179 case Instruction::AddrSpaceCast: {
3180 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3181 if (!SrcPtrTy)
3182 return false;
3183
3184 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3185 if (!DstPtrTy)
3186 return false;
3187
3188 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3189 return false;
3190
3191 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3192 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3193 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3194
3195 return false;
3196 }
3197
3198 return true;
3199 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003200 }
3201}
3202
3203TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003204 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207}
3208
3209TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003210 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213}
3214
3215ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003216 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003217) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219}
3220
3221ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003222 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225}
3226SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
Jeff Cohencc08c832006-12-02 02:22:01 +00003232SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237
3238FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003239 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003241 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242}
3243
3244FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
3250FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
3268UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
3273
3274SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003275 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278}
3279
3280SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003281 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284}
3285
3286FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003287 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290}
3291
3292FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003293 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296}
3297
3298FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003299 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302}
3303
3304FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003305 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308}
3309
3310PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003311 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314}
3315
3316PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003317 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320}
3321
3322IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003323 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326}
3327
3328IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003329 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003332}
3333
3334BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003335 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003338}
3339
3340BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003341 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003344}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003345
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003346AddrSpaceCastInst::AddrSpaceCastInst(
3347 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3348) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3350}
3351
3352AddrSpaceCastInst::AddrSpaceCastInst(
3353 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3354) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3356}
3357
Chris Lattnerf16dc002006-09-17 19:29:56 +00003358//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003359// CmpInst Classes
3360//===----------------------------------------------------------------------===//
3361
Craig Topper3186c012012-09-23 02:12:10 +00003362void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003363
Chris Lattner229907c2011-07-18 04:54:35 +00003364CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003365 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003366 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003367 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003368 OperandTraits<CmpInst>::op_begin(this),
3369 OperandTraits<CmpInst>::operands(this),
3370 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003371 Op<0>() = LHS;
3372 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003373 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003374 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003375}
Gabor Greiff6caff662008-05-10 08:32:32 +00003376
Chris Lattner229907c2011-07-18 04:54:35 +00003377CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003378 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003379 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003380 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003381 OperandTraits<CmpInst>::op_begin(this),
3382 OperandTraits<CmpInst>::operands(this),
3383 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003384 Op<0>() = LHS;
3385 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003386 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003387 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003388}
3389
3390CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003391CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003392 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003393 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003394 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003395 if (InsertBefore)
3396 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3397 S1, S2, Name);
3398 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003399 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003400 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003401 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003402
3403 if (InsertBefore)
3404 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3405 S1, S2, Name);
3406 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003407 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003408 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003409}
3410
3411CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003412CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003413 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003414 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003415 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3416 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003417 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003418 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3419 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003420}
3421
3422void CmpInst::swapOperands() {
3423 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3424 IC->swapOperands();
3425 else
3426 cast<FCmpInst>(this)->swapOperands();
3427}
3428
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003429bool CmpInst::isCommutative() const {
3430 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003431 return IC->isCommutative();
3432 return cast<FCmpInst>(this)->isCommutative();
3433}
3434
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003435bool CmpInst::isEquality() const {
3436 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003437 return IC->isEquality();
3438 return cast<FCmpInst>(this)->isEquality();
3439}
3440
3441
Dan Gohman4e724382008-05-31 02:47:54 +00003442CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003443 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003444 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003445 case ICMP_EQ: return ICMP_NE;
3446 case ICMP_NE: return ICMP_EQ;
3447 case ICMP_UGT: return ICMP_ULE;
3448 case ICMP_ULT: return ICMP_UGE;
3449 case ICMP_UGE: return ICMP_ULT;
3450 case ICMP_ULE: return ICMP_UGT;
3451 case ICMP_SGT: return ICMP_SLE;
3452 case ICMP_SLT: return ICMP_SGE;
3453 case ICMP_SGE: return ICMP_SLT;
3454 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003455
Dan Gohman4e724382008-05-31 02:47:54 +00003456 case FCMP_OEQ: return FCMP_UNE;
3457 case FCMP_ONE: return FCMP_UEQ;
3458 case FCMP_OGT: return FCMP_ULE;
3459 case FCMP_OLT: return FCMP_UGE;
3460 case FCMP_OGE: return FCMP_ULT;
3461 case FCMP_OLE: return FCMP_UGT;
3462 case FCMP_UEQ: return FCMP_ONE;
3463 case FCMP_UNE: return FCMP_OEQ;
3464 case FCMP_UGT: return FCMP_OLE;
3465 case FCMP_ULT: return FCMP_OGE;
3466 case FCMP_UGE: return FCMP_OLT;
3467 case FCMP_ULE: return FCMP_OGT;
3468 case FCMP_ORD: return FCMP_UNO;
3469 case FCMP_UNO: return FCMP_ORD;
3470 case FCMP_TRUE: return FCMP_FALSE;
3471 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003472 }
3473}
3474
Reid Spencer266e42b2006-12-23 06:05:41 +00003475ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3476 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003477 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003478 case ICMP_EQ: case ICMP_NE:
3479 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3480 return pred;
3481 case ICMP_UGT: return ICMP_SGT;
3482 case ICMP_ULT: return ICMP_SLT;
3483 case ICMP_UGE: return ICMP_SGE;
3484 case ICMP_ULE: return ICMP_SLE;
3485 }
3486}
3487
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003488ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3489 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003490 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003491 case ICMP_EQ: case ICMP_NE:
3492 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3493 return pred;
3494 case ICMP_SGT: return ICMP_UGT;
3495 case ICMP_SLT: return ICMP_ULT;
3496 case ICMP_SGE: return ICMP_UGE;
3497 case ICMP_SLE: return ICMP_ULE;
3498 }
3499}
3500
Reid Spencer0286bc12007-02-28 22:00:54 +00003501/// Initialize a set of values that all satisfy the condition with C.
3502///
3503ConstantRange
3504ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3505 APInt Lower(C);
3506 APInt Upper(C);
3507 uint32_t BitWidth = C.getBitWidth();
3508 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003509 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003510 case ICmpInst::ICMP_EQ: ++Upper; break;
3511 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003512 case ICmpInst::ICMP_ULT:
3513 Lower = APInt::getMinValue(BitWidth);
3514 // Check for an empty-set condition.
3515 if (Lower == Upper)
3516 return ConstantRange(BitWidth, /*isFullSet=*/false);
3517 break;
3518 case ICmpInst::ICMP_SLT:
3519 Lower = APInt::getSignedMinValue(BitWidth);
3520 // Check for an empty-set condition.
3521 if (Lower == Upper)
3522 return ConstantRange(BitWidth, /*isFullSet=*/false);
3523 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003524 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003525 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003526 // Check for an empty-set condition.
3527 if (Lower == Upper)
3528 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003529 break;
3530 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003531 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003532 // Check for an empty-set condition.
3533 if (Lower == Upper)
3534 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003535 break;
3536 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003537 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003538 // Check for a full-set condition.
3539 if (Lower == Upper)
3540 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003541 break;
3542 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003543 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003544 // Check for a full-set condition.
3545 if (Lower == Upper)
3546 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003547 break;
3548 case ICmpInst::ICMP_UGE:
3549 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003550 // Check for a full-set condition.
3551 if (Lower == Upper)
3552 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003553 break;
3554 case ICmpInst::ICMP_SGE:
3555 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003556 // Check for a full-set condition.
3557 if (Lower == Upper)
3558 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003559 break;
3560 }
3561 return ConstantRange(Lower, Upper);
3562}
3563
Dan Gohman4e724382008-05-31 02:47:54 +00003564CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003565 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003566 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003567 case ICMP_EQ: case ICMP_NE:
3568 return pred;
3569 case ICMP_SGT: return ICMP_SLT;
3570 case ICMP_SLT: return ICMP_SGT;
3571 case ICMP_SGE: return ICMP_SLE;
3572 case ICMP_SLE: return ICMP_SGE;
3573 case ICMP_UGT: return ICMP_ULT;
3574 case ICMP_ULT: return ICMP_UGT;
3575 case ICMP_UGE: return ICMP_ULE;
3576 case ICMP_ULE: return ICMP_UGE;
3577
Reid Spencerd9436b62006-11-20 01:22:35 +00003578 case FCMP_FALSE: case FCMP_TRUE:
3579 case FCMP_OEQ: case FCMP_ONE:
3580 case FCMP_UEQ: case FCMP_UNE:
3581 case FCMP_ORD: case FCMP_UNO:
3582 return pred;
3583 case FCMP_OGT: return FCMP_OLT;
3584 case FCMP_OLT: return FCMP_OGT;
3585 case FCMP_OGE: return FCMP_OLE;
3586 case FCMP_OLE: return FCMP_OGE;
3587 case FCMP_UGT: return FCMP_ULT;
3588 case FCMP_ULT: return FCMP_UGT;
3589 case FCMP_UGE: return FCMP_ULE;
3590 case FCMP_ULE: return FCMP_UGE;
3591 }
3592}
3593
Sanjoy Das6e78b172015-10-22 19:57:34 +00003594CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3595 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3596
3597 switch (pred) {
3598 default:
3599 llvm_unreachable("Unknown predicate!");
3600 case CmpInst::ICMP_ULT:
3601 return CmpInst::ICMP_SLT;
3602 case CmpInst::ICMP_ULE:
3603 return CmpInst::ICMP_SLE;
3604 case CmpInst::ICMP_UGT:
3605 return CmpInst::ICMP_SGT;
3606 case CmpInst::ICMP_UGE:
3607 return CmpInst::ICMP_SGE;
3608 }
3609}
3610
Reid Spencer266e42b2006-12-23 06:05:41 +00003611bool CmpInst::isUnsigned(unsigned short predicate) {
3612 switch (predicate) {
3613 default: return false;
3614 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3615 case ICmpInst::ICMP_UGE: return true;
3616 }
3617}
3618
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003619bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003620 switch (predicate) {
3621 default: return false;
3622 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3623 case ICmpInst::ICMP_SGE: return true;
3624 }
3625}
3626
3627bool CmpInst::isOrdered(unsigned short predicate) {
3628 switch (predicate) {
3629 default: return false;
3630 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3631 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3632 case FCmpInst::FCMP_ORD: return true;
3633 }
3634}
3635
3636bool CmpInst::isUnordered(unsigned short predicate) {
3637 switch (predicate) {
3638 default: return false;
3639 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3640 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3641 case FCmpInst::FCMP_UNO: return true;
3642 }
3643}
3644
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003645bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3646 switch(predicate) {
3647 default: return false;
3648 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3649 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3650 }
3651}
3652
3653bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3654 switch(predicate) {
3655 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3656 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3657 default: return false;
3658 }
3659}
3660
3661
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003662//===----------------------------------------------------------------------===//
3663// SwitchInst Implementation
3664//===----------------------------------------------------------------------===//
3665
Chris Lattnerbaf00152010-11-17 05:41:46 +00003666void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3667 assert(Value && Default && NumReserved);
3668 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003669 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003670 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003671
Pete Coopereb31b682015-05-21 22:48:54 +00003672 Op<0>() = Value;
3673 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003674}
3675
Chris Lattner2195fc42007-02-24 00:55:48 +00003676/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3677/// switch on and a default destination. The number of additional cases can
3678/// be specified here to make memory allocation more efficient. This
3679/// constructor can also autoinsert before another instruction.
3680SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3681 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003682 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003683 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003684 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003685}
3686
3687/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3688/// switch on and a default destination. The number of additional cases can
3689/// be specified here to make memory allocation more efficient. This
3690/// constructor also autoinserts at the end of the specified BasicBlock.
3691SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3692 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003693 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003694 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003695 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003696}
3697
Misha Brukmanb1c93172005-04-21 23:48:37 +00003698SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003699 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003700 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003701 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003702 Use *OL = getOperandList();
3703 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003704 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003705 OL[i] = InOL[i];
3706 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003707 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003708 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003709}
3710
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003711
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003712/// addCase - Add an entry to the switch instruction...
3713///
Chris Lattner47ac1872005-02-24 05:32:09 +00003714void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003715 unsigned NewCaseIdx = getNumCases();
3716 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003717 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003718 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003719 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003720 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003721 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003722 CaseIt Case(this, NewCaseIdx);
3723 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003724 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003725}
3726
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003727/// removeCase - This method removes the specified case and its successor
3728/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003729void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003730 unsigned idx = i.getCaseIndex();
3731
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003732 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003733
3734 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003735 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003736
Jay Foad14277722011-02-01 09:22:34 +00003737 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003738 if (2 + (idx + 1) * 2 != NumOps) {
3739 OL[2 + idx * 2] = OL[NumOps - 2];
3740 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003741 }
3742
3743 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003744 OL[NumOps-2].set(nullptr);
3745 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003746 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003747}
3748
Jay Foade98f29d2011-04-01 08:00:58 +00003749/// growOperands - grow operands - This grows the operand list in response
3750/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003751///
Jay Foade98f29d2011-04-01 08:00:58 +00003752void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003753 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003754 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003755
3756 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003757 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003758}
3759
3760
3761BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3762 return getSuccessor(idx);
3763}
3764unsigned SwitchInst::getNumSuccessorsV() const {
3765 return getNumSuccessors();
3766}
3767void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3768 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003769}
Chris Lattnerf22be932004-10-15 23:52:53 +00003770
Chris Lattner3ed871f2009-10-27 19:13:16 +00003771//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003772// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003773//===----------------------------------------------------------------------===//
3774
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003775void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003776 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003777 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003778 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003779 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003780 allocHungoffUses(ReservedSpace);
3781
Pete Coopereb31b682015-05-21 22:48:54 +00003782 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003783}
3784
3785
Jay Foade98f29d2011-04-01 08:00:58 +00003786/// growOperands - grow operands - This grows the operand list in response
3787/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788///
Jay Foade98f29d2011-04-01 08:00:58 +00003789void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003790 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003791 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003792
3793 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003794 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003795}
3796
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003797IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3798 Instruction *InsertBefore)
3799: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003800 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003801 init(Address, NumCases);
3802}
3803
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003804IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3805 BasicBlock *InsertAtEnd)
3806: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003807 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003808 init(Address, NumCases);
3809}
3810
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003811IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003812 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3813 nullptr, IBI.getNumOperands()) {
3814 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003815 Use *OL = getOperandList();
3816 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3818 OL[i] = InOL[i];
3819 SubclassOptionalData = IBI.SubclassOptionalData;
3820}
3821
Chris Lattner3ed871f2009-10-27 19:13:16 +00003822/// addDestination - Add a destination.
3823///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003824void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003825 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003826 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003827 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828 // Initialize some new operands.
3829 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003830 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003831 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003832}
3833
3834/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003835/// indirectbr instruction.
3836void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003837 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3838
3839 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003840 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003841
3842 // Replace this value with the last one.
3843 OL[idx+1] = OL[NumOps-1];
3844
3845 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003846 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003847 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003848}
3849
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003850BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003851 return getSuccessor(idx);
3852}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003853unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003854 return getNumSuccessors();
3855}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003856void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003857 setSuccessor(idx, B);
3858}
3859
3860//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003861// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003862//===----------------------------------------------------------------------===//
3863
Chris Lattnerf22be932004-10-15 23:52:53 +00003864// Define these methods here so vtables don't get emitted into every translation
3865// unit that uses these classes.
3866
Pete Cooper75403d72015-06-24 20:22:23 +00003867GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003868 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003872 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003873}
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003892 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3893 (Value *)getOperand(0), getAlignment());
3894 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3895 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003896}
3897
Pete Cooper75403d72015-06-24 20:22:23 +00003898LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003899 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3900 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003901}
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003904 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003905 getAlignment(), getOrdering(), getSynchScope());
3906
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003907}
3908
Pete Cooper75403d72015-06-24 20:22:23 +00003909AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003910 AtomicCmpXchgInst *Result =
3911 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003912 getSuccessOrdering(), getFailureOrdering(),
3913 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003914 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003915 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003916 return Result;
3917}
3918
Pete Cooper75403d72015-06-24 20:22:23 +00003919AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003920 AtomicRMWInst *Result =
3921 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3922 getOrdering(), getSynchScope());
3923 Result->setVolatile(isVolatile());
3924 return Result;
3925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003928 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3929}
3930
Pete Cooper75403d72015-06-24 20:22:23 +00003931TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003932 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003933}
3934
Pete Cooper75403d72015-06-24 20:22:23 +00003935ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003936 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003937}
3938
Pete Cooper75403d72015-06-24 20:22:23 +00003939SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003940 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941}
3942
Pete Cooper75403d72015-06-24 20:22:23 +00003943FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003944 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945}
3946
Pete Cooper75403d72015-06-24 20:22:23 +00003947FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003948 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003949}
3950
Pete Cooper75403d72015-06-24 20:22:23 +00003951UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003952 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003953}
3954
Pete Cooper75403d72015-06-24 20:22:23 +00003955SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003956 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957}
3958
Pete Cooper75403d72015-06-24 20:22:23 +00003959FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003960 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003961}
3962
Pete Cooper75403d72015-06-24 20:22:23 +00003963FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003964 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003965}
3966
Pete Cooper75403d72015-06-24 20:22:23 +00003967PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003968 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003969}
3970
Pete Cooper75403d72015-06-24 20:22:23 +00003971IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003972 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003973}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003974
Pete Cooper75403d72015-06-24 20:22:23 +00003975BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003976 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003977}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003978
Pete Cooper75403d72015-06-24 20:22:23 +00003979AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003980 return new AddrSpaceCastInst(getOperand(0), getType());
3981}
3982
Pete Cooper75403d72015-06-24 20:22:23 +00003983CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003984 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003985}
3986
Pete Cooper75403d72015-06-24 20:22:23 +00003987SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003988 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003989}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003990
Pete Cooper75403d72015-06-24 20:22:23 +00003991VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003992 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003993}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003994
Pete Cooper75403d72015-06-24 20:22:23 +00003995ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003996 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003997}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003998
Pete Cooper75403d72015-06-24 20:22:23 +00003999InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004000 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004001}
4002
Pete Cooper75403d72015-06-24 20:22:23 +00004003ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004004 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004005}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004006
Pete Cooper75403d72015-06-24 20:22:23 +00004007PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004008
Pete Cooper75403d72015-06-24 20:22:23 +00004009LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004010 return new LandingPadInst(*this);
4011}
4012
Pete Cooper75403d72015-06-24 20:22:23 +00004013ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004014 return new(getNumOperands()) ReturnInst(*this);
4015}
4016
Pete Cooper75403d72015-06-24 20:22:23 +00004017BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004018 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004019}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004020
Pete Cooper75403d72015-06-24 20:22:23 +00004021SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004022
Pete Cooper75403d72015-06-24 20:22:23 +00004023IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004024 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004025}
4026
Pete Cooper75403d72015-06-24 20:22:23 +00004027InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004028 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004029}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004030
Pete Cooper75403d72015-06-24 20:22:23 +00004031ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004032
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004033CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
4034 return new (getNumOperands()) CleanupEndPadInst(*this);
4035}
4036
David Majnemer654e1302015-07-31 17:58:14 +00004037CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4038 return new (getNumOperands()) CleanupReturnInst(*this);
4039}
4040
4041CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
4042 return new (getNumOperands()) CatchEndPadInst(*this);
4043}
4044
4045CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004046 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004047}
4048
4049CatchPadInst *CatchPadInst::cloneImpl() const {
4050 return new (getNumOperands()) CatchPadInst(*this);
4051}
4052
4053TerminatePadInst *TerminatePadInst::cloneImpl() const {
4054 return new (getNumOperands()) TerminatePadInst(*this);
4055}
4056
4057CleanupPadInst *CleanupPadInst::cloneImpl() const {
4058 return new (getNumOperands()) CleanupPadInst(*this);
4059}
4060
Pete Cooper75403d72015-06-24 20:22:23 +00004061UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004062 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004063 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004064}