blob: 6ec2e28997009fcc689a2c38d063c352a6739327 [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) {
302 CallSite CS(CI);
303 std::vector<Value *> Args(CS.arg_begin(), CS.arg_end());
304
305 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
306 InsertPt);
307 NewCI->setTailCallKind(CI->getTailCallKind());
308 NewCI->setCallingConv(CI->getCallingConv());
309 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000310 NewCI->setAttributes(CI->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000311 return NewCI;
312}
313
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000314void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000315 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000316 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000317 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000318}
319
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000320void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
321 AttributeSet PAL = getAttributes();
322 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
323 setAttributes(PAL);
324}
325
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000326void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000327 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000328 AttrBuilder B(attr);
329 LLVMContext &Context = getContext();
330 PAL = PAL.removeAttributes(Context, i,
331 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000332 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000333}
334
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000335void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
336 AttributeSet PAL = getAttributes();
337 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
338 setAttributes(PAL);
339}
340
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000341void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
342 AttributeSet PAL = getAttributes();
343 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
344 setAttributes(PAL);
345}
346
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000347bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000348 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
349
Bill Wendling749a43d2012-12-30 13:50:49 +0000350 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000351 return true;
352 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000353 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000354 return false;
355}
356
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000357bool CallInst::dataOperandHasImpliedAttr(unsigned i,
358 Attribute::AttrKind A) const {
359
Sanjoy Das776e4a72015-11-05 01:53:26 +0000360 // There are getNumOperands() - 1 data operands. The last operand is the
361 // callee.
362 assert(i < getNumOperands() && "Data operand index out of bounds!");
363
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000364 // The attribute A can either be directly specified, if the operand in
365 // question is a call argument; or be indirectly implied by the kind of its
366 // containing operand bundle, if the operand is a bundle operand.
367
368 if (i < (getNumArgOperands() + 1))
369 return paramHasAttr(i, A);
370
371 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
372 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000373 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000374}
375
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376/// IsConstantOne - Return true only if val is constant int 1
377static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000378 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000379 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
380 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000381}
382
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000383static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000384 BasicBlock *InsertAtEnd, Type *IntPtrTy,
385 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000386 Value *ArraySize, Function *MallocF,
387 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000388 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 "createMalloc needs either InsertBefore or InsertAtEnd");
390
391 // malloc(type) becomes:
392 // bitcast (i8* malloc(typeSize)) to type*
393 // malloc(type, arraySize) becomes:
394 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000395 if (!ArraySize)
396 ArraySize = ConstantInt::get(IntPtrTy, 1);
397 else if (ArraySize->getType() != IntPtrTy) {
398 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000399 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
400 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000401 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000402 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
403 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000404 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000406 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407 if (IsConstantOne(AllocSize)) {
408 AllocSize = ArraySize; // Operand * 1 = Operand
409 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
410 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
411 false /*ZExt*/);
412 // Malloc arg is constant product of type size and array size
413 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
414 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000415 // Multiply type size by the array size...
416 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000417 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
418 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000419 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000420 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
421 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000423 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000424
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000426 // Create the call to Malloc.
427 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
428 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000429 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000430 Value *MallocFunc = MallocF;
431 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000432 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000433 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000434 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000435 CallInst *MCall = nullptr;
436 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000438 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000439 Result = MCall;
440 if (Result->getType() != AllocPtrType)
441 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000442 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000443 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000444 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000445 Result = MCall;
446 if (Result->getType() != AllocPtrType) {
447 InsertAtEnd->getInstList().push_back(MCall);
448 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000449 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000450 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000451 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000452 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000453 if (Function *F = dyn_cast<Function>(MallocFunc)) {
454 MCall->setCallingConv(F->getCallingConv());
455 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
456 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000457 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000458
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000459 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000460}
461
462/// CreateMalloc - Generate the IR for a call to malloc:
463/// 1. Compute the malloc call's argument as the specified type's size,
464/// possibly multiplied by the array size if the array size is not
465/// constant 1.
466/// 2. Call malloc with that argument.
467/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000468Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000469 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000470 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000471 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000472 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000473 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000474 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475}
476
477/// CreateMalloc - Generate the IR for a call to malloc:
478/// 1. Compute the malloc call's argument as the specified type's size,
479/// possibly multiplied by the array size if the array size is not
480/// constant 1.
481/// 2. Call malloc with that argument.
482/// 3. Bitcast the result of the malloc call to the specified type.
483/// Note: This function does not add the bitcast to the basic block, that is the
484/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000485Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000486 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000487 Value *AllocSize, Value *ArraySize,
488 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000489 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000490 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000492
Victor Hernandeze2971492009-10-24 04:23:03 +0000493static Instruction* createFree(Value* Source, Instruction *InsertBefore,
494 BasicBlock *InsertAtEnd) {
495 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
496 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000497 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000498 "Can not free something of nonpointer type!");
499
500 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
501 Module* M = BB->getParent()->getParent();
502
Chris Lattner229907c2011-07-18 04:54:35 +0000503 Type *VoidTy = Type::getVoidTy(M->getContext());
504 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000505 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000506 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000507 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000508 Value *PtrCast = Source;
509 if (InsertBefore) {
510 if (Source->getType() != IntPtrTy)
511 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
512 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
513 } else {
514 if (Source->getType() != IntPtrTy)
515 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
516 Result = CallInst::Create(FreeFunc, PtrCast, "");
517 }
518 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000519 if (Function *F = dyn_cast<Function>(FreeFunc))
520 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000521
522 return Result;
523}
524
525/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000526Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000527 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000528}
529
530/// CreateFree - Generate the IR for a call to the builtin free function.
531/// Note: This function does not add the call to the basic block, that is the
532/// responsibility of the caller.
533Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000534 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000535 assert(FreeCall && "CreateFree did not create a CallInst");
536 return FreeCall;
537}
538
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000539//===----------------------------------------------------------------------===//
540// InvokeInst Implementation
541//===----------------------------------------------------------------------===//
542
David Blaikie3e807092015-05-13 18:35:26 +0000543void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
544 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000545 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000546 const Twine &NameStr) {
547 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000548
Sanjoy Das9303c242015-09-24 19:14:18 +0000549 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
550 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000551 Op<-3>() = Fn;
552 Op<-2>() = IfNormal;
553 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000554
555#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000556 assert(((Args.size() == FTy->getNumParams()) ||
557 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000558 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000559
Jay Foad5bd375a2011-07-15 08:37:34 +0000560 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000561 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000562 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000563 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000564#endif
565
566 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000567
568 auto It = populateBundleOperandInfos(Bundles, Args.size());
569 (void)It;
570 assert(It + 3 == op_end() && "Should add up!");
571
Jay Foad5bd375a2011-07-15 08:37:34 +0000572 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000573}
574
Misha Brukmanb1c93172005-04-21 23:48:37 +0000575InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000576 : TerminatorInst(II.getType(), Instruction::Invoke,
577 OperandTraits<InvokeInst>::op_end(this) -
578 II.getNumOperands(),
579 II.getNumOperands()),
580 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000581 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000582 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000583 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
584 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000585 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586}
587
Sanjoy Das2d161452015-11-18 06:23:38 +0000588InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
589 Instruction *InsertPt) {
590 CallSite CS(II);
591 std::vector<Value *> Args(CS.arg_begin(), CS.arg_end());
592
593 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
594 II->getUnwindDest(), Args, OpB,
595 II->getName(), InsertPt);
596 NewII->setCallingConv(II->getCallingConv());
597 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000598 NewII->setAttributes(II->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000599 return NewII;
600}
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
603 return getSuccessor(idx);
604}
605unsigned InvokeInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
609 return setSuccessor(idx, B);
610}
611
Michael Gottesman41748d72013-06-27 00:25:01 +0000612bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000613 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000614 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000615
616 // Operand bundles override attributes on the called function, but don't
617 // override attributes directly present on the invoke instruction.
618 if (isFnAttrDisallowedByOpBundle(A))
619 return false;
620
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000622 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000623 return false;
624}
625
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000626bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000627 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
628
Bill Wendling749a43d2012-12-30 13:50:49 +0000629 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000630 return true;
631 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000632 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000633 return false;
634}
635
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000636bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
637 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000638 // There are getNumOperands() - 3 data operands. The last three operands are
639 // the callee and the two successor basic blocks.
640 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
641
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000642 // The attribute A can either be directly specified, if the operand in
643 // question is an invoke argument; or be indirectly implied by the kind of its
644 // containing operand bundle, if the operand is a bundle operand.
645
646 if (i < (getNumArgOperands() + 1))
647 return paramHasAttr(i, A);
648
649 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
650 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000651 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000652}
653
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000654void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000655 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000656 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000657 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000658}
659
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000660void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000661 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000662 AttrBuilder B(attr);
663 PAL = PAL.removeAttributes(getContext(), i,
664 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000665 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000666}
667
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000668void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
669 AttributeSet PAL = getAttributes();
670 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
671 setAttributes(PAL);
672}
673
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000674void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
675 AttributeSet PAL = getAttributes();
676 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
677 setAttributes(PAL);
678}
679
Bill Wendlingfae14752011-08-12 20:24:12 +0000680LandingPadInst *InvokeInst::getLandingPadInst() const {
681 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
682}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000683
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684//===----------------------------------------------------------------------===//
685// ReturnInst Implementation
686//===----------------------------------------------------------------------===//
687
Chris Lattner2195fc42007-02-24 00:55:48 +0000688ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000689 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000690 OperandTraits<ReturnInst>::op_end(this) -
691 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000692 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000693 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000694 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000695 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000696}
697
Owen Anderson55f1c092009-08-13 21:58:54 +0000698ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000700 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
701 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000702 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000703 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
Owen Anderson55f1c092009-08-13 21:58:54 +0000705ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
706 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
708 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000709 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000710 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000711}
Owen Anderson55f1c092009-08-13 21:58:54 +0000712ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
713 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000714 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000715}
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717unsigned ReturnInst::getNumSuccessorsV() const {
718 return getNumSuccessors();
719}
720
Devang Patelae682fb2008-02-26 17:56:20 +0000721/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
722/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000724 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000728 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729}
730
Devang Patel59643e52008-02-23 00:35:18 +0000731ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000732}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000735// ResumeInst Implementation
736//===----------------------------------------------------------------------===//
737
738ResumeInst::ResumeInst(const ResumeInst &RI)
739 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
740 OperandTraits<ResumeInst>::op_begin(this), 1) {
741 Op<0>() = RI.Op<0>();
742}
743
744ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
745 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
746 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
747 Op<0>() = Exn;
748}
749
750ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
751 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
752 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
753 Op<0>() = Exn;
754}
755
756unsigned ResumeInst::getNumSuccessorsV() const {
757 return getNumSuccessors();
758}
759
760void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
761 llvm_unreachable("ResumeInst has no successors!");
762}
763
764BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
765 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000766}
767
768//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000769// CleanupEndPadInst Implementation
770//===----------------------------------------------------------------------===//
771
772CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
773 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
774 OperandTraits<CleanupEndPadInst>::op_end(this) -
775 CEPI.getNumOperands(),
776 CEPI.getNumOperands()) {
777 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
778 setCleanupPad(CEPI.getCleanupPad());
779 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
780 setUnwindDest(UnwindDest);
781}
782
783void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
784 setCleanupPad(CleanupPad);
785 if (UnwindBB) {
786 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
787 setUnwindDest(UnwindBB);
788 }
789}
790
791CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
792 BasicBlock *UnwindBB, unsigned Values,
793 Instruction *InsertBefore)
794 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
795 Instruction::CleanupEndPad,
796 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
797 Values, InsertBefore) {
798 init(CleanupPad, UnwindBB);
799}
800
801CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
802 BasicBlock *UnwindBB, unsigned Values,
803 BasicBlock *InsertAtEnd)
804 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
805 Instruction::CleanupEndPad,
806 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
807 Values, InsertAtEnd) {
808 init(CleanupPad, UnwindBB);
809}
810
811BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
812 assert(Idx == 0);
813 return getUnwindDest();
814}
815unsigned CleanupEndPadInst::getNumSuccessorsV() const {
816 return getNumSuccessors();
817}
818void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
819 assert(Idx == 0);
820 setUnwindDest(B);
821}
822
823//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000824// CleanupReturnInst Implementation
825//===----------------------------------------------------------------------===//
826
827CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
828 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
829 OperandTraits<CleanupReturnInst>::op_end(this) -
830 CRI.getNumOperands(),
831 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000832 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000833 Op<-1>() = CRI.Op<-1>();
834 if (CRI.hasUnwindDest())
835 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000836}
837
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000838void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000839 if (UnwindBB)
840 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000841
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000842 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000843 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000844 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000845}
846
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000847CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000848 BasicBlock *UnwindBB, unsigned Values,
849 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000850 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
851 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000852 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
853 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000854 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000855}
856
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000857CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000858 BasicBlock *UnwindBB, unsigned Values,
859 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000860 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
861 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000862 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
863 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000864 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000865}
866
867BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
868 assert(Idx == 0);
869 return getUnwindDest();
870}
871unsigned CleanupReturnInst::getNumSuccessorsV() const {
872 return getNumSuccessors();
873}
874void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
875 assert(Idx == 0);
876 setUnwindDest(B);
877}
878
879//===----------------------------------------------------------------------===//
880// CatchEndPadInst Implementation
881//===----------------------------------------------------------------------===//
882
883CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
884 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
885 OperandTraits<CatchEndPadInst>::op_end(this) -
886 CRI.getNumOperands(),
887 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000888 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000889 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
890 setUnwindDest(UnwindDest);
891}
892
893void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000894 if (UnwindBB) {
895 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
896 setUnwindDest(UnwindBB);
897 }
898}
899
900CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000901 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000902 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
903 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
904 Values, InsertBefore) {
905 init(UnwindBB);
906}
907
908CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000909 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000910 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
911 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
912 Values, InsertAtEnd) {
913 init(UnwindBB);
914}
915
916BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
917 assert(Idx == 0);
918 return getUnwindDest();
919}
920unsigned CatchEndPadInst::getNumSuccessorsV() const {
921 return getNumSuccessors();
922}
923void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
924 assert(Idx == 0);
925 setUnwindDest(B);
926}
927
928//===----------------------------------------------------------------------===//
929// CatchReturnInst Implementation
930//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000931void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
932 Op<0>() = CatchPad;
933 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000934}
David Majnemer654e1302015-07-31 17:58:14 +0000935
936CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
937 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000938 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
939 Op<0>() = CRI.Op<0>();
940 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000941}
942
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000943CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000944 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000945 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000946 OperandTraits<CatchReturnInst>::op_begin(this), 2,
947 InsertBefore) {
948 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000949}
950
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000951CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000952 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000953 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000954 OperandTraits<CatchReturnInst>::op_begin(this), 2,
955 InsertAtEnd) {
956 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000957}
958
959BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000960 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000961 return getSuccessor();
962}
963unsigned CatchReturnInst::getNumSuccessorsV() const {
964 return getNumSuccessors();
965}
966void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000967 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000968 setSuccessor(B);
969}
970
971//===----------------------------------------------------------------------===//
972// CatchPadInst Implementation
973//===----------------------------------------------------------------------===//
974void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000975 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000976 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
977 Op<-2>() = IfNormal;
978 Op<-1>() = IfException;
979 std::copy(Args.begin(), Args.end(), op_begin());
980 setName(NameStr);
981}
982
983CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
984 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
985 OperandTraits<CatchPadInst>::op_end(this) -
986 CPI.getNumOperands(),
987 CPI.getNumOperands()) {
988 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
989}
990
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000991CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
992 ArrayRef<Value *> Args, unsigned Values,
993 const Twine &NameStr, Instruction *InsertBefore)
994 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
995 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000996 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
997 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000998 init(IfNormal, IfException, Args, NameStr);
999}
1000
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001001CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
1002 ArrayRef<Value *> Args, unsigned Values,
1003 const Twine &NameStr, BasicBlock *InsertAtEnd)
1004 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
1005 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +00001006 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
1007 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +00001008 init(IfNormal, IfException, Args, NameStr);
1009}
1010
1011BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
1012 return getSuccessor(Idx);
1013}
1014unsigned CatchPadInst::getNumSuccessorsV() const {
1015 return getNumSuccessors();
1016}
1017void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1018 return setSuccessor(Idx, B);
1019}
1020
1021//===----------------------------------------------------------------------===//
1022// TerminatePadInst Implementation
1023//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +00001024void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +00001025 if (BB)
1026 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1027 if (BB)
1028 Op<-1>() = BB;
1029 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +00001030}
1031
1032TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
1033 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
1034 OperandTraits<TerminatePadInst>::op_end(this) -
1035 TPI.getNumOperands(),
1036 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +00001037 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +00001038 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
1039}
1040
1041TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001042 ArrayRef<Value *> Args, unsigned Values,
1043 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001044 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1045 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1046 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001047 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001048}
1049
1050TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001051 ArrayRef<Value *> Args, unsigned Values,
1052 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001053 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1054 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1055 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001056 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001057}
1058
1059BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
1060 assert(Idx == 0);
1061 return getUnwindDest();
1062}
1063unsigned TerminatePadInst::getNumSuccessorsV() const {
1064 return getNumSuccessors();
1065}
1066void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1067 assert(Idx == 0);
1068 return setUnwindDest(B);
1069}
1070
1071//===----------------------------------------------------------------------===//
1072// CleanupPadInst Implementation
1073//===----------------------------------------------------------------------===//
1074void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1075 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1076 std::copy(Args.begin(), Args.end(), op_begin());
1077 setName(NameStr);
1078}
1079
1080CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1081 : Instruction(CPI.getType(), Instruction::CleanupPad,
1082 OperandTraits<CleanupPadInst>::op_end(this) -
1083 CPI.getNumOperands(),
1084 CPI.getNumOperands()) {
1085 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
1086}
1087
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001088CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001089 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001090 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001091 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1092 Args.size(), InsertBefore) {
1093 init(Args, NameStr);
1094}
1095
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001096CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001097 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001098 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001099 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1100 Args.size(), InsertAtEnd) {
1101 init(Args, NameStr);
1102}
1103
1104//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001105// UnreachableInst Implementation
1106//===----------------------------------------------------------------------===//
1107
Owen Anderson55f1c092009-08-13 21:58:54 +00001108UnreachableInst::UnreachableInst(LLVMContext &Context,
1109 Instruction *InsertBefore)
1110 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001111 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001112}
Owen Anderson55f1c092009-08-13 21:58:54 +00001113UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1114 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001115 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001116}
1117
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001118unsigned UnreachableInst::getNumSuccessorsV() const {
1119 return getNumSuccessors();
1120}
1121
1122void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001123 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001124}
1125
1126BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001127 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001128}
1129
1130//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131// BranchInst Implementation
1132//===----------------------------------------------------------------------===//
1133
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001134void BranchInst::AssertOK() {
1135 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001136 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001137 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001138}
1139
Chris Lattner2195fc42007-02-24 00:55:48 +00001140BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001141 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001142 OperandTraits<BranchInst>::op_end(this) - 1,
1143 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001144 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001145 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001146}
1147BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1148 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001149 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001150 OperandTraits<BranchInst>::op_end(this) - 3,
1151 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001152 Op<-1>() = IfTrue;
1153 Op<-2>() = IfFalse;
1154 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001155#ifndef NDEBUG
1156 AssertOK();
1157#endif
1158}
1159
1160BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001161 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001162 OperandTraits<BranchInst>::op_end(this) - 1,
1163 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001164 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001165 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001166}
1167
1168BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1169 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001170 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001171 OperandTraits<BranchInst>::op_end(this) - 3,
1172 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001173 Op<-1>() = IfTrue;
1174 Op<-2>() = IfFalse;
1175 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001176#ifndef NDEBUG
1177 AssertOK();
1178#endif
1179}
1180
1181
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001183 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001184 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1185 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001186 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001187 if (BI.getNumOperands() != 1) {
1188 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001189 Op<-3>() = BI.Op<-3>();
1190 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001192 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193}
1194
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001195void BranchInst::swapSuccessors() {
1196 assert(isConditional() &&
1197 "Cannot swap successors of an unconditional branch");
1198 Op<-1>().swap(Op<-2>());
1199
1200 // Update profile metadata if present and it matches our structural
1201 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001202 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001203 if (!ProfileData || ProfileData->getNumOperands() != 3)
1204 return;
1205
1206 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001207 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1208 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001209 setMetadata(LLVMContext::MD_prof,
1210 MDNode::get(ProfileData->getContext(), Ops));
1211}
1212
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001213BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1214 return getSuccessor(idx);
1215}
1216unsigned BranchInst::getNumSuccessorsV() const {
1217 return getNumSuccessors();
1218}
1219void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1220 setSuccessor(idx, B);
1221}
1222
1223
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001225// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001226//===----------------------------------------------------------------------===//
1227
Owen Andersonb6b25302009-07-14 23:09:55 +00001228static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001229 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001230 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001231 else {
1232 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001233 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001234 assert(Amt->getType()->isIntegerTy() &&
1235 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001236 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001237 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001238}
1239
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001240AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1241 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001242
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001243AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1244 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001245
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001246AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001247 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001248 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001249
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001250AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001251 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001252 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001253
Chris Lattner229907c2011-07-18 04:54:35 +00001254AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001255 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001256 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1257 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1258 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001259 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001260 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001261 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001262}
1263
Chris Lattner229907c2011-07-18 04:54:35 +00001264AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001265 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001266 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1267 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1268 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001269 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001270 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001271 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001272}
1273
Gordon Henriksen14a55692007-12-10 02:14:30 +00001274// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001275AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001276}
1277
Victor Hernandez8acf2952009-10-23 21:09:37 +00001278void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001279 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001280 assert(Align <= MaximumAlignment &&
1281 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001282 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1283 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001284 assert(getAlignment() == Align && "Alignment representation error!");
1285}
1286
Victor Hernandez8acf2952009-10-23 21:09:37 +00001287bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001288 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001289 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001290 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001291}
1292
Chris Lattner8b291e62008-11-26 02:54:17 +00001293/// isStaticAlloca - Return true if this alloca is in the entry block of the
1294/// function and is a constant size. If so, the code generator will fold it
1295/// into the prolog/epilog code, so it is basically free.
1296bool AllocaInst::isStaticAlloca() const {
1297 // Must be constant size.
1298 if (!isa<ConstantInt>(getArraySize())) return false;
1299
1300 // Must be in the entry block.
1301 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001302 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001303}
1304
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001305//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001306// LoadInst Implementation
1307//===----------------------------------------------------------------------===//
1308
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001309void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001310 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001311 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001312 assert(!(isAtomic() && getAlignment() == 0) &&
1313 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001314}
1315
Daniel Dunbar4975db62009-07-25 04:41:11 +00001316LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001317 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001318
Daniel Dunbar4975db62009-07-25 04:41:11 +00001319LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001320 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321
David Blaikie0c28fd72015-05-20 21:46:30 +00001322LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001323 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001324 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001325
1326LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1327 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001328 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001329
David Blaikieb7a029872015-04-17 19:56:21 +00001330LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001331 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001332 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001333 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001335LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001336 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001337 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001338}
1339
David Blaikie15d9a4c2015-04-06 20:59:48 +00001340LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001341 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001342 SynchronizationScope SynchScope, Instruction *InsertBef)
1343 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001344 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001345 setVolatile(isVolatile);
1346 setAlignment(Align);
1347 setAtomic(Order, SynchScope);
1348 AssertOK();
1349 setName(Name);
1350}
1351
1352LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1353 unsigned Align, AtomicOrdering Order,
1354 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001356 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001357 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001358 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001359 setAlignment(Align);
1360 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001361 AssertOK();
1362 setName(Name);
1363}
1364
Daniel Dunbar27096822009-08-11 18:11:15 +00001365LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1366 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1367 Load, Ptr, InsertBef) {
1368 setVolatile(false);
1369 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001370 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001371 AssertOK();
1372 if (Name && Name[0]) setName(Name);
1373}
1374
1375LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1376 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1377 Load, Ptr, InsertAE) {
1378 setVolatile(false);
1379 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001380 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001381 AssertOK();
1382 if (Name && Name[0]) setName(Name);
1383}
1384
David Blaikie0c28fd72015-05-20 21:46:30 +00001385LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001386 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001387 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1388 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001389 setVolatile(isVolatile);
1390 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001391 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001392 AssertOK();
1393 if (Name && Name[0]) setName(Name);
1394}
1395
1396LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1397 BasicBlock *InsertAE)
1398 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1399 Load, Ptr, InsertAE) {
1400 setVolatile(isVolatile);
1401 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001402 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001403 AssertOK();
1404 if (Name && Name[0]) setName(Name);
1405}
1406
Christopher Lamb84485702007-04-22 19:24:39 +00001407void LoadInst::setAlignment(unsigned Align) {
1408 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001409 assert(Align <= MaximumAlignment &&
1410 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001411 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001412 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001413 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001414}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001415
1416//===----------------------------------------------------------------------===//
1417// StoreInst Implementation
1418//===----------------------------------------------------------------------===//
1419
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001420void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001421 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001422 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001423 "Ptr must have pointer type!");
1424 assert(getOperand(0)->getType() ==
1425 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001426 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001427 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001428 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001429}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001430
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001431StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001432 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001433
1434StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001435 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001436
Misha Brukmanb1c93172005-04-21 23:48:37 +00001437StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001438 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001439 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001440
1441StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001442 BasicBlock *InsertAtEnd)
1443 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1444
1445StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1446 Instruction *InsertBefore)
1447 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1448 InsertBefore) {}
1449
1450StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1451 BasicBlock *InsertAtEnd)
1452 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1453 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001454
Misha Brukmanb1c93172005-04-21 23:48:37 +00001455StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001456 unsigned Align, AtomicOrdering Order,
1457 SynchronizationScope SynchScope,
1458 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001459 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001460 OperandTraits<StoreInst>::op_begin(this),
1461 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001462 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001463 Op<0>() = val;
1464 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001465 setVolatile(isVolatile);
1466 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001467 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001468 AssertOK();
1469}
1470
1471StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001472 unsigned Align, AtomicOrdering Order,
1473 SynchronizationScope SynchScope,
1474 BasicBlock *InsertAtEnd)
1475 : Instruction(Type::getVoidTy(val->getContext()), Store,
1476 OperandTraits<StoreInst>::op_begin(this),
1477 OperandTraits<StoreInst>::operands(this),
1478 InsertAtEnd) {
1479 Op<0>() = val;
1480 Op<1>() = addr;
1481 setVolatile(isVolatile);
1482 setAlignment(Align);
1483 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001484 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001485}
1486
Christopher Lamb84485702007-04-22 19:24:39 +00001487void StoreInst::setAlignment(unsigned Align) {
1488 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001489 assert(Align <= MaximumAlignment &&
1490 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001491 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001492 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001493 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001494}
1495
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001496//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001497// AtomicCmpXchgInst Implementation
1498//===----------------------------------------------------------------------===//
1499
1500void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001501 AtomicOrdering SuccessOrdering,
1502 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001503 SynchronizationScope SynchScope) {
1504 Op<0>() = Ptr;
1505 Op<1>() = Cmp;
1506 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001507 setSuccessOrdering(SuccessOrdering);
1508 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001509 setSynchScope(SynchScope);
1510
1511 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1512 "All operands must be non-null!");
1513 assert(getOperand(0)->getType()->isPointerTy() &&
1514 "Ptr must have pointer type!");
1515 assert(getOperand(1)->getType() ==
1516 cast<PointerType>(getOperand(0)->getType())->getElementType()
1517 && "Ptr must be a pointer to Cmp type!");
1518 assert(getOperand(2)->getType() ==
1519 cast<PointerType>(getOperand(0)->getType())->getElementType()
1520 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001521 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001522 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001523 assert(FailureOrdering != NotAtomic &&
1524 "AtomicCmpXchg instructions must be atomic!");
1525 assert(SuccessOrdering >= FailureOrdering &&
1526 "AtomicCmpXchg success ordering must be at least as strong as fail");
1527 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1528 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001529}
1530
1531AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001532 AtomicOrdering SuccessOrdering,
1533 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001534 SynchronizationScope SynchScope,
1535 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001536 : Instruction(
1537 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1538 nullptr),
1539 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1540 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001541 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001542}
1543
1544AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001545 AtomicOrdering SuccessOrdering,
1546 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001547 SynchronizationScope SynchScope,
1548 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001549 : Instruction(
1550 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1551 nullptr),
1552 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1553 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001554 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001555}
Tim Northover420a2162014-06-13 14:24:07 +00001556
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001557//===----------------------------------------------------------------------===//
1558// AtomicRMWInst Implementation
1559//===----------------------------------------------------------------------===//
1560
1561void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1562 AtomicOrdering Ordering,
1563 SynchronizationScope SynchScope) {
1564 Op<0>() = Ptr;
1565 Op<1>() = Val;
1566 setOperation(Operation);
1567 setOrdering(Ordering);
1568 setSynchScope(SynchScope);
1569
1570 assert(getOperand(0) && getOperand(1) &&
1571 "All operands must be non-null!");
1572 assert(getOperand(0)->getType()->isPointerTy() &&
1573 "Ptr must have pointer type!");
1574 assert(getOperand(1)->getType() ==
1575 cast<PointerType>(getOperand(0)->getType())->getElementType()
1576 && "Ptr must be a pointer to Val type!");
1577 assert(Ordering != NotAtomic &&
1578 "AtomicRMW instructions must be atomic!");
1579}
1580
1581AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1582 AtomicOrdering Ordering,
1583 SynchronizationScope SynchScope,
1584 Instruction *InsertBefore)
1585 : Instruction(Val->getType(), AtomicRMW,
1586 OperandTraits<AtomicRMWInst>::op_begin(this),
1587 OperandTraits<AtomicRMWInst>::operands(this),
1588 InsertBefore) {
1589 Init(Operation, Ptr, Val, Ordering, SynchScope);
1590}
1591
1592AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1593 AtomicOrdering Ordering,
1594 SynchronizationScope SynchScope,
1595 BasicBlock *InsertAtEnd)
1596 : Instruction(Val->getType(), AtomicRMW,
1597 OperandTraits<AtomicRMWInst>::op_begin(this),
1598 OperandTraits<AtomicRMWInst>::operands(this),
1599 InsertAtEnd) {
1600 Init(Operation, Ptr, Val, Ordering, SynchScope);
1601}
1602
1603//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001604// FenceInst Implementation
1605//===----------------------------------------------------------------------===//
1606
1607FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1608 SynchronizationScope SynchScope,
1609 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001610 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001611 setOrdering(Ordering);
1612 setSynchScope(SynchScope);
1613}
1614
1615FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1616 SynchronizationScope SynchScope,
1617 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001618 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001619 setOrdering(Ordering);
1620 setSynchScope(SynchScope);
1621}
1622
1623//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001624// GetElementPtrInst Implementation
1625//===----------------------------------------------------------------------===//
1626
Jay Foadd1b78492011-07-25 09:48:08 +00001627void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001628 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001629 assert(getNumOperands() == 1 + IdxList.size() &&
1630 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001631 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001632 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001633 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001634}
1635
Gabor Greiff6caff662008-05-10 08:32:32 +00001636GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001637 : Instruction(GEPI.getType(), GetElementPtr,
1638 OperandTraits<GetElementPtrInst>::op_end(this) -
1639 GEPI.getNumOperands(),
1640 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001641 SourceElementType(GEPI.SourceElementType),
1642 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001643 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001644 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001645}
1646
Chris Lattner6090a422009-03-09 04:46:40 +00001647/// getIndexedType - Returns the type of the element that would be accessed with
1648/// a gep instruction with the specified parameters.
1649///
1650/// The Idxs pointer should point to a continuous piece of memory containing the
1651/// indices, either as Value* or uint64_t.
1652///
1653/// A null type is returned if the indices are invalid for the specified
1654/// pointer type.
1655///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001656template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001657static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001658 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001659 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001660 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001661
Chris Lattner6090a422009-03-09 04:46:40 +00001662 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001663 // it cannot be 'stepped over'.
1664 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001665 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001666
Dan Gohman1ecaf452008-05-31 00:58:22 +00001667 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001668 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001669 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001670 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001671 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001672 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001673 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001674 }
Craig Topperc6207612014-04-09 06:08:46 +00001675 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676}
1677
David Blaikied288fb82015-03-30 21:41:43 +00001678Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1679 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001680}
1681
David Blaikied288fb82015-03-30 21:41:43 +00001682Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001683 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001684 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001685}
1686
David Blaikied288fb82015-03-30 21:41:43 +00001687Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1688 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001689}
1690
Chris Lattner45f15572007-04-14 00:12:57 +00001691/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1692/// zeros. If so, the result pointer and the first operand have the same
1693/// value, just potentially different types.
1694bool GetElementPtrInst::hasAllZeroIndices() const {
1695 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1696 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1697 if (!CI->isZero()) return false;
1698 } else {
1699 return false;
1700 }
1701 }
1702 return true;
1703}
1704
Chris Lattner27058292007-04-27 20:35:56 +00001705/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1706/// constant integers. If so, the result pointer and the first operand have
1707/// a constant offset between them.
1708bool GetElementPtrInst::hasAllConstantIndices() const {
1709 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1710 if (!isa<ConstantInt>(getOperand(i)))
1711 return false;
1712 }
1713 return true;
1714}
1715
Dan Gohman1b849082009-09-07 23:54:19 +00001716void GetElementPtrInst::setIsInBounds(bool B) {
1717 cast<GEPOperator>(this)->setIsInBounds(B);
1718}
Chris Lattner45f15572007-04-14 00:12:57 +00001719
Nick Lewycky28a5f252009-09-27 21:33:04 +00001720bool GetElementPtrInst::isInBounds() const {
1721 return cast<GEPOperator>(this)->isInBounds();
1722}
1723
Chandler Carruth1e140532012-12-11 10:29:10 +00001724bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1725 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001726 // Delegate to the generic GEPOperator implementation.
1727 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001728}
1729
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001730//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001731// ExtractElementInst Implementation
1732//===----------------------------------------------------------------------===//
1733
1734ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001735 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001736 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001737 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001738 ExtractElement,
1739 OperandTraits<ExtractElementInst>::op_begin(this),
1740 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001741 assert(isValidOperands(Val, Index) &&
1742 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001743 Op<0>() = Val;
1744 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001745 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001746}
1747
1748ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001749 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001750 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001751 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001752 ExtractElement,
1753 OperandTraits<ExtractElementInst>::op_begin(this),
1754 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001755 assert(isValidOperands(Val, Index) &&
1756 "Invalid extractelement instruction operands!");
1757
Gabor Greif2d3024d2008-05-26 21:33:52 +00001758 Op<0>() = Val;
1759 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001760 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001761}
1762
Chris Lattner65511ff2006-10-05 06:24:58 +00001763
Chris Lattner54865b32006-04-08 04:05:48 +00001764bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001765 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001766 return false;
1767 return true;
1768}
1769
1770
Robert Bocchino23004482006-01-10 19:05:34 +00001771//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001772// InsertElementInst Implementation
1773//===----------------------------------------------------------------------===//
1774
Chris Lattner54865b32006-04-08 04:05:48 +00001775InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001776 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001777 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001778 : Instruction(Vec->getType(), InsertElement,
1779 OperandTraits<InsertElementInst>::op_begin(this),
1780 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001781 assert(isValidOperands(Vec, Elt, Index) &&
1782 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001783 Op<0>() = Vec;
1784 Op<1>() = Elt;
1785 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001786 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001787}
1788
Chris Lattner54865b32006-04-08 04:05:48 +00001789InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001790 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001791 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001792 : Instruction(Vec->getType(), InsertElement,
1793 OperandTraits<InsertElementInst>::op_begin(this),
1794 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001795 assert(isValidOperands(Vec, Elt, Index) &&
1796 "Invalid insertelement instruction operands!");
1797
Gabor Greif2d3024d2008-05-26 21:33:52 +00001798 Op<0>() = Vec;
1799 Op<1>() = Elt;
1800 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001801 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001802}
1803
Chris Lattner54865b32006-04-08 04:05:48 +00001804bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1805 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001806 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001807 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001808
Reid Spencerd84d35b2007-02-15 02:26:10 +00001809 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001810 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001811
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001812 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001813 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001814 return true;
1815}
1816
1817
Robert Bocchinoca27f032006-01-17 20:07:22 +00001818//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001819// ShuffleVectorInst Implementation
1820//===----------------------------------------------------------------------===//
1821
1822ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001823 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001824 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001825: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001826 cast<VectorType>(Mask->getType())->getNumElements()),
1827 ShuffleVector,
1828 OperandTraits<ShuffleVectorInst>::op_begin(this),
1829 OperandTraits<ShuffleVectorInst>::operands(this),
1830 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001831 assert(isValidOperands(V1, V2, Mask) &&
1832 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001833 Op<0>() = V1;
1834 Op<1>() = V2;
1835 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001836 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001837}
1838
1839ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001840 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001841 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001842: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1843 cast<VectorType>(Mask->getType())->getNumElements()),
1844 ShuffleVector,
1845 OperandTraits<ShuffleVectorInst>::op_begin(this),
1846 OperandTraits<ShuffleVectorInst>::operands(this),
1847 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001848 assert(isValidOperands(V1, V2, Mask) &&
1849 "Invalid shuffle vector instruction operands!");
1850
Gabor Greif2d3024d2008-05-26 21:33:52 +00001851 Op<0>() = V1;
1852 Op<1>() = V2;
1853 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001854 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001855}
1856
Mon P Wang25f01062008-11-10 04:46:22 +00001857bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001858 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001859 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001860 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001861 return false;
1862
Chris Lattner1dcb6542012-01-25 23:49:49 +00001863 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001864 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001865 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001866 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001867
1868 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001869 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1870 return true;
1871
Nate Begeman60a31c32010-08-13 00:16:46 +00001872 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001873 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001874 for (Value *Op : MV->operands()) {
1875 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001876 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001877 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001878 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001879 return false;
1880 }
1881 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001883 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001884
1885 if (const ConstantDataSequential *CDS =
1886 dyn_cast<ConstantDataSequential>(Mask)) {
1887 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1888 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1889 if (CDS->getElementAsInteger(i) >= V1Size*2)
1890 return false;
1891 return true;
1892 }
1893
1894 // The bitcode reader can create a place holder for a forward reference
1895 // used as the shuffle mask. When this occurs, the shuffle mask will
1896 // fall into this case and fail. To avoid this error, do this bit of
1897 // ugliness to allow such a mask pass.
1898 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1899 if (CE->getOpcode() == Instruction::UserOp1)
1900 return true;
1901
1902 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001903}
1904
Chris Lattnerf724e342008-03-02 05:28:33 +00001905/// getMaskValue - Return the index from the shuffle mask for the specified
1906/// output result. This is either -1 if the element is undef or a number less
1907/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001908int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1909 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1910 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001911 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001912 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001913 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001914 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001915 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001916}
1917
Chris Lattner1dcb6542012-01-25 23:49:49 +00001918/// getShuffleMask - Return the full mask for this instruction, where each
1919/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001920void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1921 SmallVectorImpl<int> &Result) {
1922 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001923
Chris Lattnercf129702012-01-26 02:51:13 +00001924 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001925 for (unsigned i = 0; i != NumElts; ++i)
1926 Result.push_back(CDS->getElementAsInteger(i));
1927 return;
1928 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001929 for (unsigned i = 0; i != NumElts; ++i) {
1930 Constant *C = Mask->getAggregateElement(i);
1931 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001932 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001933 }
1934}
1935
1936
Dan Gohman12fce772008-05-15 19:50:34 +00001937//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001938// InsertValueInst Class
1939//===----------------------------------------------------------------------===//
1940
Jay Foad57aa6362011-07-13 10:26:04 +00001941void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1942 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001943 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001944
1945 // There's no fundamental reason why we require at least one index
1946 // (other than weirdness with &*IdxBegin being invalid; see
1947 // getelementptr's init routine for example). But there's no
1948 // present need to support it.
1949 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1950
1951 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001952 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001953 Op<0>() = Agg;
1954 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001955
Jay Foad57aa6362011-07-13 10:26:04 +00001956 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001957 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001958}
1959
1960InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001961 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001962 OperandTraits<InsertValueInst>::op_begin(this), 2),
1963 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001964 Op<0>() = IVI.getOperand(0);
1965 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001966 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001967}
1968
1969//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001970// ExtractValueInst Class
1971//===----------------------------------------------------------------------===//
1972
Jay Foad57aa6362011-07-13 10:26:04 +00001973void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001974 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001975
Jay Foad57aa6362011-07-13 10:26:04 +00001976 // There's no fundamental reason why we require at least one index.
1977 // But there's no present need to support it.
1978 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001979
Jay Foad57aa6362011-07-13 10:26:04 +00001980 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001981 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001982}
1983
1984ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001985 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001986 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001987 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001988}
1989
Dan Gohman12fce772008-05-15 19:50:34 +00001990// getIndexedType - Returns the type of the element that would be extracted
1991// with an extractvalue instruction with the specified parameters.
1992//
1993// A null type is returned if the indices are invalid for the specified
1994// pointer type.
1995//
Chris Lattner229907c2011-07-18 04:54:35 +00001996Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001997 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001998 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001999 // We can't use CompositeType::indexValid(Index) here.
2000 // indexValid() always returns true for arrays because getelementptr allows
2001 // out-of-bounds indices. Since we don't allow those for extractvalue and
2002 // insertvalue we need to check array indexing manually.
2003 // Since the only other types we can index into are struct types it's just
2004 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002005 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002006 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002007 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002008 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002009 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002010 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002011 } else {
2012 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002013 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002014 }
2015
2016 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002017 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002018 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002019}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002020
2021//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002022// BinaryOperator Class
2023//===----------------------------------------------------------------------===//
2024
Chris Lattner2195fc42007-02-24 00:55:48 +00002025BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002026 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002027 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002028 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002029 OperandTraits<BinaryOperator>::op_begin(this),
2030 OperandTraits<BinaryOperator>::operands(this),
2031 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002032 Op<0>() = S1;
2033 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002034 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002035 setName(Name);
2036}
2037
2038BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002039 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002040 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002041 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002042 OperandTraits<BinaryOperator>::op_begin(this),
2043 OperandTraits<BinaryOperator>::operands(this),
2044 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002045 Op<0>() = S1;
2046 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002047 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002048 setName(Name);
2049}
2050
2051
2052void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002053 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002054 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002055 assert(LHS->getType() == RHS->getType() &&
2056 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002057#ifndef NDEBUG
2058 switch (iType) {
2059 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002060 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002061 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002062 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002063 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002064 "Tried to create an integer operation on a non-integer type!");
2065 break;
2066 case FAdd: case FSub:
2067 case FMul:
2068 assert(getType() == LHS->getType() &&
2069 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002070 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002071 "Tried to create a floating-point operation on a "
2072 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002073 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002074 case UDiv:
2075 case SDiv:
2076 assert(getType() == LHS->getType() &&
2077 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002078 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002079 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002080 "Incorrect operand type (not integer) for S/UDIV");
2081 break;
2082 case FDiv:
2083 assert(getType() == LHS->getType() &&
2084 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002085 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002086 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002087 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002088 case URem:
2089 case SRem:
2090 assert(getType() == LHS->getType() &&
2091 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002092 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002093 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002094 "Incorrect operand type (not integer) for S/UREM");
2095 break;
2096 case FRem:
2097 assert(getType() == LHS->getType() &&
2098 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002099 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002100 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002101 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002102 case Shl:
2103 case LShr:
2104 case AShr:
2105 assert(getType() == LHS->getType() &&
2106 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002107 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002108 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002109 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002110 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002111 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 case And: case Or:
2113 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002114 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002116 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002117 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002118 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002119 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002120 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002121 default:
2122 break;
2123 }
2124#endif
2125}
2126
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002127BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002128 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129 Instruction *InsertBefore) {
2130 assert(S1->getType() == S2->getType() &&
2131 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002132 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002133}
2134
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002135BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002136 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002137 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002138 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002139 InsertAtEnd->getInstList().push_back(Res);
2140 return Res;
2141}
2142
Dan Gohman5476cfd2009-08-12 16:23:25 +00002143BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002144 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002145 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002146 return new BinaryOperator(Instruction::Sub,
2147 zero, Op,
2148 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002149}
2150
Dan Gohman5476cfd2009-08-12 16:23:25 +00002151BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002153 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002154 return new BinaryOperator(Instruction::Sub,
2155 zero, Op,
2156 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002157}
2158
Dan Gohman4ab44202009-12-18 02:58:50 +00002159BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2160 Instruction *InsertBefore) {
2161 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2162 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2163}
2164
2165BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2166 BasicBlock *InsertAtEnd) {
2167 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2168 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2169}
2170
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002171BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2172 Instruction *InsertBefore) {
2173 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2174 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2175}
2176
2177BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2178 BasicBlock *InsertAtEnd) {
2179 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2180 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2181}
2182
Dan Gohman5476cfd2009-08-12 16:23:25 +00002183BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002184 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002185 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002186 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002187 Op->getType(), Name, InsertBefore);
2188}
2189
Dan Gohman5476cfd2009-08-12 16:23:25 +00002190BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002191 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002192 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002193 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002194 Op->getType(), Name, InsertAtEnd);
2195}
2196
Dan Gohman5476cfd2009-08-12 16:23:25 +00002197BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002199 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002200 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002201 Op->getType(), Name, InsertBefore);
2202}
2203
Dan Gohman5476cfd2009-08-12 16:23:25 +00002204BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002205 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002206 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002207 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002208 Op->getType(), Name, InsertAtEnd);
2209}
2210
2211
2212// isConstantAllOnes - Helper function for several functions below
2213static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002214 if (const Constant *C = dyn_cast<Constant>(V))
2215 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002216 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002217}
2218
Owen Andersonbb2501b2009-07-13 22:18:28 +00002219bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002220 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2221 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002222 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2223 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002224 return false;
2225}
2226
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002227bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002228 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2229 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002230 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2231 if (!IgnoreZeroSign)
2232 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2233 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2234 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002235 return false;
2236}
2237
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002238bool BinaryOperator::isNot(const Value *V) {
2239 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2240 return (Bop->getOpcode() == Instruction::Xor &&
2241 (isConstantAllOnes(Bop->getOperand(1)) ||
2242 isConstantAllOnes(Bop->getOperand(0))));
2243 return false;
2244}
2245
Chris Lattner2c7d1772005-04-24 07:28:37 +00002246Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002247 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002248}
2249
Chris Lattner2c7d1772005-04-24 07:28:37 +00002250const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2251 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002252}
2253
Dan Gohmana5b96452009-06-04 22:49:04 +00002254Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002255 return cast<BinaryOperator>(BinOp)->getOperand(1);
2256}
2257
2258const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2259 return getFNegArgument(const_cast<Value*>(BinOp));
2260}
2261
Chris Lattner2c7d1772005-04-24 07:28:37 +00002262Value *BinaryOperator::getNotArgument(Value *BinOp) {
2263 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2264 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2265 Value *Op0 = BO->getOperand(0);
2266 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002267 if (isConstantAllOnes(Op0)) return Op1;
2268
2269 assert(isConstantAllOnes(Op1));
2270 return Op0;
2271}
2272
Chris Lattner2c7d1772005-04-24 07:28:37 +00002273const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2274 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002275}
2276
2277
2278// swapOperands - Exchange the two operands to this instruction. This
2279// instruction is safe to use on any binary instruction and does not
2280// modify the semantics of the instruction. If the instruction is
2281// order dependent (SetLT f.e.) the opcode is changed.
2282//
2283bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002284 if (!isCommutative())
2285 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002286 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002287 return false;
2288}
2289
Dan Gohman1b849082009-09-07 23:54:19 +00002290void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2291 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2292}
2293
2294void BinaryOperator::setHasNoSignedWrap(bool b) {
2295 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2296}
2297
2298void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002299 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002300}
2301
Nick Lewycky28a5f252009-09-27 21:33:04 +00002302bool BinaryOperator::hasNoUnsignedWrap() const {
2303 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2304}
2305
2306bool BinaryOperator::hasNoSignedWrap() const {
2307 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2308}
2309
2310bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002311 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002312}
2313
Sanjay Patela982d992014-09-03 01:06:50 +00002314void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002315 // Copy the wrapping flags.
2316 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2317 setHasNoSignedWrap(OB->hasNoSignedWrap());
2318 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2319 }
2320
2321 // Copy the exact flag.
2322 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2323 setIsExact(PE->isExact());
2324
2325 // Copy the fast-math flags.
2326 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002327 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002328}
2329
Sanjay Patela982d992014-09-03 01:06:50 +00002330void BinaryOperator::andIRFlags(const Value *V) {
2331 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2332 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2333 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2334 }
2335
2336 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2337 setIsExact(isExact() & PE->isExact());
2338
2339 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2340 FastMathFlags FM = getFastMathFlags();
2341 FM &= FP->getFastMathFlags();
2342 copyFastMathFlags(FM);
2343 }
2344}
2345
2346
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002347//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002348// FPMathOperator Class
2349//===----------------------------------------------------------------------===//
2350
2351/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2352/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002353/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002354float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002355 const MDNode *MD =
2356 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002357 if (!MD)
2358 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002359 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002360 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002361}
2362
2363
2364//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002365// CastInst Class
2366//===----------------------------------------------------------------------===//
2367
David Blaikie3a15e142011-12-01 08:00:17 +00002368void CastInst::anchor() {}
2369
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370// Just determine if this cast only deals with integral->integral conversion.
2371bool CastInst::isIntegerCast() const {
2372 switch (getOpcode()) {
2373 default: return false;
2374 case Instruction::ZExt:
2375 case Instruction::SExt:
2376 case Instruction::Trunc:
2377 return true;
2378 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002379 return getOperand(0)->getType()->isIntegerTy() &&
2380 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002382}
2383
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384bool CastInst::isLosslessCast() const {
2385 // Only BitCast can be lossless, exit fast if we're not BitCast
2386 if (getOpcode() != Instruction::BitCast)
2387 return false;
2388
2389 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002390 Type* SrcTy = getOperand(0)->getType();
2391 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 if (SrcTy == DstTy)
2393 return true;
2394
Reid Spencer8d9336d2006-12-31 05:26:44 +00002395 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002396 if (SrcTy->isPointerTy())
2397 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 return false; // Other types have no identity values
2399}
2400
2401/// This function determines if the CastInst does not require any bits to be
2402/// changed in order to effect the cast. Essentially, it identifies cases where
2403/// no code gen is necessary for the cast, hence the name no-op cast. For
2404/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002405/// # bitcast i32* %x to i8*
2406/// # bitcast <2 x i32> %x to <4 x i16>
2407/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002408/// @brief Determine if the described cast is a no-op.
2409bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002410 Type *SrcTy,
2411 Type *DestTy,
2412 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002413 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002414 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 case Instruction::Trunc:
2416 case Instruction::ZExt:
2417 case Instruction::SExt:
2418 case Instruction::FPTrunc:
2419 case Instruction::FPExt:
2420 case Instruction::UIToFP:
2421 case Instruction::SIToFP:
2422 case Instruction::FPToUI:
2423 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002424 case Instruction::AddrSpaceCast:
2425 // TODO: Target informations may give a more accurate answer here.
2426 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 case Instruction::BitCast:
2428 return true; // BitCast never modifies bits.
2429 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002430 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002431 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002433 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002434 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 }
2436}
2437
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002438/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002439bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002440 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2441}
2442
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002443bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002444 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002445 if (getOpcode() == Instruction::PtrToInt)
2446 PtrOpTy = getOperand(0)->getType();
2447 else if (getOpcode() == Instruction::IntToPtr)
2448 PtrOpTy = getType();
2449
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002450 Type *IntPtrTy =
2451 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002452
2453 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2454}
2455
2456/// This function determines if a pair of casts can be eliminated and what
2457/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458/// instructions like this:
2459/// * %F = firstOpcode SrcTy %x to MidTy
2460/// * %S = secondOpcode MidTy %F to DstTy
2461/// The function returns a resultOpcode so these two casts can be replaced with:
2462/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2463/// If no such cast is permited, the function returns 0.
2464unsigned CastInst::isEliminableCastPair(
2465 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002466 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2467 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 // Define the 144 possibilities for these two cast instructions. The values
2469 // in this matrix determine what to do in a given situation and select the
2470 // case in the switch below. The rows correspond to firstOp, the columns
2471 // correspond to secondOp. In looking at the table below, keep in mind
2472 // the following cast properties:
2473 //
2474 // Size Compare Source Destination
2475 // Operator Src ? Size Type Sign Type Sign
2476 // -------- ------------ ------------------- ---------------------
2477 // TRUNC > Integer Any Integral Any
2478 // ZEXT < Integral Unsigned Integer Any
2479 // SEXT < Integral Signed Integer Any
2480 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002481 // FPTOSI n/a FloatPt n/a Integral Signed
2482 // UITOFP n/a Integral Unsigned FloatPt n/a
2483 // SITOFP n/a Integral Signed FloatPt n/a
2484 // FPTRUNC > FloatPt n/a FloatPt n/a
2485 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 // PTRTOINT n/a Pointer n/a Integral Unsigned
2487 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 // BITCAST = FirstClass n/a FirstClass n/a
2489 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002490 //
2491 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002492 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2493 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002494 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002495 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002497 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002498 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2500 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002501 // T F F U S F F P I B A -+
2502 // R Z S P P I I T P 2 N T S |
2503 // U E E 2 2 2 2 R E I T C C +- secondOp
2504 // N X X U S F F N X N 2 V V |
2505 // C T T I I P P C T T P T T -+
2506 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002507 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002508 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2509 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2510 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2511 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2512 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002513 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002514 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2515 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2516 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2517 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2518 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002520
Chris Lattner25eea4d2010-07-12 01:19:22 +00002521 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002522 // merging. However, bitcast of A->B->A are allowed.
2523 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2524 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2525 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2526
2527 // Check if any of the bitcasts convert scalars<->vectors.
2528 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2529 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2530 // Unless we are bitcasing to the original type, disallow optimizations.
2531 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532
2533 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2534 [secondOp-Instruction::CastOpsBegin];
2535 switch (ElimCase) {
2536 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 return 0;
2539 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 return firstOp;
2542 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002543 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 return secondOp;
2545 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002546 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002547 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002548 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002549 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 return firstOp;
2551 return 0;
2552 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002553 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002554 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002555 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556 return firstOp;
2557 return 0;
2558 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002559 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002560 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002561 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 return secondOp;
2563 return 0;
2564 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002565 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002566 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002567 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568 return secondOp;
2569 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002570 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002571 // Cannot simplify if address spaces are different!
2572 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2573 return 0;
2574
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002575 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002576 // We can still fold this without knowing the actual sizes as long we
2577 // know that the intermediate pointer is the largest possible
2578 // pointer size.
2579 // FIXME: Is this always true?
2580 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002581 return Instruction::BitCast;
2582
2583 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002584 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002585 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002586 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587 if (MidSize >= PtrSize)
2588 return Instruction::BitCast;
2589 return 0;
2590 }
2591 case 8: {
2592 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2593 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2594 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002595 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2596 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597 if (SrcSize == DstSize)
2598 return Instruction::BitCast;
2599 else if (SrcSize < DstSize)
2600 return firstOp;
2601 return secondOp;
2602 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002603 case 9:
2604 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605 return Instruction::ZExt;
2606 case 10:
2607 // fpext followed by ftrunc is allowed if the bit size returned to is
2608 // the same as the original, in which case its just a bitcast
2609 if (SrcTy == DstTy)
2610 return Instruction::BitCast;
2611 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002612 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002614 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002615 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002616 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002617 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2618 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 if (SrcSize <= PtrSize && SrcSize == DstSize)
2620 return Instruction::BitCast;
2621 return 0;
2622 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002623 case 12: {
2624 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2625 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2626 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2627 return Instruction::AddrSpaceCast;
2628 return Instruction::BitCast;
2629 }
2630 case 13:
2631 // FIXME: this state can be merged with (1), but the following assert
2632 // is useful to check the correcteness of the sequence due to semantic
2633 // change of bitcast.
2634 assert(
2635 SrcTy->isPtrOrPtrVectorTy() &&
2636 MidTy->isPtrOrPtrVectorTy() &&
2637 DstTy->isPtrOrPtrVectorTy() &&
2638 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2639 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2640 "Illegal addrspacecast, bitcast sequence!");
2641 // Allowed, use first cast's opcode
2642 return firstOp;
2643 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002644 // bitcast, addrspacecast -> addrspacecast if the element type of
2645 // bitcast's source is the same as that of addrspacecast's destination.
2646 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2647 return Instruction::AddrSpaceCast;
2648 return 0;
2649
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002650 case 15:
2651 // FIXME: this state can be merged with (1), but the following assert
2652 // is useful to check the correcteness of the sequence due to semantic
2653 // change of bitcast.
2654 assert(
2655 SrcTy->isIntOrIntVectorTy() &&
2656 MidTy->isPtrOrPtrVectorTy() &&
2657 DstTy->isPtrOrPtrVectorTy() &&
2658 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2659 "Illegal inttoptr, bitcast sequence!");
2660 // Allowed, use first cast's opcode
2661 return firstOp;
2662 case 16:
2663 // FIXME: this state can be merged with (2), but the following assert
2664 // is useful to check the correcteness of the sequence due to semantic
2665 // change of bitcast.
2666 assert(
2667 SrcTy->isPtrOrPtrVectorTy() &&
2668 MidTy->isPtrOrPtrVectorTy() &&
2669 DstTy->isIntOrIntVectorTy() &&
2670 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2671 "Illegal bitcast, ptrtoint sequence!");
2672 // Allowed, use second cast's opcode
2673 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002674 case 17:
2675 // (sitofp (zext x)) -> (uitofp x)
2676 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002678 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002680 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 default:
Craig Topperc514b542012-02-05 22:14:15 +00002682 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684}
2685
Chris Lattner229907c2011-07-18 04:54:35 +00002686CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002687 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002688 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689 // Construct and return the appropriate CastInst subclass
2690 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002691 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2692 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2693 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2694 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2695 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2696 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2697 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2698 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2699 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2700 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2701 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2702 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2703 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2704 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706}
2707
Chris Lattner229907c2011-07-18 04:54:35 +00002708CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002709 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002710 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002711 // Construct and return the appropriate CastInst subclass
2712 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002713 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2714 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2715 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2716 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2717 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2718 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2719 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2720 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2721 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2722 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2723 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2724 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2725 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2726 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728}
2729
Chris Lattner229907c2011-07-18 04:54:35 +00002730CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002731 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002732 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002733 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002734 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2735 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002736}
2737
Chris Lattner229907c2011-07-18 04:54:35 +00002738CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002739 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002740 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002741 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002742 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2743 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002744}
2745
Chris Lattner229907c2011-07-18 04:54:35 +00002746CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002747 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002748 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002749 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002750 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2751 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002752}
2753
Chris Lattner229907c2011-07-18 04:54:35 +00002754CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002755 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002756 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002757 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002758 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2759 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002760}
2761
Chris Lattner229907c2011-07-18 04:54:35 +00002762CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002763 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002764 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002765 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002766 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2767 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002768}
2769
Chris Lattner229907c2011-07-18 04:54:35 +00002770CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002771 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002772 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002773 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002774 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2775 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002776}
2777
Chris Lattner229907c2011-07-18 04:54:35 +00002778CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002779 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002780 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002781 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2782 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2783 "Invalid cast");
2784 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002785 assert((!Ty->isVectorTy() ||
2786 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002787 "Invalid cast");
2788
Matt Arsenault065ced92013-07-31 00:17:33 +00002789 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002790 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002791
Matt Arsenault740980e2014-07-14 17:24:35 +00002792 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002793}
2794
2795/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002796CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2797 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002798 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002799 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2800 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002801 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002802 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002803 assert((!Ty->isVectorTy() ||
2804 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002805 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002806
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002807 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002808 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002809
Matt Arsenault740980e2014-07-14 17:24:35 +00002810 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2811}
2812
2813CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2814 Value *S, Type *Ty,
2815 const Twine &Name,
2816 BasicBlock *InsertAtEnd) {
2817 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2818 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2819
2820 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2821 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2822
2823 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2824}
2825
2826CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2827 Value *S, Type *Ty,
2828 const Twine &Name,
2829 Instruction *InsertBefore) {
2830 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2831 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2832
2833 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002834 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2835
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002836 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002837}
2838
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002839CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2840 const Twine &Name,
2841 Instruction *InsertBefore) {
2842 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2843 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2844 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2845 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2846
2847 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2848}
2849
Matt Arsenault740980e2014-07-14 17:24:35 +00002850CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002851 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002852 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002853 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002854 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002855 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2856 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002857 Instruction::CastOps opcode =
2858 (SrcBits == DstBits ? Instruction::BitCast :
2859 (SrcBits > DstBits ? Instruction::Trunc :
2860 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002861 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002862}
2863
Chris Lattner229907c2011-07-18 04:54:35 +00002864CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002865 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002866 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002867 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002868 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002869 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2870 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002871 Instruction::CastOps opcode =
2872 (SrcBits == DstBits ? Instruction::BitCast :
2873 (SrcBits > DstBits ? Instruction::Trunc :
2874 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002875 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002876}
2877
Chris Lattner229907c2011-07-18 04:54:35 +00002878CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002879 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002880 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002881 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002882 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002883 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2884 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002885 Instruction::CastOps opcode =
2886 (SrcBits == DstBits ? Instruction::BitCast :
2887 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002888 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002889}
2890
Chris Lattner229907c2011-07-18 04:54:35 +00002891CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002892 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002893 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002894 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002895 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002896 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2897 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002898 Instruction::CastOps opcode =
2899 (SrcBits == DstBits ? Instruction::BitCast :
2900 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002901 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002902}
2903
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002904// Check whether it is valid to call getCastOpcode for these types.
2905// This routine must be kept in sync with getCastOpcode.
2906bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2907 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2908 return false;
2909
2910 if (SrcTy == DestTy)
2911 return true;
2912
2913 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2914 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2915 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2916 // An element by element cast. Valid if casting the elements is valid.
2917 SrcTy = SrcVecTy->getElementType();
2918 DestTy = DestVecTy->getElementType();
2919 }
2920
2921 // Get the bit sizes, we'll need these
2922 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2923 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2924
2925 // Run through the possibilities ...
2926 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002927 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002928 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002929 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002930 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002931 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002932 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002933 // Casting from something else
2934 return SrcTy->isPointerTy();
2935 }
2936 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2937 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002938 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002939 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002940 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002941 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002942 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002943 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002944 return false;
2945 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002946 if (DestTy->isVectorTy()) // Casting to vector
2947 return DestBits == SrcBits;
2948 if (DestTy->isPointerTy()) { // Casting to pointer
2949 if (SrcTy->isPointerTy()) // Casting from pointer
2950 return true;
2951 return SrcTy->isIntegerTy(); // Casting from integral
2952 }
2953 if (DestTy->isX86_MMXTy()) {
2954 if (SrcTy->isVectorTy())
2955 return DestBits == SrcBits; // 64-bit vector to MMX
2956 return false;
2957 } // Casting to something else
2958 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002959}
2960
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002961bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2962 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2963 return false;
2964
2965 if (SrcTy == DestTy)
2966 return true;
2967
2968 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2969 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2970 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2971 // An element by element cast. Valid if casting the elements is valid.
2972 SrcTy = SrcVecTy->getElementType();
2973 DestTy = DestVecTy->getElementType();
2974 }
2975 }
2976 }
2977
2978 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2979 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2980 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2981 }
2982 }
2983
2984 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2985 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2986
2987 // Could still have vectors of pointers if the number of elements doesn't
2988 // match
2989 if (SrcBits == 0 || DestBits == 0)
2990 return false;
2991
2992 if (SrcBits != DestBits)
2993 return false;
2994
2995 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2996 return false;
2997
2998 return true;
2999}
3000
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003001bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003002 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003003 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3004 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003005 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003006 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3007 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003008 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003009
3010 return isBitCastable(SrcTy, DestTy);
3011}
3012
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003013// Provide a way to get a "cast" where the cast opcode is inferred from the
3014// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003015// logic in the castIsValid function below. This axiom should hold:
3016// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3017// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003018// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003019// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003020Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003021CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003022 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3023 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024
Duncan Sands55e50902008-01-06 10:12:28 +00003025 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3026 "Only first class types are castable!");
3027
Duncan Sandsa8514532011-05-18 07:13:41 +00003028 if (SrcTy == DestTy)
3029 return BitCast;
3030
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003031 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003032 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3033 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003034 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3035 // An element by element cast. Find the appropriate opcode based on the
3036 // element types.
3037 SrcTy = SrcVecTy->getElementType();
3038 DestTy = DestVecTy->getElementType();
3039 }
3040
3041 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003042 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3043 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003044
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003046 if (DestTy->isIntegerTy()) { // Casting to integral
3047 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 if (DestBits < SrcBits)
3049 return Trunc; // int -> smaller int
3050 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003051 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003052 return SExt; // signed -> SEXT
3053 else
3054 return ZExt; // unsigned -> ZEXT
3055 } else {
3056 return BitCast; // Same size, No-op cast
3057 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003058 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003059 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 return FPToSI; // FP -> sint
3061 else
3062 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003063 } else if (SrcTy->isVectorTy()) {
3064 assert(DestBits == SrcBits &&
3065 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003066 return BitCast; // Same size, no-op cast
3067 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003068 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003069 "Casting from a value that is not first-class type");
3070 return PtrToInt; // ptr -> int
3071 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003072 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3073 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003074 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 return SIToFP; // sint -> FP
3076 else
3077 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003078 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003079 if (DestBits < SrcBits) {
3080 return FPTrunc; // FP -> smaller FP
3081 } else if (DestBits > SrcBits) {
3082 return FPExt; // FP -> larger FP
3083 } else {
3084 return BitCast; // same size, no-op cast
3085 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003086 } else if (SrcTy->isVectorTy()) {
3087 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003088 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003089 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003090 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003091 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003092 } else if (DestTy->isVectorTy()) {
3093 assert(DestBits == SrcBits &&
3094 "Illegal cast to vector (wrong type or size)");
3095 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003096 } else if (DestTy->isPointerTy()) {
3097 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003098 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3099 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003101 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003102 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003104 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003105 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003106 if (SrcTy->isVectorTy()) {
3107 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003108 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003109 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003110 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003111 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003112 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113}
3114
3115//===----------------------------------------------------------------------===//
3116// CastInst SubClass Constructors
3117//===----------------------------------------------------------------------===//
3118
3119/// Check that the construction parameters for a CastInst are correct. This
3120/// could be broken out into the separate constructors but it is useful to have
3121/// it in one place and to eliminate the redundant code for getting the sizes
3122/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003123bool
Chris Lattner229907c2011-07-18 04:54:35 +00003124CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125
3126 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003127 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003128
Chris Lattner37bc78a2010-01-26 21:51:43 +00003129 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3130 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131 return false;
3132
3133 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003134 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3135 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136
Duncan Sands7f646562011-05-18 09:21:57 +00003137 // If these are vector types, get the lengths of the vectors (using zero for
3138 // scalar types means that checking that vector lengths match also checks that
3139 // scalars are not being converted to vectors or vectors to scalars).
3140 unsigned SrcLength = SrcTy->isVectorTy() ?
3141 cast<VectorType>(SrcTy)->getNumElements() : 0;
3142 unsigned DstLength = DstTy->isVectorTy() ?
3143 cast<VectorType>(DstTy)->getNumElements() : 0;
3144
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145 // Switch on the opcode provided
3146 switch (op) {
3147 default: return false; // This is an input error
3148 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003149 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3150 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003152 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3153 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003155 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3156 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003158 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3159 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003161 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3162 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003164 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003165 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3166 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003169 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3170 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003172 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003173 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003174 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3175 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3176 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003177 return SrcTy->getScalarType()->isPointerTy() &&
3178 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003180 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003181 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003182 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3183 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3184 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003185 return SrcTy->getScalarType()->isIntegerTy() &&
3186 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003187 case Instruction::BitCast: {
3188 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3189 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3190
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191 // BitCast implies a no-op cast of type only. No bits change.
3192 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003193 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003194 return false;
3195
Alp Tokerf907b892013-12-05 05:44:44 +00003196 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003197 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003198 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003199 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3200
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003201 // If both are pointers then the address spaces must match.
3202 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3203 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003204
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003205 // A vector of pointers must have the same number of elements.
3206 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3207 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3208 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3209
3210 return false;
3211 }
3212
3213 return true;
3214 }
3215 case Instruction::AddrSpaceCast: {
3216 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3217 if (!SrcPtrTy)
3218 return false;
3219
3220 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3221 if (!DstPtrTy)
3222 return false;
3223
3224 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3225 return false;
3226
3227 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3228 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3229 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3230
3231 return false;
3232 }
3233
3234 return true;
3235 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236 }
3237}
3238
3239TruncInst::TruncInst(
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, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243}
3244
3245TruncInst::TruncInst(
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, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003249}
3250
3251ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003254 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003255}
3256
3257ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003260 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003261}
3262SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
Jeff Cohencc08c832006-12-02 02:22:01 +00003268SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
3273
3274FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003275 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278}
3279
3280FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003281 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284}
3285
3286FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003287 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290}
3291
3292FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003293 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296}
3297
3298UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003299 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302}
3303
3304UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003305 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308}
3309
3310SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003311 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314}
3315
3316SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003317 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320}
3321
3322FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003323 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326}
3327
3328FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003329 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003332}
3333
3334FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003335 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003338}
3339
3340FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003341 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003344}
3345
3346PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003347 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003348) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003350}
3351
3352PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003353 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003354) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003356}
3357
3358IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003359 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003360) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003362}
3363
3364IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003365 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003366) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003368}
3369
3370BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003371 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003372) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003374}
3375
3376BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003377 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003378) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003380}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003381
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003382AddrSpaceCastInst::AddrSpaceCastInst(
3383 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3384) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3386}
3387
3388AddrSpaceCastInst::AddrSpaceCastInst(
3389 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3390) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3392}
3393
Chris Lattnerf16dc002006-09-17 19:29:56 +00003394//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003395// CmpInst Classes
3396//===----------------------------------------------------------------------===//
3397
Craig Topper3186c012012-09-23 02:12:10 +00003398void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003399
Chris Lattner229907c2011-07-18 04:54:35 +00003400CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003401 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003402 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003403 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003404 OperandTraits<CmpInst>::op_begin(this),
3405 OperandTraits<CmpInst>::operands(this),
3406 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003407 Op<0>() = LHS;
3408 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003409 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003410 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003411}
Gabor Greiff6caff662008-05-10 08:32:32 +00003412
Chris Lattner229907c2011-07-18 04:54:35 +00003413CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003414 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003415 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003416 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003417 OperandTraits<CmpInst>::op_begin(this),
3418 OperandTraits<CmpInst>::operands(this),
3419 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003420 Op<0>() = LHS;
3421 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003422 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003423 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003424}
3425
3426CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003427CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003428 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003429 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003430 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003431 if (InsertBefore)
3432 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3433 S1, S2, Name);
3434 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003435 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003436 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003437 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003438
3439 if (InsertBefore)
3440 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3441 S1, S2, Name);
3442 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003443 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003444 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003445}
3446
3447CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003448CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003449 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003450 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003451 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3452 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003453 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003454 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3455 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003456}
3457
3458void CmpInst::swapOperands() {
3459 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3460 IC->swapOperands();
3461 else
3462 cast<FCmpInst>(this)->swapOperands();
3463}
3464
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003465bool CmpInst::isCommutative() const {
3466 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003467 return IC->isCommutative();
3468 return cast<FCmpInst>(this)->isCommutative();
3469}
3470
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003471bool CmpInst::isEquality() const {
3472 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003473 return IC->isEquality();
3474 return cast<FCmpInst>(this)->isEquality();
3475}
3476
3477
Dan Gohman4e724382008-05-31 02:47:54 +00003478CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003479 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003480 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003481 case ICMP_EQ: return ICMP_NE;
3482 case ICMP_NE: return ICMP_EQ;
3483 case ICMP_UGT: return ICMP_ULE;
3484 case ICMP_ULT: return ICMP_UGE;
3485 case ICMP_UGE: return ICMP_ULT;
3486 case ICMP_ULE: return ICMP_UGT;
3487 case ICMP_SGT: return ICMP_SLE;
3488 case ICMP_SLT: return ICMP_SGE;
3489 case ICMP_SGE: return ICMP_SLT;
3490 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003491
Dan Gohman4e724382008-05-31 02:47:54 +00003492 case FCMP_OEQ: return FCMP_UNE;
3493 case FCMP_ONE: return FCMP_UEQ;
3494 case FCMP_OGT: return FCMP_ULE;
3495 case FCMP_OLT: return FCMP_UGE;
3496 case FCMP_OGE: return FCMP_ULT;
3497 case FCMP_OLE: return FCMP_UGT;
3498 case FCMP_UEQ: return FCMP_ONE;
3499 case FCMP_UNE: return FCMP_OEQ;
3500 case FCMP_UGT: return FCMP_OLE;
3501 case FCMP_ULT: return FCMP_OGE;
3502 case FCMP_UGE: return FCMP_OLT;
3503 case FCMP_ULE: return FCMP_OGT;
3504 case FCMP_ORD: return FCMP_UNO;
3505 case FCMP_UNO: return FCMP_ORD;
3506 case FCMP_TRUE: return FCMP_FALSE;
3507 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003508 }
3509}
3510
Reid Spencer266e42b2006-12-23 06:05:41 +00003511ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3512 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003513 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003514 case ICMP_EQ: case ICMP_NE:
3515 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3516 return pred;
3517 case ICMP_UGT: return ICMP_SGT;
3518 case ICMP_ULT: return ICMP_SLT;
3519 case ICMP_UGE: return ICMP_SGE;
3520 case ICMP_ULE: return ICMP_SLE;
3521 }
3522}
3523
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003524ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3525 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003526 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003527 case ICMP_EQ: case ICMP_NE:
3528 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3529 return pred;
3530 case ICMP_SGT: return ICMP_UGT;
3531 case ICMP_SLT: return ICMP_ULT;
3532 case ICMP_SGE: return ICMP_UGE;
3533 case ICMP_SLE: return ICMP_ULE;
3534 }
3535}
3536
Reid Spencer0286bc12007-02-28 22:00:54 +00003537/// Initialize a set of values that all satisfy the condition with C.
3538///
3539ConstantRange
3540ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3541 APInt Lower(C);
3542 APInt Upper(C);
3543 uint32_t BitWidth = C.getBitWidth();
3544 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003545 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003546 case ICmpInst::ICMP_EQ: ++Upper; break;
3547 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003548 case ICmpInst::ICMP_ULT:
3549 Lower = APInt::getMinValue(BitWidth);
3550 // Check for an empty-set condition.
3551 if (Lower == Upper)
3552 return ConstantRange(BitWidth, /*isFullSet=*/false);
3553 break;
3554 case ICmpInst::ICMP_SLT:
3555 Lower = APInt::getSignedMinValue(BitWidth);
3556 // Check for an empty-set condition.
3557 if (Lower == Upper)
3558 return ConstantRange(BitWidth, /*isFullSet=*/false);
3559 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003560 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003561 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003562 // Check for an empty-set condition.
3563 if (Lower == Upper)
3564 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003565 break;
3566 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003567 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003568 // Check for an empty-set condition.
3569 if (Lower == Upper)
3570 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003571 break;
3572 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003573 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003574 // Check for a full-set condition.
3575 if (Lower == Upper)
3576 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003577 break;
3578 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003579 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003580 // Check for a full-set condition.
3581 if (Lower == Upper)
3582 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003583 break;
3584 case ICmpInst::ICMP_UGE:
3585 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003586 // Check for a full-set condition.
3587 if (Lower == Upper)
3588 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003589 break;
3590 case ICmpInst::ICMP_SGE:
3591 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003592 // Check for a full-set condition.
3593 if (Lower == Upper)
3594 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003595 break;
3596 }
3597 return ConstantRange(Lower, Upper);
3598}
3599
Dan Gohman4e724382008-05-31 02:47:54 +00003600CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003601 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003602 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003603 case ICMP_EQ: case ICMP_NE:
3604 return pred;
3605 case ICMP_SGT: return ICMP_SLT;
3606 case ICMP_SLT: return ICMP_SGT;
3607 case ICMP_SGE: return ICMP_SLE;
3608 case ICMP_SLE: return ICMP_SGE;
3609 case ICMP_UGT: return ICMP_ULT;
3610 case ICMP_ULT: return ICMP_UGT;
3611 case ICMP_UGE: return ICMP_ULE;
3612 case ICMP_ULE: return ICMP_UGE;
3613
Reid Spencerd9436b62006-11-20 01:22:35 +00003614 case FCMP_FALSE: case FCMP_TRUE:
3615 case FCMP_OEQ: case FCMP_ONE:
3616 case FCMP_UEQ: case FCMP_UNE:
3617 case FCMP_ORD: case FCMP_UNO:
3618 return pred;
3619 case FCMP_OGT: return FCMP_OLT;
3620 case FCMP_OLT: return FCMP_OGT;
3621 case FCMP_OGE: return FCMP_OLE;
3622 case FCMP_OLE: return FCMP_OGE;
3623 case FCMP_UGT: return FCMP_ULT;
3624 case FCMP_ULT: return FCMP_UGT;
3625 case FCMP_UGE: return FCMP_ULE;
3626 case FCMP_ULE: return FCMP_UGE;
3627 }
3628}
3629
Sanjoy Das6e78b172015-10-22 19:57:34 +00003630CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3631 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3632
3633 switch (pred) {
3634 default:
3635 llvm_unreachable("Unknown predicate!");
3636 case CmpInst::ICMP_ULT:
3637 return CmpInst::ICMP_SLT;
3638 case CmpInst::ICMP_ULE:
3639 return CmpInst::ICMP_SLE;
3640 case CmpInst::ICMP_UGT:
3641 return CmpInst::ICMP_SGT;
3642 case CmpInst::ICMP_UGE:
3643 return CmpInst::ICMP_SGE;
3644 }
3645}
3646
Reid Spencer266e42b2006-12-23 06:05:41 +00003647bool CmpInst::isUnsigned(unsigned short predicate) {
3648 switch (predicate) {
3649 default: return false;
3650 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3651 case ICmpInst::ICMP_UGE: return true;
3652 }
3653}
3654
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003655bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003656 switch (predicate) {
3657 default: return false;
3658 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3659 case ICmpInst::ICMP_SGE: return true;
3660 }
3661}
3662
3663bool CmpInst::isOrdered(unsigned short predicate) {
3664 switch (predicate) {
3665 default: return false;
3666 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3667 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3668 case FCmpInst::FCMP_ORD: return true;
3669 }
3670}
3671
3672bool CmpInst::isUnordered(unsigned short predicate) {
3673 switch (predicate) {
3674 default: return false;
3675 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3676 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3677 case FCmpInst::FCMP_UNO: return true;
3678 }
3679}
3680
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003681bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3682 switch(predicate) {
3683 default: return false;
3684 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3685 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3686 }
3687}
3688
3689bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3690 switch(predicate) {
3691 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3692 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3693 default: return false;
3694 }
3695}
3696
3697
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003698//===----------------------------------------------------------------------===//
3699// SwitchInst Implementation
3700//===----------------------------------------------------------------------===//
3701
Chris Lattnerbaf00152010-11-17 05:41:46 +00003702void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3703 assert(Value && Default && NumReserved);
3704 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003705 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003706 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003707
Pete Coopereb31b682015-05-21 22:48:54 +00003708 Op<0>() = Value;
3709 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003710}
3711
Chris Lattner2195fc42007-02-24 00:55:48 +00003712/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3713/// switch on and a default destination. The number of additional cases can
3714/// be specified here to make memory allocation more efficient. This
3715/// constructor can also autoinsert before another instruction.
3716SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3717 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003718 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003719 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003720 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003721}
3722
3723/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3724/// switch on and a default destination. The number of additional cases can
3725/// be specified here to make memory allocation more efficient. This
3726/// constructor also autoinserts at the end of the specified BasicBlock.
3727SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3728 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003729 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003730 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003731 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003732}
3733
Misha Brukmanb1c93172005-04-21 23:48:37 +00003734SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003735 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003736 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003737 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003738 Use *OL = getOperandList();
3739 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003740 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003741 OL[i] = InOL[i];
3742 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003743 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003744 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003745}
3746
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003747
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003748/// addCase - Add an entry to the switch instruction...
3749///
Chris Lattner47ac1872005-02-24 05:32:09 +00003750void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003751 unsigned NewCaseIdx = getNumCases();
3752 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003753 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003754 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003755 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003756 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003757 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003758 CaseIt Case(this, NewCaseIdx);
3759 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003760 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003761}
3762
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003763/// removeCase - This method removes the specified case and its successor
3764/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003765void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003766 unsigned idx = i.getCaseIndex();
3767
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003768 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003769
3770 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003771 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003772
Jay Foad14277722011-02-01 09:22:34 +00003773 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003774 if (2 + (idx + 1) * 2 != NumOps) {
3775 OL[2 + idx * 2] = OL[NumOps - 2];
3776 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003777 }
3778
3779 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003780 OL[NumOps-2].set(nullptr);
3781 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003782 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003783}
3784
Jay Foade98f29d2011-04-01 08:00:58 +00003785/// growOperands - grow operands - This grows the operand list in response
3786/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003787///
Jay Foade98f29d2011-04-01 08:00:58 +00003788void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003789 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003790 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003791
3792 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003793 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003794}
3795
3796
3797BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3798 return getSuccessor(idx);
3799}
3800unsigned SwitchInst::getNumSuccessorsV() const {
3801 return getNumSuccessors();
3802}
3803void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3804 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003805}
Chris Lattnerf22be932004-10-15 23:52:53 +00003806
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003808// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003809//===----------------------------------------------------------------------===//
3810
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003811void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003812 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003813 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003814 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003815 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003816 allocHungoffUses(ReservedSpace);
3817
Pete Coopereb31b682015-05-21 22:48:54 +00003818 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003819}
3820
3821
Jay Foade98f29d2011-04-01 08:00:58 +00003822/// growOperands - grow operands - This grows the operand list in response
3823/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003824///
Jay Foade98f29d2011-04-01 08:00:58 +00003825void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003826 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003827 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828
3829 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003830 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003831}
3832
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003833IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3834 Instruction *InsertBefore)
3835: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003836 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003837 init(Address, NumCases);
3838}
3839
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003840IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3841 BasicBlock *InsertAtEnd)
3842: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003843 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003844 init(Address, NumCases);
3845}
3846
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003847IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003848 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3849 nullptr, IBI.getNumOperands()) {
3850 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003851 Use *OL = getOperandList();
3852 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003853 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3854 OL[i] = InOL[i];
3855 SubclassOptionalData = IBI.SubclassOptionalData;
3856}
3857
Chris Lattner3ed871f2009-10-27 19:13:16 +00003858/// addDestination - Add a destination.
3859///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003860void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003861 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003862 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003863 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003864 // Initialize some new operands.
3865 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003866 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003867 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003868}
3869
3870/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003871/// indirectbr instruction.
3872void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003873 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3874
3875 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003876 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003877
3878 // Replace this value with the last one.
3879 OL[idx+1] = OL[NumOps-1];
3880
3881 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003882 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003883 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003884}
3885
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003886BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003887 return getSuccessor(idx);
3888}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003889unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003890 return getNumSuccessors();
3891}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003892void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003893 setSuccessor(idx, B);
3894}
3895
3896//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003897// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003898//===----------------------------------------------------------------------===//
3899
Chris Lattnerf22be932004-10-15 23:52:53 +00003900// Define these methods here so vtables don't get emitted into every translation
3901// unit that uses these classes.
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003904 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003905}
3906
Pete Cooper75403d72015-06-24 20:22:23 +00003907BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003908 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003909}
3910
Pete Cooper75403d72015-06-24 20:22:23 +00003911FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003912 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003913}
3914
Pete Cooper75403d72015-06-24 20:22:23 +00003915ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003916 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003917}
3918
Pete Cooper75403d72015-06-24 20:22:23 +00003919ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003920 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003921}
3922
Pete Cooper75403d72015-06-24 20:22:23 +00003923InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003924 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003928 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3929 (Value *)getOperand(0), getAlignment());
3930 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3931 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003932}
3933
Pete Cooper75403d72015-06-24 20:22:23 +00003934LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003935 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3936 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003937}
3938
Pete Cooper75403d72015-06-24 20:22:23 +00003939StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003940 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003941 getAlignment(), getOrdering(), getSynchScope());
3942
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003946 AtomicCmpXchgInst *Result =
3947 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003948 getSuccessOrdering(), getFailureOrdering(),
3949 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003950 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003951 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003952 return Result;
3953}
3954
Pete Cooper75403d72015-06-24 20:22:23 +00003955AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003956 AtomicRMWInst *Result =
3957 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3958 getOrdering(), getSynchScope());
3959 Result->setVolatile(isVolatile());
3960 return Result;
3961}
3962
Pete Cooper75403d72015-06-24 20:22:23 +00003963FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003964 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3965}
3966
Pete Cooper75403d72015-06-24 20:22:23 +00003967TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003968 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003969}
3970
Pete Cooper75403d72015-06-24 20:22:23 +00003971ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003972 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003973}
3974
Pete Cooper75403d72015-06-24 20:22:23 +00003975SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003976 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003977}
3978
Pete Cooper75403d72015-06-24 20:22:23 +00003979FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003980 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003981}
3982
Pete Cooper75403d72015-06-24 20:22:23 +00003983FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003984 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003985}
3986
Pete Cooper75403d72015-06-24 20:22:23 +00003987UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003988 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003989}
3990
Pete Cooper75403d72015-06-24 20:22:23 +00003991SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003992 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003993}
3994
Pete Cooper75403d72015-06-24 20:22:23 +00003995FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003996 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003997}
3998
Pete Cooper75403d72015-06-24 20:22:23 +00003999FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004000 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004001}
4002
Pete Cooper75403d72015-06-24 20:22:23 +00004003PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004004 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004005}
4006
Pete Cooper75403d72015-06-24 20:22:23 +00004007IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004008 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004009}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004010
Pete Cooper75403d72015-06-24 20:22:23 +00004011BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004012 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004013}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00004014
Pete Cooper75403d72015-06-24 20:22:23 +00004015AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004016 return new AddrSpaceCastInst(getOperand(0), getType());
4017}
4018
Pete Cooper75403d72015-06-24 20:22:23 +00004019CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004020 if (hasOperandBundles()) {
4021 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4022 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4023 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004024 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004025}
4026
Pete Cooper75403d72015-06-24 20:22:23 +00004027SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004028 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004029}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004030
Pete Cooper75403d72015-06-24 20:22:23 +00004031VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004032 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004033}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004034
Pete Cooper75403d72015-06-24 20:22:23 +00004035ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004036 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004037}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004038
Pete Cooper75403d72015-06-24 20:22:23 +00004039InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004040 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004041}
4042
Pete Cooper75403d72015-06-24 20:22:23 +00004043ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004044 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004045}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004046
Pete Cooper75403d72015-06-24 20:22:23 +00004047PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004048
Pete Cooper75403d72015-06-24 20:22:23 +00004049LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004050 return new LandingPadInst(*this);
4051}
4052
Pete Cooper75403d72015-06-24 20:22:23 +00004053ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004054 return new(getNumOperands()) ReturnInst(*this);
4055}
4056
Pete Cooper75403d72015-06-24 20:22:23 +00004057BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004058 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004059}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004060
Pete Cooper75403d72015-06-24 20:22:23 +00004061SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004062
Pete Cooper75403d72015-06-24 20:22:23 +00004063IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004064 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004065}
4066
Pete Cooper75403d72015-06-24 20:22:23 +00004067InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004068 if (hasOperandBundles()) {
4069 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4070 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4071 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004072 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004073}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004074
Pete Cooper75403d72015-06-24 20:22:23 +00004075ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004076
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004077CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
4078 return new (getNumOperands()) CleanupEndPadInst(*this);
4079}
4080
David Majnemer654e1302015-07-31 17:58:14 +00004081CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4082 return new (getNumOperands()) CleanupReturnInst(*this);
4083}
4084
4085CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
4086 return new (getNumOperands()) CatchEndPadInst(*this);
4087}
4088
4089CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004090 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004091}
4092
4093CatchPadInst *CatchPadInst::cloneImpl() const {
4094 return new (getNumOperands()) CatchPadInst(*this);
4095}
4096
4097TerminatePadInst *TerminatePadInst::cloneImpl() const {
4098 return new (getNumOperands()) TerminatePadInst(*this);
4099}
4100
4101CleanupPadInst *CleanupPadInst::cloneImpl() const {
4102 return new (getNumOperands()) CleanupPadInst(*this);
4103}
4104
Pete Cooper75403d72015-06-24 20:22:23 +00004105UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004106 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004107 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004108}