blob: 5863456adfcd5b325f344f603852cd9b1cf06ca1 [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
Sanjoy Das2d161452015-11-18 06:23:38 +0000300CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
301 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000302 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000303
304 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
305 InsertPt);
306 NewCI->setTailCallKind(CI->getTailCallKind());
307 NewCI->setCallingConv(CI->getCallingConv());
308 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000309 NewCI->setAttributes(CI->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000310 return NewCI;
311}
312
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000313void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000314 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000315 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000316 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000317}
318
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000319void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
320 AttributeSet PAL = getAttributes();
321 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
322 setAttributes(PAL);
323}
324
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000325void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000326 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000327 AttrBuilder B(attr);
328 LLVMContext &Context = getContext();
329 PAL = PAL.removeAttributes(Context, i,
330 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000331 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000332}
333
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000334void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
335 AttributeSet PAL = getAttributes();
336 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
337 setAttributes(PAL);
338}
339
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000340void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
341 AttributeSet PAL = getAttributes();
342 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
343 setAttributes(PAL);
344}
345
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000346bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000347 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
348
Bill Wendling749a43d2012-12-30 13:50:49 +0000349 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000350 return true;
351 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000352 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000353 return false;
354}
355
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000356bool CallInst::dataOperandHasImpliedAttr(unsigned i,
357 Attribute::AttrKind A) const {
358
Sanjoy Das776e4a72015-11-05 01:53:26 +0000359 // There are getNumOperands() - 1 data operands. The last operand is the
360 // callee.
361 assert(i < getNumOperands() && "Data operand index out of bounds!");
362
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000363 // The attribute A can either be directly specified, if the operand in
364 // question is a call argument; or be indirectly implied by the kind of its
365 // containing operand bundle, if the operand is a bundle operand.
366
367 if (i < (getNumArgOperands() + 1))
368 return paramHasAttr(i, A);
369
370 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
371 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000372 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000373}
374
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375/// IsConstantOne - Return true only if val is constant int 1
376static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000377 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000378 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
379 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380}
381
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000382static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000383 BasicBlock *InsertAtEnd, Type *IntPtrTy,
384 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000385 Value *ArraySize, Function *MallocF,
386 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000387 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000388 "createMalloc needs either InsertBefore or InsertAtEnd");
389
390 // malloc(type) becomes:
391 // bitcast (i8* malloc(typeSize)) to type*
392 // malloc(type, arraySize) becomes:
393 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000394 if (!ArraySize)
395 ArraySize = ConstantInt::get(IntPtrTy, 1);
396 else if (ArraySize->getType() != IntPtrTy) {
397 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000398 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
399 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000400 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000401 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
402 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000403 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000405 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406 if (IsConstantOne(AllocSize)) {
407 AllocSize = ArraySize; // Operand * 1 = Operand
408 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
409 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
410 false /*ZExt*/);
411 // Malloc arg is constant product of type size and array size
412 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
413 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000414 // Multiply type size by the array size...
415 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000416 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
417 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000418 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000419 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
420 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000422 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423
Victor Hernandez788eaab2009-09-18 19:20:02 +0000424 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425 // Create the call to Malloc.
426 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
427 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000428 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000429 Value *MallocFunc = MallocF;
430 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000431 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000432 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000433 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000434 CallInst *MCall = nullptr;
435 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000436 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000437 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000438 Result = MCall;
439 if (Result->getType() != AllocPtrType)
440 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000441 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000442 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000443 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000444 Result = MCall;
445 if (Result->getType() != AllocPtrType) {
446 InsertAtEnd->getInstList().push_back(MCall);
447 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000448 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000449 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000450 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000452 if (Function *F = dyn_cast<Function>(MallocFunc)) {
453 MCall->setCallingConv(F->getCallingConv());
454 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
455 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000456 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000457
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000458 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000459}
460
461/// CreateMalloc - Generate the IR for a call to malloc:
462/// 1. Compute the malloc call's argument as the specified type's size,
463/// possibly multiplied by the array size if the array size is not
464/// constant 1.
465/// 2. Call malloc with that argument.
466/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000467Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000468 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000470 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000472 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000473 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474}
475
476/// CreateMalloc - Generate the IR for a call to malloc:
477/// 1. Compute the malloc call's argument as the specified type's size,
478/// possibly multiplied by the array size if the array size is not
479/// constant 1.
480/// 2. Call malloc with that argument.
481/// 3. Bitcast the result of the malloc call to the specified type.
482/// Note: This function does not add the bitcast to the basic block, that is the
483/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000484Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000485 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000486 Value *AllocSize, Value *ArraySize,
487 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000488 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000489 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000491
Victor Hernandeze2971492009-10-24 04:23:03 +0000492static Instruction* createFree(Value* Source, Instruction *InsertBefore,
493 BasicBlock *InsertAtEnd) {
494 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
495 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000496 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000497 "Can not free something of nonpointer type!");
498
499 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
500 Module* M = BB->getParent()->getParent();
501
Chris Lattner229907c2011-07-18 04:54:35 +0000502 Type *VoidTy = Type::getVoidTy(M->getContext());
503 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000504 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000505 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000506 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000507 Value *PtrCast = Source;
508 if (InsertBefore) {
509 if (Source->getType() != IntPtrTy)
510 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
511 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
512 } else {
513 if (Source->getType() != IntPtrTy)
514 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
515 Result = CallInst::Create(FreeFunc, PtrCast, "");
516 }
517 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000518 if (Function *F = dyn_cast<Function>(FreeFunc))
519 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000520
521 return Result;
522}
523
524/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000525Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000526 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000527}
528
529/// CreateFree - Generate the IR for a call to the builtin free function.
530/// Note: This function does not add the call to the basic block, that is the
531/// responsibility of the caller.
532Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000533 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000534 assert(FreeCall && "CreateFree did not create a CallInst");
535 return FreeCall;
536}
537
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538//===----------------------------------------------------------------------===//
539// InvokeInst Implementation
540//===----------------------------------------------------------------------===//
541
David Blaikie3e807092015-05-13 18:35:26 +0000542void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
543 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000544 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000545 const Twine &NameStr) {
546 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000547
Sanjoy Das9303c242015-09-24 19:14:18 +0000548 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
549 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000550 Op<-3>() = Fn;
551 Op<-2>() = IfNormal;
552 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000553
554#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000555 assert(((Args.size() == FTy->getNumParams()) ||
556 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000557 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000558
Jay Foad5bd375a2011-07-15 08:37:34 +0000559 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000560 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000562 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000563#endif
564
565 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000566
567 auto It = populateBundleOperandInfos(Bundles, Args.size());
568 (void)It;
569 assert(It + 3 == op_end() && "Should add up!");
570
Jay Foad5bd375a2011-07-15 08:37:34 +0000571 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572}
573
Misha Brukmanb1c93172005-04-21 23:48:37 +0000574InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000575 : TerminatorInst(II.getType(), Instruction::Invoke,
576 OperandTraits<InvokeInst>::op_end(this) -
577 II.getNumOperands(),
578 II.getNumOperands()),
579 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000580 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000581 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000582 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
583 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000584 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
Sanjoy Das2d161452015-11-18 06:23:38 +0000587InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
588 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000589 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000590
591 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
592 II->getUnwindDest(), Args, OpB,
593 II->getName(), InsertPt);
594 NewII->setCallingConv(II->getCallingConv());
595 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000596 NewII->setAttributes(II->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000597 return NewII;
598}
599
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
601 return getSuccessor(idx);
602}
603unsigned InvokeInst::getNumSuccessorsV() const {
604 return getNumSuccessors();
605}
606void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
607 return setSuccessor(idx, B);
608}
609
Michael Gottesman41748d72013-06-27 00:25:01 +0000610bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000611 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000612 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000613
614 // Operand bundles override attributes on the called function, but don't
615 // override attributes directly present on the invoke instruction.
616 if (isFnAttrDisallowedByOpBundle(A))
617 return false;
618
Bill Wendling375eb1f2012-10-09 00:28:54 +0000619 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000620 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 return false;
622}
623
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000624bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000625 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
626
Bill Wendling749a43d2012-12-30 13:50:49 +0000627 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000628 return true;
629 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000630 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000631 return false;
632}
633
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000634bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
635 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000636 // There are getNumOperands() - 3 data operands. The last three operands are
637 // the callee and the two successor basic blocks.
638 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
639
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000640 // The attribute A can either be directly specified, if the operand in
641 // question is an invoke argument; or be indirectly implied by the kind of its
642 // containing operand bundle, if the operand is a bundle operand.
643
644 if (i < (getNumArgOperands() + 1))
645 return paramHasAttr(i, A);
646
647 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
648 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000649 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000650}
651
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000652void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000653 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000654 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000655 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000656}
657
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000658void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000659 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000660 AttrBuilder B(attr);
661 PAL = PAL.removeAttributes(getContext(), i,
662 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000663 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000664}
665
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000666void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
667 AttributeSet PAL = getAttributes();
668 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
669 setAttributes(PAL);
670}
671
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000672void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
673 AttributeSet PAL = getAttributes();
674 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
675 setAttributes(PAL);
676}
677
Bill Wendlingfae14752011-08-12 20:24:12 +0000678LandingPadInst *InvokeInst::getLandingPadInst() const {
679 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
680}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000681
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682//===----------------------------------------------------------------------===//
683// ReturnInst Implementation
684//===----------------------------------------------------------------------===//
685
Chris Lattner2195fc42007-02-24 00:55:48 +0000686ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000688 OperandTraits<ReturnInst>::op_end(this) -
689 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000690 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000691 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000692 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000693 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000694}
695
Owen Anderson55f1c092009-08-13 21:58:54 +0000696ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
697 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
699 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000700 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000701 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000702}
Owen Anderson55f1c092009-08-13 21:58:54 +0000703ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
704 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000705 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
706 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000707 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000708 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000709}
Owen Anderson55f1c092009-08-13 21:58:54 +0000710ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
711 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000712 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000713}
714
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715unsigned ReturnInst::getNumSuccessorsV() const {
716 return getNumSuccessors();
717}
718
Devang Patelae682fb2008-02-26 17:56:20 +0000719/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
720/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000722 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000726 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727}
728
Devang Patel59643e52008-02-23 00:35:18 +0000729ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000730}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000733// ResumeInst Implementation
734//===----------------------------------------------------------------------===//
735
736ResumeInst::ResumeInst(const ResumeInst &RI)
737 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
738 OperandTraits<ResumeInst>::op_begin(this), 1) {
739 Op<0>() = RI.Op<0>();
740}
741
742ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
743 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
744 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
745 Op<0>() = Exn;
746}
747
748ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
749 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
750 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
751 Op<0>() = Exn;
752}
753
754unsigned ResumeInst::getNumSuccessorsV() const {
755 return getNumSuccessors();
756}
757
758void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
759 llvm_unreachable("ResumeInst has no successors!");
760}
761
762BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
763 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000764}
765
766//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000767// CleanupReturnInst Implementation
768//===----------------------------------------------------------------------===//
769
770CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
771 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
772 OperandTraits<CleanupReturnInst>::op_end(this) -
773 CRI.getNumOperands(),
774 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000775 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000776 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000777 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000778 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000779}
780
David Majnemer8a1c45d2015-12-12 05:38:55 +0000781void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000782 if (UnwindBB)
783 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000784
David Majnemer8a1c45d2015-12-12 05:38:55 +0000785 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000786 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000787 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000788}
789
David Majnemer8a1c45d2015-12-12 05:38:55 +0000790CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
791 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000792 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
793 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000794 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
795 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000796 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000797}
798
David Majnemer8a1c45d2015-12-12 05:38:55 +0000799CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
800 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000801 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
802 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000803 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
804 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000805 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000806}
807
808BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
809 assert(Idx == 0);
810 return getUnwindDest();
811}
812unsigned CleanupReturnInst::getNumSuccessorsV() const {
813 return getNumSuccessors();
814}
815void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
816 assert(Idx == 0);
817 setUnwindDest(B);
818}
819
820//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000821// CatchReturnInst Implementation
822//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000823void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000824 Op<0>() = CatchPad;
825 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000826}
David Majnemer654e1302015-07-31 17:58:14 +0000827
828CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
829 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000830 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
831 Op<0>() = CRI.Op<0>();
832 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000833}
834
David Majnemer8a1c45d2015-12-12 05:38:55 +0000835CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000836 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000837 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000838 OperandTraits<CatchReturnInst>::op_begin(this), 2,
839 InsertBefore) {
840 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000841}
842
David Majnemer8a1c45d2015-12-12 05:38:55 +0000843CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000844 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000845 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000846 OperandTraits<CatchReturnInst>::op_begin(this), 2,
847 InsertAtEnd) {
848 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000849}
850
851BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000852 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000853 return getSuccessor();
854}
855unsigned CatchReturnInst::getNumSuccessorsV() const {
856 return getNumSuccessors();
857}
858void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000859 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000860 setSuccessor(B);
861}
862
863//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000864// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000865//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000866
867CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
868 unsigned NumReservedValues,
869 const Twine &NameStr,
870 Instruction *InsertBefore)
871 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
872 InsertBefore) {
873 if (UnwindDest)
874 ++NumReservedValues;
875 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000876 setName(NameStr);
877}
878
David Majnemer8a1c45d2015-12-12 05:38:55 +0000879CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
880 unsigned NumReservedValues,
881 const Twine &NameStr, BasicBlock *InsertAtEnd)
882 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000883 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000884 if (UnwindDest)
885 ++NumReservedValues;
886 init(ParentPad, UnwindDest, NumReservedValues + 1);
887 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000888}
889
David Majnemer8a1c45d2015-12-12 05:38:55 +0000890CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
891 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
892 CSI.getNumOperands()) {
893 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
894 setNumHungOffUseOperands(ReservedSpace);
895 Use *OL = getOperandList();
896 const Use *InOL = CSI.getOperandList();
897 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
898 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000899}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000900
901void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
902 unsigned NumReservedValues) {
903 assert(ParentPad && NumReservedValues);
904
905 ReservedSpace = NumReservedValues;
906 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
907 allocHungoffUses(ReservedSpace);
908
909 Op<0>() = ParentPad;
910 if (UnwindDest) {
911 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
912 setUnwindDest(UnwindDest);
913 }
914}
915
916/// growOperands - grow operands - This grows the operand list in response to a
917/// push_back style of operation. This grows the number of ops by 2 times.
918void CatchSwitchInst::growOperands(unsigned Size) {
919 unsigned NumOperands = getNumOperands();
920 assert(NumOperands >= 1);
921 if (ReservedSpace >= NumOperands + Size)
922 return;
923 ReservedSpace = (NumOperands + Size / 2) * 2;
924 growHungoffUses(ReservedSpace);
925}
926
927void CatchSwitchInst::addHandler(BasicBlock *Handler) {
928 unsigned OpNo = getNumOperands();
929 growOperands(1);
930 assert(OpNo < ReservedSpace && "Growing didn't work!");
931 setNumHungOffUseOperands(getNumOperands() + 1);
932 getOperandList()[OpNo] = Handler;
933}
934
935BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
936 return getSuccessor(idx);
937}
938unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +0000939 return getNumSuccessors();
940}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000941void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
942 setSuccessor(idx, B);
943}
944
945//===----------------------------------------------------------------------===//
946// FuncletPadInst Implementation
947//===----------------------------------------------------------------------===//
948void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
949 const Twine &NameStr) {
950 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
951 std::copy(Args.begin(), Args.end(), op_begin());
952 setParentPad(ParentPad);
953 setName(NameStr);
954}
955
956FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
957 : Instruction(FPI.getType(), FPI.getOpcode(),
958 OperandTraits<FuncletPadInst>::op_end(this) -
959 FPI.getNumOperands(),
960 FPI.getNumOperands()) {
961 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
962 setParentPad(FPI.getParentPad());
963}
964
965FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
966 ArrayRef<Value *> Args, unsigned Values,
967 const Twine &NameStr, Instruction *InsertBefore)
968 : Instruction(ParentPad->getType(), Op,
969 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
970 InsertBefore) {
971 init(ParentPad, Args, NameStr);
972}
973
974FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
975 ArrayRef<Value *> Args, unsigned Values,
976 const Twine &NameStr, BasicBlock *InsertAtEnd)
977 : Instruction(ParentPad->getType(), Op,
978 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
979 InsertAtEnd) {
980 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000981}
982
983//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000984// UnreachableInst Implementation
985//===----------------------------------------------------------------------===//
986
Owen Anderson55f1c092009-08-13 21:58:54 +0000987UnreachableInst::UnreachableInst(LLVMContext &Context,
988 Instruction *InsertBefore)
989 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000990 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000991}
Owen Anderson55f1c092009-08-13 21:58:54 +0000992UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
993 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000994 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000995}
996
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997unsigned UnreachableInst::getNumSuccessorsV() const {
998 return getNumSuccessors();
999}
1000
1001void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001002 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003}
1004
1005BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001006 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001007}
1008
1009//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001010// BranchInst Implementation
1011//===----------------------------------------------------------------------===//
1012
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013void BranchInst::AssertOK() {
1014 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001015 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001017}
1018
Chris Lattner2195fc42007-02-24 00:55:48 +00001019BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001020 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001021 OperandTraits<BranchInst>::op_end(this) - 1,
1022 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001023 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001024 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001025}
1026BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1027 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001028 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001029 OperandTraits<BranchInst>::op_end(this) - 3,
1030 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001031 Op<-1>() = IfTrue;
1032 Op<-2>() = IfFalse;
1033 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001034#ifndef NDEBUG
1035 AssertOK();
1036#endif
1037}
1038
1039BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001040 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001041 OperandTraits<BranchInst>::op_end(this) - 1,
1042 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001043 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001044 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001045}
1046
1047BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1048 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001049 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 OperandTraits<BranchInst>::op_end(this) - 3,
1051 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001052 Op<-1>() = IfTrue;
1053 Op<-2>() = IfFalse;
1054 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001055#ifndef NDEBUG
1056 AssertOK();
1057#endif
1058}
1059
1060
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1064 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001065 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001066 if (BI.getNumOperands() != 1) {
1067 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001068 Op<-3>() = BI.Op<-3>();
1069 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001071 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001072}
1073
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001074void BranchInst::swapSuccessors() {
1075 assert(isConditional() &&
1076 "Cannot swap successors of an unconditional branch");
1077 Op<-1>().swap(Op<-2>());
1078
1079 // Update profile metadata if present and it matches our structural
1080 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001081 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001082 if (!ProfileData || ProfileData->getNumOperands() != 3)
1083 return;
1084
1085 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001086 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1087 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001088 setMetadata(LLVMContext::MD_prof,
1089 MDNode::get(ProfileData->getContext(), Ops));
1090}
1091
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001092BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1093 return getSuccessor(idx);
1094}
1095unsigned BranchInst::getNumSuccessorsV() const {
1096 return getNumSuccessors();
1097}
1098void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1099 setSuccessor(idx, B);
1100}
1101
1102
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001104// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001105//===----------------------------------------------------------------------===//
1106
Owen Andersonb6b25302009-07-14 23:09:55 +00001107static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001108 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001109 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001110 else {
1111 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001112 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001113 assert(Amt->getType()->isIntegerTy() &&
1114 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001116 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117}
1118
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001119AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1120 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001121
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001122AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1123 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001124
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001125AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001126 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001127 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001128
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001129AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001130 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001131 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001132
Chris Lattner229907c2011-07-18 04:54:35 +00001133AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001134 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001135 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1136 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1137 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001138 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001139 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001140 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001141}
1142
Chris Lattner229907c2011-07-18 04:54:35 +00001143AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001144 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001145 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1146 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1147 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001148 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001149 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001150 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001151}
1152
Gordon Henriksen14a55692007-12-10 02:14:30 +00001153// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001154AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001155}
1156
Victor Hernandez8acf2952009-10-23 21:09:37 +00001157void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001158 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001159 assert(Align <= MaximumAlignment &&
1160 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001161 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1162 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001163 assert(getAlignment() == Align && "Alignment representation error!");
1164}
1165
Victor Hernandez8acf2952009-10-23 21:09:37 +00001166bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001167 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001168 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001169 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001170}
1171
Chris Lattner8b291e62008-11-26 02:54:17 +00001172/// isStaticAlloca - Return true if this alloca is in the entry block of the
1173/// function and is a constant size. If so, the code generator will fold it
1174/// into the prolog/epilog code, so it is basically free.
1175bool AllocaInst::isStaticAlloca() const {
1176 // Must be constant size.
1177 if (!isa<ConstantInt>(getArraySize())) return false;
1178
1179 // Must be in the entry block.
1180 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001181 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001182}
1183
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001185// LoadInst Implementation
1186//===----------------------------------------------------------------------===//
1187
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001188void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001189 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001191 assert(!(isAtomic() && getAlignment() == 0) &&
1192 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193}
1194
Daniel Dunbar4975db62009-07-25 04:41:11 +00001195LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001196 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001197
Daniel Dunbar4975db62009-07-25 04:41:11 +00001198LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001199 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200
David Blaikie0c28fd72015-05-20 21:46:30 +00001201LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001203 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001204
1205LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1206 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001207 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001208
David Blaikieb7a029872015-04-17 19:56:21 +00001209LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001210 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001211 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001212 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001214LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001215 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001216 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001217}
1218
David Blaikie15d9a4c2015-04-06 20:59:48 +00001219LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001220 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001221 SynchronizationScope SynchScope, Instruction *InsertBef)
1222 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001223 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001224 setVolatile(isVolatile);
1225 setAlignment(Align);
1226 setAtomic(Order, SynchScope);
1227 AssertOK();
1228 setName(Name);
1229}
1230
1231LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1232 unsigned Align, AtomicOrdering Order,
1233 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001235 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001236 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001237 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001238 setAlignment(Align);
1239 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001240 AssertOK();
1241 setName(Name);
1242}
1243
Daniel Dunbar27096822009-08-11 18:11:15 +00001244LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1245 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1246 Load, Ptr, InsertBef) {
1247 setVolatile(false);
1248 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001249 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001250 AssertOK();
1251 if (Name && Name[0]) setName(Name);
1252}
1253
1254LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1255 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1256 Load, Ptr, InsertAE) {
1257 setVolatile(false);
1258 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001259 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001260 AssertOK();
1261 if (Name && Name[0]) setName(Name);
1262}
1263
David Blaikie0c28fd72015-05-20 21:46:30 +00001264LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001265 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001266 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1267 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001268 setVolatile(isVolatile);
1269 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001270 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001271 AssertOK();
1272 if (Name && Name[0]) setName(Name);
1273}
1274
1275LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1276 BasicBlock *InsertAE)
1277 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1278 Load, Ptr, InsertAE) {
1279 setVolatile(isVolatile);
1280 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001281 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001282 AssertOK();
1283 if (Name && Name[0]) setName(Name);
1284}
1285
Christopher Lamb84485702007-04-22 19:24:39 +00001286void LoadInst::setAlignment(unsigned Align) {
1287 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001288 assert(Align <= MaximumAlignment &&
1289 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001290 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001291 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001292 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001293}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294
1295//===----------------------------------------------------------------------===//
1296// StoreInst Implementation
1297//===----------------------------------------------------------------------===//
1298
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001299void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001300 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001301 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001302 "Ptr must have pointer type!");
1303 assert(getOperand(0)->getType() ==
1304 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001305 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001306 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001307 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001308}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001309
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001310StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001311 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001312
1313StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001314 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001315
Misha Brukmanb1c93172005-04-21 23:48:37 +00001316StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001317 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001318 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001319
1320StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001321 BasicBlock *InsertAtEnd)
1322 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1323
1324StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1325 Instruction *InsertBefore)
1326 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1327 InsertBefore) {}
1328
1329StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1330 BasicBlock *InsertAtEnd)
1331 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1332 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001333
Misha Brukmanb1c93172005-04-21 23:48:37 +00001334StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001335 unsigned Align, AtomicOrdering Order,
1336 SynchronizationScope SynchScope,
1337 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001338 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001339 OperandTraits<StoreInst>::op_begin(this),
1340 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001341 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001342 Op<0>() = val;
1343 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001344 setVolatile(isVolatile);
1345 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001346 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001347 AssertOK();
1348}
1349
1350StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001351 unsigned Align, AtomicOrdering Order,
1352 SynchronizationScope SynchScope,
1353 BasicBlock *InsertAtEnd)
1354 : Instruction(Type::getVoidTy(val->getContext()), Store,
1355 OperandTraits<StoreInst>::op_begin(this),
1356 OperandTraits<StoreInst>::operands(this),
1357 InsertAtEnd) {
1358 Op<0>() = val;
1359 Op<1>() = addr;
1360 setVolatile(isVolatile);
1361 setAlignment(Align);
1362 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001363 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001364}
1365
Christopher Lamb84485702007-04-22 19:24:39 +00001366void StoreInst::setAlignment(unsigned Align) {
1367 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001368 assert(Align <= MaximumAlignment &&
1369 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001370 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001371 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001372 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001373}
1374
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001375//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001376// AtomicCmpXchgInst Implementation
1377//===----------------------------------------------------------------------===//
1378
1379void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001380 AtomicOrdering SuccessOrdering,
1381 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001382 SynchronizationScope SynchScope) {
1383 Op<0>() = Ptr;
1384 Op<1>() = Cmp;
1385 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001386 setSuccessOrdering(SuccessOrdering);
1387 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001388 setSynchScope(SynchScope);
1389
1390 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1391 "All operands must be non-null!");
1392 assert(getOperand(0)->getType()->isPointerTy() &&
1393 "Ptr must have pointer type!");
1394 assert(getOperand(1)->getType() ==
1395 cast<PointerType>(getOperand(0)->getType())->getElementType()
1396 && "Ptr must be a pointer to Cmp type!");
1397 assert(getOperand(2)->getType() ==
1398 cast<PointerType>(getOperand(0)->getType())->getElementType()
1399 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001400 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001401 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001402 assert(FailureOrdering != NotAtomic &&
1403 "AtomicCmpXchg instructions must be atomic!");
1404 assert(SuccessOrdering >= FailureOrdering &&
1405 "AtomicCmpXchg success ordering must be at least as strong as fail");
1406 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1407 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001408}
1409
1410AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001411 AtomicOrdering SuccessOrdering,
1412 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001413 SynchronizationScope SynchScope,
1414 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001415 : Instruction(
1416 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1417 nullptr),
1418 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1419 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001420 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001421}
1422
1423AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001424 AtomicOrdering SuccessOrdering,
1425 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001426 SynchronizationScope SynchScope,
1427 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001428 : Instruction(
1429 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1430 nullptr),
1431 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1432 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001433 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001434}
Tim Northover420a2162014-06-13 14:24:07 +00001435
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001436//===----------------------------------------------------------------------===//
1437// AtomicRMWInst Implementation
1438//===----------------------------------------------------------------------===//
1439
1440void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1441 AtomicOrdering Ordering,
1442 SynchronizationScope SynchScope) {
1443 Op<0>() = Ptr;
1444 Op<1>() = Val;
1445 setOperation(Operation);
1446 setOrdering(Ordering);
1447 setSynchScope(SynchScope);
1448
1449 assert(getOperand(0) && getOperand(1) &&
1450 "All operands must be non-null!");
1451 assert(getOperand(0)->getType()->isPointerTy() &&
1452 "Ptr must have pointer type!");
1453 assert(getOperand(1)->getType() ==
1454 cast<PointerType>(getOperand(0)->getType())->getElementType()
1455 && "Ptr must be a pointer to Val type!");
1456 assert(Ordering != NotAtomic &&
1457 "AtomicRMW instructions must be atomic!");
1458}
1459
1460AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1461 AtomicOrdering Ordering,
1462 SynchronizationScope SynchScope,
1463 Instruction *InsertBefore)
1464 : Instruction(Val->getType(), AtomicRMW,
1465 OperandTraits<AtomicRMWInst>::op_begin(this),
1466 OperandTraits<AtomicRMWInst>::operands(this),
1467 InsertBefore) {
1468 Init(Operation, Ptr, Val, Ordering, SynchScope);
1469}
1470
1471AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1472 AtomicOrdering Ordering,
1473 SynchronizationScope SynchScope,
1474 BasicBlock *InsertAtEnd)
1475 : Instruction(Val->getType(), AtomicRMW,
1476 OperandTraits<AtomicRMWInst>::op_begin(this),
1477 OperandTraits<AtomicRMWInst>::operands(this),
1478 InsertAtEnd) {
1479 Init(Operation, Ptr, Val, Ordering, SynchScope);
1480}
1481
1482//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001483// FenceInst Implementation
1484//===----------------------------------------------------------------------===//
1485
1486FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1487 SynchronizationScope SynchScope,
1488 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001489 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001490 setOrdering(Ordering);
1491 setSynchScope(SynchScope);
1492}
1493
1494FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1495 SynchronizationScope SynchScope,
1496 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001497 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001498 setOrdering(Ordering);
1499 setSynchScope(SynchScope);
1500}
1501
1502//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001503// GetElementPtrInst Implementation
1504//===----------------------------------------------------------------------===//
1505
Jay Foadd1b78492011-07-25 09:48:08 +00001506void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001507 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001508 assert(getNumOperands() == 1 + IdxList.size() &&
1509 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001510 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001511 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001512 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001513}
1514
Gabor Greiff6caff662008-05-10 08:32:32 +00001515GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001516 : Instruction(GEPI.getType(), GetElementPtr,
1517 OperandTraits<GetElementPtrInst>::op_end(this) -
1518 GEPI.getNumOperands(),
1519 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001520 SourceElementType(GEPI.SourceElementType),
1521 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001522 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001523 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001524}
1525
Chris Lattner6090a422009-03-09 04:46:40 +00001526/// getIndexedType - Returns the type of the element that would be accessed with
1527/// a gep instruction with the specified parameters.
1528///
1529/// The Idxs pointer should point to a continuous piece of memory containing the
1530/// indices, either as Value* or uint64_t.
1531///
1532/// A null type is returned if the indices are invalid for the specified
1533/// pointer type.
1534///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001535template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001536static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001537 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001538 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001539 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001540
Chris Lattner6090a422009-03-09 04:46:40 +00001541 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001542 // it cannot be 'stepped over'.
1543 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001544 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001545
Dan Gohman1ecaf452008-05-31 00:58:22 +00001546 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001547 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001548 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001549 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001550 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001551 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001552 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001553 }
Craig Topperc6207612014-04-09 06:08:46 +00001554 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555}
1556
David Blaikied288fb82015-03-30 21:41:43 +00001557Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1558 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001559}
1560
David Blaikied288fb82015-03-30 21:41:43 +00001561Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001562 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001563 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001564}
1565
David Blaikied288fb82015-03-30 21:41:43 +00001566Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1567 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001568}
1569
Chris Lattner45f15572007-04-14 00:12:57 +00001570/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1571/// zeros. If so, the result pointer and the first operand have the same
1572/// value, just potentially different types.
1573bool GetElementPtrInst::hasAllZeroIndices() const {
1574 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1575 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1576 if (!CI->isZero()) return false;
1577 } else {
1578 return false;
1579 }
1580 }
1581 return true;
1582}
1583
Chris Lattner27058292007-04-27 20:35:56 +00001584/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1585/// constant integers. If so, the result pointer and the first operand have
1586/// a constant offset between them.
1587bool GetElementPtrInst::hasAllConstantIndices() const {
1588 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1589 if (!isa<ConstantInt>(getOperand(i)))
1590 return false;
1591 }
1592 return true;
1593}
1594
Dan Gohman1b849082009-09-07 23:54:19 +00001595void GetElementPtrInst::setIsInBounds(bool B) {
1596 cast<GEPOperator>(this)->setIsInBounds(B);
1597}
Chris Lattner45f15572007-04-14 00:12:57 +00001598
Nick Lewycky28a5f252009-09-27 21:33:04 +00001599bool GetElementPtrInst::isInBounds() const {
1600 return cast<GEPOperator>(this)->isInBounds();
1601}
1602
Chandler Carruth1e140532012-12-11 10:29:10 +00001603bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1604 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001605 // Delegate to the generic GEPOperator implementation.
1606 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001607}
1608
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001610// ExtractElementInst Implementation
1611//===----------------------------------------------------------------------===//
1612
1613ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001614 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001615 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001616 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001617 ExtractElement,
1618 OperandTraits<ExtractElementInst>::op_begin(this),
1619 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001620 assert(isValidOperands(Val, Index) &&
1621 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001622 Op<0>() = Val;
1623 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001624 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001625}
1626
1627ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001628 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001629 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001630 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001631 ExtractElement,
1632 OperandTraits<ExtractElementInst>::op_begin(this),
1633 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001634 assert(isValidOperands(Val, Index) &&
1635 "Invalid extractelement instruction operands!");
1636
Gabor Greif2d3024d2008-05-26 21:33:52 +00001637 Op<0>() = Val;
1638 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001639 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001640}
1641
Chris Lattner65511ff2006-10-05 06:24:58 +00001642
Chris Lattner54865b32006-04-08 04:05:48 +00001643bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001644 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001645 return false;
1646 return true;
1647}
1648
1649
Robert Bocchino23004482006-01-10 19:05:34 +00001650//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001651// InsertElementInst Implementation
1652//===----------------------------------------------------------------------===//
1653
Chris Lattner54865b32006-04-08 04:05:48 +00001654InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001655 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001656 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001657 : Instruction(Vec->getType(), InsertElement,
1658 OperandTraits<InsertElementInst>::op_begin(this),
1659 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001660 assert(isValidOperands(Vec, Elt, Index) &&
1661 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001662 Op<0>() = Vec;
1663 Op<1>() = Elt;
1664 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001665 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001666}
1667
Chris Lattner54865b32006-04-08 04:05:48 +00001668InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001669 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001670 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001671 : Instruction(Vec->getType(), InsertElement,
1672 OperandTraits<InsertElementInst>::op_begin(this),
1673 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001674 assert(isValidOperands(Vec, Elt, Index) &&
1675 "Invalid insertelement instruction operands!");
1676
Gabor Greif2d3024d2008-05-26 21:33:52 +00001677 Op<0>() = Vec;
1678 Op<1>() = Elt;
1679 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001680 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001681}
1682
Chris Lattner54865b32006-04-08 04:05:48 +00001683bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1684 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001685 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001686 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001687
Reid Spencerd84d35b2007-02-15 02:26:10 +00001688 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001689 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001690
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001691 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001692 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001693 return true;
1694}
1695
1696
Robert Bocchinoca27f032006-01-17 20:07:22 +00001697//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001698// ShuffleVectorInst Implementation
1699//===----------------------------------------------------------------------===//
1700
1701ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001702 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001703 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001704: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001705 cast<VectorType>(Mask->getType())->getNumElements()),
1706 ShuffleVector,
1707 OperandTraits<ShuffleVectorInst>::op_begin(this),
1708 OperandTraits<ShuffleVectorInst>::operands(this),
1709 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001710 assert(isValidOperands(V1, V2, Mask) &&
1711 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001712 Op<0>() = V1;
1713 Op<1>() = V2;
1714 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001715 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001716}
1717
1718ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001719 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001720 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001721: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1722 cast<VectorType>(Mask->getType())->getNumElements()),
1723 ShuffleVector,
1724 OperandTraits<ShuffleVectorInst>::op_begin(this),
1725 OperandTraits<ShuffleVectorInst>::operands(this),
1726 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001727 assert(isValidOperands(V1, V2, Mask) &&
1728 "Invalid shuffle vector instruction operands!");
1729
Gabor Greif2d3024d2008-05-26 21:33:52 +00001730 Op<0>() = V1;
1731 Op<1>() = V2;
1732 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001733 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001734}
1735
Mon P Wang25f01062008-11-10 04:46:22 +00001736bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001737 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001738 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001739 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001740 return false;
1741
Chris Lattner1dcb6542012-01-25 23:49:49 +00001742 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001743 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001744 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001745 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001746
1747 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001748 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1749 return true;
1750
Nate Begeman60a31c32010-08-13 00:16:46 +00001751 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001752 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001753 for (Value *Op : MV->operands()) {
1754 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001755 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001756 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001757 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001758 return false;
1759 }
1760 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001761 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001762 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001763
1764 if (const ConstantDataSequential *CDS =
1765 dyn_cast<ConstantDataSequential>(Mask)) {
1766 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1767 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1768 if (CDS->getElementAsInteger(i) >= V1Size*2)
1769 return false;
1770 return true;
1771 }
1772
1773 // The bitcode reader can create a place holder for a forward reference
1774 // used as the shuffle mask. When this occurs, the shuffle mask will
1775 // fall into this case and fail. To avoid this error, do this bit of
1776 // ugliness to allow such a mask pass.
1777 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1778 if (CE->getOpcode() == Instruction::UserOp1)
1779 return true;
1780
1781 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001782}
1783
Chris Lattnerf724e342008-03-02 05:28:33 +00001784/// getMaskValue - Return the index from the shuffle mask for the specified
1785/// output result. This is either -1 if the element is undef or a number less
1786/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001787int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1788 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1789 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001790 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001791 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001792 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001793 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001794 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001795}
1796
Chris Lattner1dcb6542012-01-25 23:49:49 +00001797/// getShuffleMask - Return the full mask for this instruction, where each
1798/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001799void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1800 SmallVectorImpl<int> &Result) {
1801 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001802
Chris Lattnercf129702012-01-26 02:51:13 +00001803 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001804 for (unsigned i = 0; i != NumElts; ++i)
1805 Result.push_back(CDS->getElementAsInteger(i));
1806 return;
1807 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001808 for (unsigned i = 0; i != NumElts; ++i) {
1809 Constant *C = Mask->getAggregateElement(i);
1810 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001811 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001812 }
1813}
1814
1815
Dan Gohman12fce772008-05-15 19:50:34 +00001816//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001817// InsertValueInst Class
1818//===----------------------------------------------------------------------===//
1819
Jay Foad57aa6362011-07-13 10:26:04 +00001820void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1821 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001822 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001823
1824 // There's no fundamental reason why we require at least one index
1825 // (other than weirdness with &*IdxBegin being invalid; see
1826 // getelementptr's init routine for example). But there's no
1827 // present need to support it.
1828 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1829
1830 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001831 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001832 Op<0>() = Agg;
1833 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001834
Jay Foad57aa6362011-07-13 10:26:04 +00001835 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001836 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001837}
1838
1839InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001840 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001841 OperandTraits<InsertValueInst>::op_begin(this), 2),
1842 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001843 Op<0>() = IVI.getOperand(0);
1844 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001845 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001846}
1847
1848//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001849// ExtractValueInst Class
1850//===----------------------------------------------------------------------===//
1851
Jay Foad57aa6362011-07-13 10:26:04 +00001852void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001853 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001854
Jay Foad57aa6362011-07-13 10:26:04 +00001855 // There's no fundamental reason why we require at least one index.
1856 // But there's no present need to support it.
1857 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001858
Jay Foad57aa6362011-07-13 10:26:04 +00001859 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001860 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001861}
1862
1863ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001864 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001865 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001866 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001867}
1868
Dan Gohman12fce772008-05-15 19:50:34 +00001869// getIndexedType - Returns the type of the element that would be extracted
1870// with an extractvalue instruction with the specified parameters.
1871//
1872// A null type is returned if the indices are invalid for the specified
1873// pointer type.
1874//
Chris Lattner229907c2011-07-18 04:54:35 +00001875Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001876 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001877 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001878 // We can't use CompositeType::indexValid(Index) here.
1879 // indexValid() always returns true for arrays because getelementptr allows
1880 // out-of-bounds indices. Since we don't allow those for extractvalue and
1881 // insertvalue we need to check array indexing manually.
1882 // Since the only other types we can index into are struct types it's just
1883 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001884 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001885 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001886 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001887 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001888 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001889 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001890 } else {
1891 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001892 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001893 }
1894
1895 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001896 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001897 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001898}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001899
1900//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001901// BinaryOperator Class
1902//===----------------------------------------------------------------------===//
1903
Chris Lattner2195fc42007-02-24 00:55:48 +00001904BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001905 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001906 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001907 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001908 OperandTraits<BinaryOperator>::op_begin(this),
1909 OperandTraits<BinaryOperator>::operands(this),
1910 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001911 Op<0>() = S1;
1912 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001913 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001914 setName(Name);
1915}
1916
1917BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001918 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001919 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001920 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001921 OperandTraits<BinaryOperator>::op_begin(this),
1922 OperandTraits<BinaryOperator>::operands(this),
1923 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001924 Op<0>() = S1;
1925 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001926 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001927 setName(Name);
1928}
1929
1930
1931void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001932 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001933 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001934 assert(LHS->getType() == RHS->getType() &&
1935 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001936#ifndef NDEBUG
1937 switch (iType) {
1938 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001939 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001940 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001941 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001942 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001943 "Tried to create an integer operation on a non-integer type!");
1944 break;
1945 case FAdd: case FSub:
1946 case FMul:
1947 assert(getType() == LHS->getType() &&
1948 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001949 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001950 "Tried to create a floating-point operation on a "
1951 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001952 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001953 case UDiv:
1954 case SDiv:
1955 assert(getType() == LHS->getType() &&
1956 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001957 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001958 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001959 "Incorrect operand type (not integer) for S/UDIV");
1960 break;
1961 case FDiv:
1962 assert(getType() == LHS->getType() &&
1963 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001964 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001965 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001966 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001967 case URem:
1968 case SRem:
1969 assert(getType() == LHS->getType() &&
1970 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001971 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001972 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001973 "Incorrect operand type (not integer) for S/UREM");
1974 break;
1975 case FRem:
1976 assert(getType() == LHS->getType() &&
1977 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001978 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001979 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001980 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001981 case Shl:
1982 case LShr:
1983 case AShr:
1984 assert(getType() == LHS->getType() &&
1985 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001986 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001987 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001988 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001989 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001990 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001991 case And: case Or:
1992 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001993 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001994 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001995 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001996 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001997 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001998 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001999 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002000 default:
2001 break;
2002 }
2003#endif
2004}
2005
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002007 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002008 Instruction *InsertBefore) {
2009 assert(S1->getType() == S2->getType() &&
2010 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002011 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002012}
2013
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002015 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002016 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002017 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002018 InsertAtEnd->getInstList().push_back(Res);
2019 return Res;
2020}
2021
Dan Gohman5476cfd2009-08-12 16:23:25 +00002022BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002023 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002024 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002025 return new BinaryOperator(Instruction::Sub,
2026 zero, Op,
2027 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002028}
2029
Dan Gohman5476cfd2009-08-12 16:23:25 +00002030BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002031 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002032 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002033 return new BinaryOperator(Instruction::Sub,
2034 zero, Op,
2035 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036}
2037
Dan Gohman4ab44202009-12-18 02:58:50 +00002038BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2039 Instruction *InsertBefore) {
2040 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2041 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2042}
2043
2044BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2045 BasicBlock *InsertAtEnd) {
2046 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2047 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2048}
2049
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002050BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2051 Instruction *InsertBefore) {
2052 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2053 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2054}
2055
2056BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2057 BasicBlock *InsertAtEnd) {
2058 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2059 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2060}
2061
Dan Gohman5476cfd2009-08-12 16:23:25 +00002062BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002063 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002064 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002065 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002066 Op->getType(), Name, InsertBefore);
2067}
2068
Dan Gohman5476cfd2009-08-12 16:23:25 +00002069BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002070 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002071 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002072 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002073 Op->getType(), Name, InsertAtEnd);
2074}
2075
Dan Gohman5476cfd2009-08-12 16:23:25 +00002076BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002077 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002078 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002079 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002080 Op->getType(), Name, InsertBefore);
2081}
2082
Dan Gohman5476cfd2009-08-12 16:23:25 +00002083BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002085 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002086 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002087 Op->getType(), Name, InsertAtEnd);
2088}
2089
2090
2091// isConstantAllOnes - Helper function for several functions below
2092static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002093 if (const Constant *C = dyn_cast<Constant>(V))
2094 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002095 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002096}
2097
Owen Andersonbb2501b2009-07-13 22:18:28 +00002098bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002099 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2100 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002101 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2102 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002103 return false;
2104}
2105
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002106bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002107 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2108 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002109 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2110 if (!IgnoreZeroSign)
2111 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2112 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2113 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002114 return false;
2115}
2116
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002117bool BinaryOperator::isNot(const Value *V) {
2118 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2119 return (Bop->getOpcode() == Instruction::Xor &&
2120 (isConstantAllOnes(Bop->getOperand(1)) ||
2121 isConstantAllOnes(Bop->getOperand(0))));
2122 return false;
2123}
2124
Chris Lattner2c7d1772005-04-24 07:28:37 +00002125Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002126 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002127}
2128
Chris Lattner2c7d1772005-04-24 07:28:37 +00002129const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2130 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002131}
2132
Dan Gohmana5b96452009-06-04 22:49:04 +00002133Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002134 return cast<BinaryOperator>(BinOp)->getOperand(1);
2135}
2136
2137const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2138 return getFNegArgument(const_cast<Value*>(BinOp));
2139}
2140
Chris Lattner2c7d1772005-04-24 07:28:37 +00002141Value *BinaryOperator::getNotArgument(Value *BinOp) {
2142 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2143 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2144 Value *Op0 = BO->getOperand(0);
2145 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002146 if (isConstantAllOnes(Op0)) return Op1;
2147
2148 assert(isConstantAllOnes(Op1));
2149 return Op0;
2150}
2151
Chris Lattner2c7d1772005-04-24 07:28:37 +00002152const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2153 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002154}
2155
2156
2157// swapOperands - Exchange the two operands to this instruction. This
2158// instruction is safe to use on any binary instruction and does not
2159// modify the semantics of the instruction. If the instruction is
2160// order dependent (SetLT f.e.) the opcode is changed.
2161//
2162bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002163 if (!isCommutative())
2164 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002165 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002166 return false;
2167}
2168
Dan Gohman1b849082009-09-07 23:54:19 +00002169void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2170 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2171}
2172
2173void BinaryOperator::setHasNoSignedWrap(bool b) {
2174 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2175}
2176
2177void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002178 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002179}
2180
Nick Lewycky28a5f252009-09-27 21:33:04 +00002181bool BinaryOperator::hasNoUnsignedWrap() const {
2182 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2183}
2184
2185bool BinaryOperator::hasNoSignedWrap() const {
2186 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2187}
2188
2189bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002190 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002191}
2192
Sanjay Patela982d992014-09-03 01:06:50 +00002193void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002194 // Copy the wrapping flags.
2195 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2196 setHasNoSignedWrap(OB->hasNoSignedWrap());
2197 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2198 }
2199
2200 // Copy the exact flag.
2201 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2202 setIsExact(PE->isExact());
2203
2204 // Copy the fast-math flags.
2205 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002206 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002207}
2208
Sanjay Patela982d992014-09-03 01:06:50 +00002209void BinaryOperator::andIRFlags(const Value *V) {
2210 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2211 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2212 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2213 }
2214
2215 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2216 setIsExact(isExact() & PE->isExact());
2217
2218 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2219 FastMathFlags FM = getFastMathFlags();
2220 FM &= FP->getFastMathFlags();
2221 copyFastMathFlags(FM);
2222 }
2223}
2224
2225
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002226//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002227// FPMathOperator Class
2228//===----------------------------------------------------------------------===//
2229
2230/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2231/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002232/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002233float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002234 const MDNode *MD =
2235 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002236 if (!MD)
2237 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002238 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002239 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002240}
2241
2242
2243//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002244// CastInst Class
2245//===----------------------------------------------------------------------===//
2246
David Blaikie3a15e142011-12-01 08:00:17 +00002247void CastInst::anchor() {}
2248
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249// Just determine if this cast only deals with integral->integral conversion.
2250bool CastInst::isIntegerCast() const {
2251 switch (getOpcode()) {
2252 default: return false;
2253 case Instruction::ZExt:
2254 case Instruction::SExt:
2255 case Instruction::Trunc:
2256 return true;
2257 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002258 return getOperand(0)->getType()->isIntegerTy() &&
2259 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002261}
2262
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263bool CastInst::isLosslessCast() const {
2264 // Only BitCast can be lossless, exit fast if we're not BitCast
2265 if (getOpcode() != Instruction::BitCast)
2266 return false;
2267
2268 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002269 Type* SrcTy = getOperand(0)->getType();
2270 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 if (SrcTy == DstTy)
2272 return true;
2273
Reid Spencer8d9336d2006-12-31 05:26:44 +00002274 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002275 if (SrcTy->isPointerTy())
2276 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277 return false; // Other types have no identity values
2278}
2279
2280/// This function determines if the CastInst does not require any bits to be
2281/// changed in order to effect the cast. Essentially, it identifies cases where
2282/// no code gen is necessary for the cast, hence the name no-op cast. For
2283/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002284/// # bitcast i32* %x to i8*
2285/// # bitcast <2 x i32> %x to <4 x i16>
2286/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002287/// @brief Determine if the described cast is a no-op.
2288bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002289 Type *SrcTy,
2290 Type *DestTy,
2291 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002292 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002293 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 case Instruction::Trunc:
2295 case Instruction::ZExt:
2296 case Instruction::SExt:
2297 case Instruction::FPTrunc:
2298 case Instruction::FPExt:
2299 case Instruction::UIToFP:
2300 case Instruction::SIToFP:
2301 case Instruction::FPToUI:
2302 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002303 case Instruction::AddrSpaceCast:
2304 // TODO: Target informations may give a more accurate answer here.
2305 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 case Instruction::BitCast:
2307 return true; // BitCast never modifies bits.
2308 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002309 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002310 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002312 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002313 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 }
2315}
2316
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002317/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002318bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002319 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2320}
2321
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002322bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002323 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002324 if (getOpcode() == Instruction::PtrToInt)
2325 PtrOpTy = getOperand(0)->getType();
2326 else if (getOpcode() == Instruction::IntToPtr)
2327 PtrOpTy = getType();
2328
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002329 Type *IntPtrTy =
2330 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002331
2332 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2333}
2334
2335/// This function determines if a pair of casts can be eliminated and what
2336/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337/// instructions like this:
2338/// * %F = firstOpcode SrcTy %x to MidTy
2339/// * %S = secondOpcode MidTy %F to DstTy
2340/// The function returns a resultOpcode so these two casts can be replaced with:
2341/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002342/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343unsigned CastInst::isEliminableCastPair(
2344 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002345 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2346 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347 // Define the 144 possibilities for these two cast instructions. The values
2348 // in this matrix determine what to do in a given situation and select the
2349 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002350 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 // the following cast properties:
2352 //
2353 // Size Compare Source Destination
2354 // Operator Src ? Size Type Sign Type Sign
2355 // -------- ------------ ------------------- ---------------------
2356 // TRUNC > Integer Any Integral Any
2357 // ZEXT < Integral Unsigned Integer Any
2358 // SEXT < Integral Signed Integer Any
2359 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002360 // FPTOSI n/a FloatPt n/a Integral Signed
2361 // UITOFP n/a Integral Unsigned FloatPt n/a
2362 // SITOFP n/a Integral Signed FloatPt n/a
2363 // FPTRUNC > FloatPt n/a FloatPt n/a
2364 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 // PTRTOINT n/a Pointer n/a Integral Unsigned
2366 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002367 // BITCAST = FirstClass n/a FirstClass n/a
2368 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002369 //
2370 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002371 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2372 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002373 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002374 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002375 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002376 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002377 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2379 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380 // T F F U S F F P I B A -+
2381 // R Z S P P I I T P 2 N T S |
2382 // U E E 2 2 2 2 R E I T C C +- secondOp
2383 // N X X U S F F N X N 2 V V |
2384 // C T T I I P P C T T P T T -+
2385 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002386 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002387 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2388 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2389 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2390 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2391 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002392 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002393 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2394 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2395 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2396 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2397 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002400 // TODO: This logic could be encoded into the table above and handled in the
2401 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002402 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002403 // merging. However, any pair of bitcasts are allowed.
2404 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2405 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2406 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002407
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002408 // Check if any of the casts convert scalars <-> vectors.
2409 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2410 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2411 if (!AreBothBitcasts)
2412 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413
2414 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2415 [secondOp-Instruction::CastOpsBegin];
2416 switch (ElimCase) {
2417 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002418 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 return 0;
2420 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002421 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return firstOp;
2423 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002424 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 return secondOp;
2426 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002427 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002428 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002429 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002430 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431 return firstOp;
2432 return 0;
2433 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002434 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002435 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002436 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 return firstOp;
2438 return 0;
2439 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002440 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002441 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002442 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 return secondOp;
2444 return 0;
2445 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002446 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002447 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002448 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 return secondOp;
2450 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002451 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002452 // Cannot simplify if address spaces are different!
2453 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2454 return 0;
2455
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002456 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002457 // We can still fold this without knowing the actual sizes as long we
2458 // know that the intermediate pointer is the largest possible
2459 // pointer size.
2460 // FIXME: Is this always true?
2461 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002462 return Instruction::BitCast;
2463
2464 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002465 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002466 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002467 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 if (MidSize >= PtrSize)
2469 return Instruction::BitCast;
2470 return 0;
2471 }
2472 case 8: {
2473 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2474 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2475 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002476 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2477 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 if (SrcSize == DstSize)
2479 return Instruction::BitCast;
2480 else if (SrcSize < DstSize)
2481 return firstOp;
2482 return secondOp;
2483 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002484 case 9:
2485 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 return Instruction::ZExt;
2487 case 10:
2488 // fpext followed by ftrunc is allowed if the bit size returned to is
2489 // the same as the original, in which case its just a bitcast
2490 if (SrcTy == DstTy)
2491 return Instruction::BitCast;
2492 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002493 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002495 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002496 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002497 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002498 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2499 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500 if (SrcSize <= PtrSize && SrcSize == DstSize)
2501 return Instruction::BitCast;
2502 return 0;
2503 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002504 case 12: {
2505 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2506 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2507 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2508 return Instruction::AddrSpaceCast;
2509 return Instruction::BitCast;
2510 }
2511 case 13:
2512 // FIXME: this state can be merged with (1), but the following assert
2513 // is useful to check the correcteness of the sequence due to semantic
2514 // change of bitcast.
2515 assert(
2516 SrcTy->isPtrOrPtrVectorTy() &&
2517 MidTy->isPtrOrPtrVectorTy() &&
2518 DstTy->isPtrOrPtrVectorTy() &&
2519 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2520 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2521 "Illegal addrspacecast, bitcast sequence!");
2522 // Allowed, use first cast's opcode
2523 return firstOp;
2524 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002525 // bitcast, addrspacecast -> addrspacecast if the element type of
2526 // bitcast's source is the same as that of addrspacecast's destination.
2527 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2528 return Instruction::AddrSpaceCast;
2529 return 0;
2530
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002531 case 15:
2532 // FIXME: this state can be merged with (1), but the following assert
2533 // is useful to check the correcteness of the sequence due to semantic
2534 // change of bitcast.
2535 assert(
2536 SrcTy->isIntOrIntVectorTy() &&
2537 MidTy->isPtrOrPtrVectorTy() &&
2538 DstTy->isPtrOrPtrVectorTy() &&
2539 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2540 "Illegal inttoptr, bitcast sequence!");
2541 // Allowed, use first cast's opcode
2542 return firstOp;
2543 case 16:
2544 // FIXME: this state can be merged with (2), but the following assert
2545 // is useful to check the correcteness of the sequence due to semantic
2546 // change of bitcast.
2547 assert(
2548 SrcTy->isPtrOrPtrVectorTy() &&
2549 MidTy->isPtrOrPtrVectorTy() &&
2550 DstTy->isIntOrIntVectorTy() &&
2551 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2552 "Illegal bitcast, ptrtoint sequence!");
2553 // Allowed, use second cast's opcode
2554 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002555 case 17:
2556 // (sitofp (zext x)) -> (uitofp x)
2557 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002559 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002561 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 default:
Craig Topperc514b542012-02-05 22:14:15 +00002563 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566
Chris Lattner229907c2011-07-18 04:54:35 +00002567CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002568 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002569 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570 // Construct and return the appropriate CastInst subclass
2571 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002572 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2573 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2574 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2575 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2576 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2577 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2578 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2579 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2580 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2581 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2582 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2583 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2584 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2585 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
Chris Lattner229907c2011-07-18 04:54:35 +00002589CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002590 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002591 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592 // Construct and return the appropriate CastInst subclass
2593 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002594 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2595 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2596 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2597 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2598 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2599 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2600 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2601 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2602 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2603 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2604 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2605 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2606 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2607 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609}
2610
Chris Lattner229907c2011-07-18 04:54:35 +00002611CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002613 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002614 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002615 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2616 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002617}
2618
Chris Lattner229907c2011-07-18 04:54:35 +00002619CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002620 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002621 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002622 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002623 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2624 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002625}
2626
Chris Lattner229907c2011-07-18 04:54:35 +00002627CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002628 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002629 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002630 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002631 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2632 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002633}
2634
Chris Lattner229907c2011-07-18 04:54:35 +00002635CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002636 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002637 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002638 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002639 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2640 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002641}
2642
Chris Lattner229907c2011-07-18 04:54:35 +00002643CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002644 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002645 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002646 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002647 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2648 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002649}
2650
Chris Lattner229907c2011-07-18 04:54:35 +00002651CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002652 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002653 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002654 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002655 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2656 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002657}
2658
Chris Lattner229907c2011-07-18 04:54:35 +00002659CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002660 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002661 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002662 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2663 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2664 "Invalid cast");
2665 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002666 assert((!Ty->isVectorTy() ||
2667 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002668 "Invalid cast");
2669
Matt Arsenault065ced92013-07-31 00:17:33 +00002670 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002671 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002672
Matt Arsenault740980e2014-07-14 17:24:35 +00002673 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002674}
2675
2676/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002677CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2678 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002679 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002680 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2681 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002682 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002683 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002684 assert((!Ty->isVectorTy() ||
2685 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002686 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002687
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002688 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002689 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002690
Matt Arsenault740980e2014-07-14 17:24:35 +00002691 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2692}
2693
2694CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2695 Value *S, Type *Ty,
2696 const Twine &Name,
2697 BasicBlock *InsertAtEnd) {
2698 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2699 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2700
2701 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2702 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2703
2704 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2705}
2706
2707CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2708 Value *S, Type *Ty,
2709 const Twine &Name,
2710 Instruction *InsertBefore) {
2711 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2712 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2713
2714 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002715 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2716
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002717 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002718}
2719
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002720CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2721 const Twine &Name,
2722 Instruction *InsertBefore) {
2723 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2724 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2725 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2726 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2727
2728 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2729}
2730
Matt Arsenault740980e2014-07-14 17:24:35 +00002731CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002732 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002733 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002734 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002735 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002736 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2737 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002738 Instruction::CastOps opcode =
2739 (SrcBits == DstBits ? Instruction::BitCast :
2740 (SrcBits > DstBits ? Instruction::Trunc :
2741 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002742 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002743}
2744
Chris Lattner229907c2011-07-18 04:54:35 +00002745CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002746 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002747 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002748 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002749 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002750 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2751 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002752 Instruction::CastOps opcode =
2753 (SrcBits == DstBits ? Instruction::BitCast :
2754 (SrcBits > DstBits ? Instruction::Trunc :
2755 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002756 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002757}
2758
Chris Lattner229907c2011-07-18 04:54:35 +00002759CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002760 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002761 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002762 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002763 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002764 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2765 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002766 Instruction::CastOps opcode =
2767 (SrcBits == DstBits ? Instruction::BitCast :
2768 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002769 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002770}
2771
Chris Lattner229907c2011-07-18 04:54:35 +00002772CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002773 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002774 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002775 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002776 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002777 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2778 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002779 Instruction::CastOps opcode =
2780 (SrcBits == DstBits ? Instruction::BitCast :
2781 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002782 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002783}
2784
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002785// Check whether it is valid to call getCastOpcode for these types.
2786// This routine must be kept in sync with getCastOpcode.
2787bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2788 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2789 return false;
2790
2791 if (SrcTy == DestTy)
2792 return true;
2793
2794 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2795 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2796 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2797 // An element by element cast. Valid if casting the elements is valid.
2798 SrcTy = SrcVecTy->getElementType();
2799 DestTy = DestVecTy->getElementType();
2800 }
2801
2802 // Get the bit sizes, we'll need these
2803 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2804 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2805
2806 // Run through the possibilities ...
2807 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002808 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002809 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002810 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002811 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002812 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002813 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002814 // Casting from something else
2815 return SrcTy->isPointerTy();
2816 }
2817 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2818 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002819 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002820 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002821 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002822 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002823 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002824 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002825 return false;
2826 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002827 if (DestTy->isVectorTy()) // Casting to vector
2828 return DestBits == SrcBits;
2829 if (DestTy->isPointerTy()) { // Casting to pointer
2830 if (SrcTy->isPointerTy()) // Casting from pointer
2831 return true;
2832 return SrcTy->isIntegerTy(); // Casting from integral
2833 }
2834 if (DestTy->isX86_MMXTy()) {
2835 if (SrcTy->isVectorTy())
2836 return DestBits == SrcBits; // 64-bit vector to MMX
2837 return false;
2838 } // Casting to something else
2839 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002840}
2841
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002842bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2843 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2844 return false;
2845
2846 if (SrcTy == DestTy)
2847 return true;
2848
2849 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2850 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2851 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2852 // An element by element cast. Valid if casting the elements is valid.
2853 SrcTy = SrcVecTy->getElementType();
2854 DestTy = DestVecTy->getElementType();
2855 }
2856 }
2857 }
2858
2859 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2860 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2861 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2862 }
2863 }
2864
2865 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2866 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2867
2868 // Could still have vectors of pointers if the number of elements doesn't
2869 // match
2870 if (SrcBits == 0 || DestBits == 0)
2871 return false;
2872
2873 if (SrcBits != DestBits)
2874 return false;
2875
2876 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2877 return false;
2878
2879 return true;
2880}
2881
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002882bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002883 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002884 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2885 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002886 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002887 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2888 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002889 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002890
2891 return isBitCastable(SrcTy, DestTy);
2892}
2893
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002894// Provide a way to get a "cast" where the cast opcode is inferred from the
2895// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002896// logic in the castIsValid function below. This axiom should hold:
2897// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2898// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002899// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002900// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002902CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002903 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2904 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905
Duncan Sands55e50902008-01-06 10:12:28 +00002906 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2907 "Only first class types are castable!");
2908
Duncan Sandsa8514532011-05-18 07:13:41 +00002909 if (SrcTy == DestTy)
2910 return BitCast;
2911
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002912 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002913 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2914 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002915 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2916 // An element by element cast. Find the appropriate opcode based on the
2917 // element types.
2918 SrcTy = SrcVecTy->getElementType();
2919 DestTy = DestVecTy->getElementType();
2920 }
2921
2922 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002923 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2924 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002925
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002927 if (DestTy->isIntegerTy()) { // Casting to integral
2928 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929 if (DestBits < SrcBits)
2930 return Trunc; // int -> smaller int
2931 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002932 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933 return SExt; // signed -> SEXT
2934 else
2935 return ZExt; // unsigned -> ZEXT
2936 } else {
2937 return BitCast; // Same size, No-op cast
2938 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002939 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002940 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 return FPToSI; // FP -> sint
2942 else
2943 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002944 } else if (SrcTy->isVectorTy()) {
2945 assert(DestBits == SrcBits &&
2946 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947 return BitCast; // Same size, no-op cast
2948 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002949 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950 "Casting from a value that is not first-class type");
2951 return PtrToInt; // ptr -> int
2952 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002953 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2954 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002955 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956 return SIToFP; // sint -> FP
2957 else
2958 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002959 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002960 if (DestBits < SrcBits) {
2961 return FPTrunc; // FP -> smaller FP
2962 } else if (DestBits > SrcBits) {
2963 return FPExt; // FP -> larger FP
2964 } else {
2965 return BitCast; // same size, no-op cast
2966 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002967 } else if (SrcTy->isVectorTy()) {
2968 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002969 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002970 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002972 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002973 } else if (DestTy->isVectorTy()) {
2974 assert(DestBits == SrcBits &&
2975 "Illegal cast to vector (wrong type or size)");
2976 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002977 } else if (DestTy->isPointerTy()) {
2978 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002979 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2980 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002982 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002985 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002986 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002987 if (SrcTy->isVectorTy()) {
2988 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002989 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002990 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002991 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002992 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002993 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002994}
2995
2996//===----------------------------------------------------------------------===//
2997// CastInst SubClass Constructors
2998//===----------------------------------------------------------------------===//
2999
3000/// Check that the construction parameters for a CastInst are correct. This
3001/// could be broken out into the separate constructors but it is useful to have
3002/// it in one place and to eliminate the redundant code for getting the sizes
3003/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003004bool
Chris Lattner229907c2011-07-18 04:54:35 +00003005CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006
3007 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003008 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003009
Chris Lattner37bc78a2010-01-26 21:51:43 +00003010 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3011 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003012 return false;
3013
3014 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003015 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3016 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017
Duncan Sands7f646562011-05-18 09:21:57 +00003018 // If these are vector types, get the lengths of the vectors (using zero for
3019 // scalar types means that checking that vector lengths match also checks that
3020 // scalars are not being converted to vectors or vectors to scalars).
3021 unsigned SrcLength = SrcTy->isVectorTy() ?
3022 cast<VectorType>(SrcTy)->getNumElements() : 0;
3023 unsigned DstLength = DstTy->isVectorTy() ?
3024 cast<VectorType>(DstTy)->getNumElements() : 0;
3025
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003026 // Switch on the opcode provided
3027 switch (op) {
3028 default: return false; // This is an input error
3029 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003030 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3031 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003032 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003033 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3034 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003036 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3037 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003039 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3040 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003042 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3043 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003044 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003046 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3047 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003050 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3051 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003052 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003053 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003054 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003055 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3056 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3057 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003058 return SrcTy->getScalarType()->isPointerTy() &&
3059 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003061 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003062 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003063 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3064 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3065 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003066 return SrcTy->getScalarType()->isIntegerTy() &&
3067 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003068 case Instruction::BitCast: {
3069 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3070 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3071
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 // BitCast implies a no-op cast of type only. No bits change.
3073 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003074 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 return false;
3076
Alp Tokerf907b892013-12-05 05:44:44 +00003077 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003078 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003079 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003080 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3081
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003082 // If both are pointers then the address spaces must match.
3083 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3084 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003085
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003086 // A vector of pointers must have the same number of elements.
3087 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3088 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3089 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3090
3091 return false;
3092 }
3093
3094 return true;
3095 }
3096 case Instruction::AddrSpaceCast: {
3097 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3098 if (!SrcPtrTy)
3099 return false;
3100
3101 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3102 if (!DstPtrTy)
3103 return false;
3104
3105 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3106 return false;
3107
3108 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3109 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3110 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3111
3112 return false;
3113 }
3114
3115 return true;
3116 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003117 }
3118}
3119
3120TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003121 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003122) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003123 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003124}
3125
3126TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003127 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003129 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003130}
3131
3132ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003133 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003134) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003135 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136}
3137
3138ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003139 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003141 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142}
3143SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003144 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003147}
3148
Jeff Cohencc08c832006-12-02 02:22:01 +00003149SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003150 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003152 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003153}
3154
3155FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003156 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159}
3160
3161FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003162 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003165}
3166
3167FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003168 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003170 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171}
3172
3173FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003174 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003175) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003176 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003177}
3178
3179UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003180 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003182 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003183}
3184
3185UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003186 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189}
3190
3191SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003192 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195}
3196
3197SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003198 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201}
3202
3203FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003204 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207}
3208
3209FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003210 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213}
3214
3215FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003216 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003217) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219}
3220
3221FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003222 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225}
3226
3227PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003228 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003229) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231}
3232
3233PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003234 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237}
3238
3239IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003240 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243}
3244
3245IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003246 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003249}
3250
3251BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003252 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003254 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003255}
3256
3257BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003258 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003260 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003261}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003262
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003263AddrSpaceCastInst::AddrSpaceCastInst(
3264 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3265) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3266 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3267}
3268
3269AddrSpaceCastInst::AddrSpaceCastInst(
3270 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3271) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3272 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3273}
3274
Chris Lattnerf16dc002006-09-17 19:29:56 +00003275//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003276// CmpInst Classes
3277//===----------------------------------------------------------------------===//
3278
Craig Topper3186c012012-09-23 02:12:10 +00003279void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003280
Chris Lattner229907c2011-07-18 04:54:35 +00003281CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003282 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003283 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003284 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003285 OperandTraits<CmpInst>::op_begin(this),
3286 OperandTraits<CmpInst>::operands(this),
3287 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003288 Op<0>() = LHS;
3289 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003290 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003291 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003292}
Gabor Greiff6caff662008-05-10 08:32:32 +00003293
Chris Lattner229907c2011-07-18 04:54:35 +00003294CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003295 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003296 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003297 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003298 OperandTraits<CmpInst>::op_begin(this),
3299 OperandTraits<CmpInst>::operands(this),
3300 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003301 Op<0>() = LHS;
3302 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003303 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003304 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003305}
3306
3307CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003308CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003309 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003310 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003311 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312 if (InsertBefore)
3313 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3314 S1, S2, Name);
3315 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003316 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003318 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319
3320 if (InsertBefore)
3321 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3322 S1, S2, Name);
3323 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003324 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003325 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003326}
3327
3328CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003329CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003330 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003331 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3333 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003334 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003335 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3336 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003337}
3338
3339void CmpInst::swapOperands() {
3340 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3341 IC->swapOperands();
3342 else
3343 cast<FCmpInst>(this)->swapOperands();
3344}
3345
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003346bool CmpInst::isCommutative() const {
3347 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003348 return IC->isCommutative();
3349 return cast<FCmpInst>(this)->isCommutative();
3350}
3351
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003352bool CmpInst::isEquality() const {
3353 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003354 return IC->isEquality();
3355 return cast<FCmpInst>(this)->isEquality();
3356}
3357
3358
Dan Gohman4e724382008-05-31 02:47:54 +00003359CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003360 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003361 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003362 case ICMP_EQ: return ICMP_NE;
3363 case ICMP_NE: return ICMP_EQ;
3364 case ICMP_UGT: return ICMP_ULE;
3365 case ICMP_ULT: return ICMP_UGE;
3366 case ICMP_UGE: return ICMP_ULT;
3367 case ICMP_ULE: return ICMP_UGT;
3368 case ICMP_SGT: return ICMP_SLE;
3369 case ICMP_SLT: return ICMP_SGE;
3370 case ICMP_SGE: return ICMP_SLT;
3371 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003372
Dan Gohman4e724382008-05-31 02:47:54 +00003373 case FCMP_OEQ: return FCMP_UNE;
3374 case FCMP_ONE: return FCMP_UEQ;
3375 case FCMP_OGT: return FCMP_ULE;
3376 case FCMP_OLT: return FCMP_UGE;
3377 case FCMP_OGE: return FCMP_ULT;
3378 case FCMP_OLE: return FCMP_UGT;
3379 case FCMP_UEQ: return FCMP_ONE;
3380 case FCMP_UNE: return FCMP_OEQ;
3381 case FCMP_UGT: return FCMP_OLE;
3382 case FCMP_ULT: return FCMP_OGE;
3383 case FCMP_UGE: return FCMP_OLT;
3384 case FCMP_ULE: return FCMP_OGT;
3385 case FCMP_ORD: return FCMP_UNO;
3386 case FCMP_UNO: return FCMP_ORD;
3387 case FCMP_TRUE: return FCMP_FALSE;
3388 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003389 }
3390}
3391
Reid Spencer266e42b2006-12-23 06:05:41 +00003392ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3393 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003394 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003395 case ICMP_EQ: case ICMP_NE:
3396 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3397 return pred;
3398 case ICMP_UGT: return ICMP_SGT;
3399 case ICMP_ULT: return ICMP_SLT;
3400 case ICMP_UGE: return ICMP_SGE;
3401 case ICMP_ULE: return ICMP_SLE;
3402 }
3403}
3404
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003405ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3406 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003407 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003408 case ICMP_EQ: case ICMP_NE:
3409 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3410 return pred;
3411 case ICMP_SGT: return ICMP_UGT;
3412 case ICMP_SLT: return ICMP_ULT;
3413 case ICMP_SGE: return ICMP_UGE;
3414 case ICMP_SLE: return ICMP_ULE;
3415 }
3416}
3417
Reid Spencer0286bc12007-02-28 22:00:54 +00003418/// Initialize a set of values that all satisfy the condition with C.
3419///
3420ConstantRange
3421ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3422 APInt Lower(C);
3423 APInt Upper(C);
3424 uint32_t BitWidth = C.getBitWidth();
3425 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003426 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003427 case ICmpInst::ICMP_EQ: ++Upper; break;
3428 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003429 case ICmpInst::ICMP_ULT:
3430 Lower = APInt::getMinValue(BitWidth);
3431 // Check for an empty-set condition.
3432 if (Lower == Upper)
3433 return ConstantRange(BitWidth, /*isFullSet=*/false);
3434 break;
3435 case ICmpInst::ICMP_SLT:
3436 Lower = APInt::getSignedMinValue(BitWidth);
3437 // Check for an empty-set condition.
3438 if (Lower == Upper)
3439 return ConstantRange(BitWidth, /*isFullSet=*/false);
3440 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003441 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003442 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003443 // Check for an empty-set condition.
3444 if (Lower == Upper)
3445 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003446 break;
3447 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003448 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003449 // Check for an empty-set condition.
3450 if (Lower == Upper)
3451 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003452 break;
3453 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003454 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003455 // Check for a full-set condition.
3456 if (Lower == Upper)
3457 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003458 break;
3459 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003460 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003461 // Check for a full-set condition.
3462 if (Lower == Upper)
3463 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003464 break;
3465 case ICmpInst::ICMP_UGE:
3466 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003467 // Check for a full-set condition.
3468 if (Lower == Upper)
3469 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003470 break;
3471 case ICmpInst::ICMP_SGE:
3472 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003473 // Check for a full-set condition.
3474 if (Lower == Upper)
3475 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003476 break;
3477 }
3478 return ConstantRange(Lower, Upper);
3479}
3480
Dan Gohman4e724382008-05-31 02:47:54 +00003481CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003482 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003483 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003484 case ICMP_EQ: case ICMP_NE:
3485 return pred;
3486 case ICMP_SGT: return ICMP_SLT;
3487 case ICMP_SLT: return ICMP_SGT;
3488 case ICMP_SGE: return ICMP_SLE;
3489 case ICMP_SLE: return ICMP_SGE;
3490 case ICMP_UGT: return ICMP_ULT;
3491 case ICMP_ULT: return ICMP_UGT;
3492 case ICMP_UGE: return ICMP_ULE;
3493 case ICMP_ULE: return ICMP_UGE;
3494
Reid Spencerd9436b62006-11-20 01:22:35 +00003495 case FCMP_FALSE: case FCMP_TRUE:
3496 case FCMP_OEQ: case FCMP_ONE:
3497 case FCMP_UEQ: case FCMP_UNE:
3498 case FCMP_ORD: case FCMP_UNO:
3499 return pred;
3500 case FCMP_OGT: return FCMP_OLT;
3501 case FCMP_OLT: return FCMP_OGT;
3502 case FCMP_OGE: return FCMP_OLE;
3503 case FCMP_OLE: return FCMP_OGE;
3504 case FCMP_UGT: return FCMP_ULT;
3505 case FCMP_ULT: return FCMP_UGT;
3506 case FCMP_UGE: return FCMP_ULE;
3507 case FCMP_ULE: return FCMP_UGE;
3508 }
3509}
3510
Sanjoy Das6e78b172015-10-22 19:57:34 +00003511CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3512 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3513
3514 switch (pred) {
3515 default:
3516 llvm_unreachable("Unknown predicate!");
3517 case CmpInst::ICMP_ULT:
3518 return CmpInst::ICMP_SLT;
3519 case CmpInst::ICMP_ULE:
3520 return CmpInst::ICMP_SLE;
3521 case CmpInst::ICMP_UGT:
3522 return CmpInst::ICMP_SGT;
3523 case CmpInst::ICMP_UGE:
3524 return CmpInst::ICMP_SGE;
3525 }
3526}
3527
Reid Spencer266e42b2006-12-23 06:05:41 +00003528bool CmpInst::isUnsigned(unsigned short predicate) {
3529 switch (predicate) {
3530 default: return false;
3531 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3532 case ICmpInst::ICMP_UGE: return true;
3533 }
3534}
3535
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003536bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003537 switch (predicate) {
3538 default: return false;
3539 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3540 case ICmpInst::ICMP_SGE: return true;
3541 }
3542}
3543
3544bool CmpInst::isOrdered(unsigned short predicate) {
3545 switch (predicate) {
3546 default: return false;
3547 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3548 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3549 case FCmpInst::FCMP_ORD: return true;
3550 }
3551}
3552
3553bool CmpInst::isUnordered(unsigned short predicate) {
3554 switch (predicate) {
3555 default: return false;
3556 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3557 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3558 case FCmpInst::FCMP_UNO: return true;
3559 }
3560}
3561
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003562bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3563 switch(predicate) {
3564 default: return false;
3565 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3566 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3567 }
3568}
3569
3570bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3571 switch(predicate) {
3572 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3573 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3574 default: return false;
3575 }
3576}
3577
3578
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003579//===----------------------------------------------------------------------===//
3580// SwitchInst Implementation
3581//===----------------------------------------------------------------------===//
3582
Chris Lattnerbaf00152010-11-17 05:41:46 +00003583void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3584 assert(Value && Default && NumReserved);
3585 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003586 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003587 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003588
Pete Coopereb31b682015-05-21 22:48:54 +00003589 Op<0>() = Value;
3590 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003591}
3592
Chris Lattner2195fc42007-02-24 00:55:48 +00003593/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3594/// switch on and a default destination. The number of additional cases can
3595/// be specified here to make memory allocation more efficient. This
3596/// constructor can also autoinsert before another instruction.
3597SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3598 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003599 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003600 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003601 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003602}
3603
3604/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3605/// switch on and a default destination. The number of additional cases can
3606/// be specified here to make memory allocation more efficient. This
3607/// constructor also autoinserts at the end of the specified BasicBlock.
3608SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3609 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003610 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003611 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003612 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003613}
3614
Misha Brukmanb1c93172005-04-21 23:48:37 +00003615SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003616 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003617 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003618 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003619 Use *OL = getOperandList();
3620 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003621 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003622 OL[i] = InOL[i];
3623 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003624 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003625 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003626}
3627
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003629/// addCase - Add an entry to the switch instruction...
3630///
Chris Lattner47ac1872005-02-24 05:32:09 +00003631void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003632 unsigned NewCaseIdx = getNumCases();
3633 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003634 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003635 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003636 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003637 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003638 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003639 CaseIt Case(this, NewCaseIdx);
3640 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003641 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003642}
3643
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003644/// removeCase - This method removes the specified case and its successor
3645/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003646void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003647 unsigned idx = i.getCaseIndex();
3648
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003649 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003650
3651 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003652 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003653
Jay Foad14277722011-02-01 09:22:34 +00003654 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003655 if (2 + (idx + 1) * 2 != NumOps) {
3656 OL[2 + idx * 2] = OL[NumOps - 2];
3657 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003658 }
3659
3660 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003661 OL[NumOps-2].set(nullptr);
3662 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003663 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003664}
3665
Jay Foade98f29d2011-04-01 08:00:58 +00003666/// growOperands - grow operands - This grows the operand list in response
3667/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003668///
Jay Foade98f29d2011-04-01 08:00:58 +00003669void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003670 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003671 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003672
3673 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003674 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003675}
3676
3677
3678BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3679 return getSuccessor(idx);
3680}
3681unsigned SwitchInst::getNumSuccessorsV() const {
3682 return getNumSuccessors();
3683}
3684void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3685 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003686}
Chris Lattnerf22be932004-10-15 23:52:53 +00003687
Chris Lattner3ed871f2009-10-27 19:13:16 +00003688//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003689// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003690//===----------------------------------------------------------------------===//
3691
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003692void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003693 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003694 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003695 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003696 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003697 allocHungoffUses(ReservedSpace);
3698
Pete Coopereb31b682015-05-21 22:48:54 +00003699 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003700}
3701
3702
Jay Foade98f29d2011-04-01 08:00:58 +00003703/// growOperands - grow operands - This grows the operand list in response
3704/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003705///
Jay Foade98f29d2011-04-01 08:00:58 +00003706void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003707 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003708 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709
3710 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003711 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003712}
3713
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003714IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3715 Instruction *InsertBefore)
3716: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003717 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003718 init(Address, NumCases);
3719}
3720
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003721IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3722 BasicBlock *InsertAtEnd)
3723: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003724 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003725 init(Address, NumCases);
3726}
3727
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003728IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003729 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3730 nullptr, IBI.getNumOperands()) {
3731 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003732 Use *OL = getOperandList();
3733 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003734 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3735 OL[i] = InOL[i];
3736 SubclassOptionalData = IBI.SubclassOptionalData;
3737}
3738
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739/// addDestination - Add a destination.
3740///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003741void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003742 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003743 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003744 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003745 // Initialize some new operands.
3746 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003747 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003748 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003749}
3750
3751/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003752/// indirectbr instruction.
3753void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003754 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3755
3756 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003757 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758
3759 // Replace this value with the last one.
3760 OL[idx+1] = OL[NumOps-1];
3761
3762 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003763 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003764 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003765}
3766
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003767BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003768 return getSuccessor(idx);
3769}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003770unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003771 return getNumSuccessors();
3772}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003773void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003774 setSuccessor(idx, B);
3775}
3776
3777//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003778// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003779//===----------------------------------------------------------------------===//
3780
Chris Lattnerf22be932004-10-15 23:52:53 +00003781// Define these methods here so vtables don't get emitted into every translation
3782// unit that uses these classes.
3783
Pete Cooper75403d72015-06-24 20:22:23 +00003784GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003785 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003786}
3787
Pete Cooper75403d72015-06-24 20:22:23 +00003788BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003789 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003790}
3791
Pete Cooper75403d72015-06-24 20:22:23 +00003792FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003793 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003794}
3795
Pete Cooper75403d72015-06-24 20:22:23 +00003796ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003797 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003798}
3799
Pete Cooper75403d72015-06-24 20:22:23 +00003800ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003801 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003802}
3803
Pete Cooper75403d72015-06-24 20:22:23 +00003804InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003805 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003806}
3807
Pete Cooper75403d72015-06-24 20:22:23 +00003808AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003809 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3810 (Value *)getOperand(0), getAlignment());
3811 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3812 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003813}
3814
Pete Cooper75403d72015-06-24 20:22:23 +00003815LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003816 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3817 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003818}
3819
Pete Cooper75403d72015-06-24 20:22:23 +00003820StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003821 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003822 getAlignment(), getOrdering(), getSynchScope());
3823
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003824}
3825
Pete Cooper75403d72015-06-24 20:22:23 +00003826AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003827 AtomicCmpXchgInst *Result =
3828 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003829 getSuccessOrdering(), getFailureOrdering(),
3830 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003831 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003832 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003833 return Result;
3834}
3835
Pete Cooper75403d72015-06-24 20:22:23 +00003836AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003837 AtomicRMWInst *Result =
3838 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3839 getOrdering(), getSynchScope());
3840 Result->setVolatile(isVolatile());
3841 return Result;
3842}
3843
Pete Cooper75403d72015-06-24 20:22:23 +00003844FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003845 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3846}
3847
Pete Cooper75403d72015-06-24 20:22:23 +00003848TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003849 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003850}
3851
Pete Cooper75403d72015-06-24 20:22:23 +00003852ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003853 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003854}
3855
Pete Cooper75403d72015-06-24 20:22:23 +00003856SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003857 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003858}
3859
Pete Cooper75403d72015-06-24 20:22:23 +00003860FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003861 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003862}
3863
Pete Cooper75403d72015-06-24 20:22:23 +00003864FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003865 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003866}
3867
Pete Cooper75403d72015-06-24 20:22:23 +00003868UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003869 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003870}
3871
Pete Cooper75403d72015-06-24 20:22:23 +00003872SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003873 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003874}
3875
Pete Cooper75403d72015-06-24 20:22:23 +00003876FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003877 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003878}
3879
Pete Cooper75403d72015-06-24 20:22:23 +00003880FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003881 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003882}
3883
Pete Cooper75403d72015-06-24 20:22:23 +00003884PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003885 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003886}
3887
Pete Cooper75403d72015-06-24 20:22:23 +00003888IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003889 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003890}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003891
Pete Cooper75403d72015-06-24 20:22:23 +00003892BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003893 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003894}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003895
Pete Cooper75403d72015-06-24 20:22:23 +00003896AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003897 return new AddrSpaceCastInst(getOperand(0), getType());
3898}
3899
Pete Cooper75403d72015-06-24 20:22:23 +00003900CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003901 if (hasOperandBundles()) {
3902 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3903 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3904 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003905 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003906}
3907
Pete Cooper75403d72015-06-24 20:22:23 +00003908SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003909 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003910}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003911
Pete Cooper75403d72015-06-24 20:22:23 +00003912VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003913 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003914}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003915
Pete Cooper75403d72015-06-24 20:22:23 +00003916ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003917 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003918}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919
Pete Cooper75403d72015-06-24 20:22:23 +00003920InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003921 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003922}
3923
Pete Cooper75403d72015-06-24 20:22:23 +00003924ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003925 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003926}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003927
Pete Cooper75403d72015-06-24 20:22:23 +00003928PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003929
Pete Cooper75403d72015-06-24 20:22:23 +00003930LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003931 return new LandingPadInst(*this);
3932}
3933
Pete Cooper75403d72015-06-24 20:22:23 +00003934ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003935 return new(getNumOperands()) ReturnInst(*this);
3936}
3937
Pete Cooper75403d72015-06-24 20:22:23 +00003938BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003939 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003940}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941
Pete Cooper75403d72015-06-24 20:22:23 +00003942SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943
Pete Cooper75403d72015-06-24 20:22:23 +00003944IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003945 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003946}
3947
Pete Cooper75403d72015-06-24 20:22:23 +00003948InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003949 if (hasOperandBundles()) {
3950 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3951 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3952 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003953 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003954}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955
Pete Cooper75403d72015-06-24 20:22:23 +00003956ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003957
David Majnemer654e1302015-07-31 17:58:14 +00003958CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3959 return new (getNumOperands()) CleanupReturnInst(*this);
3960}
3961
David Majnemer654e1302015-07-31 17:58:14 +00003962CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003963 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003964}
3965
David Majnemer8a1c45d2015-12-12 05:38:55 +00003966CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3967 return new CatchSwitchInst(*this);
3968}
3969
3970FuncletPadInst *FuncletPadInst::cloneImpl() const {
3971 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003972}
3973
Pete Cooper75403d72015-06-24 20:22:23 +00003974UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003975 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003976 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003977}