blob: 9b6dfc2bf6aeac5e849219f38f661817bd57fc0f [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
Sanjoy Das776e4a72015-11-05 01:53:26 +0000346 // There are getNumOperands() - 1 data operands. The last operand is the
347 // callee.
348 assert(i < getNumOperands() && "Data operand index out of bounds!");
349
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000350 // The attribute A can either be directly specified, if the operand in
351 // question is a call argument; or be indirectly implied by the kind of its
352 // containing operand bundle, if the operand is a bundle operand.
353
354 if (i < (getNumArgOperands() + 1))
355 return paramHasAttr(i, A);
356
357 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
358 "Must be either a call argument or an operand bundle!");
359 return getOperandBundleForOperand(i - 1).operandsHaveAttr(A);
360}
361
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000362/// IsConstantOne - Return true only if val is constant int 1
363static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000364 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000365 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
366 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000367}
368
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000369static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000370 BasicBlock *InsertAtEnd, Type *IntPtrTy,
371 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000372 Value *ArraySize, Function *MallocF,
373 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000374 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000375 "createMalloc needs either InsertBefore or InsertAtEnd");
376
377 // malloc(type) becomes:
378 // bitcast (i8* malloc(typeSize)) to type*
379 // malloc(type, arraySize) becomes:
380 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000381 if (!ArraySize)
382 ArraySize = ConstantInt::get(IntPtrTy, 1);
383 else if (ArraySize->getType() != IntPtrTy) {
384 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000385 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
386 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000387 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000388 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
389 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000390 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000392 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000393 if (IsConstantOne(AllocSize)) {
394 AllocSize = ArraySize; // Operand * 1 = Operand
395 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
396 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
397 false /*ZExt*/);
398 // Malloc arg is constant product of type size and array size
399 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
400 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000401 // Multiply type size by the array size...
402 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000403 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
404 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000406 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
407 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410
Victor Hernandez788eaab2009-09-18 19:20:02 +0000411 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412 // Create the call to Malloc.
413 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
414 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000415 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000416 Value *MallocFunc = MallocF;
417 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000418 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000419 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000420 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000421 CallInst *MCall = nullptr;
422 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000424 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000425 Result = MCall;
426 if (Result->getType() != AllocPtrType)
427 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000428 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000429 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000430 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000431 Result = MCall;
432 if (Result->getType() != AllocPtrType) {
433 InsertAtEnd->getInstList().push_back(MCall);
434 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000435 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000436 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 if (Function *F = dyn_cast<Function>(MallocFunc)) {
440 MCall->setCallingConv(F->getCallingConv());
441 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
442 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000443 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000445 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000446}
447
448/// CreateMalloc - Generate the IR for a call to malloc:
449/// 1. Compute the malloc call's argument as the specified type's size,
450/// possibly multiplied by the array size if the array size is not
451/// constant 1.
452/// 2. Call malloc with that argument.
453/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000454Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000455 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000456 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000457 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000458 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000459 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000460 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
469/// Note: This function does not add the bitcast to the basic block, that is the
470/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000471Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000472 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 Value *AllocSize, Value *ArraySize,
474 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000475 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000476 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478
Victor Hernandeze2971492009-10-24 04:23:03 +0000479static Instruction* createFree(Value* Source, Instruction *InsertBefore,
480 BasicBlock *InsertAtEnd) {
481 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
482 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000483 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000484 "Can not free something of nonpointer type!");
485
486 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
487 Module* M = BB->getParent()->getParent();
488
Chris Lattner229907c2011-07-18 04:54:35 +0000489 Type *VoidTy = Type::getVoidTy(M->getContext());
490 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000491 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000492 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000493 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000494 Value *PtrCast = Source;
495 if (InsertBefore) {
496 if (Source->getType() != IntPtrTy)
497 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
498 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
499 } else {
500 if (Source->getType() != IntPtrTy)
501 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
502 Result = CallInst::Create(FreeFunc, PtrCast, "");
503 }
504 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000505 if (Function *F = dyn_cast<Function>(FreeFunc))
506 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000507
508 return Result;
509}
510
511/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000512Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000513 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000514}
515
516/// CreateFree - Generate the IR for a call to the builtin free function.
517/// Note: This function does not add the call to the basic block, that is the
518/// responsibility of the caller.
519Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000520 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000521 assert(FreeCall && "CreateFree did not create a CallInst");
522 return FreeCall;
523}
524
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525//===----------------------------------------------------------------------===//
526// InvokeInst Implementation
527//===----------------------------------------------------------------------===//
528
David Blaikie3e807092015-05-13 18:35:26 +0000529void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
530 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000531 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000532 const Twine &NameStr) {
533 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000534
Sanjoy Das9303c242015-09-24 19:14:18 +0000535 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
536 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000537 Op<-3>() = Fn;
538 Op<-2>() = IfNormal;
539 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000540
541#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000542 assert(((Args.size() == FTy->getNumParams()) ||
543 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000544 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000545
Jay Foad5bd375a2011-07-15 08:37:34 +0000546 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000547 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000548 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000549 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000550#endif
551
552 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000553
554 auto It = populateBundleOperandInfos(Bundles, Args.size());
555 (void)It;
556 assert(It + 3 == op_end() && "Should add up!");
557
Jay Foad5bd375a2011-07-15 08:37:34 +0000558 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000559}
560
Misha Brukmanb1c93172005-04-21 23:48:37 +0000561InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000562 : TerminatorInst(II.getType(), Instruction::Invoke,
563 OperandTraits<InvokeInst>::op_end(this) -
564 II.getNumOperands(),
565 II.getNumOperands()),
566 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000567 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000568 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000569 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
570 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000571 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572}
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
575 return getSuccessor(idx);
576}
577unsigned InvokeInst::getNumSuccessorsV() const {
578 return getNumSuccessors();
579}
580void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
581 return setSuccessor(idx, B);
582}
583
Michael Gottesman41748d72013-06-27 00:25:01 +0000584bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000585 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000586 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000587
588 // Operand bundles override attributes on the called function, but don't
589 // override attributes directly present on the invoke instruction.
590 if (isFnAttrDisallowedByOpBundle(A))
591 return false;
592
Bill Wendling375eb1f2012-10-09 00:28:54 +0000593 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000594 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000595 return false;
596}
597
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000598bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000599 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
600
Bill Wendling749a43d2012-12-30 13:50:49 +0000601 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000602 return true;
603 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000604 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000605 return false;
606}
607
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000608bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
609 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000610 // There are getNumOperands() - 3 data operands. The last three operands are
611 // the callee and the two successor basic blocks.
612 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
613
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000614 // The attribute A can either be directly specified, if the operand in
615 // question is an invoke argument; or be indirectly implied by the kind of its
616 // containing operand bundle, if the operand is a bundle operand.
617
618 if (i < (getNumArgOperands() + 1))
619 return paramHasAttr(i, A);
620
621 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
622 "Must be either an invoke argument or an operand bundle!");
623 return getOperandBundleForOperand(i - 1).operandsHaveAttr(A);
624}
625
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000626void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000627 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000628 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000629 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000630}
631
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000632void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000633 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000634 AttrBuilder B(attr);
635 PAL = PAL.removeAttributes(getContext(), i,
636 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000637 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000638}
639
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000640void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
641 AttributeSet PAL = getAttributes();
642 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
643 setAttributes(PAL);
644}
645
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000646void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
647 AttributeSet PAL = getAttributes();
648 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
649 setAttributes(PAL);
650}
651
Bill Wendlingfae14752011-08-12 20:24:12 +0000652LandingPadInst *InvokeInst::getLandingPadInst() const {
653 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
654}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000655
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656//===----------------------------------------------------------------------===//
657// ReturnInst Implementation
658//===----------------------------------------------------------------------===//
659
Chris Lattner2195fc42007-02-24 00:55:48 +0000660ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000661 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000662 OperandTraits<ReturnInst>::op_end(this) -
663 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000664 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000665 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000666 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000667 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000668}
669
Owen Anderson55f1c092009-08-13 21:58:54 +0000670ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
671 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000672 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
673 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000674 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000675 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000676}
Owen Anderson55f1c092009-08-13 21:58:54 +0000677ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
678 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000679 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
680 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000681 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000682 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000683}
Owen Anderson55f1c092009-08-13 21:58:54 +0000684ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
685 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000686 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000687}
688
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689unsigned ReturnInst::getNumSuccessorsV() const {
690 return getNumSuccessors();
691}
692
Devang Patelae682fb2008-02-26 17:56:20 +0000693/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
694/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000695void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000696 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000697}
698
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000700 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701}
702
Devang Patel59643e52008-02-23 00:35:18 +0000703ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000704}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000706//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000707// ResumeInst Implementation
708//===----------------------------------------------------------------------===//
709
710ResumeInst::ResumeInst(const ResumeInst &RI)
711 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
712 OperandTraits<ResumeInst>::op_begin(this), 1) {
713 Op<0>() = RI.Op<0>();
714}
715
716ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
717 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
718 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
719 Op<0>() = Exn;
720}
721
722ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
723 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
724 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
725 Op<0>() = Exn;
726}
727
728unsigned ResumeInst::getNumSuccessorsV() const {
729 return getNumSuccessors();
730}
731
732void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
733 llvm_unreachable("ResumeInst has no successors!");
734}
735
736BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
737 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000738}
739
740//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000741// CleanupEndPadInst Implementation
742//===----------------------------------------------------------------------===//
743
744CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
745 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
746 OperandTraits<CleanupEndPadInst>::op_end(this) -
747 CEPI.getNumOperands(),
748 CEPI.getNumOperands()) {
749 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
750 setCleanupPad(CEPI.getCleanupPad());
751 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
752 setUnwindDest(UnwindDest);
753}
754
755void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
756 setCleanupPad(CleanupPad);
757 if (UnwindBB) {
758 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
759 setUnwindDest(UnwindBB);
760 }
761}
762
763CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
764 BasicBlock *UnwindBB, unsigned Values,
765 Instruction *InsertBefore)
766 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
767 Instruction::CleanupEndPad,
768 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
769 Values, InsertBefore) {
770 init(CleanupPad, UnwindBB);
771}
772
773CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
774 BasicBlock *UnwindBB, unsigned Values,
775 BasicBlock *InsertAtEnd)
776 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
777 Instruction::CleanupEndPad,
778 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
779 Values, InsertAtEnd) {
780 init(CleanupPad, UnwindBB);
781}
782
783BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
784 assert(Idx == 0);
785 return getUnwindDest();
786}
787unsigned CleanupEndPadInst::getNumSuccessorsV() const {
788 return getNumSuccessors();
789}
790void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
791 assert(Idx == 0);
792 setUnwindDest(B);
793}
794
795//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000796// CleanupReturnInst Implementation
797//===----------------------------------------------------------------------===//
798
799CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
800 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
801 OperandTraits<CleanupReturnInst>::op_end(this) -
802 CRI.getNumOperands(),
803 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000804 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000805 Op<-1>() = CRI.Op<-1>();
806 if (CRI.hasUnwindDest())
807 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000808}
809
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000810void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000811 if (UnwindBB)
812 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000813
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000814 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000815 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000816 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000817}
818
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000819CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000820 BasicBlock *UnwindBB, unsigned Values,
821 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000822 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
823 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000824 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
825 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000826 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000827}
828
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000829CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000830 BasicBlock *UnwindBB, unsigned Values,
831 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000832 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
833 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000834 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
835 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000836 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000837}
838
839BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
840 assert(Idx == 0);
841 return getUnwindDest();
842}
843unsigned CleanupReturnInst::getNumSuccessorsV() const {
844 return getNumSuccessors();
845}
846void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
847 assert(Idx == 0);
848 setUnwindDest(B);
849}
850
851//===----------------------------------------------------------------------===//
852// CatchEndPadInst Implementation
853//===----------------------------------------------------------------------===//
854
855CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
856 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
857 OperandTraits<CatchEndPadInst>::op_end(this) -
858 CRI.getNumOperands(),
859 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000860 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000861 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
862 setUnwindDest(UnwindDest);
863}
864
865void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000866 if (UnwindBB) {
867 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
868 setUnwindDest(UnwindBB);
869 }
870}
871
872CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000873 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000874 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
875 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
876 Values, InsertBefore) {
877 init(UnwindBB);
878}
879
880CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000881 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000882 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
883 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
884 Values, InsertAtEnd) {
885 init(UnwindBB);
886}
887
888BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
889 assert(Idx == 0);
890 return getUnwindDest();
891}
892unsigned CatchEndPadInst::getNumSuccessorsV() const {
893 return getNumSuccessors();
894}
895void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
896 assert(Idx == 0);
897 setUnwindDest(B);
898}
899
900//===----------------------------------------------------------------------===//
901// CatchReturnInst Implementation
902//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000903void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
904 Op<0>() = CatchPad;
905 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000906}
David Majnemer654e1302015-07-31 17:58:14 +0000907
908CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
909 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000910 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
911 Op<0>() = CRI.Op<0>();
912 Op<1>() = CRI.Op<1>();
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 Instruction *InsertBefore)
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 InsertBefore) {
920 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000921}
922
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000923CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000924 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000925 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000926 OperandTraits<CatchReturnInst>::op_begin(this), 2,
927 InsertAtEnd) {
928 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000929}
930
931BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000932 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000933 return getSuccessor();
934}
935unsigned CatchReturnInst::getNumSuccessorsV() const {
936 return getNumSuccessors();
937}
938void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000939 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000940 setSuccessor(B);
941}
942
943//===----------------------------------------------------------------------===//
944// CatchPadInst Implementation
945//===----------------------------------------------------------------------===//
946void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000947 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000948 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
949 Op<-2>() = IfNormal;
950 Op<-1>() = IfException;
951 std::copy(Args.begin(), Args.end(), op_begin());
952 setName(NameStr);
953}
954
955CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
956 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
957 OperandTraits<CatchPadInst>::op_end(this) -
958 CPI.getNumOperands(),
959 CPI.getNumOperands()) {
960 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
961}
962
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000963CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
964 ArrayRef<Value *> Args, unsigned Values,
965 const Twine &NameStr, Instruction *InsertBefore)
966 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
967 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000968 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
969 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000970 init(IfNormal, IfException, Args, NameStr);
971}
972
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000973CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
974 ArrayRef<Value *> Args, unsigned Values,
975 const Twine &NameStr, BasicBlock *InsertAtEnd)
976 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
977 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000978 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
979 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000980 init(IfNormal, IfException, Args, NameStr);
981}
982
983BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
984 return getSuccessor(Idx);
985}
986unsigned CatchPadInst::getNumSuccessorsV() const {
987 return getNumSuccessors();
988}
989void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
990 return setSuccessor(Idx, B);
991}
992
993//===----------------------------------------------------------------------===//
994// TerminatePadInst Implementation
995//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000996void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000997 if (BB)
998 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
999 if (BB)
1000 Op<-1>() = BB;
1001 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +00001002}
1003
1004TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
1005 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
1006 OperandTraits<TerminatePadInst>::op_end(this) -
1007 TPI.getNumOperands(),
1008 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +00001009 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +00001010 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
1011}
1012
1013TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001014 ArrayRef<Value *> Args, unsigned Values,
1015 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001016 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1017 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1018 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001019 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001020}
1021
1022TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001023 ArrayRef<Value *> Args, unsigned Values,
1024 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001025 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1026 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1027 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001028 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001029}
1030
1031BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
1032 assert(Idx == 0);
1033 return getUnwindDest();
1034}
1035unsigned TerminatePadInst::getNumSuccessorsV() const {
1036 return getNumSuccessors();
1037}
1038void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1039 assert(Idx == 0);
1040 return setUnwindDest(B);
1041}
1042
1043//===----------------------------------------------------------------------===//
1044// CleanupPadInst Implementation
1045//===----------------------------------------------------------------------===//
1046void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1047 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1048 std::copy(Args.begin(), Args.end(), op_begin());
1049 setName(NameStr);
1050}
1051
1052CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1053 : Instruction(CPI.getType(), Instruction::CleanupPad,
1054 OperandTraits<CleanupPadInst>::op_end(this) -
1055 CPI.getNumOperands(),
1056 CPI.getNumOperands()) {
1057 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
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, Instruction *InsertBefore)
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(), InsertBefore) {
1065 init(Args, NameStr);
1066}
1067
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001068CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001069 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001070 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001071 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1072 Args.size(), InsertAtEnd) {
1073 init(Args, NameStr);
1074}
1075
1076//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001077// UnreachableInst Implementation
1078//===----------------------------------------------------------------------===//
1079
Owen Anderson55f1c092009-08-13 21:58:54 +00001080UnreachableInst::UnreachableInst(LLVMContext &Context,
1081 Instruction *InsertBefore)
1082 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001083 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001084}
Owen Anderson55f1c092009-08-13 21:58:54 +00001085UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1086 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001087 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001088}
1089
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001090unsigned UnreachableInst::getNumSuccessorsV() const {
1091 return getNumSuccessors();
1092}
1093
1094void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001095 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096}
1097
1098BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001099 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001100}
1101
1102//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103// BranchInst Implementation
1104//===----------------------------------------------------------------------===//
1105
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001106void BranchInst::AssertOK() {
1107 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001108 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110}
1111
Chris Lattner2195fc42007-02-24 00:55:48 +00001112BranchInst::BranchInst(BasicBlock *IfTrue, 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) - 1,
1115 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001116 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001117 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001118}
1119BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1120 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001121 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001122 OperandTraits<BranchInst>::op_end(this) - 3,
1123 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001124 Op<-1>() = IfTrue;
1125 Op<-2>() = IfFalse;
1126 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001127#ifndef NDEBUG
1128 AssertOK();
1129#endif
1130}
1131
1132BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001133 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001134 OperandTraits<BranchInst>::op_end(this) - 1,
1135 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001136 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001137 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001138}
1139
1140BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1141 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001142 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001143 OperandTraits<BranchInst>::op_end(this) - 3,
1144 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001145 Op<-1>() = IfTrue;
1146 Op<-2>() = IfFalse;
1147 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001148#ifndef NDEBUG
1149 AssertOK();
1150#endif
1151}
1152
1153
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001154BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001155 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001156 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1157 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001158 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001159 if (BI.getNumOperands() != 1) {
1160 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001161 Op<-3>() = BI.Op<-3>();
1162 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001164 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001165}
1166
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001167void BranchInst::swapSuccessors() {
1168 assert(isConditional() &&
1169 "Cannot swap successors of an unconditional branch");
1170 Op<-1>().swap(Op<-2>());
1171
1172 // Update profile metadata if present and it matches our structural
1173 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001174 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001175 if (!ProfileData || ProfileData->getNumOperands() != 3)
1176 return;
1177
1178 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001179 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1180 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001181 setMetadata(LLVMContext::MD_prof,
1182 MDNode::get(ProfileData->getContext(), Ops));
1183}
1184
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001185BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1186 return getSuccessor(idx);
1187}
1188unsigned BranchInst::getNumSuccessorsV() const {
1189 return getNumSuccessors();
1190}
1191void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1192 setSuccessor(idx, B);
1193}
1194
1195
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001196//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001197// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198//===----------------------------------------------------------------------===//
1199
Owen Andersonb6b25302009-07-14 23:09:55 +00001200static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001201 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001202 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001203 else {
1204 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001205 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001206 assert(Amt->getType()->isIntegerTy() &&
1207 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001208 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001209 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001210}
1211
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001212AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1213 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001214
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001215AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1216 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001217
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001218AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001219 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001220 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001221
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001222AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001223 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001224 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001225
Chris Lattner229907c2011-07-18 04:54:35 +00001226AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001227 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001228 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1229 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1230 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001231 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001232 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001233 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234}
1235
Chris Lattner229907c2011-07-18 04:54:35 +00001236AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001237 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001238 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1239 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1240 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001241 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001242 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001243 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001244}
1245
Gordon Henriksen14a55692007-12-10 02:14:30 +00001246// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001247AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001248}
1249
Victor Hernandez8acf2952009-10-23 21:09:37 +00001250void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001251 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001252 assert(Align <= MaximumAlignment &&
1253 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001254 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1255 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001256 assert(getAlignment() == Align && "Alignment representation error!");
1257}
1258
Victor Hernandez8acf2952009-10-23 21:09:37 +00001259bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001260 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001261 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001262 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001263}
1264
Chris Lattner8b291e62008-11-26 02:54:17 +00001265/// isStaticAlloca - Return true if this alloca is in the entry block of the
1266/// function and is a constant size. If so, the code generator will fold it
1267/// into the prolog/epilog code, so it is basically free.
1268bool AllocaInst::isStaticAlloca() const {
1269 // Must be constant size.
1270 if (!isa<ConstantInt>(getArraySize())) return false;
1271
1272 // Must be in the entry block.
1273 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001274 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001275}
1276
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001278// LoadInst Implementation
1279//===----------------------------------------------------------------------===//
1280
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001282 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001283 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001284 assert(!(isAtomic() && getAlignment() == 0) &&
1285 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001286}
1287
Daniel Dunbar4975db62009-07-25 04:41:11 +00001288LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001289 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001290
Daniel Dunbar4975db62009-07-25 04:41:11 +00001291LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001292 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293
David Blaikie0c28fd72015-05-20 21:46:30 +00001294LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001296 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001297
1298LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1299 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001300 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001301
David Blaikieb7a029872015-04-17 19:56:21 +00001302LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001303 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001304 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001305 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001306
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001307LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001308 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001309 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001310}
1311
David Blaikie15d9a4c2015-04-06 20:59:48 +00001312LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001313 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001314 SynchronizationScope SynchScope, Instruction *InsertBef)
1315 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001316 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001317 setVolatile(isVolatile);
1318 setAlignment(Align);
1319 setAtomic(Order, SynchScope);
1320 AssertOK();
1321 setName(Name);
1322}
1323
1324LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1325 unsigned Align, AtomicOrdering Order,
1326 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001328 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001329 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001330 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001331 setAlignment(Align);
1332 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001333 AssertOK();
1334 setName(Name);
1335}
1336
Daniel Dunbar27096822009-08-11 18:11:15 +00001337LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1338 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1339 Load, Ptr, InsertBef) {
1340 setVolatile(false);
1341 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001342 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001343 AssertOK();
1344 if (Name && Name[0]) setName(Name);
1345}
1346
1347LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1348 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1349 Load, Ptr, InsertAE) {
1350 setVolatile(false);
1351 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001352 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001353 AssertOK();
1354 if (Name && Name[0]) setName(Name);
1355}
1356
David Blaikie0c28fd72015-05-20 21:46:30 +00001357LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001358 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001359 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1360 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001361 setVolatile(isVolatile);
1362 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001363 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001364 AssertOK();
1365 if (Name && Name[0]) setName(Name);
1366}
1367
1368LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1369 BasicBlock *InsertAE)
1370 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1371 Load, Ptr, InsertAE) {
1372 setVolatile(isVolatile);
1373 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001374 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001375 AssertOK();
1376 if (Name && Name[0]) setName(Name);
1377}
1378
Christopher Lamb84485702007-04-22 19:24:39 +00001379void LoadInst::setAlignment(unsigned Align) {
1380 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001381 assert(Align <= MaximumAlignment &&
1382 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001383 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001384 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001385 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001386}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001387
1388//===----------------------------------------------------------------------===//
1389// StoreInst Implementation
1390//===----------------------------------------------------------------------===//
1391
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001392void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001393 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001394 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001395 "Ptr must have pointer type!");
1396 assert(getOperand(0)->getType() ==
1397 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001398 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001399 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001400 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001401}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001402
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001403StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001404 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001405
1406StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001407 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001408
Misha Brukmanb1c93172005-04-21 23:48:37 +00001409StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001410 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001411 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001412
1413StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001414 BasicBlock *InsertAtEnd)
1415 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1416
1417StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1418 Instruction *InsertBefore)
1419 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1420 InsertBefore) {}
1421
1422StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1423 BasicBlock *InsertAtEnd)
1424 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1425 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001426
Misha Brukmanb1c93172005-04-21 23:48:37 +00001427StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001428 unsigned Align, AtomicOrdering Order,
1429 SynchronizationScope SynchScope,
1430 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001431 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001432 OperandTraits<StoreInst>::op_begin(this),
1433 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001434 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001435 Op<0>() = val;
1436 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001437 setVolatile(isVolatile);
1438 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001439 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001440 AssertOK();
1441}
1442
1443StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001444 unsigned Align, AtomicOrdering Order,
1445 SynchronizationScope SynchScope,
1446 BasicBlock *InsertAtEnd)
1447 : Instruction(Type::getVoidTy(val->getContext()), Store,
1448 OperandTraits<StoreInst>::op_begin(this),
1449 OperandTraits<StoreInst>::operands(this),
1450 InsertAtEnd) {
1451 Op<0>() = val;
1452 Op<1>() = addr;
1453 setVolatile(isVolatile);
1454 setAlignment(Align);
1455 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001456 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001457}
1458
Christopher Lamb84485702007-04-22 19:24:39 +00001459void StoreInst::setAlignment(unsigned Align) {
1460 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001461 assert(Align <= MaximumAlignment &&
1462 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001463 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001464 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001465 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001466}
1467
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001469// AtomicCmpXchgInst Implementation
1470//===----------------------------------------------------------------------===//
1471
1472void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001473 AtomicOrdering SuccessOrdering,
1474 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001475 SynchronizationScope SynchScope) {
1476 Op<0>() = Ptr;
1477 Op<1>() = Cmp;
1478 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001479 setSuccessOrdering(SuccessOrdering);
1480 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001481 setSynchScope(SynchScope);
1482
1483 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1484 "All operands must be non-null!");
1485 assert(getOperand(0)->getType()->isPointerTy() &&
1486 "Ptr must have pointer type!");
1487 assert(getOperand(1)->getType() ==
1488 cast<PointerType>(getOperand(0)->getType())->getElementType()
1489 && "Ptr must be a pointer to Cmp type!");
1490 assert(getOperand(2)->getType() ==
1491 cast<PointerType>(getOperand(0)->getType())->getElementType()
1492 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001493 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001494 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001495 assert(FailureOrdering != NotAtomic &&
1496 "AtomicCmpXchg instructions must be atomic!");
1497 assert(SuccessOrdering >= FailureOrdering &&
1498 "AtomicCmpXchg success ordering must be at least as strong as fail");
1499 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1500 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001501}
1502
1503AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001504 AtomicOrdering SuccessOrdering,
1505 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001506 SynchronizationScope SynchScope,
1507 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001508 : Instruction(
1509 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1510 nullptr),
1511 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1512 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001513 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001514}
1515
1516AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001517 AtomicOrdering SuccessOrdering,
1518 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001519 SynchronizationScope SynchScope,
1520 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001521 : Instruction(
1522 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1523 nullptr),
1524 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1525 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001526 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001527}
Tim Northover420a2162014-06-13 14:24:07 +00001528
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001529//===----------------------------------------------------------------------===//
1530// AtomicRMWInst Implementation
1531//===----------------------------------------------------------------------===//
1532
1533void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1534 AtomicOrdering Ordering,
1535 SynchronizationScope SynchScope) {
1536 Op<0>() = Ptr;
1537 Op<1>() = Val;
1538 setOperation(Operation);
1539 setOrdering(Ordering);
1540 setSynchScope(SynchScope);
1541
1542 assert(getOperand(0) && getOperand(1) &&
1543 "All operands must be non-null!");
1544 assert(getOperand(0)->getType()->isPointerTy() &&
1545 "Ptr must have pointer type!");
1546 assert(getOperand(1)->getType() ==
1547 cast<PointerType>(getOperand(0)->getType())->getElementType()
1548 && "Ptr must be a pointer to Val type!");
1549 assert(Ordering != NotAtomic &&
1550 "AtomicRMW instructions must be atomic!");
1551}
1552
1553AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1554 AtomicOrdering Ordering,
1555 SynchronizationScope SynchScope,
1556 Instruction *InsertBefore)
1557 : Instruction(Val->getType(), AtomicRMW,
1558 OperandTraits<AtomicRMWInst>::op_begin(this),
1559 OperandTraits<AtomicRMWInst>::operands(this),
1560 InsertBefore) {
1561 Init(Operation, Ptr, Val, Ordering, SynchScope);
1562}
1563
1564AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1565 AtomicOrdering Ordering,
1566 SynchronizationScope SynchScope,
1567 BasicBlock *InsertAtEnd)
1568 : Instruction(Val->getType(), AtomicRMW,
1569 OperandTraits<AtomicRMWInst>::op_begin(this),
1570 OperandTraits<AtomicRMWInst>::operands(this),
1571 InsertAtEnd) {
1572 Init(Operation, Ptr, Val, Ordering, SynchScope);
1573}
1574
1575//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001576// FenceInst Implementation
1577//===----------------------------------------------------------------------===//
1578
1579FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1580 SynchronizationScope SynchScope,
1581 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001582 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001583 setOrdering(Ordering);
1584 setSynchScope(SynchScope);
1585}
1586
1587FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1588 SynchronizationScope SynchScope,
1589 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001590 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001591 setOrdering(Ordering);
1592 setSynchScope(SynchScope);
1593}
1594
1595//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596// GetElementPtrInst Implementation
1597//===----------------------------------------------------------------------===//
1598
Jay Foadd1b78492011-07-25 09:48:08 +00001599void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001600 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001601 assert(getNumOperands() == 1 + IdxList.size() &&
1602 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001603 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001604 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001605 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001606}
1607
Gabor Greiff6caff662008-05-10 08:32:32 +00001608GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001609 : Instruction(GEPI.getType(), GetElementPtr,
1610 OperandTraits<GetElementPtrInst>::op_end(this) -
1611 GEPI.getNumOperands(),
1612 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001613 SourceElementType(GEPI.SourceElementType),
1614 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001615 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001616 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001617}
1618
Chris Lattner6090a422009-03-09 04:46:40 +00001619/// getIndexedType - Returns the type of the element that would be accessed with
1620/// a gep instruction with the specified parameters.
1621///
1622/// The Idxs pointer should point to a continuous piece of memory containing the
1623/// indices, either as Value* or uint64_t.
1624///
1625/// A null type is returned if the indices are invalid for the specified
1626/// pointer type.
1627///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001628template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001629static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001630 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001631 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001632 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001633
Chris Lattner6090a422009-03-09 04:46:40 +00001634 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001635 // it cannot be 'stepped over'.
1636 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001637 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001638
Dan Gohman1ecaf452008-05-31 00:58:22 +00001639 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001640 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001641 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001642 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001643 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001644 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001645 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001646 }
Craig Topperc6207612014-04-09 06:08:46 +00001647 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648}
1649
David Blaikied288fb82015-03-30 21:41:43 +00001650Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1651 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001652}
1653
David Blaikied288fb82015-03-30 21:41:43 +00001654Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001655 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001656 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001657}
1658
David Blaikied288fb82015-03-30 21:41:43 +00001659Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1660 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001661}
1662
Chris Lattner45f15572007-04-14 00:12:57 +00001663/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1664/// zeros. If so, the result pointer and the first operand have the same
1665/// value, just potentially different types.
1666bool GetElementPtrInst::hasAllZeroIndices() const {
1667 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1668 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1669 if (!CI->isZero()) return false;
1670 } else {
1671 return false;
1672 }
1673 }
1674 return true;
1675}
1676
Chris Lattner27058292007-04-27 20:35:56 +00001677/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1678/// constant integers. If so, the result pointer and the first operand have
1679/// a constant offset between them.
1680bool GetElementPtrInst::hasAllConstantIndices() const {
1681 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1682 if (!isa<ConstantInt>(getOperand(i)))
1683 return false;
1684 }
1685 return true;
1686}
1687
Dan Gohman1b849082009-09-07 23:54:19 +00001688void GetElementPtrInst::setIsInBounds(bool B) {
1689 cast<GEPOperator>(this)->setIsInBounds(B);
1690}
Chris Lattner45f15572007-04-14 00:12:57 +00001691
Nick Lewycky28a5f252009-09-27 21:33:04 +00001692bool GetElementPtrInst::isInBounds() const {
1693 return cast<GEPOperator>(this)->isInBounds();
1694}
1695
Chandler Carruth1e140532012-12-11 10:29:10 +00001696bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1697 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001698 // Delegate to the generic GEPOperator implementation.
1699 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001700}
1701
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001702//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001703// ExtractElementInst Implementation
1704//===----------------------------------------------------------------------===//
1705
1706ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001707 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001708 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001709 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001710 ExtractElement,
1711 OperandTraits<ExtractElementInst>::op_begin(this),
1712 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001713 assert(isValidOperands(Val, Index) &&
1714 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001715 Op<0>() = Val;
1716 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001717 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001718}
1719
1720ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001721 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001722 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001723 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001724 ExtractElement,
1725 OperandTraits<ExtractElementInst>::op_begin(this),
1726 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001727 assert(isValidOperands(Val, Index) &&
1728 "Invalid extractelement instruction operands!");
1729
Gabor Greif2d3024d2008-05-26 21:33:52 +00001730 Op<0>() = Val;
1731 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001732 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001733}
1734
Chris Lattner65511ff2006-10-05 06:24:58 +00001735
Chris Lattner54865b32006-04-08 04:05:48 +00001736bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001737 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001738 return false;
1739 return true;
1740}
1741
1742
Robert Bocchino23004482006-01-10 19:05:34 +00001743//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001744// InsertElementInst Implementation
1745//===----------------------------------------------------------------------===//
1746
Chris Lattner54865b32006-04-08 04:05:48 +00001747InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001748 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001750 : Instruction(Vec->getType(), InsertElement,
1751 OperandTraits<InsertElementInst>::op_begin(this),
1752 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001753 assert(isValidOperands(Vec, Elt, Index) &&
1754 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001755 Op<0>() = Vec;
1756 Op<1>() = Elt;
1757 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001758 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001759}
1760
Chris Lattner54865b32006-04-08 04:05:48 +00001761InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001762 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001763 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001764 : Instruction(Vec->getType(), InsertElement,
1765 OperandTraits<InsertElementInst>::op_begin(this),
1766 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001767 assert(isValidOperands(Vec, Elt, Index) &&
1768 "Invalid insertelement instruction operands!");
1769
Gabor Greif2d3024d2008-05-26 21:33:52 +00001770 Op<0>() = Vec;
1771 Op<1>() = Elt;
1772 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001773 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001774}
1775
Chris Lattner54865b32006-04-08 04:05:48 +00001776bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1777 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001778 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001779 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001780
Reid Spencerd84d35b2007-02-15 02:26:10 +00001781 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001782 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001783
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001784 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001785 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001786 return true;
1787}
1788
1789
Robert Bocchinoca27f032006-01-17 20:07:22 +00001790//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001791// ShuffleVectorInst Implementation
1792//===----------------------------------------------------------------------===//
1793
1794ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001795 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001796 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001797: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001798 cast<VectorType>(Mask->getType())->getNumElements()),
1799 ShuffleVector,
1800 OperandTraits<ShuffleVectorInst>::op_begin(this),
1801 OperandTraits<ShuffleVectorInst>::operands(this),
1802 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001803 assert(isValidOperands(V1, V2, Mask) &&
1804 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001805 Op<0>() = V1;
1806 Op<1>() = V2;
1807 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001808 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001809}
1810
1811ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001812 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001813 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001814: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1815 cast<VectorType>(Mask->getType())->getNumElements()),
1816 ShuffleVector,
1817 OperandTraits<ShuffleVectorInst>::op_begin(this),
1818 OperandTraits<ShuffleVectorInst>::operands(this),
1819 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001820 assert(isValidOperands(V1, V2, Mask) &&
1821 "Invalid shuffle vector instruction operands!");
1822
Gabor Greif2d3024d2008-05-26 21:33:52 +00001823 Op<0>() = V1;
1824 Op<1>() = V2;
1825 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001826 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001827}
1828
Mon P Wang25f01062008-11-10 04:46:22 +00001829bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001830 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001831 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001832 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001833 return false;
1834
Chris Lattner1dcb6542012-01-25 23:49:49 +00001835 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001836 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001837 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001838 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001839
1840 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001841 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1842 return true;
1843
Nate Begeman60a31c32010-08-13 00:16:46 +00001844 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001845 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001846 for (Value *Op : MV->operands()) {
1847 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001848 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001849 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001850 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001851 return false;
1852 }
1853 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001854 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001855 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001856
1857 if (const ConstantDataSequential *CDS =
1858 dyn_cast<ConstantDataSequential>(Mask)) {
1859 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1860 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1861 if (CDS->getElementAsInteger(i) >= V1Size*2)
1862 return false;
1863 return true;
1864 }
1865
1866 // The bitcode reader can create a place holder for a forward reference
1867 // used as the shuffle mask. When this occurs, the shuffle mask will
1868 // fall into this case and fail. To avoid this error, do this bit of
1869 // ugliness to allow such a mask pass.
1870 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1871 if (CE->getOpcode() == Instruction::UserOp1)
1872 return true;
1873
1874 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001875}
1876
Chris Lattnerf724e342008-03-02 05:28:33 +00001877/// getMaskValue - Return the index from the shuffle mask for the specified
1878/// output result. This is either -1 if the element is undef or a number less
1879/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001880int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1881 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1882 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001883 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001884 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001885 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001886 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001887 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001888}
1889
Chris Lattner1dcb6542012-01-25 23:49:49 +00001890/// getShuffleMask - Return the full mask for this instruction, where each
1891/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001892void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1893 SmallVectorImpl<int> &Result) {
1894 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001895
Chris Lattnercf129702012-01-26 02:51:13 +00001896 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001897 for (unsigned i = 0; i != NumElts; ++i)
1898 Result.push_back(CDS->getElementAsInteger(i));
1899 return;
1900 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001901 for (unsigned i = 0; i != NumElts; ++i) {
1902 Constant *C = Mask->getAggregateElement(i);
1903 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001904 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001905 }
1906}
1907
1908
Dan Gohman12fce772008-05-15 19:50:34 +00001909//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001910// InsertValueInst Class
1911//===----------------------------------------------------------------------===//
1912
Jay Foad57aa6362011-07-13 10:26:04 +00001913void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1914 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001915 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001916
1917 // There's no fundamental reason why we require at least one index
1918 // (other than weirdness with &*IdxBegin being invalid; see
1919 // getelementptr's init routine for example). But there's no
1920 // present need to support it.
1921 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1922
1923 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001924 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001925 Op<0>() = Agg;
1926 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001927
Jay Foad57aa6362011-07-13 10:26:04 +00001928 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001929 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001930}
1931
1932InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001933 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001934 OperandTraits<InsertValueInst>::op_begin(this), 2),
1935 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001936 Op<0>() = IVI.getOperand(0);
1937 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001938 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001939}
1940
1941//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001942// ExtractValueInst Class
1943//===----------------------------------------------------------------------===//
1944
Jay Foad57aa6362011-07-13 10:26:04 +00001945void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001946 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001947
Jay Foad57aa6362011-07-13 10:26:04 +00001948 // There's no fundamental reason why we require at least one index.
1949 // But there's no present need to support it.
1950 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001951
Jay Foad57aa6362011-07-13 10:26:04 +00001952 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001953 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001954}
1955
1956ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001957 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001958 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001959 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001960}
1961
Dan Gohman12fce772008-05-15 19:50:34 +00001962// getIndexedType - Returns the type of the element that would be extracted
1963// with an extractvalue instruction with the specified parameters.
1964//
1965// A null type is returned if the indices are invalid for the specified
1966// pointer type.
1967//
Chris Lattner229907c2011-07-18 04:54:35 +00001968Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001969 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001970 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001971 // We can't use CompositeType::indexValid(Index) here.
1972 // indexValid() always returns true for arrays because getelementptr allows
1973 // out-of-bounds indices. Since we don't allow those for extractvalue and
1974 // insertvalue we need to check array indexing manually.
1975 // Since the only other types we can index into are struct types it's just
1976 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001977 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001978 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001979 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001980 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001981 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001982 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001983 } else {
1984 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001985 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001986 }
1987
1988 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001989 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001990 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001991}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001992
1993//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001994// BinaryOperator Class
1995//===----------------------------------------------------------------------===//
1996
Chris Lattner2195fc42007-02-24 00:55:48 +00001997BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001998 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001999 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002000 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002001 OperandTraits<BinaryOperator>::op_begin(this),
2002 OperandTraits<BinaryOperator>::operands(this),
2003 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002004 Op<0>() = S1;
2005 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002006 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002007 setName(Name);
2008}
2009
2010BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002011 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002012 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002013 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002014 OperandTraits<BinaryOperator>::op_begin(this),
2015 OperandTraits<BinaryOperator>::operands(this),
2016 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002017 Op<0>() = S1;
2018 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002019 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002020 setName(Name);
2021}
2022
2023
2024void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002025 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002026 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002027 assert(LHS->getType() == RHS->getType() &&
2028 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002029#ifndef NDEBUG
2030 switch (iType) {
2031 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002032 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002033 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002034 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002035 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002036 "Tried to create an integer operation on a non-integer type!");
2037 break;
2038 case FAdd: case FSub:
2039 case FMul:
2040 assert(getType() == LHS->getType() &&
2041 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002042 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002043 "Tried to create a floating-point operation on a "
2044 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002045 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002046 case UDiv:
2047 case SDiv:
2048 assert(getType() == LHS->getType() &&
2049 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002050 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002051 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002052 "Incorrect operand type (not integer) for S/UDIV");
2053 break;
2054 case FDiv:
2055 assert(getType() == LHS->getType() &&
2056 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002057 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002058 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002059 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002060 case URem:
2061 case SRem:
2062 assert(getType() == LHS->getType() &&
2063 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002064 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002065 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002066 "Incorrect operand type (not integer) for S/UREM");
2067 break;
2068 case FRem:
2069 assert(getType() == LHS->getType() &&
2070 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002071 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002072 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002073 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002074 case Shl:
2075 case LShr:
2076 case AShr:
2077 assert(getType() == LHS->getType() &&
2078 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002079 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002080 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002081 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002082 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002083 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 case And: case Or:
2085 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002086 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002087 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002088 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002089 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002090 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002091 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002092 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002093 default:
2094 break;
2095 }
2096#endif
2097}
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 Instruction *InsertBefore) {
2102 assert(S1->getType() == S2->getType() &&
2103 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002104 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002105}
2106
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002107BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002108 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002109 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002110 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002111 InsertAtEnd->getInstList().push_back(Res);
2112 return Res;
2113}
2114
Dan Gohman5476cfd2009-08-12 16:23:25 +00002115BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002116 Instruction *InsertBefore) {
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, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002121}
2122
Dan Gohman5476cfd2009-08-12 16:23:25 +00002123BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002124 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002125 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002126 return new BinaryOperator(Instruction::Sub,
2127 zero, Op,
2128 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129}
2130
Dan Gohman4ab44202009-12-18 02:58:50 +00002131BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2132 Instruction *InsertBefore) {
2133 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2134 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2135}
2136
2137BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2138 BasicBlock *InsertAtEnd) {
2139 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2140 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2141}
2142
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002143BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2144 Instruction *InsertBefore) {
2145 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2146 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2147}
2148
2149BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2150 BasicBlock *InsertAtEnd) {
2151 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2152 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2153}
2154
Dan Gohman5476cfd2009-08-12 16:23:25 +00002155BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002156 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002157 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002158 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002159 Op->getType(), Name, InsertBefore);
2160}
2161
Dan Gohman5476cfd2009-08-12 16:23:25 +00002162BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002163 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002164 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002165 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002166 Op->getType(), Name, InsertAtEnd);
2167}
2168
Dan Gohman5476cfd2009-08-12 16:23:25 +00002169BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002170 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002171 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002172 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173 Op->getType(), Name, InsertBefore);
2174}
2175
Dan Gohman5476cfd2009-08-12 16:23:25 +00002176BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002178 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002179 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002180 Op->getType(), Name, InsertAtEnd);
2181}
2182
2183
2184// isConstantAllOnes - Helper function for several functions below
2185static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002186 if (const Constant *C = dyn_cast<Constant>(V))
2187 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002188 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002189}
2190
Owen Andersonbb2501b2009-07-13 22:18:28 +00002191bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002192 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2193 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002194 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2195 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002196 return false;
2197}
2198
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002199bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002200 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2201 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002202 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2203 if (!IgnoreZeroSign)
2204 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2205 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2206 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002207 return false;
2208}
2209
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002210bool BinaryOperator::isNot(const Value *V) {
2211 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2212 return (Bop->getOpcode() == Instruction::Xor &&
2213 (isConstantAllOnes(Bop->getOperand(1)) ||
2214 isConstantAllOnes(Bop->getOperand(0))));
2215 return false;
2216}
2217
Chris Lattner2c7d1772005-04-24 07:28:37 +00002218Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002219 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002220}
2221
Chris Lattner2c7d1772005-04-24 07:28:37 +00002222const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2223 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002224}
2225
Dan Gohmana5b96452009-06-04 22:49:04 +00002226Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002227 return cast<BinaryOperator>(BinOp)->getOperand(1);
2228}
2229
2230const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2231 return getFNegArgument(const_cast<Value*>(BinOp));
2232}
2233
Chris Lattner2c7d1772005-04-24 07:28:37 +00002234Value *BinaryOperator::getNotArgument(Value *BinOp) {
2235 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2236 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2237 Value *Op0 = BO->getOperand(0);
2238 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002239 if (isConstantAllOnes(Op0)) return Op1;
2240
2241 assert(isConstantAllOnes(Op1));
2242 return Op0;
2243}
2244
Chris Lattner2c7d1772005-04-24 07:28:37 +00002245const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2246 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002247}
2248
2249
2250// swapOperands - Exchange the two operands to this instruction. This
2251// instruction is safe to use on any binary instruction and does not
2252// modify the semantics of the instruction. If the instruction is
2253// order dependent (SetLT f.e.) the opcode is changed.
2254//
2255bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002256 if (!isCommutative())
2257 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002258 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002259 return false;
2260}
2261
Dan Gohman1b849082009-09-07 23:54:19 +00002262void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2263 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2264}
2265
2266void BinaryOperator::setHasNoSignedWrap(bool b) {
2267 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2268}
2269
2270void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002271 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002272}
2273
Nick Lewycky28a5f252009-09-27 21:33:04 +00002274bool BinaryOperator::hasNoUnsignedWrap() const {
2275 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2276}
2277
2278bool BinaryOperator::hasNoSignedWrap() const {
2279 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2280}
2281
2282bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002283 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002284}
2285
Sanjay Patela982d992014-09-03 01:06:50 +00002286void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002287 // Copy the wrapping flags.
2288 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2289 setHasNoSignedWrap(OB->hasNoSignedWrap());
2290 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2291 }
2292
2293 // Copy the exact flag.
2294 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2295 setIsExact(PE->isExact());
2296
2297 // Copy the fast-math flags.
2298 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002299 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002300}
2301
Sanjay Patela982d992014-09-03 01:06:50 +00002302void BinaryOperator::andIRFlags(const Value *V) {
2303 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2304 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2305 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2306 }
2307
2308 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2309 setIsExact(isExact() & PE->isExact());
2310
2311 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2312 FastMathFlags FM = getFastMathFlags();
2313 FM &= FP->getFastMathFlags();
2314 copyFastMathFlags(FM);
2315 }
2316}
2317
2318
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002319//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002320// FPMathOperator Class
2321//===----------------------------------------------------------------------===//
2322
2323/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2324/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002325/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002326float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002327 const MDNode *MD =
2328 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002329 if (!MD)
2330 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002331 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002332 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002333}
2334
2335
2336//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002337// CastInst Class
2338//===----------------------------------------------------------------------===//
2339
David Blaikie3a15e142011-12-01 08:00:17 +00002340void CastInst::anchor() {}
2341
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342// Just determine if this cast only deals with integral->integral conversion.
2343bool CastInst::isIntegerCast() const {
2344 switch (getOpcode()) {
2345 default: return false;
2346 case Instruction::ZExt:
2347 case Instruction::SExt:
2348 case Instruction::Trunc:
2349 return true;
2350 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002351 return getOperand(0)->getType()->isIntegerTy() &&
2352 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002354}
2355
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356bool CastInst::isLosslessCast() const {
2357 // Only BitCast can be lossless, exit fast if we're not BitCast
2358 if (getOpcode() != Instruction::BitCast)
2359 return false;
2360
2361 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002362 Type* SrcTy = getOperand(0)->getType();
2363 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 if (SrcTy == DstTy)
2365 return true;
2366
Reid Spencer8d9336d2006-12-31 05:26:44 +00002367 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002368 if (SrcTy->isPointerTy())
2369 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 return false; // Other types have no identity values
2371}
2372
2373/// This function determines if the CastInst does not require any bits to be
2374/// changed in order to effect the cast. Essentially, it identifies cases where
2375/// no code gen is necessary for the cast, hence the name no-op cast. For
2376/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002377/// # bitcast i32* %x to i8*
2378/// # bitcast <2 x i32> %x to <4 x i16>
2379/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002380/// @brief Determine if the described cast is a no-op.
2381bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002382 Type *SrcTy,
2383 Type *DestTy,
2384 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002385 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002386 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 case Instruction::Trunc:
2388 case Instruction::ZExt:
2389 case Instruction::SExt:
2390 case Instruction::FPTrunc:
2391 case Instruction::FPExt:
2392 case Instruction::UIToFP:
2393 case Instruction::SIToFP:
2394 case Instruction::FPToUI:
2395 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002396 case Instruction::AddrSpaceCast:
2397 // TODO: Target informations may give a more accurate answer here.
2398 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 case Instruction::BitCast:
2400 return true; // BitCast never modifies bits.
2401 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002402 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002403 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002405 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002406 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 }
2408}
2409
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002410/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002411bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002412 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2413}
2414
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002415bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002416 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002417 if (getOpcode() == Instruction::PtrToInt)
2418 PtrOpTy = getOperand(0)->getType();
2419 else if (getOpcode() == Instruction::IntToPtr)
2420 PtrOpTy = getType();
2421
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002422 Type *IntPtrTy =
2423 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002424
2425 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2426}
2427
2428/// This function determines if a pair of casts can be eliminated and what
2429/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430/// instructions like this:
2431/// * %F = firstOpcode SrcTy %x to MidTy
2432/// * %S = secondOpcode MidTy %F to DstTy
2433/// The function returns a resultOpcode so these two casts can be replaced with:
2434/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2435/// If no such cast is permited, the function returns 0.
2436unsigned CastInst::isEliminableCastPair(
2437 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002438 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2439 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 // Define the 144 possibilities for these two cast instructions. The values
2441 // in this matrix determine what to do in a given situation and select the
2442 // case in the switch below. The rows correspond to firstOp, the columns
2443 // correspond to secondOp. In looking at the table below, keep in mind
2444 // the following cast properties:
2445 //
2446 // Size Compare Source Destination
2447 // Operator Src ? Size Type Sign Type Sign
2448 // -------- ------------ ------------------- ---------------------
2449 // TRUNC > Integer Any Integral Any
2450 // ZEXT < Integral Unsigned Integer Any
2451 // SEXT < Integral Signed Integer Any
2452 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002453 // FPTOSI n/a FloatPt n/a Integral Signed
2454 // UITOFP n/a Integral Unsigned FloatPt n/a
2455 // SITOFP n/a Integral Signed FloatPt n/a
2456 // FPTRUNC > FloatPt n/a FloatPt n/a
2457 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 // PTRTOINT n/a Pointer n/a Integral Unsigned
2459 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002460 // BITCAST = FirstClass n/a FirstClass n/a
2461 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002462 //
2463 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002464 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2465 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002466 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002467 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002469 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002470 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2472 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002473 // T F F U S F F P I B A -+
2474 // R Z S P P I I T P 2 N T S |
2475 // U E E 2 2 2 2 R E I T C C +- secondOp
2476 // N X X U S F F N X N 2 V V |
2477 // C T T I I P P C T T P T T -+
2478 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002479 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002480 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2481 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2482 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2483 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2484 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002485 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002486 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2487 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2488 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2489 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2490 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002492
Chris Lattner25eea4d2010-07-12 01:19:22 +00002493 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002494 // merging. However, bitcast of A->B->A are allowed.
2495 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2496 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2497 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2498
2499 // Check if any of the bitcasts convert scalars<->vectors.
2500 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2501 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2502 // Unless we are bitcasing to the original type, disallow optimizations.
2503 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504
2505 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2506 [secondOp-Instruction::CastOpsBegin];
2507 switch (ElimCase) {
2508 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002509 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 return 0;
2511 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002512 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513 return firstOp;
2514 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002515 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 return secondOp;
2517 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002518 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002519 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002520 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002521 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 return firstOp;
2523 return 0;
2524 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002525 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002526 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002527 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 return firstOp;
2529 return 0;
2530 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002531 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002532 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002533 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534 return secondOp;
2535 return 0;
2536 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002538 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002539 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 return secondOp;
2541 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002542 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002543 // Cannot simplify if address spaces are different!
2544 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2545 return 0;
2546
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002547 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002548 // We can still fold this without knowing the actual sizes as long we
2549 // know that the intermediate pointer is the largest possible
2550 // pointer size.
2551 // FIXME: Is this always true?
2552 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002553 return Instruction::BitCast;
2554
2555 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002556 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002557 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002558 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559 if (MidSize >= PtrSize)
2560 return Instruction::BitCast;
2561 return 0;
2562 }
2563 case 8: {
2564 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2565 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2566 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002567 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2568 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 if (SrcSize == DstSize)
2570 return Instruction::BitCast;
2571 else if (SrcSize < DstSize)
2572 return firstOp;
2573 return secondOp;
2574 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002575 case 9:
2576 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 return Instruction::ZExt;
2578 case 10:
2579 // fpext followed by ftrunc is allowed if the bit size returned to is
2580 // the same as the original, in which case its just a bitcast
2581 if (SrcTy == DstTy)
2582 return Instruction::BitCast;
2583 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002584 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002586 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002587 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002588 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002589 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2590 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 if (SrcSize <= PtrSize && SrcSize == DstSize)
2592 return Instruction::BitCast;
2593 return 0;
2594 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002595 case 12: {
2596 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2597 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2598 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2599 return Instruction::AddrSpaceCast;
2600 return Instruction::BitCast;
2601 }
2602 case 13:
2603 // FIXME: this state can be merged with (1), but the following assert
2604 // is useful to check the correcteness of the sequence due to semantic
2605 // change of bitcast.
2606 assert(
2607 SrcTy->isPtrOrPtrVectorTy() &&
2608 MidTy->isPtrOrPtrVectorTy() &&
2609 DstTy->isPtrOrPtrVectorTy() &&
2610 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2611 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2612 "Illegal addrspacecast, bitcast sequence!");
2613 // Allowed, use first cast's opcode
2614 return firstOp;
2615 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002616 // bitcast, addrspacecast -> addrspacecast if the element type of
2617 // bitcast's source is the same as that of addrspacecast's destination.
2618 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2619 return Instruction::AddrSpaceCast;
2620 return 0;
2621
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002622 case 15:
2623 // FIXME: this state can be merged with (1), but the following assert
2624 // is useful to check the correcteness of the sequence due to semantic
2625 // change of bitcast.
2626 assert(
2627 SrcTy->isIntOrIntVectorTy() &&
2628 MidTy->isPtrOrPtrVectorTy() &&
2629 DstTy->isPtrOrPtrVectorTy() &&
2630 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2631 "Illegal inttoptr, bitcast sequence!");
2632 // Allowed, use first cast's opcode
2633 return firstOp;
2634 case 16:
2635 // FIXME: this state can be merged with (2), but the following assert
2636 // is useful to check the correcteness of the sequence due to semantic
2637 // change of bitcast.
2638 assert(
2639 SrcTy->isPtrOrPtrVectorTy() &&
2640 MidTy->isPtrOrPtrVectorTy() &&
2641 DstTy->isIntOrIntVectorTy() &&
2642 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2643 "Illegal bitcast, ptrtoint sequence!");
2644 // Allowed, use second cast's opcode
2645 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002646 case 17:
2647 // (sitofp (zext x)) -> (uitofp x)
2648 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002650 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002652 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 default:
Craig Topperc514b542012-02-05 22:14:15 +00002654 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656}
2657
Chris Lattner229907c2011-07-18 04:54:35 +00002658CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002660 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 // Construct and return the appropriate CastInst subclass
2662 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002663 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2664 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2665 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2666 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2667 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2668 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2669 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2670 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2671 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2672 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2673 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2674 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2675 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2676 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678}
2679
Chris Lattner229907c2011-07-18 04:54:35 +00002680CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002681 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002682 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683 // Construct and return the appropriate CastInst subclass
2684 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002685 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2686 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2687 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2688 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2689 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2690 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2691 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2692 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2693 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2694 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2695 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2696 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2697 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2698 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +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 Instruction *InsertBefore) {
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, InsertBefore);
2707 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002708}
2709
Chris Lattner229907c2011-07-18 04:54:35 +00002710CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002711 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002712 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2715 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
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 Instruction *InsertBefore) {
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, InsertBefore);
2723 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002724}
2725
Chris Lattner229907c2011-07-18 04:54:35 +00002726CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002727 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002728 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2731 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
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 Instruction *InsertBefore) {
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, InsertBefore);
2739 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002740}
2741
Chris Lattner229907c2011-07-18 04:54:35 +00002742CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002743 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002744 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002745 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002746 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2747 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002748}
2749
Chris Lattner229907c2011-07-18 04:54:35 +00002750CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002751 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002752 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002753 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2754 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2755 "Invalid cast");
2756 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002757 assert((!Ty->isVectorTy() ||
2758 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002759 "Invalid cast");
2760
Matt Arsenault065ced92013-07-31 00:17:33 +00002761 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002762 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002763
Matt Arsenault740980e2014-07-14 17:24:35 +00002764 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002765}
2766
2767/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002768CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2769 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002770 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002771 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2772 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002773 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002774 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002775 assert((!Ty->isVectorTy() ||
2776 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002777 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002778
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002779 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002780 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002781
Matt Arsenault740980e2014-07-14 17:24:35 +00002782 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2783}
2784
2785CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2786 Value *S, Type *Ty,
2787 const Twine &Name,
2788 BasicBlock *InsertAtEnd) {
2789 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2790 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2791
2792 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2793 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2794
2795 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2796}
2797
2798CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2799 Value *S, Type *Ty,
2800 const Twine &Name,
2801 Instruction *InsertBefore) {
2802 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2803 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2804
2805 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002806 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2807
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002808 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002809}
2810
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002811CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2812 const Twine &Name,
2813 Instruction *InsertBefore) {
2814 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2815 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2816 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2817 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2818
2819 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2820}
2821
Matt Arsenault740980e2014-07-14 17:24:35 +00002822CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002823 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002824 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002825 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002826 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002827 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2828 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002829 Instruction::CastOps opcode =
2830 (SrcBits == DstBits ? Instruction::BitCast :
2831 (SrcBits > DstBits ? Instruction::Trunc :
2832 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002833 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002834}
2835
Chris Lattner229907c2011-07-18 04:54:35 +00002836CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002837 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002838 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002839 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002840 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002841 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2842 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002843 Instruction::CastOps opcode =
2844 (SrcBits == DstBits ? Instruction::BitCast :
2845 (SrcBits > DstBits ? Instruction::Trunc :
2846 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002847 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002848}
2849
Chris Lattner229907c2011-07-18 04:54:35 +00002850CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002851 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002852 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002853 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002854 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002855 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2856 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002857 Instruction::CastOps opcode =
2858 (SrcBits == DstBits ? Instruction::BitCast :
2859 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002860 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002861}
2862
Chris Lattner229907c2011-07-18 04:54:35 +00002863CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002864 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002865 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002866 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002867 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002868 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2869 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002870 Instruction::CastOps opcode =
2871 (SrcBits == DstBits ? Instruction::BitCast :
2872 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002873 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002874}
2875
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002876// Check whether it is valid to call getCastOpcode for these types.
2877// This routine must be kept in sync with getCastOpcode.
2878bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2879 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2880 return false;
2881
2882 if (SrcTy == DestTy)
2883 return true;
2884
2885 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2886 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2887 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2888 // An element by element cast. Valid if casting the elements is valid.
2889 SrcTy = SrcVecTy->getElementType();
2890 DestTy = DestVecTy->getElementType();
2891 }
2892
2893 // Get the bit sizes, we'll need these
2894 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2895 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2896
2897 // Run through the possibilities ...
2898 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002899 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002900 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002901 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002902 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002903 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002904 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002905 // Casting from something else
2906 return SrcTy->isPointerTy();
2907 }
2908 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2909 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002910 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002911 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002912 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002913 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002914 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002915 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002916 return false;
2917 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002918 if (DestTy->isVectorTy()) // Casting to vector
2919 return DestBits == SrcBits;
2920 if (DestTy->isPointerTy()) { // Casting to pointer
2921 if (SrcTy->isPointerTy()) // Casting from pointer
2922 return true;
2923 return SrcTy->isIntegerTy(); // Casting from integral
2924 }
2925 if (DestTy->isX86_MMXTy()) {
2926 if (SrcTy->isVectorTy())
2927 return DestBits == SrcBits; // 64-bit vector to MMX
2928 return false;
2929 } // Casting to something else
2930 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002931}
2932
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002933bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2934 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2935 return false;
2936
2937 if (SrcTy == DestTy)
2938 return true;
2939
2940 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2941 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2942 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2943 // An element by element cast. Valid if casting the elements is valid.
2944 SrcTy = SrcVecTy->getElementType();
2945 DestTy = DestVecTy->getElementType();
2946 }
2947 }
2948 }
2949
2950 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2951 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2952 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2953 }
2954 }
2955
2956 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2957 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2958
2959 // Could still have vectors of pointers if the number of elements doesn't
2960 // match
2961 if (SrcBits == 0 || DestBits == 0)
2962 return false;
2963
2964 if (SrcBits != DestBits)
2965 return false;
2966
2967 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2968 return false;
2969
2970 return true;
2971}
2972
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002973bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002974 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002975 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2976 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002977 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002978 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2979 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002980 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002981
2982 return isBitCastable(SrcTy, DestTy);
2983}
2984
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002985// Provide a way to get a "cast" where the cast opcode is inferred from the
2986// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002987// logic in the castIsValid function below. This axiom should hold:
2988// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2989// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002990// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002991// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002992Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002993CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002994 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2995 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996
Duncan Sands55e50902008-01-06 10:12:28 +00002997 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2998 "Only first class types are castable!");
2999
Duncan Sandsa8514532011-05-18 07:13:41 +00003000 if (SrcTy == DestTy)
3001 return BitCast;
3002
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003003 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003004 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3005 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003006 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3007 // An element by element cast. Find the appropriate opcode based on the
3008 // element types.
3009 SrcTy = SrcVecTy->getElementType();
3010 DestTy = DestVecTy->getElementType();
3011 }
3012
3013 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003014 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3015 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003016
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003018 if (DestTy->isIntegerTy()) { // Casting to integral
3019 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003020 if (DestBits < SrcBits)
3021 return Trunc; // int -> smaller int
3022 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003023 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 return SExt; // signed -> SEXT
3025 else
3026 return ZExt; // unsigned -> ZEXT
3027 } else {
3028 return BitCast; // Same size, No-op cast
3029 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003030 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003031 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003032 return FPToSI; // FP -> sint
3033 else
3034 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003035 } else if (SrcTy->isVectorTy()) {
3036 assert(DestBits == SrcBits &&
3037 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 return BitCast; // Same size, no-op cast
3039 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003040 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 "Casting from a value that is not first-class type");
3042 return PtrToInt; // ptr -> int
3043 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003044 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3045 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003046 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047 return SIToFP; // sint -> FP
3048 else
3049 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003050 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 if (DestBits < SrcBits) {
3052 return FPTrunc; // FP -> smaller FP
3053 } else if (DestBits > SrcBits) {
3054 return FPExt; // FP -> larger FP
3055 } else {
3056 return BitCast; // same size, no-op cast
3057 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003058 } else if (SrcTy->isVectorTy()) {
3059 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003060 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003061 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003062 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003063 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003064 } else if (DestTy->isVectorTy()) {
3065 assert(DestBits == SrcBits &&
3066 "Illegal cast to vector (wrong type or size)");
3067 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003068 } else if (DestTy->isPointerTy()) {
3069 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003070 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3071 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003073 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003074 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003076 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003077 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003078 if (SrcTy->isVectorTy()) {
3079 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003080 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003081 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003082 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003084 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085}
3086
3087//===----------------------------------------------------------------------===//
3088// CastInst SubClass Constructors
3089//===----------------------------------------------------------------------===//
3090
3091/// Check that the construction parameters for a CastInst are correct. This
3092/// could be broken out into the separate constructors but it is useful to have
3093/// it in one place and to eliminate the redundant code for getting the sizes
3094/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003095bool
Chris Lattner229907c2011-07-18 04:54:35 +00003096CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003097
3098 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003099 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003100
Chris Lattner37bc78a2010-01-26 21:51:43 +00003101 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3102 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103 return false;
3104
3105 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003106 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3107 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003108
Duncan Sands7f646562011-05-18 09:21:57 +00003109 // If these are vector types, get the lengths of the vectors (using zero for
3110 // scalar types means that checking that vector lengths match also checks that
3111 // scalars are not being converted to vectors or vectors to scalars).
3112 unsigned SrcLength = SrcTy->isVectorTy() ?
3113 cast<VectorType>(SrcTy)->getNumElements() : 0;
3114 unsigned DstLength = DstTy->isVectorTy() ?
3115 cast<VectorType>(DstTy)->getNumElements() : 0;
3116
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003117 // Switch on the opcode provided
3118 switch (op) {
3119 default: return false; // This is an input error
3120 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003121 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3122 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003124 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3125 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003127 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3128 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003130 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3131 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003133 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3134 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003137 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3138 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003141 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3142 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 case Instruction::PtrToInt:
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()->isPointerTy() &&
3150 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003152 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003153 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003154 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3155 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3156 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003157 return SrcTy->getScalarType()->isIntegerTy() &&
3158 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003159 case Instruction::BitCast: {
3160 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3161 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3162
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163 // BitCast implies a no-op cast of type only. No bits change.
3164 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003165 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166 return false;
3167
Alp Tokerf907b892013-12-05 05:44:44 +00003168 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003169 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003170 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003171 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3172
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003173 // If both are pointers then the address spaces must match.
3174 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3175 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003176
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003177 // A vector of pointers must have the same number of elements.
3178 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3179 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3180 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3181
3182 return false;
3183 }
3184
3185 return true;
3186 }
3187 case Instruction::AddrSpaceCast: {
3188 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3189 if (!SrcPtrTy)
3190 return false;
3191
3192 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3193 if (!DstPtrTy)
3194 return false;
3195
3196 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3197 return false;
3198
3199 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3200 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3201 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3202
3203 return false;
3204 }
3205
3206 return true;
3207 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208 }
3209}
3210
3211TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215}
3216
3217TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003218 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221}
3222
3223ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003224 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003227}
3228
3229ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003230 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233}
3234SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003235 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003237 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238}
3239
Jeff Cohencc08c832006-12-02 02:22:01 +00003240SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003241 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003243 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244}
3245
3246FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003247 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003249 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003250}
3251
3252FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003253 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003255 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003256}
3257
3258FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003259 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003261 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262}
3263
3264FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003265 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003267 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003268}
3269
3270UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003271 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003273 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003274}
3275
3276UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003277 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003279 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003280}
3281
3282SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003283 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003285 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003286}
3287
3288SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003289 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003291 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003292}
3293
3294FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003295 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003297 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003298}
3299
3300FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003301 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003303 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003304}
3305
3306FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003307 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003309 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003310}
3311
3312FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003313 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003315 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003316}
3317
3318PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003319 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003321 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003322}
3323
3324PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003325 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003327 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003328}
3329
3330IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003331 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003332) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003333 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003334}
3335
3336IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003337 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003338) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003339 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003340}
3341
3342BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003343 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003344) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003345 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003346}
3347
3348BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003349 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003350) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003351 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003352}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003353
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003354AddrSpaceCastInst::AddrSpaceCastInst(
3355 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3356) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3357 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3358}
3359
3360AddrSpaceCastInst::AddrSpaceCastInst(
3361 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3362) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3363 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3364}
3365
Chris Lattnerf16dc002006-09-17 19:29:56 +00003366//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003367// CmpInst Classes
3368//===----------------------------------------------------------------------===//
3369
Craig Topper3186c012012-09-23 02:12:10 +00003370void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003371
Chris Lattner229907c2011-07-18 04:54:35 +00003372CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003373 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003374 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003375 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003376 OperandTraits<CmpInst>::op_begin(this),
3377 OperandTraits<CmpInst>::operands(this),
3378 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003379 Op<0>() = LHS;
3380 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003381 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003382 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003383}
Gabor Greiff6caff662008-05-10 08:32:32 +00003384
Chris Lattner229907c2011-07-18 04:54:35 +00003385CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003386 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003387 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003388 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003389 OperandTraits<CmpInst>::op_begin(this),
3390 OperandTraits<CmpInst>::operands(this),
3391 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003392 Op<0>() = LHS;
3393 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003394 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003395 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003396}
3397
3398CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003399CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003400 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003401 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003402 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003403 if (InsertBefore)
3404 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3405 S1, S2, Name);
3406 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003407 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003408 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003409 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003410
3411 if (InsertBefore)
3412 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3413 S1, S2, Name);
3414 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003415 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003416 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003417}
3418
3419CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003420CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003421 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003422 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3424 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003425 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003426 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3427 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003428}
3429
3430void CmpInst::swapOperands() {
3431 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3432 IC->swapOperands();
3433 else
3434 cast<FCmpInst>(this)->swapOperands();
3435}
3436
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003437bool CmpInst::isCommutative() const {
3438 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003439 return IC->isCommutative();
3440 return cast<FCmpInst>(this)->isCommutative();
3441}
3442
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003443bool CmpInst::isEquality() const {
3444 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003445 return IC->isEquality();
3446 return cast<FCmpInst>(this)->isEquality();
3447}
3448
3449
Dan Gohman4e724382008-05-31 02:47:54 +00003450CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003451 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003452 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003453 case ICMP_EQ: return ICMP_NE;
3454 case ICMP_NE: return ICMP_EQ;
3455 case ICMP_UGT: return ICMP_ULE;
3456 case ICMP_ULT: return ICMP_UGE;
3457 case ICMP_UGE: return ICMP_ULT;
3458 case ICMP_ULE: return ICMP_UGT;
3459 case ICMP_SGT: return ICMP_SLE;
3460 case ICMP_SLT: return ICMP_SGE;
3461 case ICMP_SGE: return ICMP_SLT;
3462 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003463
Dan Gohman4e724382008-05-31 02:47:54 +00003464 case FCMP_OEQ: return FCMP_UNE;
3465 case FCMP_ONE: return FCMP_UEQ;
3466 case FCMP_OGT: return FCMP_ULE;
3467 case FCMP_OLT: return FCMP_UGE;
3468 case FCMP_OGE: return FCMP_ULT;
3469 case FCMP_OLE: return FCMP_UGT;
3470 case FCMP_UEQ: return FCMP_ONE;
3471 case FCMP_UNE: return FCMP_OEQ;
3472 case FCMP_UGT: return FCMP_OLE;
3473 case FCMP_ULT: return FCMP_OGE;
3474 case FCMP_UGE: return FCMP_OLT;
3475 case FCMP_ULE: return FCMP_OGT;
3476 case FCMP_ORD: return FCMP_UNO;
3477 case FCMP_UNO: return FCMP_ORD;
3478 case FCMP_TRUE: return FCMP_FALSE;
3479 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003480 }
3481}
3482
Reid Spencer266e42b2006-12-23 06:05:41 +00003483ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3484 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003485 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003486 case ICMP_EQ: case ICMP_NE:
3487 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3488 return pred;
3489 case ICMP_UGT: return ICMP_SGT;
3490 case ICMP_ULT: return ICMP_SLT;
3491 case ICMP_UGE: return ICMP_SGE;
3492 case ICMP_ULE: return ICMP_SLE;
3493 }
3494}
3495
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003496ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3497 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003498 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003499 case ICMP_EQ: case ICMP_NE:
3500 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3501 return pred;
3502 case ICMP_SGT: return ICMP_UGT;
3503 case ICMP_SLT: return ICMP_ULT;
3504 case ICMP_SGE: return ICMP_UGE;
3505 case ICMP_SLE: return ICMP_ULE;
3506 }
3507}
3508
Reid Spencer0286bc12007-02-28 22:00:54 +00003509/// Initialize a set of values that all satisfy the condition with C.
3510///
3511ConstantRange
3512ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3513 APInt Lower(C);
3514 APInt Upper(C);
3515 uint32_t BitWidth = C.getBitWidth();
3516 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003517 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003518 case ICmpInst::ICMP_EQ: ++Upper; break;
3519 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003520 case ICmpInst::ICMP_ULT:
3521 Lower = APInt::getMinValue(BitWidth);
3522 // Check for an empty-set condition.
3523 if (Lower == Upper)
3524 return ConstantRange(BitWidth, /*isFullSet=*/false);
3525 break;
3526 case ICmpInst::ICMP_SLT:
3527 Lower = APInt::getSignedMinValue(BitWidth);
3528 // Check for an empty-set condition.
3529 if (Lower == Upper)
3530 return ConstantRange(BitWidth, /*isFullSet=*/false);
3531 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003532 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003533 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003534 // Check for an empty-set condition.
3535 if (Lower == Upper)
3536 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003537 break;
3538 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003539 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003540 // Check for an empty-set condition.
3541 if (Lower == Upper)
3542 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003543 break;
3544 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003545 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003546 // Check for a full-set condition.
3547 if (Lower == Upper)
3548 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003549 break;
3550 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003551 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003552 // Check for a full-set condition.
3553 if (Lower == Upper)
3554 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003555 break;
3556 case ICmpInst::ICMP_UGE:
3557 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003558 // Check for a full-set condition.
3559 if (Lower == Upper)
3560 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003561 break;
3562 case ICmpInst::ICMP_SGE:
3563 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003564 // Check for a full-set condition.
3565 if (Lower == Upper)
3566 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003567 break;
3568 }
3569 return ConstantRange(Lower, Upper);
3570}
3571
Dan Gohman4e724382008-05-31 02:47:54 +00003572CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003573 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003574 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003575 case ICMP_EQ: case ICMP_NE:
3576 return pred;
3577 case ICMP_SGT: return ICMP_SLT;
3578 case ICMP_SLT: return ICMP_SGT;
3579 case ICMP_SGE: return ICMP_SLE;
3580 case ICMP_SLE: return ICMP_SGE;
3581 case ICMP_UGT: return ICMP_ULT;
3582 case ICMP_ULT: return ICMP_UGT;
3583 case ICMP_UGE: return ICMP_ULE;
3584 case ICMP_ULE: return ICMP_UGE;
3585
Reid Spencerd9436b62006-11-20 01:22:35 +00003586 case FCMP_FALSE: case FCMP_TRUE:
3587 case FCMP_OEQ: case FCMP_ONE:
3588 case FCMP_UEQ: case FCMP_UNE:
3589 case FCMP_ORD: case FCMP_UNO:
3590 return pred;
3591 case FCMP_OGT: return FCMP_OLT;
3592 case FCMP_OLT: return FCMP_OGT;
3593 case FCMP_OGE: return FCMP_OLE;
3594 case FCMP_OLE: return FCMP_OGE;
3595 case FCMP_UGT: return FCMP_ULT;
3596 case FCMP_ULT: return FCMP_UGT;
3597 case FCMP_UGE: return FCMP_ULE;
3598 case FCMP_ULE: return FCMP_UGE;
3599 }
3600}
3601
Sanjoy Das6e78b172015-10-22 19:57:34 +00003602CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3603 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3604
3605 switch (pred) {
3606 default:
3607 llvm_unreachable("Unknown predicate!");
3608 case CmpInst::ICMP_ULT:
3609 return CmpInst::ICMP_SLT;
3610 case CmpInst::ICMP_ULE:
3611 return CmpInst::ICMP_SLE;
3612 case CmpInst::ICMP_UGT:
3613 return CmpInst::ICMP_SGT;
3614 case CmpInst::ICMP_UGE:
3615 return CmpInst::ICMP_SGE;
3616 }
3617}
3618
Reid Spencer266e42b2006-12-23 06:05:41 +00003619bool CmpInst::isUnsigned(unsigned short predicate) {
3620 switch (predicate) {
3621 default: return false;
3622 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3623 case ICmpInst::ICMP_UGE: return true;
3624 }
3625}
3626
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003627bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003628 switch (predicate) {
3629 default: return false;
3630 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3631 case ICmpInst::ICMP_SGE: return true;
3632 }
3633}
3634
3635bool CmpInst::isOrdered(unsigned short predicate) {
3636 switch (predicate) {
3637 default: return false;
3638 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3639 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3640 case FCmpInst::FCMP_ORD: return true;
3641 }
3642}
3643
3644bool CmpInst::isUnordered(unsigned short predicate) {
3645 switch (predicate) {
3646 default: return false;
3647 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3648 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3649 case FCmpInst::FCMP_UNO: return true;
3650 }
3651}
3652
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003653bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3654 switch(predicate) {
3655 default: return false;
3656 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3657 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3658 }
3659}
3660
3661bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3662 switch(predicate) {
3663 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3664 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3665 default: return false;
3666 }
3667}
3668
3669
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003670//===----------------------------------------------------------------------===//
3671// SwitchInst Implementation
3672//===----------------------------------------------------------------------===//
3673
Chris Lattnerbaf00152010-11-17 05:41:46 +00003674void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3675 assert(Value && Default && NumReserved);
3676 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003677 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003678 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003679
Pete Coopereb31b682015-05-21 22:48:54 +00003680 Op<0>() = Value;
3681 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003682}
3683
Chris Lattner2195fc42007-02-24 00:55:48 +00003684/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3685/// switch on and a default destination. The number of additional cases can
3686/// be specified here to make memory allocation more efficient. This
3687/// constructor can also autoinsert before another instruction.
3688SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3689 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003690 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003691 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003692 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003693}
3694
3695/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3696/// switch on and a default destination. The number of additional cases can
3697/// be specified here to make memory allocation more efficient. This
3698/// constructor also autoinserts at the end of the specified BasicBlock.
3699SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3700 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003701 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003702 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003703 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003704}
3705
Misha Brukmanb1c93172005-04-21 23:48:37 +00003706SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003707 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003708 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003709 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003710 Use *OL = getOperandList();
3711 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003712 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003713 OL[i] = InOL[i];
3714 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003715 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003716 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003717}
3718
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003719
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003720/// addCase - Add an entry to the switch instruction...
3721///
Chris Lattner47ac1872005-02-24 05:32:09 +00003722void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003723 unsigned NewCaseIdx = getNumCases();
3724 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003725 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003726 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003727 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003728 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003729 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003730 CaseIt Case(this, NewCaseIdx);
3731 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003732 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003733}
3734
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003735/// removeCase - This method removes the specified case and its successor
3736/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003737void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003738 unsigned idx = i.getCaseIndex();
3739
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003740 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003741
3742 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003743 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003744
Jay Foad14277722011-02-01 09:22:34 +00003745 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003746 if (2 + (idx + 1) * 2 != NumOps) {
3747 OL[2 + idx * 2] = OL[NumOps - 2];
3748 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003749 }
3750
3751 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003752 OL[NumOps-2].set(nullptr);
3753 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003754 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003755}
3756
Jay Foade98f29d2011-04-01 08:00:58 +00003757/// growOperands - grow operands - This grows the operand list in response
3758/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003759///
Jay Foade98f29d2011-04-01 08:00:58 +00003760void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003761 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003762 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003763
3764 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003765 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003766}
3767
3768
3769BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3770 return getSuccessor(idx);
3771}
3772unsigned SwitchInst::getNumSuccessorsV() const {
3773 return getNumSuccessors();
3774}
3775void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3776 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003777}
Chris Lattnerf22be932004-10-15 23:52:53 +00003778
Chris Lattner3ed871f2009-10-27 19:13:16 +00003779//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003780// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003781//===----------------------------------------------------------------------===//
3782
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003783void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003784 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003785 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003786 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003787 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003788 allocHungoffUses(ReservedSpace);
3789
Pete Coopereb31b682015-05-21 22:48:54 +00003790 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003791}
3792
3793
Jay Foade98f29d2011-04-01 08:00:58 +00003794/// growOperands - grow operands - This grows the operand list in response
3795/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003796///
Jay Foade98f29d2011-04-01 08:00:58 +00003797void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003799 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003800
3801 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003802 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003803}
3804
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003805IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3806 Instruction *InsertBefore)
3807: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003808 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003809 init(Address, NumCases);
3810}
3811
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003812IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3813 BasicBlock *InsertAtEnd)
3814: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003815 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003816 init(Address, NumCases);
3817}
3818
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003819IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003820 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3821 nullptr, IBI.getNumOperands()) {
3822 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003823 Use *OL = getOperandList();
3824 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003825 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3826 OL[i] = InOL[i];
3827 SubclassOptionalData = IBI.SubclassOptionalData;
3828}
3829
Chris Lattner3ed871f2009-10-27 19:13:16 +00003830/// addDestination - Add a destination.
3831///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003832void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003833 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003834 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003835 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003836 // Initialize some new operands.
3837 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003838 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003839 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003840}
3841
3842/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003843/// indirectbr instruction.
3844void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003845 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3846
3847 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003848 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003849
3850 // Replace this value with the last one.
3851 OL[idx+1] = OL[NumOps-1];
3852
3853 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003854 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003855 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003856}
3857
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003858BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003859 return getSuccessor(idx);
3860}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003861unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003862 return getNumSuccessors();
3863}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003864void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003865 setSuccessor(idx, B);
3866}
3867
3868//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003869// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003870//===----------------------------------------------------------------------===//
3871
Chris Lattnerf22be932004-10-15 23:52:53 +00003872// Define these methods here so vtables don't get emitted into every translation
3873// unit that uses these classes.
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003897}
3898
Pete Cooper75403d72015-06-24 20:22:23 +00003899AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003900 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3901 (Value *)getOperand(0), getAlignment());
3902 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3903 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003904}
3905
Pete Cooper75403d72015-06-24 20:22:23 +00003906LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003907 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3908 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909}
3910
Pete Cooper75403d72015-06-24 20:22:23 +00003911StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003912 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003913 getAlignment(), getOrdering(), getSynchScope());
3914
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003915}
3916
Pete Cooper75403d72015-06-24 20:22:23 +00003917AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003918 AtomicCmpXchgInst *Result =
3919 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003920 getSuccessOrdering(), getFailureOrdering(),
3921 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003922 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003923 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003924 return Result;
3925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003928 AtomicRMWInst *Result =
3929 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3930 getOrdering(), getSynchScope());
3931 Result->setVolatile(isVolatile());
3932 return Result;
3933}
3934
Pete Cooper75403d72015-06-24 20:22:23 +00003935FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003936 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3937}
3938
Pete Cooper75403d72015-06-24 20:22:23 +00003939TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003940 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941}
3942
Pete Cooper75403d72015-06-24 20:22:23 +00003943ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003944 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945}
3946
Pete Cooper75403d72015-06-24 20:22:23 +00003947SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003948 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003949}
3950
Pete Cooper75403d72015-06-24 20:22:23 +00003951FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003952 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003953}
3954
Pete Cooper75403d72015-06-24 20:22:23 +00003955FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003956 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957}
3958
Pete Cooper75403d72015-06-24 20:22:23 +00003959UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003960 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003961}
3962
Pete Cooper75403d72015-06-24 20:22:23 +00003963SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003964 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003965}
3966
Pete Cooper75403d72015-06-24 20:22:23 +00003967FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003968 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003969}
3970
Pete Cooper75403d72015-06-24 20:22:23 +00003971FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003972 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003973}
3974
Pete Cooper75403d72015-06-24 20:22:23 +00003975PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003976 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003977}
3978
Pete Cooper75403d72015-06-24 20:22:23 +00003979IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003980 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003981}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003982
Pete Cooper75403d72015-06-24 20:22:23 +00003983BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003984 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003985}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003986
Pete Cooper75403d72015-06-24 20:22:23 +00003987AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003988 return new AddrSpaceCastInst(getOperand(0), getType());
3989}
3990
Pete Cooper75403d72015-06-24 20:22:23 +00003991CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003992 if (hasOperandBundles()) {
3993 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3994 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3995 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003996 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003997}
3998
Pete Cooper75403d72015-06-24 20:22:23 +00003999SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004000 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004001}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004002
Pete Cooper75403d72015-06-24 20:22:23 +00004003VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004004 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004005}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004006
Pete Cooper75403d72015-06-24 20:22:23 +00004007ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004008 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004009}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004010
Pete Cooper75403d72015-06-24 20:22:23 +00004011InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004012 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004013}
4014
Pete Cooper75403d72015-06-24 20:22:23 +00004015ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004016 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004017}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004018
Pete Cooper75403d72015-06-24 20:22:23 +00004019PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004020
Pete Cooper75403d72015-06-24 20:22:23 +00004021LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004022 return new LandingPadInst(*this);
4023}
4024
Pete Cooper75403d72015-06-24 20:22:23 +00004025ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004026 return new(getNumOperands()) ReturnInst(*this);
4027}
4028
Pete Cooper75403d72015-06-24 20:22:23 +00004029BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004030 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004031}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004032
Pete Cooper75403d72015-06-24 20:22:23 +00004033SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004034
Pete Cooper75403d72015-06-24 20:22:23 +00004035IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004036 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004037}
4038
Pete Cooper75403d72015-06-24 20:22:23 +00004039InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004040 if (hasOperandBundles()) {
4041 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4042 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4043 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004044 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004045}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004046
Pete Cooper75403d72015-06-24 20:22:23 +00004047ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004048
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004049CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
4050 return new (getNumOperands()) CleanupEndPadInst(*this);
4051}
4052
David Majnemer654e1302015-07-31 17:58:14 +00004053CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4054 return new (getNumOperands()) CleanupReturnInst(*this);
4055}
4056
4057CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
4058 return new (getNumOperands()) CatchEndPadInst(*this);
4059}
4060
4061CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004062 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004063}
4064
4065CatchPadInst *CatchPadInst::cloneImpl() const {
4066 return new (getNumOperands()) CatchPadInst(*this);
4067}
4068
4069TerminatePadInst *TerminatePadInst::cloneImpl() const {
4070 return new (getNumOperands()) TerminatePadInst(*this);
4071}
4072
4073CleanupPadInst *CleanupPadInst::cloneImpl() const {
4074 return new (getNumOperands()) CleanupPadInst(*this);
4075}
4076
Pete Cooper75403d72015-06-24 20:22:23 +00004077UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004078 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004079 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004080}