blob: f185caacdf6c46bf007013cd01db7713371bb0db [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
90PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000091 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
92 ReservedSpace(PN.getNumOperands()) {
93 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000094 std::copy(PN.op_begin(), PN.op_end(), op_begin());
95 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000096 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099// removeIncomingValue - Remove an incoming value. This is useful if a
100// predecessor basic block is deleted.
101Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000102 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000103
104 // Move everything after this operand down.
105 //
106 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000107 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
110 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111
112 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000113 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000114 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000117 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000119 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 eraseFromParent();
121 }
122 return Removed;
123}
124
Jay Foade98f29d2011-04-01 08:00:58 +0000125/// growOperands - grow operands - This grows the operand list in response
126/// to a push_back style of operation. This grows the number of ops by 1.5
127/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128///
Jay Foade98f29d2011-04-01 08:00:58 +0000129void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000130 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000131 unsigned NumOps = e + e / 2;
132 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
133
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000134 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000135 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136}
137
Nate Begemanb3923212005-08-04 23:24:19 +0000138/// hasConstantValue - If the specified PHI node always merges together the same
139/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000140Value *PHINode::hasConstantValue() const {
141 // Exploit the fact that phi nodes always have at least one entry.
142 Value *ConstantValue = getIncomingValue(0);
143 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000144 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
145 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000146 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000147 // The case where the first value is this PHI.
148 ConstantValue = getIncomingValue(i);
149 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000150 if (ConstantValue == this)
151 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000152 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000153}
154
Bill Wendlingfae14752011-08-12 20:24:12 +0000155//===----------------------------------------------------------------------===//
156// LandingPadInst Implementation
157//===----------------------------------------------------------------------===//
158
David Majnemer7fddecc2015-06-17 20:52:32 +0000159LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
160 const Twine &NameStr, Instruction *InsertBefore)
161 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
162 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000163}
164
David Majnemer7fddecc2015-06-17 20:52:32 +0000165LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
166 const Twine &NameStr, BasicBlock *InsertAtEnd)
167 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
168 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000169}
170
171LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000172 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
173 LP.getNumOperands()),
174 ReservedSpace(LP.getNumOperands()) {
175 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000176 Use *OL = getOperandList();
177 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000178 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
179 OL[I] = InOL[I];
180
181 setCleanup(LP.isCleanup());
182}
183
David Majnemer7fddecc2015-06-17 20:52:32 +0000184LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000185 const Twine &NameStr,
186 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000187 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000188}
189
David Majnemer7fddecc2015-06-17 20:52:32 +0000190LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000191 const Twine &NameStr,
192 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000193 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000194}
195
David Majnemer7fddecc2015-06-17 20:52:32 +0000196void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000197 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000198 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000199 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000200 setName(NameStr);
201 setCleanup(false);
202}
203
204/// growOperands - grow operands - This grows the operand list in response to a
205/// push_back style of operation. This grows the number of ops by 2 times.
206void LandingPadInst::growOperands(unsigned Size) {
207 unsigned e = getNumOperands();
208 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000209 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000210 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000211}
212
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000213void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000214 unsigned OpNo = getNumOperands();
215 growOperands(1);
216 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000217 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000218 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000219}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220
221//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222// CallInst Implementation
223//===----------------------------------------------------------------------===//
224
Gordon Henriksen14a55692007-12-10 02:14:30 +0000225CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226}
227
David Blaikie348de692015-04-23 21:36:23 +0000228void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000229 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000230 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000231 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
232 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000233 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234
Jay Foad5bd375a2011-07-15 08:37:34 +0000235#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000236 assert((Args.size() == FTy->getNumParams() ||
237 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000238 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000239
240 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000241 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000242 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000243 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000244#endif
245
246 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000247
248 auto It = populateBundleOperandInfos(Bundles, Args.size());
249 (void)It;
250 assert(It + 1 == op_end() && "Should add up!");
251
Jay Foad5bd375a2011-07-15 08:37:34 +0000252 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253}
254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000256 FTy =
257 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000258 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000259 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000262
263 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264}
265
Daniel Dunbar4975db62009-07-25 04:41:11 +0000266CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 Instruction *InsertBefore)
268 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
269 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000270 Instruction::Call,
271 OperandTraits<CallInst>::op_end(this) - 1,
272 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274}
275
Daniel Dunbar4975db62009-07-25 04:41:11 +0000276CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 BasicBlock *InsertAtEnd)
278 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
279 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000280 Instruction::Call,
281 OperandTraits<CallInst>::op_end(this) - 1,
282 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000283 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000287 : Instruction(CI.getType(), Instruction::Call,
288 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
289 CI.getNumOperands()),
290 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000291 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000292 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000293
Jay Foad5bd375a2011-07-15 08:37:34 +0000294 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000295 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
296 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000297 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Sanjoy Das2d161452015-11-18 06:23:38 +0000300CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
301 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000302 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000303
304 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
305 InsertPt);
306 NewCI->setTailCallKind(CI->getTailCallKind());
307 NewCI->setCallingConv(CI->getCallingConv());
308 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000309 NewCI->setAttributes(CI->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000310 return NewCI;
311}
312
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000313void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000314 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000315 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000316 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000317}
318
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000319void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
320 AttributeSet PAL = getAttributes();
321 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
322 setAttributes(PAL);
323}
324
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000325void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000326 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000327 AttrBuilder B(attr);
328 LLVMContext &Context = getContext();
329 PAL = PAL.removeAttributes(Context, i,
330 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000331 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000332}
333
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000334void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
335 AttributeSet PAL = getAttributes();
336 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
337 setAttributes(PAL);
338}
339
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000340void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
341 AttributeSet PAL = getAttributes();
342 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
343 setAttributes(PAL);
344}
345
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000346bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000347 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
348
Bill Wendling749a43d2012-12-30 13:50:49 +0000349 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000350 return true;
351 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000352 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000353 return false;
354}
355
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000356bool CallInst::dataOperandHasImpliedAttr(unsigned i,
357 Attribute::AttrKind A) const {
358
Sanjoy Das776e4a72015-11-05 01:53:26 +0000359 // There are getNumOperands() - 1 data operands. The last operand is the
360 // callee.
361 assert(i < getNumOperands() && "Data operand index out of bounds!");
362
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000363 // The attribute A can either be directly specified, if the operand in
364 // question is a call argument; or be indirectly implied by the kind of its
365 // containing operand bundle, if the operand is a bundle operand.
366
367 if (i < (getNumArgOperands() + 1))
368 return paramHasAttr(i, A);
369
370 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
371 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000372 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000373}
374
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375/// IsConstantOne - Return true only if val is constant int 1
376static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000377 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000378 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
379 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380}
381
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000382static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000383 BasicBlock *InsertAtEnd, Type *IntPtrTy,
384 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000385 Value *ArraySize, Function *MallocF,
386 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000387 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000388 "createMalloc needs either InsertBefore or InsertAtEnd");
389
390 // malloc(type) becomes:
391 // bitcast (i8* malloc(typeSize)) to type*
392 // malloc(type, arraySize) becomes:
393 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000394 if (!ArraySize)
395 ArraySize = ConstantInt::get(IntPtrTy, 1);
396 else if (ArraySize->getType() != IntPtrTy) {
397 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000398 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
399 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000400 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000401 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
402 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000403 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000405 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406 if (IsConstantOne(AllocSize)) {
407 AllocSize = ArraySize; // Operand * 1 = Operand
408 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
409 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
410 false /*ZExt*/);
411 // Malloc arg is constant product of type size and array size
412 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
413 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000414 // Multiply type size by the array size...
415 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000416 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
417 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000418 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000419 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
420 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000422 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423
Victor Hernandez788eaab2009-09-18 19:20:02 +0000424 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425 // Create the call to Malloc.
426 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
427 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000428 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000429 Value *MallocFunc = MallocF;
430 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000431 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000432 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000433 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000434 CallInst *MCall = nullptr;
435 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000436 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000437 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000438 Result = MCall;
439 if (Result->getType() != AllocPtrType)
440 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000441 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000442 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000443 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000444 Result = MCall;
445 if (Result->getType() != AllocPtrType) {
446 InsertAtEnd->getInstList().push_back(MCall);
447 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000448 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000449 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000450 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000452 if (Function *F = dyn_cast<Function>(MallocFunc)) {
453 MCall->setCallingConv(F->getCallingConv());
454 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
455 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000456 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000457
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000458 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000459}
460
461/// CreateMalloc - Generate the IR for a call to malloc:
462/// 1. Compute the malloc call's argument as the specified type's size,
463/// possibly multiplied by the array size if the array size is not
464/// constant 1.
465/// 2. Call malloc with that argument.
466/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000467Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000468 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000470 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000472 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000473 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474}
475
476/// CreateMalloc - Generate the IR for a call to malloc:
477/// 1. Compute the malloc call's argument as the specified type's size,
478/// possibly multiplied by the array size if the array size is not
479/// constant 1.
480/// 2. Call malloc with that argument.
481/// 3. Bitcast the result of the malloc call to the specified type.
482/// Note: This function does not add the bitcast to the basic block, that is the
483/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000484Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000485 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000486 Value *AllocSize, Value *ArraySize,
487 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000488 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000489 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000491
Victor Hernandeze2971492009-10-24 04:23:03 +0000492static Instruction* createFree(Value* Source, Instruction *InsertBefore,
493 BasicBlock *InsertAtEnd) {
494 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
495 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000496 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000497 "Can not free something of nonpointer type!");
498
499 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
500 Module* M = BB->getParent()->getParent();
501
Chris Lattner229907c2011-07-18 04:54:35 +0000502 Type *VoidTy = Type::getVoidTy(M->getContext());
503 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000504 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000505 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000506 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000507 Value *PtrCast = Source;
508 if (InsertBefore) {
509 if (Source->getType() != IntPtrTy)
510 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
511 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
512 } else {
513 if (Source->getType() != IntPtrTy)
514 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
515 Result = CallInst::Create(FreeFunc, PtrCast, "");
516 }
517 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000518 if (Function *F = dyn_cast<Function>(FreeFunc))
519 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000520
521 return Result;
522}
523
524/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000525Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000526 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000527}
528
529/// CreateFree - Generate the IR for a call to the builtin free function.
530/// Note: This function does not add the call to the basic block, that is the
531/// responsibility of the caller.
532Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000533 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000534 assert(FreeCall && "CreateFree did not create a CallInst");
535 return FreeCall;
536}
537
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538//===----------------------------------------------------------------------===//
539// InvokeInst Implementation
540//===----------------------------------------------------------------------===//
541
David Blaikie3e807092015-05-13 18:35:26 +0000542void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
543 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000544 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000545 const Twine &NameStr) {
546 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000547
Sanjoy Das9303c242015-09-24 19:14:18 +0000548 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
549 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000550 Op<-3>() = Fn;
551 Op<-2>() = IfNormal;
552 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000553
554#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000555 assert(((Args.size() == FTy->getNumParams()) ||
556 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000557 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000558
Jay Foad5bd375a2011-07-15 08:37:34 +0000559 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000560 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000562 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000563#endif
564
565 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000566
567 auto It = populateBundleOperandInfos(Bundles, Args.size());
568 (void)It;
569 assert(It + 3 == op_end() && "Should add up!");
570
Jay Foad5bd375a2011-07-15 08:37:34 +0000571 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572}
573
Misha Brukmanb1c93172005-04-21 23:48:37 +0000574InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000575 : TerminatorInst(II.getType(), Instruction::Invoke,
576 OperandTraits<InvokeInst>::op_end(this) -
577 II.getNumOperands(),
578 II.getNumOperands()),
579 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000580 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000581 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000582 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
583 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000584 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
Sanjoy Das2d161452015-11-18 06:23:38 +0000587InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
588 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000589 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000590
591 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
592 II->getUnwindDest(), Args, OpB,
593 II->getName(), InsertPt);
594 NewII->setCallingConv(II->getCallingConv());
595 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000596 NewII->setAttributes(II->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000597 return NewII;
598}
599
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
601 return getSuccessor(idx);
602}
603unsigned InvokeInst::getNumSuccessorsV() const {
604 return getNumSuccessors();
605}
606void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
607 return setSuccessor(idx, B);
608}
609
Michael Gottesman41748d72013-06-27 00:25:01 +0000610bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000611 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000612 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000613
614 // Operand bundles override attributes on the called function, but don't
615 // override attributes directly present on the invoke instruction.
616 if (isFnAttrDisallowedByOpBundle(A))
617 return false;
618
Bill Wendling375eb1f2012-10-09 00:28:54 +0000619 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000620 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 return false;
622}
623
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000624bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000625 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
626
Bill Wendling749a43d2012-12-30 13:50:49 +0000627 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000628 return true;
629 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000630 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000631 return false;
632}
633
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000634bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
635 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000636 // There are getNumOperands() - 3 data operands. The last three operands are
637 // the callee and the two successor basic blocks.
638 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
639
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000640 // The attribute A can either be directly specified, if the operand in
641 // question is an invoke argument; or be indirectly implied by the kind of its
642 // containing operand bundle, if the operand is a bundle operand.
643
644 if (i < (getNumArgOperands() + 1))
645 return paramHasAttr(i, A);
646
647 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
648 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000649 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000650}
651
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000652void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000653 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000654 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000655 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000656}
657
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000658void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000659 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000660 AttrBuilder B(attr);
661 PAL = PAL.removeAttributes(getContext(), i,
662 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000663 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000664}
665
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000666void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
667 AttributeSet PAL = getAttributes();
668 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
669 setAttributes(PAL);
670}
671
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000672void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
673 AttributeSet PAL = getAttributes();
674 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
675 setAttributes(PAL);
676}
677
Bill Wendlingfae14752011-08-12 20:24:12 +0000678LandingPadInst *InvokeInst::getLandingPadInst() const {
679 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
680}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000681
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682//===----------------------------------------------------------------------===//
683// ReturnInst Implementation
684//===----------------------------------------------------------------------===//
685
Chris Lattner2195fc42007-02-24 00:55:48 +0000686ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000688 OperandTraits<ReturnInst>::op_end(this) -
689 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000690 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000691 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000692 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000693 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000694}
695
Owen Anderson55f1c092009-08-13 21:58:54 +0000696ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
697 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
699 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000700 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000701 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000702}
Owen Anderson55f1c092009-08-13 21:58:54 +0000703ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
704 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000705 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
706 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000707 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000708 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000709}
Owen Anderson55f1c092009-08-13 21:58:54 +0000710ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
711 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000712 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000713}
714
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715unsigned ReturnInst::getNumSuccessorsV() const {
716 return getNumSuccessors();
717}
718
Devang Patelae682fb2008-02-26 17:56:20 +0000719/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
720/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000722 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000726 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727}
728
Devang Patel59643e52008-02-23 00:35:18 +0000729ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000730}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000733// ResumeInst Implementation
734//===----------------------------------------------------------------------===//
735
736ResumeInst::ResumeInst(const ResumeInst &RI)
737 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
738 OperandTraits<ResumeInst>::op_begin(this), 1) {
739 Op<0>() = RI.Op<0>();
740}
741
742ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
743 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
744 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
745 Op<0>() = Exn;
746}
747
748ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
749 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
750 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
751 Op<0>() = Exn;
752}
753
754unsigned ResumeInst::getNumSuccessorsV() const {
755 return getNumSuccessors();
756}
757
758void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
759 llvm_unreachable("ResumeInst has no successors!");
760}
761
762BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
763 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000764}
765
766//===----------------------------------------------------------------------===//
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000767// CleanupEndPadInst Implementation
768//===----------------------------------------------------------------------===//
769
770CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
771 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
772 OperandTraits<CleanupEndPadInst>::op_end(this) -
773 CEPI.getNumOperands(),
774 CEPI.getNumOperands()) {
775 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
776 setCleanupPad(CEPI.getCleanupPad());
777 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
778 setUnwindDest(UnwindDest);
779}
780
781void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
782 setCleanupPad(CleanupPad);
783 if (UnwindBB) {
784 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
785 setUnwindDest(UnwindBB);
786 }
787}
788
789CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
790 BasicBlock *UnwindBB, unsigned Values,
791 Instruction *InsertBefore)
792 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
793 Instruction::CleanupEndPad,
794 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
795 Values, InsertBefore) {
796 init(CleanupPad, UnwindBB);
797}
798
799CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
800 BasicBlock *UnwindBB, unsigned Values,
801 BasicBlock *InsertAtEnd)
802 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
803 Instruction::CleanupEndPad,
804 OperandTraits<CleanupEndPadInst>::op_end(this) - Values,
805 Values, InsertAtEnd) {
806 init(CleanupPad, UnwindBB);
807}
808
809BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
810 assert(Idx == 0);
811 return getUnwindDest();
812}
813unsigned CleanupEndPadInst::getNumSuccessorsV() const {
814 return getNumSuccessors();
815}
816void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
817 assert(Idx == 0);
818 setUnwindDest(B);
819}
820
821//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000822// CleanupReturnInst Implementation
823//===----------------------------------------------------------------------===//
824
825CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
826 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
827 OperandTraits<CleanupReturnInst>::op_end(this) -
828 CRI.getNumOperands(),
829 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000830 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000831 Op<-1>() = CRI.Op<-1>();
832 if (CRI.hasUnwindDest())
833 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000834}
835
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000836void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000837 if (UnwindBB)
838 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000839
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000840 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000841 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000842 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000843}
844
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000845CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000846 BasicBlock *UnwindBB, unsigned Values,
847 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000848 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
849 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000850 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
851 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000852 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000853}
854
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000855CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000856 BasicBlock *UnwindBB, unsigned Values,
857 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000858 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
859 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000860 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
861 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000862 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000863}
864
865BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
866 assert(Idx == 0);
867 return getUnwindDest();
868}
869unsigned CleanupReturnInst::getNumSuccessorsV() const {
870 return getNumSuccessors();
871}
872void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
873 assert(Idx == 0);
874 setUnwindDest(B);
875}
876
877//===----------------------------------------------------------------------===//
878// CatchEndPadInst Implementation
879//===----------------------------------------------------------------------===//
880
881CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
882 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
883 OperandTraits<CatchEndPadInst>::op_end(this) -
884 CRI.getNumOperands(),
885 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000886 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000887 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
888 setUnwindDest(UnwindDest);
889}
890
891void CatchEndPadInst::init(BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000892 if (UnwindBB) {
893 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
894 setUnwindDest(UnwindBB);
895 }
896}
897
898CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000899 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000900 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
901 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
902 Values, InsertBefore) {
903 init(UnwindBB);
904}
905
906CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000907 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000908 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
909 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
910 Values, InsertAtEnd) {
911 init(UnwindBB);
912}
913
914BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
915 assert(Idx == 0);
916 return getUnwindDest();
917}
918unsigned CatchEndPadInst::getNumSuccessorsV() const {
919 return getNumSuccessors();
920}
921void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
922 assert(Idx == 0);
923 setUnwindDest(B);
924}
925
926//===----------------------------------------------------------------------===//
927// CatchReturnInst Implementation
928//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000929void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
930 Op<0>() = CatchPad;
931 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000932}
David Majnemer654e1302015-07-31 17:58:14 +0000933
934CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
935 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000936 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
937 Op<0>() = CRI.Op<0>();
938 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000939}
940
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000941CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000942 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000943 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000944 OperandTraits<CatchReturnInst>::op_begin(this), 2,
945 InsertBefore) {
946 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000947}
948
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000949CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000950 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000951 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000952 OperandTraits<CatchReturnInst>::op_begin(this), 2,
953 InsertAtEnd) {
954 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000955}
956
957BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000958 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000959 return getSuccessor();
960}
961unsigned CatchReturnInst::getNumSuccessorsV() const {
962 return getNumSuccessors();
963}
964void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000965 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000966 setSuccessor(B);
967}
968
969//===----------------------------------------------------------------------===//
970// CatchPadInst Implementation
971//===----------------------------------------------------------------------===//
972void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000973 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000974 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
975 Op<-2>() = IfNormal;
976 Op<-1>() = IfException;
977 std::copy(Args.begin(), Args.end(), op_begin());
978 setName(NameStr);
979}
980
981CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
982 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
983 OperandTraits<CatchPadInst>::op_end(this) -
984 CPI.getNumOperands(),
985 CPI.getNumOperands()) {
986 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
987}
988
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000989CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
990 ArrayRef<Value *> Args, unsigned Values,
991 const Twine &NameStr, Instruction *InsertBefore)
992 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
993 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000994 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
995 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000996 init(IfNormal, IfException, Args, NameStr);
997}
998
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000999CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
1000 ArrayRef<Value *> Args, unsigned Values,
1001 const Twine &NameStr, BasicBlock *InsertAtEnd)
1002 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
1003 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +00001004 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
1005 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +00001006 init(IfNormal, IfException, Args, NameStr);
1007}
1008
1009BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
1010 return getSuccessor(Idx);
1011}
1012unsigned CatchPadInst::getNumSuccessorsV() const {
1013 return getNumSuccessors();
1014}
1015void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1016 return setSuccessor(Idx, B);
1017}
1018
1019//===----------------------------------------------------------------------===//
1020// TerminatePadInst Implementation
1021//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +00001022void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +00001023 if (BB)
1024 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1025 if (BB)
1026 Op<-1>() = BB;
1027 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +00001028}
1029
1030TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
1031 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
1032 OperandTraits<TerminatePadInst>::op_end(this) -
1033 TPI.getNumOperands(),
1034 TPI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +00001035 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +00001036 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
1037}
1038
1039TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001040 ArrayRef<Value *> Args, unsigned Values,
1041 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001042 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1043 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1044 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001045 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001046}
1047
1048TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +00001049 ArrayRef<Value *> Args, unsigned Values,
1050 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001051 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
1052 OperandTraits<TerminatePadInst>::op_end(this) - Values,
1053 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +00001054 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +00001055}
1056
1057BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
1058 assert(Idx == 0);
1059 return getUnwindDest();
1060}
1061unsigned TerminatePadInst::getNumSuccessorsV() const {
1062 return getNumSuccessors();
1063}
1064void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1065 assert(Idx == 0);
1066 return setUnwindDest(B);
1067}
1068
1069//===----------------------------------------------------------------------===//
1070// CleanupPadInst Implementation
1071//===----------------------------------------------------------------------===//
1072void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
1073 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
1074 std::copy(Args.begin(), Args.end(), op_begin());
1075 setName(NameStr);
1076}
1077
1078CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
1079 : Instruction(CPI.getType(), Instruction::CleanupPad,
1080 OperandTraits<CleanupPadInst>::op_end(this) -
1081 CPI.getNumOperands(),
1082 CPI.getNumOperands()) {
1083 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
1084}
1085
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001086CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001087 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001088 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001089 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1090 Args.size(), InsertBefore) {
1091 init(Args, NameStr);
1092}
1093
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001094CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +00001095 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001096 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +00001097 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
1098 Args.size(), InsertAtEnd) {
1099 init(Args, NameStr);
1100}
1101
1102//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001103// UnreachableInst Implementation
1104//===----------------------------------------------------------------------===//
1105
Owen Anderson55f1c092009-08-13 21:58:54 +00001106UnreachableInst::UnreachableInst(LLVMContext &Context,
1107 Instruction *InsertBefore)
1108 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001109 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001110}
Owen Anderson55f1c092009-08-13 21:58:54 +00001111UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1112 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001113 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001114}
1115
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001116unsigned UnreachableInst::getNumSuccessorsV() const {
1117 return getNumSuccessors();
1118}
1119
1120void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001121 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001122}
1123
1124BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001125 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001126}
1127
1128//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129// BranchInst Implementation
1130//===----------------------------------------------------------------------===//
1131
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001132void BranchInst::AssertOK() {
1133 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001134 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001135 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136}
1137
Chris Lattner2195fc42007-02-24 00:55:48 +00001138BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001139 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 OperandTraits<BranchInst>::op_end(this) - 1,
1141 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001142 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001143 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001144}
1145BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1146 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001147 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001148 OperandTraits<BranchInst>::op_end(this) - 3,
1149 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001150 Op<-1>() = IfTrue;
1151 Op<-2>() = IfFalse;
1152 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001153#ifndef NDEBUG
1154 AssertOK();
1155#endif
1156}
1157
1158BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001159 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001160 OperandTraits<BranchInst>::op_end(this) - 1,
1161 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001162 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001163 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001164}
1165
1166BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1167 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001168 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 OperandTraits<BranchInst>::op_end(this) - 3,
1170 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001171 Op<-1>() = IfTrue;
1172 Op<-2>() = IfFalse;
1173 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001174#ifndef NDEBUG
1175 AssertOK();
1176#endif
1177}
1178
1179
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001180BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001181 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001182 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1183 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001184 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001185 if (BI.getNumOperands() != 1) {
1186 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001187 Op<-3>() = BI.Op<-3>();
1188 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001190 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191}
1192
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001193void BranchInst::swapSuccessors() {
1194 assert(isConditional() &&
1195 "Cannot swap successors of an unconditional branch");
1196 Op<-1>().swap(Op<-2>());
1197
1198 // Update profile metadata if present and it matches our structural
1199 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001200 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001201 if (!ProfileData || ProfileData->getNumOperands() != 3)
1202 return;
1203
1204 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001205 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1206 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001207 setMetadata(LLVMContext::MD_prof,
1208 MDNode::get(ProfileData->getContext(), Ops));
1209}
1210
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001211BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1212 return getSuccessor(idx);
1213}
1214unsigned BranchInst::getNumSuccessorsV() const {
1215 return getNumSuccessors();
1216}
1217void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1218 setSuccessor(idx, B);
1219}
1220
1221
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001222//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001223// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224//===----------------------------------------------------------------------===//
1225
Owen Andersonb6b25302009-07-14 23:09:55 +00001226static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001227 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001228 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001229 else {
1230 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001231 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001232 assert(Amt->getType()->isIntegerTy() &&
1233 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001234 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001235 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236}
1237
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001238AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1239 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001240
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001241AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1242 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001243
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001244AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001245 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001246 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001247
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001248AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001249 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001250 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001251
Chris Lattner229907c2011-07-18 04:54:35 +00001252AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001253 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001254 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1255 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1256 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001257 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001258 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001259 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001260}
1261
Chris Lattner229907c2011-07-18 04:54:35 +00001262AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001263 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001264 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1265 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1266 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001267 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001268 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001269 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001270}
1271
Gordon Henriksen14a55692007-12-10 02:14:30 +00001272// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001273AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001274}
1275
Victor Hernandez8acf2952009-10-23 21:09:37 +00001276void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001277 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001278 assert(Align <= MaximumAlignment &&
1279 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001280 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1281 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001282 assert(getAlignment() == Align && "Alignment representation error!");
1283}
1284
Victor Hernandez8acf2952009-10-23 21:09:37 +00001285bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001286 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001287 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001288 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001289}
1290
Chris Lattner8b291e62008-11-26 02:54:17 +00001291/// isStaticAlloca - Return true if this alloca is in the entry block of the
1292/// function and is a constant size. If so, the code generator will fold it
1293/// into the prolog/epilog code, so it is basically free.
1294bool AllocaInst::isStaticAlloca() const {
1295 // Must be constant size.
1296 if (!isa<ConstantInt>(getArraySize())) return false;
1297
1298 // Must be in the entry block.
1299 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001300 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001301}
1302
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001303//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001304// LoadInst Implementation
1305//===----------------------------------------------------------------------===//
1306
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001307void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001308 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001309 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001310 assert(!(isAtomic() && getAlignment() == 0) &&
1311 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001312}
1313
Daniel Dunbar4975db62009-07-25 04:41:11 +00001314LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001315 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001316
Daniel Dunbar4975db62009-07-25 04:41:11 +00001317LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001318 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319
David Blaikie0c28fd72015-05-20 21:46:30 +00001320LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001322 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001323
1324LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1325 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001326 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001327
David Blaikieb7a029872015-04-17 19:56:21 +00001328LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001329 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001330 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001331 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001332
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001333LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001334 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001335 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001336}
1337
David Blaikie15d9a4c2015-04-06 20:59:48 +00001338LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001339 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001340 SynchronizationScope SynchScope, Instruction *InsertBef)
1341 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001342 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001343 setVolatile(isVolatile);
1344 setAlignment(Align);
1345 setAtomic(Order, SynchScope);
1346 AssertOK();
1347 setName(Name);
1348}
1349
1350LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1351 unsigned Align, AtomicOrdering Order,
1352 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001353 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001354 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001355 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001356 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001357 setAlignment(Align);
1358 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001359 AssertOK();
1360 setName(Name);
1361}
1362
Daniel Dunbar27096822009-08-11 18:11:15 +00001363LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1364 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1365 Load, Ptr, InsertBef) {
1366 setVolatile(false);
1367 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001368 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001369 AssertOK();
1370 if (Name && Name[0]) setName(Name);
1371}
1372
1373LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1374 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1375 Load, Ptr, InsertAE) {
1376 setVolatile(false);
1377 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001378 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001379 AssertOK();
1380 if (Name && Name[0]) setName(Name);
1381}
1382
David Blaikie0c28fd72015-05-20 21:46:30 +00001383LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001384 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001385 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1386 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001387 setVolatile(isVolatile);
1388 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001389 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001390 AssertOK();
1391 if (Name && Name[0]) setName(Name);
1392}
1393
1394LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1395 BasicBlock *InsertAE)
1396 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1397 Load, Ptr, InsertAE) {
1398 setVolatile(isVolatile);
1399 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001400 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001401 AssertOK();
1402 if (Name && Name[0]) setName(Name);
1403}
1404
Christopher Lamb84485702007-04-22 19:24:39 +00001405void LoadInst::setAlignment(unsigned Align) {
1406 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001407 assert(Align <= MaximumAlignment &&
1408 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001409 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001410 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001411 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001412}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001413
1414//===----------------------------------------------------------------------===//
1415// StoreInst Implementation
1416//===----------------------------------------------------------------------===//
1417
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001418void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001419 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001420 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001421 "Ptr must have pointer type!");
1422 assert(getOperand(0)->getType() ==
1423 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001424 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001425 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001426 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001427}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001429StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001430 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001431
1432StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001433 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001434
Misha Brukmanb1c93172005-04-21 23:48:37 +00001435StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001436 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001437 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001438
1439StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001440 BasicBlock *InsertAtEnd)
1441 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1442
1443StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1444 Instruction *InsertBefore)
1445 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1446 InsertBefore) {}
1447
1448StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1449 BasicBlock *InsertAtEnd)
1450 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1451 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001452
Misha Brukmanb1c93172005-04-21 23:48:37 +00001453StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001454 unsigned Align, AtomicOrdering Order,
1455 SynchronizationScope SynchScope,
1456 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001457 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001458 OperandTraits<StoreInst>::op_begin(this),
1459 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001460 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001461 Op<0>() = val;
1462 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001463 setVolatile(isVolatile);
1464 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001465 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001466 AssertOK();
1467}
1468
1469StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001470 unsigned Align, AtomicOrdering Order,
1471 SynchronizationScope SynchScope,
1472 BasicBlock *InsertAtEnd)
1473 : Instruction(Type::getVoidTy(val->getContext()), Store,
1474 OperandTraits<StoreInst>::op_begin(this),
1475 OperandTraits<StoreInst>::operands(this),
1476 InsertAtEnd) {
1477 Op<0>() = val;
1478 Op<1>() = addr;
1479 setVolatile(isVolatile);
1480 setAlignment(Align);
1481 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001482 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483}
1484
Christopher Lamb84485702007-04-22 19:24:39 +00001485void StoreInst::setAlignment(unsigned Align) {
1486 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001487 assert(Align <= MaximumAlignment &&
1488 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001489 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001490 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001491 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001492}
1493
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001494//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001495// AtomicCmpXchgInst Implementation
1496//===----------------------------------------------------------------------===//
1497
1498void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001499 AtomicOrdering SuccessOrdering,
1500 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001501 SynchronizationScope SynchScope) {
1502 Op<0>() = Ptr;
1503 Op<1>() = Cmp;
1504 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001505 setSuccessOrdering(SuccessOrdering);
1506 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001507 setSynchScope(SynchScope);
1508
1509 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1510 "All operands must be non-null!");
1511 assert(getOperand(0)->getType()->isPointerTy() &&
1512 "Ptr must have pointer type!");
1513 assert(getOperand(1)->getType() ==
1514 cast<PointerType>(getOperand(0)->getType())->getElementType()
1515 && "Ptr must be a pointer to Cmp type!");
1516 assert(getOperand(2)->getType() ==
1517 cast<PointerType>(getOperand(0)->getType())->getElementType()
1518 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001519 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001520 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001521 assert(FailureOrdering != NotAtomic &&
1522 "AtomicCmpXchg instructions must be atomic!");
1523 assert(SuccessOrdering >= FailureOrdering &&
1524 "AtomicCmpXchg success ordering must be at least as strong as fail");
1525 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1526 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001527}
1528
1529AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001530 AtomicOrdering SuccessOrdering,
1531 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001532 SynchronizationScope SynchScope,
1533 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001534 : Instruction(
1535 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1536 nullptr),
1537 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1538 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001539 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001540}
1541
1542AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001543 AtomicOrdering SuccessOrdering,
1544 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001545 SynchronizationScope SynchScope,
1546 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001547 : Instruction(
1548 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1549 nullptr),
1550 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1551 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001552 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001553}
Tim Northover420a2162014-06-13 14:24:07 +00001554
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001555//===----------------------------------------------------------------------===//
1556// AtomicRMWInst Implementation
1557//===----------------------------------------------------------------------===//
1558
1559void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1560 AtomicOrdering Ordering,
1561 SynchronizationScope SynchScope) {
1562 Op<0>() = Ptr;
1563 Op<1>() = Val;
1564 setOperation(Operation);
1565 setOrdering(Ordering);
1566 setSynchScope(SynchScope);
1567
1568 assert(getOperand(0) && getOperand(1) &&
1569 "All operands must be non-null!");
1570 assert(getOperand(0)->getType()->isPointerTy() &&
1571 "Ptr must have pointer type!");
1572 assert(getOperand(1)->getType() ==
1573 cast<PointerType>(getOperand(0)->getType())->getElementType()
1574 && "Ptr must be a pointer to Val type!");
1575 assert(Ordering != NotAtomic &&
1576 "AtomicRMW instructions must be atomic!");
1577}
1578
1579AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1580 AtomicOrdering Ordering,
1581 SynchronizationScope SynchScope,
1582 Instruction *InsertBefore)
1583 : Instruction(Val->getType(), AtomicRMW,
1584 OperandTraits<AtomicRMWInst>::op_begin(this),
1585 OperandTraits<AtomicRMWInst>::operands(this),
1586 InsertBefore) {
1587 Init(Operation, Ptr, Val, Ordering, SynchScope);
1588}
1589
1590AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1591 AtomicOrdering Ordering,
1592 SynchronizationScope SynchScope,
1593 BasicBlock *InsertAtEnd)
1594 : Instruction(Val->getType(), AtomicRMW,
1595 OperandTraits<AtomicRMWInst>::op_begin(this),
1596 OperandTraits<AtomicRMWInst>::operands(this),
1597 InsertAtEnd) {
1598 Init(Operation, Ptr, Val, Ordering, SynchScope);
1599}
1600
1601//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001602// FenceInst Implementation
1603//===----------------------------------------------------------------------===//
1604
1605FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1606 SynchronizationScope SynchScope,
1607 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001608 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001609 setOrdering(Ordering);
1610 setSynchScope(SynchScope);
1611}
1612
1613FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1614 SynchronizationScope SynchScope,
1615 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001616 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001617 setOrdering(Ordering);
1618 setSynchScope(SynchScope);
1619}
1620
1621//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622// GetElementPtrInst Implementation
1623//===----------------------------------------------------------------------===//
1624
Jay Foadd1b78492011-07-25 09:48:08 +00001625void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001626 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001627 assert(getNumOperands() == 1 + IdxList.size() &&
1628 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001629 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001630 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001631 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001632}
1633
Gabor Greiff6caff662008-05-10 08:32:32 +00001634GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001635 : Instruction(GEPI.getType(), GetElementPtr,
1636 OperandTraits<GetElementPtrInst>::op_end(this) -
1637 GEPI.getNumOperands(),
1638 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001639 SourceElementType(GEPI.SourceElementType),
1640 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001641 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001642 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001643}
1644
Chris Lattner6090a422009-03-09 04:46:40 +00001645/// getIndexedType - Returns the type of the element that would be accessed with
1646/// a gep instruction with the specified parameters.
1647///
1648/// The Idxs pointer should point to a continuous piece of memory containing the
1649/// indices, either as Value* or uint64_t.
1650///
1651/// A null type is returned if the indices are invalid for the specified
1652/// pointer type.
1653///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001654template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001655static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001656 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001657 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001658 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001659
Chris Lattner6090a422009-03-09 04:46:40 +00001660 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001661 // it cannot be 'stepped over'.
1662 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001663 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001664
Dan Gohman1ecaf452008-05-31 00:58:22 +00001665 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001666 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001667 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001668 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001669 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001670 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001671 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001672 }
Craig Topperc6207612014-04-09 06:08:46 +00001673 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001674}
1675
David Blaikied288fb82015-03-30 21:41:43 +00001676Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1677 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001678}
1679
David Blaikied288fb82015-03-30 21:41:43 +00001680Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001681 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001682 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001683}
1684
David Blaikied288fb82015-03-30 21:41:43 +00001685Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1686 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001687}
1688
Chris Lattner45f15572007-04-14 00:12:57 +00001689/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1690/// zeros. If so, the result pointer and the first operand have the same
1691/// value, just potentially different types.
1692bool GetElementPtrInst::hasAllZeroIndices() const {
1693 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1694 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1695 if (!CI->isZero()) return false;
1696 } else {
1697 return false;
1698 }
1699 }
1700 return true;
1701}
1702
Chris Lattner27058292007-04-27 20:35:56 +00001703/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1704/// constant integers. If so, the result pointer and the first operand have
1705/// a constant offset between them.
1706bool GetElementPtrInst::hasAllConstantIndices() const {
1707 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1708 if (!isa<ConstantInt>(getOperand(i)))
1709 return false;
1710 }
1711 return true;
1712}
1713
Dan Gohman1b849082009-09-07 23:54:19 +00001714void GetElementPtrInst::setIsInBounds(bool B) {
1715 cast<GEPOperator>(this)->setIsInBounds(B);
1716}
Chris Lattner45f15572007-04-14 00:12:57 +00001717
Nick Lewycky28a5f252009-09-27 21:33:04 +00001718bool GetElementPtrInst::isInBounds() const {
1719 return cast<GEPOperator>(this)->isInBounds();
1720}
1721
Chandler Carruth1e140532012-12-11 10:29:10 +00001722bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1723 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001724 // Delegate to the generic GEPOperator implementation.
1725 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001726}
1727
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001729// ExtractElementInst Implementation
1730//===----------------------------------------------------------------------===//
1731
1732ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001733 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001734 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001735 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001736 ExtractElement,
1737 OperandTraits<ExtractElementInst>::op_begin(this),
1738 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001739 assert(isValidOperands(Val, Index) &&
1740 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001741 Op<0>() = Val;
1742 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001743 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001744}
1745
1746ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001747 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001748 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001749 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001750 ExtractElement,
1751 OperandTraits<ExtractElementInst>::op_begin(this),
1752 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001753 assert(isValidOperands(Val, Index) &&
1754 "Invalid extractelement instruction operands!");
1755
Gabor Greif2d3024d2008-05-26 21:33:52 +00001756 Op<0>() = Val;
1757 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001758 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001759}
1760
Chris Lattner65511ff2006-10-05 06:24:58 +00001761
Chris Lattner54865b32006-04-08 04:05:48 +00001762bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001763 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001764 return false;
1765 return true;
1766}
1767
1768
Robert Bocchino23004482006-01-10 19:05:34 +00001769//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001770// InsertElementInst Implementation
1771//===----------------------------------------------------------------------===//
1772
Chris Lattner54865b32006-04-08 04:05:48 +00001773InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001774 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001775 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001776 : Instruction(Vec->getType(), InsertElement,
1777 OperandTraits<InsertElementInst>::op_begin(this),
1778 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001779 assert(isValidOperands(Vec, Elt, Index) &&
1780 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001781 Op<0>() = Vec;
1782 Op<1>() = Elt;
1783 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001784 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001785}
1786
Chris Lattner54865b32006-04-08 04:05:48 +00001787InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001788 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001789 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001790 : Instruction(Vec->getType(), InsertElement,
1791 OperandTraits<InsertElementInst>::op_begin(this),
1792 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001793 assert(isValidOperands(Vec, Elt, Index) &&
1794 "Invalid insertelement instruction operands!");
1795
Gabor Greif2d3024d2008-05-26 21:33:52 +00001796 Op<0>() = Vec;
1797 Op<1>() = Elt;
1798 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001799 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001800}
1801
Chris Lattner54865b32006-04-08 04:05:48 +00001802bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1803 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001804 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001805 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001806
Reid Spencerd84d35b2007-02-15 02:26:10 +00001807 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001808 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001809
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001810 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001811 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001812 return true;
1813}
1814
1815
Robert Bocchinoca27f032006-01-17 20:07:22 +00001816//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001817// ShuffleVectorInst Implementation
1818//===----------------------------------------------------------------------===//
1819
1820ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001821 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001822 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001823: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001824 cast<VectorType>(Mask->getType())->getNumElements()),
1825 ShuffleVector,
1826 OperandTraits<ShuffleVectorInst>::op_begin(this),
1827 OperandTraits<ShuffleVectorInst>::operands(this),
1828 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001829 assert(isValidOperands(V1, V2, Mask) &&
1830 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001831 Op<0>() = V1;
1832 Op<1>() = V2;
1833 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001834 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001835}
1836
1837ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001838 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001839 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001840: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1841 cast<VectorType>(Mask->getType())->getNumElements()),
1842 ShuffleVector,
1843 OperandTraits<ShuffleVectorInst>::op_begin(this),
1844 OperandTraits<ShuffleVectorInst>::operands(this),
1845 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001846 assert(isValidOperands(V1, V2, Mask) &&
1847 "Invalid shuffle vector instruction operands!");
1848
Gabor Greif2d3024d2008-05-26 21:33:52 +00001849 Op<0>() = V1;
1850 Op<1>() = V2;
1851 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001852 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001853}
1854
Mon P Wang25f01062008-11-10 04:46:22 +00001855bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001856 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001857 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001858 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001859 return false;
1860
Chris Lattner1dcb6542012-01-25 23:49:49 +00001861 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001862 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001863 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001864 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001865
1866 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001867 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1868 return true;
1869
Nate Begeman60a31c32010-08-13 00:16:46 +00001870 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001871 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001872 for (Value *Op : MV->operands()) {
1873 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001874 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001875 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001876 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001877 return false;
1878 }
1879 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001880 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001881 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882
1883 if (const ConstantDataSequential *CDS =
1884 dyn_cast<ConstantDataSequential>(Mask)) {
1885 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1886 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1887 if (CDS->getElementAsInteger(i) >= V1Size*2)
1888 return false;
1889 return true;
1890 }
1891
1892 // The bitcode reader can create a place holder for a forward reference
1893 // used as the shuffle mask. When this occurs, the shuffle mask will
1894 // fall into this case and fail. To avoid this error, do this bit of
1895 // ugliness to allow such a mask pass.
1896 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1897 if (CE->getOpcode() == Instruction::UserOp1)
1898 return true;
1899
1900 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001901}
1902
Chris Lattnerf724e342008-03-02 05:28:33 +00001903/// getMaskValue - Return the index from the shuffle mask for the specified
1904/// output result. This is either -1 if the element is undef or a number less
1905/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001906int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1907 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1908 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001909 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001910 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001911 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001912 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001913 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001914}
1915
Chris Lattner1dcb6542012-01-25 23:49:49 +00001916/// getShuffleMask - Return the full mask for this instruction, where each
1917/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001918void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1919 SmallVectorImpl<int> &Result) {
1920 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001921
Chris Lattnercf129702012-01-26 02:51:13 +00001922 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001923 for (unsigned i = 0; i != NumElts; ++i)
1924 Result.push_back(CDS->getElementAsInteger(i));
1925 return;
1926 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001927 for (unsigned i = 0; i != NumElts; ++i) {
1928 Constant *C = Mask->getAggregateElement(i);
1929 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001930 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001931 }
1932}
1933
1934
Dan Gohman12fce772008-05-15 19:50:34 +00001935//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001936// InsertValueInst Class
1937//===----------------------------------------------------------------------===//
1938
Jay Foad57aa6362011-07-13 10:26:04 +00001939void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1940 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001941 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001942
1943 // There's no fundamental reason why we require at least one index
1944 // (other than weirdness with &*IdxBegin being invalid; see
1945 // getelementptr's init routine for example). But there's no
1946 // present need to support it.
1947 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1948
1949 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001950 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001951 Op<0>() = Agg;
1952 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001953
Jay Foad57aa6362011-07-13 10:26:04 +00001954 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001955 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001956}
1957
1958InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001959 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001960 OperandTraits<InsertValueInst>::op_begin(this), 2),
1961 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001962 Op<0>() = IVI.getOperand(0);
1963 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001964 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001965}
1966
1967//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001968// ExtractValueInst Class
1969//===----------------------------------------------------------------------===//
1970
Jay Foad57aa6362011-07-13 10:26:04 +00001971void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001972 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001973
Jay Foad57aa6362011-07-13 10:26:04 +00001974 // There's no fundamental reason why we require at least one index.
1975 // But there's no present need to support it.
1976 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001977
Jay Foad57aa6362011-07-13 10:26:04 +00001978 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001979 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001980}
1981
1982ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001983 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001984 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001985 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001986}
1987
Dan Gohman12fce772008-05-15 19:50:34 +00001988// getIndexedType - Returns the type of the element that would be extracted
1989// with an extractvalue instruction with the specified parameters.
1990//
1991// A null type is returned if the indices are invalid for the specified
1992// pointer type.
1993//
Chris Lattner229907c2011-07-18 04:54:35 +00001994Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001995 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001996 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001997 // We can't use CompositeType::indexValid(Index) here.
1998 // indexValid() always returns true for arrays because getelementptr allows
1999 // out-of-bounds indices. Since we don't allow those for extractvalue and
2000 // insertvalue we need to check array indexing manually.
2001 // Since the only other types we can index into are struct types it's just
2002 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002003 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002004 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002005 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002006 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002007 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002008 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002009 } else {
2010 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002011 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002012 }
2013
2014 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002015 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002016 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002017}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002018
2019//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002020// BinaryOperator Class
2021//===----------------------------------------------------------------------===//
2022
Chris Lattner2195fc42007-02-24 00:55:48 +00002023BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002024 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002025 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002026 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002027 OperandTraits<BinaryOperator>::op_begin(this),
2028 OperandTraits<BinaryOperator>::operands(this),
2029 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002030 Op<0>() = S1;
2031 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002032 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002033 setName(Name);
2034}
2035
2036BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002037 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002038 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002039 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002040 OperandTraits<BinaryOperator>::op_begin(this),
2041 OperandTraits<BinaryOperator>::operands(this),
2042 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002043 Op<0>() = S1;
2044 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002045 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002046 setName(Name);
2047}
2048
2049
2050void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002051 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002052 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002053 assert(LHS->getType() == RHS->getType() &&
2054 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002055#ifndef NDEBUG
2056 switch (iType) {
2057 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002058 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002059 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002060 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002061 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002062 "Tried to create an integer operation on a non-integer type!");
2063 break;
2064 case FAdd: case FSub:
2065 case FMul:
2066 assert(getType() == LHS->getType() &&
2067 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002068 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002069 "Tried to create a floating-point operation on a "
2070 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002071 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002072 case UDiv:
2073 case SDiv:
2074 assert(getType() == LHS->getType() &&
2075 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002076 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002077 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002078 "Incorrect operand type (not integer) for S/UDIV");
2079 break;
2080 case FDiv:
2081 assert(getType() == LHS->getType() &&
2082 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002083 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002084 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002085 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002086 case URem:
2087 case SRem:
2088 assert(getType() == LHS->getType() &&
2089 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002090 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002091 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002092 "Incorrect operand type (not integer) for S/UREM");
2093 break;
2094 case FRem:
2095 assert(getType() == LHS->getType() &&
2096 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002097 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002098 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002099 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002100 case Shl:
2101 case LShr:
2102 case AShr:
2103 assert(getType() == LHS->getType() &&
2104 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002105 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002106 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002107 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002108 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002109 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002110 case And: case Or:
2111 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002112 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002113 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002114 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002115 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002116 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002117 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002119 default:
2120 break;
2121 }
2122#endif
2123}
2124
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002125BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002126 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002127 Instruction *InsertBefore) {
2128 assert(S1->getType() == S2->getType() &&
2129 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002130 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002131}
2132
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002133BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002134 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002135 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002136 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002137 InsertAtEnd->getInstList().push_back(Res);
2138 return Res;
2139}
2140
Dan Gohman5476cfd2009-08-12 16:23:25 +00002141BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002142 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002143 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002144 return new BinaryOperator(Instruction::Sub,
2145 zero, Op,
2146 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002147}
2148
Dan Gohman5476cfd2009-08-12 16:23:25 +00002149BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002150 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002151 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002152 return new BinaryOperator(Instruction::Sub,
2153 zero, Op,
2154 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002155}
2156
Dan Gohman4ab44202009-12-18 02:58:50 +00002157BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2158 Instruction *InsertBefore) {
2159 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2160 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2161}
2162
2163BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2164 BasicBlock *InsertAtEnd) {
2165 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2166 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2167}
2168
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002169BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2170 Instruction *InsertBefore) {
2171 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2172 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2173}
2174
2175BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2176 BasicBlock *InsertAtEnd) {
2177 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2178 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2179}
2180
Dan Gohman5476cfd2009-08-12 16:23:25 +00002181BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002182 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002183 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002184 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002185 Op->getType(), Name, InsertBefore);
2186}
2187
Dan Gohman5476cfd2009-08-12 16:23:25 +00002188BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002189 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002190 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002191 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002192 Op->getType(), Name, InsertAtEnd);
2193}
2194
Dan Gohman5476cfd2009-08-12 16:23:25 +00002195BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002196 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002197 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002198 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002199 Op->getType(), Name, InsertBefore);
2200}
2201
Dan Gohman5476cfd2009-08-12 16:23:25 +00002202BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002203 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002204 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002205 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002206 Op->getType(), Name, InsertAtEnd);
2207}
2208
2209
2210// isConstantAllOnes - Helper function for several functions below
2211static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002212 if (const Constant *C = dyn_cast<Constant>(V))
2213 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002214 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002215}
2216
Owen Andersonbb2501b2009-07-13 22:18:28 +00002217bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002218 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2219 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002220 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2221 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002222 return false;
2223}
2224
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002225bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002226 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2227 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002228 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2229 if (!IgnoreZeroSign)
2230 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2231 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2232 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002233 return false;
2234}
2235
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002236bool BinaryOperator::isNot(const Value *V) {
2237 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2238 return (Bop->getOpcode() == Instruction::Xor &&
2239 (isConstantAllOnes(Bop->getOperand(1)) ||
2240 isConstantAllOnes(Bop->getOperand(0))));
2241 return false;
2242}
2243
Chris Lattner2c7d1772005-04-24 07:28:37 +00002244Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002245 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002246}
2247
Chris Lattner2c7d1772005-04-24 07:28:37 +00002248const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2249 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002250}
2251
Dan Gohmana5b96452009-06-04 22:49:04 +00002252Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002253 return cast<BinaryOperator>(BinOp)->getOperand(1);
2254}
2255
2256const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2257 return getFNegArgument(const_cast<Value*>(BinOp));
2258}
2259
Chris Lattner2c7d1772005-04-24 07:28:37 +00002260Value *BinaryOperator::getNotArgument(Value *BinOp) {
2261 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2262 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2263 Value *Op0 = BO->getOperand(0);
2264 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002265 if (isConstantAllOnes(Op0)) return Op1;
2266
2267 assert(isConstantAllOnes(Op1));
2268 return Op0;
2269}
2270
Chris Lattner2c7d1772005-04-24 07:28:37 +00002271const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2272 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002273}
2274
2275
2276// swapOperands - Exchange the two operands to this instruction. This
2277// instruction is safe to use on any binary instruction and does not
2278// modify the semantics of the instruction. If the instruction is
2279// order dependent (SetLT f.e.) the opcode is changed.
2280//
2281bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002282 if (!isCommutative())
2283 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002284 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002285 return false;
2286}
2287
Dan Gohman1b849082009-09-07 23:54:19 +00002288void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2289 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2290}
2291
2292void BinaryOperator::setHasNoSignedWrap(bool b) {
2293 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2294}
2295
2296void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002297 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002298}
2299
Nick Lewycky28a5f252009-09-27 21:33:04 +00002300bool BinaryOperator::hasNoUnsignedWrap() const {
2301 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2302}
2303
2304bool BinaryOperator::hasNoSignedWrap() const {
2305 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2306}
2307
2308bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002309 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002310}
2311
Sanjay Patela982d992014-09-03 01:06:50 +00002312void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002313 // Copy the wrapping flags.
2314 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2315 setHasNoSignedWrap(OB->hasNoSignedWrap());
2316 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2317 }
2318
2319 // Copy the exact flag.
2320 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2321 setIsExact(PE->isExact());
2322
2323 // Copy the fast-math flags.
2324 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002325 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002326}
2327
Sanjay Patela982d992014-09-03 01:06:50 +00002328void BinaryOperator::andIRFlags(const Value *V) {
2329 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2330 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2331 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2332 }
2333
2334 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2335 setIsExact(isExact() & PE->isExact());
2336
2337 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2338 FastMathFlags FM = getFastMathFlags();
2339 FM &= FP->getFastMathFlags();
2340 copyFastMathFlags(FM);
2341 }
2342}
2343
2344
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002345//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002346// FPMathOperator Class
2347//===----------------------------------------------------------------------===//
2348
2349/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2350/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002351/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002352float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002353 const MDNode *MD =
2354 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002355 if (!MD)
2356 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002357 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002358 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002359}
2360
2361
2362//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002363// CastInst Class
2364//===----------------------------------------------------------------------===//
2365
David Blaikie3a15e142011-12-01 08:00:17 +00002366void CastInst::anchor() {}
2367
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368// Just determine if this cast only deals with integral->integral conversion.
2369bool CastInst::isIntegerCast() const {
2370 switch (getOpcode()) {
2371 default: return false;
2372 case Instruction::ZExt:
2373 case Instruction::SExt:
2374 case Instruction::Trunc:
2375 return true;
2376 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002377 return getOperand(0)->getType()->isIntegerTy() &&
2378 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002380}
2381
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382bool CastInst::isLosslessCast() const {
2383 // Only BitCast can be lossless, exit fast if we're not BitCast
2384 if (getOpcode() != Instruction::BitCast)
2385 return false;
2386
2387 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002388 Type* SrcTy = getOperand(0)->getType();
2389 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 if (SrcTy == DstTy)
2391 return true;
2392
Reid Spencer8d9336d2006-12-31 05:26:44 +00002393 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002394 if (SrcTy->isPointerTy())
2395 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 return false; // Other types have no identity values
2397}
2398
2399/// This function determines if the CastInst does not require any bits to be
2400/// changed in order to effect the cast. Essentially, it identifies cases where
2401/// no code gen is necessary for the cast, hence the name no-op cast. For
2402/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002403/// # bitcast i32* %x to i8*
2404/// # bitcast <2 x i32> %x to <4 x i16>
2405/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002406/// @brief Determine if the described cast is a no-op.
2407bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002408 Type *SrcTy,
2409 Type *DestTy,
2410 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002411 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002412 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 case Instruction::Trunc:
2414 case Instruction::ZExt:
2415 case Instruction::SExt:
2416 case Instruction::FPTrunc:
2417 case Instruction::FPExt:
2418 case Instruction::UIToFP:
2419 case Instruction::SIToFP:
2420 case Instruction::FPToUI:
2421 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002422 case Instruction::AddrSpaceCast:
2423 // TODO: Target informations may give a more accurate answer here.
2424 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 case Instruction::BitCast:
2426 return true; // BitCast never modifies bits.
2427 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002428 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002429 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002431 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002432 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 }
2434}
2435
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002436/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002437bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002438 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2439}
2440
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002441bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002442 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002443 if (getOpcode() == Instruction::PtrToInt)
2444 PtrOpTy = getOperand(0)->getType();
2445 else if (getOpcode() == Instruction::IntToPtr)
2446 PtrOpTy = getType();
2447
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002448 Type *IntPtrTy =
2449 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002450
2451 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2452}
2453
2454/// This function determines if a pair of casts can be eliminated and what
2455/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456/// instructions like this:
2457/// * %F = firstOpcode SrcTy %x to MidTy
2458/// * %S = secondOpcode MidTy %F to DstTy
2459/// The function returns a resultOpcode so these two casts can be replaced with:
2460/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2461/// If no such cast is permited, the function returns 0.
2462unsigned CastInst::isEliminableCastPair(
2463 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002464 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2465 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466 // Define the 144 possibilities for these two cast instructions. The values
2467 // in this matrix determine what to do in a given situation and select the
2468 // case in the switch below. The rows correspond to firstOp, the columns
2469 // correspond to secondOp. In looking at the table below, keep in mind
2470 // the following cast properties:
2471 //
2472 // Size Compare Source Destination
2473 // Operator Src ? Size Type Sign Type Sign
2474 // -------- ------------ ------------------- ---------------------
2475 // TRUNC > Integer Any Integral Any
2476 // ZEXT < Integral Unsigned Integer Any
2477 // SEXT < Integral Signed Integer Any
2478 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002479 // FPTOSI n/a FloatPt n/a Integral Signed
2480 // UITOFP n/a Integral Unsigned FloatPt n/a
2481 // SITOFP n/a Integral Signed FloatPt n/a
2482 // FPTRUNC > FloatPt n/a FloatPt n/a
2483 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 // PTRTOINT n/a Pointer n/a Integral Unsigned
2485 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002486 // BITCAST = FirstClass n/a FirstClass n/a
2487 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002488 //
2489 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002490 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2491 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002492 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002493 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002494 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002495 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2498 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002499 // T F F U S F F P I B A -+
2500 // R Z S P P I I T P 2 N T S |
2501 // U E E 2 2 2 2 R E I T C C +- secondOp
2502 // N X X U S F F N X N 2 V V |
2503 // C T T I I P P C T T P T T -+
2504 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002505 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002506 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2507 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2508 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2509 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2510 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002511 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002512 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2513 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2514 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2515 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2516 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002518
Chris Lattner25eea4d2010-07-12 01:19:22 +00002519 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002520 // merging. However, bitcast of A->B->A are allowed.
2521 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2522 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2523 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2524
2525 // Check if any of the bitcasts convert scalars<->vectors.
2526 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2527 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2528 // Unless we are bitcasing to the original type, disallow optimizations.
2529 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530
2531 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2532 [secondOp-Instruction::CastOpsBegin];
2533 switch (ElimCase) {
2534 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002535 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 return 0;
2537 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002538 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 return firstOp;
2540 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002541 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 return secondOp;
2543 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002544 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002545 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002546 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002547 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 return firstOp;
2549 return 0;
2550 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002551 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002552 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002553 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554 return firstOp;
2555 return 0;
2556 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002557 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002558 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002559 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560 return secondOp;
2561 return 0;
2562 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002563 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002564 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002565 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566 return secondOp;
2567 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002568 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002569 // Cannot simplify if address spaces are different!
2570 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2571 return 0;
2572
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002573 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002574 // We can still fold this without knowing the actual sizes as long we
2575 // know that the intermediate pointer is the largest possible
2576 // pointer size.
2577 // FIXME: Is this always true?
2578 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002579 return Instruction::BitCast;
2580
2581 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002582 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002583 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002584 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 if (MidSize >= PtrSize)
2586 return Instruction::BitCast;
2587 return 0;
2588 }
2589 case 8: {
2590 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2591 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2592 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002593 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2594 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 if (SrcSize == DstSize)
2596 return Instruction::BitCast;
2597 else if (SrcSize < DstSize)
2598 return firstOp;
2599 return secondOp;
2600 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002601 case 9:
2602 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603 return Instruction::ZExt;
2604 case 10:
2605 // fpext followed by ftrunc is allowed if the bit size returned to is
2606 // the same as the original, in which case its just a bitcast
2607 if (SrcTy == DstTy)
2608 return Instruction::BitCast;
2609 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002610 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002612 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002613 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002614 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002615 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2616 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617 if (SrcSize <= PtrSize && SrcSize == DstSize)
2618 return Instruction::BitCast;
2619 return 0;
2620 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002621 case 12: {
2622 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2623 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2624 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2625 return Instruction::AddrSpaceCast;
2626 return Instruction::BitCast;
2627 }
2628 case 13:
2629 // FIXME: this state can be merged with (1), but the following assert
2630 // is useful to check the correcteness of the sequence due to semantic
2631 // change of bitcast.
2632 assert(
2633 SrcTy->isPtrOrPtrVectorTy() &&
2634 MidTy->isPtrOrPtrVectorTy() &&
2635 DstTy->isPtrOrPtrVectorTy() &&
2636 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2637 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2638 "Illegal addrspacecast, bitcast sequence!");
2639 // Allowed, use first cast's opcode
2640 return firstOp;
2641 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002642 // bitcast, addrspacecast -> addrspacecast if the element type of
2643 // bitcast's source is the same as that of addrspacecast's destination.
2644 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2645 return Instruction::AddrSpaceCast;
2646 return 0;
2647
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002648 case 15:
2649 // FIXME: this state can be merged with (1), but the following assert
2650 // is useful to check the correcteness of the sequence due to semantic
2651 // change of bitcast.
2652 assert(
2653 SrcTy->isIntOrIntVectorTy() &&
2654 MidTy->isPtrOrPtrVectorTy() &&
2655 DstTy->isPtrOrPtrVectorTy() &&
2656 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2657 "Illegal inttoptr, bitcast sequence!");
2658 // Allowed, use first cast's opcode
2659 return firstOp;
2660 case 16:
2661 // FIXME: this state can be merged with (2), but the following assert
2662 // is useful to check the correcteness of the sequence due to semantic
2663 // change of bitcast.
2664 assert(
2665 SrcTy->isPtrOrPtrVectorTy() &&
2666 MidTy->isPtrOrPtrVectorTy() &&
2667 DstTy->isIntOrIntVectorTy() &&
2668 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2669 "Illegal bitcast, ptrtoint sequence!");
2670 // Allowed, use second cast's opcode
2671 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002672 case 17:
2673 // (sitofp (zext x)) -> (uitofp x)
2674 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002676 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002678 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 default:
Craig Topperc514b542012-02-05 22:14:15 +00002680 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682}
2683
Chris Lattner229907c2011-07-18 04:54:35 +00002684CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002686 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 // Construct and return the appropriate CastInst subclass
2688 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002689 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2690 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2691 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2692 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2693 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2694 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2695 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2696 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2697 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2698 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2699 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2700 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2701 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2702 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704}
2705
Chris Lattner229907c2011-07-18 04:54:35 +00002706CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002707 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002708 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709 // Construct and return the appropriate CastInst subclass
2710 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002711 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2712 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2713 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2714 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2715 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2716 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2717 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2718 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2719 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2720 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2721 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2722 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2723 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2724 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002726}
2727
Chris Lattner229907c2011-07-18 04:54:35 +00002728CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002729 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002730 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002731 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002732 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2733 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002734}
2735
Chris Lattner229907c2011-07-18 04:54:35 +00002736CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002737 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002738 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002739 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2741 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002742}
2743
Chris Lattner229907c2011-07-18 04:54:35 +00002744CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002745 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002746 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002747 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002748 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2749 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002750}
2751
Chris Lattner229907c2011-07-18 04:54:35 +00002752CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002753 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002754 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002755 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002756 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2757 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002758}
2759
Chris Lattner229907c2011-07-18 04:54:35 +00002760CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002761 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002762 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002763 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002764 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2765 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002766}
2767
Chris Lattner229907c2011-07-18 04:54:35 +00002768CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002769 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002770 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002771 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002772 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2773 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002774}
2775
Chris Lattner229907c2011-07-18 04:54:35 +00002776CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002777 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002778 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002779 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2780 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2781 "Invalid cast");
2782 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002783 assert((!Ty->isVectorTy() ||
2784 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002785 "Invalid cast");
2786
Matt Arsenault065ced92013-07-31 00:17:33 +00002787 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002788 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002789
Matt Arsenault740980e2014-07-14 17:24:35 +00002790 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002791}
2792
2793/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002794CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2795 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002796 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002797 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2798 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002799 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002800 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002801 assert((!Ty->isVectorTy() ||
2802 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002803 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002804
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002805 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002806 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002807
Matt Arsenault740980e2014-07-14 17:24:35 +00002808 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2809}
2810
2811CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2812 Value *S, Type *Ty,
2813 const Twine &Name,
2814 BasicBlock *InsertAtEnd) {
2815 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2816 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2817
2818 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2819 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2820
2821 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2822}
2823
2824CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2825 Value *S, Type *Ty,
2826 const Twine &Name,
2827 Instruction *InsertBefore) {
2828 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2829 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2830
2831 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002832 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2833
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002834 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002835}
2836
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002837CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2838 const Twine &Name,
2839 Instruction *InsertBefore) {
2840 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2841 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2842 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2843 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2844
2845 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2846}
2847
Matt Arsenault740980e2014-07-14 17:24:35 +00002848CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002849 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002850 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002851 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002852 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002853 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2854 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002855 Instruction::CastOps opcode =
2856 (SrcBits == DstBits ? Instruction::BitCast :
2857 (SrcBits > DstBits ? Instruction::Trunc :
2858 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002859 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002860}
2861
Chris Lattner229907c2011-07-18 04:54:35 +00002862CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002863 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002864 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002865 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002866 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002867 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2868 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002869 Instruction::CastOps opcode =
2870 (SrcBits == DstBits ? Instruction::BitCast :
2871 (SrcBits > DstBits ? Instruction::Trunc :
2872 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002873 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002874}
2875
Chris Lattner229907c2011-07-18 04:54:35 +00002876CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002877 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002878 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002879 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002880 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002881 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2882 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002883 Instruction::CastOps opcode =
2884 (SrcBits == DstBits ? Instruction::BitCast :
2885 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002886 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002887}
2888
Chris Lattner229907c2011-07-18 04:54:35 +00002889CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002890 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002891 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002892 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002893 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002894 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2895 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002896 Instruction::CastOps opcode =
2897 (SrcBits == DstBits ? Instruction::BitCast :
2898 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002899 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002900}
2901
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002902// Check whether it is valid to call getCastOpcode for these types.
2903// This routine must be kept in sync with getCastOpcode.
2904bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2905 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2906 return false;
2907
2908 if (SrcTy == DestTy)
2909 return true;
2910
2911 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2912 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2913 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2914 // An element by element cast. Valid if casting the elements is valid.
2915 SrcTy = SrcVecTy->getElementType();
2916 DestTy = DestVecTy->getElementType();
2917 }
2918
2919 // Get the bit sizes, we'll need these
2920 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2921 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2922
2923 // Run through the possibilities ...
2924 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002925 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002926 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002927 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002928 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002929 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002930 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002931 // Casting from something else
2932 return SrcTy->isPointerTy();
2933 }
2934 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2935 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002936 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002937 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002938 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002939 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002940 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002941 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002942 return false;
2943 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002944 if (DestTy->isVectorTy()) // Casting to vector
2945 return DestBits == SrcBits;
2946 if (DestTy->isPointerTy()) { // Casting to pointer
2947 if (SrcTy->isPointerTy()) // Casting from pointer
2948 return true;
2949 return SrcTy->isIntegerTy(); // Casting from integral
2950 }
2951 if (DestTy->isX86_MMXTy()) {
2952 if (SrcTy->isVectorTy())
2953 return DestBits == SrcBits; // 64-bit vector to MMX
2954 return false;
2955 } // Casting to something else
2956 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002957}
2958
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002959bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2960 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2961 return false;
2962
2963 if (SrcTy == DestTy)
2964 return true;
2965
2966 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2967 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2968 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2969 // An element by element cast. Valid if casting the elements is valid.
2970 SrcTy = SrcVecTy->getElementType();
2971 DestTy = DestVecTy->getElementType();
2972 }
2973 }
2974 }
2975
2976 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2977 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2978 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2979 }
2980 }
2981
2982 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2983 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2984
2985 // Could still have vectors of pointers if the number of elements doesn't
2986 // match
2987 if (SrcBits == 0 || DestBits == 0)
2988 return false;
2989
2990 if (SrcBits != DestBits)
2991 return false;
2992
2993 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2994 return false;
2995
2996 return true;
2997}
2998
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002999bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003000 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003001 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3002 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003003 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003004 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3005 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003006 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003007
3008 return isBitCastable(SrcTy, DestTy);
3009}
3010
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003011// Provide a way to get a "cast" where the cast opcode is inferred from the
3012// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003013// logic in the castIsValid function below. This axiom should hold:
3014// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3015// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003017// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003018Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003019CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003020 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3021 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003022
Duncan Sands55e50902008-01-06 10:12:28 +00003023 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3024 "Only first class types are castable!");
3025
Duncan Sandsa8514532011-05-18 07:13:41 +00003026 if (SrcTy == DestTy)
3027 return BitCast;
3028
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003029 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003030 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3031 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003032 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3033 // An element by element cast. Find the appropriate opcode based on the
3034 // element types.
3035 SrcTy = SrcVecTy->getElementType();
3036 DestTy = DestVecTy->getElementType();
3037 }
3038
3039 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003040 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3041 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003042
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003044 if (DestTy->isIntegerTy()) { // Casting to integral
3045 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003046 if (DestBits < SrcBits)
3047 return Trunc; // int -> smaller int
3048 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003049 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050 return SExt; // signed -> SEXT
3051 else
3052 return ZExt; // unsigned -> ZEXT
3053 } else {
3054 return BitCast; // Same size, No-op cast
3055 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003056 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003057 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003058 return FPToSI; // FP -> sint
3059 else
3060 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003061 } else if (SrcTy->isVectorTy()) {
3062 assert(DestBits == SrcBits &&
3063 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 return BitCast; // Same size, no-op cast
3065 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003066 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067 "Casting from a value that is not first-class type");
3068 return PtrToInt; // ptr -> int
3069 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003070 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3071 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003072 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003073 return SIToFP; // sint -> FP
3074 else
3075 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003076 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077 if (DestBits < SrcBits) {
3078 return FPTrunc; // FP -> smaller FP
3079 } else if (DestBits > SrcBits) {
3080 return FPExt; // FP -> larger FP
3081 } else {
3082 return BitCast; // same size, no-op cast
3083 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003084 } else if (SrcTy->isVectorTy()) {
3085 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003086 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003087 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003088 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003089 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003090 } else if (DestTy->isVectorTy()) {
3091 assert(DestBits == SrcBits &&
3092 "Illegal cast to vector (wrong type or size)");
3093 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003094 } else if (DestTy->isPointerTy()) {
3095 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003096 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3097 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003098 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003099 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003102 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003103 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003104 if (SrcTy->isVectorTy()) {
3105 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003106 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003107 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003108 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003109 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003110 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003111}
3112
3113//===----------------------------------------------------------------------===//
3114// CastInst SubClass Constructors
3115//===----------------------------------------------------------------------===//
3116
3117/// Check that the construction parameters for a CastInst are correct. This
3118/// could be broken out into the separate constructors but it is useful to have
3119/// it in one place and to eliminate the redundant code for getting the sizes
3120/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003121bool
Chris Lattner229907c2011-07-18 04:54:35 +00003122CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123
3124 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003125 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003126
Chris Lattner37bc78a2010-01-26 21:51:43 +00003127 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3128 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129 return false;
3130
3131 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003132 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3133 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003134
Duncan Sands7f646562011-05-18 09:21:57 +00003135 // If these are vector types, get the lengths of the vectors (using zero for
3136 // scalar types means that checking that vector lengths match also checks that
3137 // scalars are not being converted to vectors or vectors to scalars).
3138 unsigned SrcLength = SrcTy->isVectorTy() ?
3139 cast<VectorType>(SrcTy)->getNumElements() : 0;
3140 unsigned DstLength = DstTy->isVectorTy() ?
3141 cast<VectorType>(DstTy)->getNumElements() : 0;
3142
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 // Switch on the opcode provided
3144 switch (op) {
3145 default: return false; // This is an input error
3146 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003147 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3148 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003150 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3151 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003153 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3154 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003156 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3157 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003158 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003159 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3160 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003163 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3164 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003165 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003167 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3168 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003170 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003171 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003172 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3173 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3174 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003175 return SrcTy->getScalarType()->isPointerTy() &&
3176 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003177 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003178 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003179 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003180 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3181 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3182 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003183 return SrcTy->getScalarType()->isIntegerTy() &&
3184 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003185 case Instruction::BitCast: {
3186 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3187 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3188
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189 // BitCast implies a no-op cast of type only. No bits change.
3190 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003191 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192 return false;
3193
Alp Tokerf907b892013-12-05 05:44:44 +00003194 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003195 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003196 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003197 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3198
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003199 // If both are pointers then the address spaces must match.
3200 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3201 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003202
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003203 // A vector of pointers must have the same number of elements.
3204 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3205 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3206 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3207
3208 return false;
3209 }
3210
3211 return true;
3212 }
3213 case Instruction::AddrSpaceCast: {
3214 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3215 if (!SrcPtrTy)
3216 return false;
3217
3218 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3219 if (!DstPtrTy)
3220 return false;
3221
3222 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3223 return false;
3224
3225 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3226 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3227 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3228
3229 return false;
3230 }
3231
3232 return true;
3233 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234 }
3235}
3236
3237TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
3243TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254
3255ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003256 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259}
3260SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003261 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
3265
Jeff Cohencc08c832006-12-02 02:22:01 +00003266SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003267 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003268) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270}
3271
3272FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003273 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003274) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276}
3277
3278FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003279 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003280) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282}
3283
3284FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003285 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003286) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288}
3289
3290FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003291 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003292) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294}
3295
3296UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003297 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003298) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300}
3301
3302UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003303 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003304) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306}
3307
3308SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003309 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003310) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312}
3313
3314SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003315 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003316) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318}
3319
3320FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003321 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003322) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324}
3325
3326FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003327 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003328) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330}
3331
3332FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003333 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003334) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336}
3337
3338FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003339 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003340) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342}
3343
3344PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003345 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003346) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003348}
3349
3350PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003351 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003352) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003354}
3355
3356IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003357 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003358) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003360}
3361
3362IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003363 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003364) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003366}
3367
3368BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003369 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003370) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003372}
3373
3374BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003375 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003376) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003378}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003379
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003380AddrSpaceCastInst::AddrSpaceCastInst(
3381 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3382) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3384}
3385
3386AddrSpaceCastInst::AddrSpaceCastInst(
3387 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3388) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3389 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3390}
3391
Chris Lattnerf16dc002006-09-17 19:29:56 +00003392//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003393// CmpInst Classes
3394//===----------------------------------------------------------------------===//
3395
Craig Topper3186c012012-09-23 02:12:10 +00003396void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003397
Chris Lattner229907c2011-07-18 04:54:35 +00003398CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003399 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003400 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003401 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003402 OperandTraits<CmpInst>::op_begin(this),
3403 OperandTraits<CmpInst>::operands(this),
3404 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003405 Op<0>() = LHS;
3406 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003407 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003408 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003409}
Gabor Greiff6caff662008-05-10 08:32:32 +00003410
Chris Lattner229907c2011-07-18 04:54:35 +00003411CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003412 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003413 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003414 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003415 OperandTraits<CmpInst>::op_begin(this),
3416 OperandTraits<CmpInst>::operands(this),
3417 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003418 Op<0>() = LHS;
3419 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003420 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003421 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003422}
3423
3424CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003425CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003426 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003427 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003428 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003429 if (InsertBefore)
3430 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3431 S1, S2, Name);
3432 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003433 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003434 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003435 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003436
3437 if (InsertBefore)
3438 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3439 S1, S2, Name);
3440 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003441 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003442 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003443}
3444
3445CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003446CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003447 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003448 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003449 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3450 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003451 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003452 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3453 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003454}
3455
3456void CmpInst::swapOperands() {
3457 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3458 IC->swapOperands();
3459 else
3460 cast<FCmpInst>(this)->swapOperands();
3461}
3462
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003463bool CmpInst::isCommutative() const {
3464 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003465 return IC->isCommutative();
3466 return cast<FCmpInst>(this)->isCommutative();
3467}
3468
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003469bool CmpInst::isEquality() const {
3470 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003471 return IC->isEquality();
3472 return cast<FCmpInst>(this)->isEquality();
3473}
3474
3475
Dan Gohman4e724382008-05-31 02:47:54 +00003476CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003477 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003478 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003479 case ICMP_EQ: return ICMP_NE;
3480 case ICMP_NE: return ICMP_EQ;
3481 case ICMP_UGT: return ICMP_ULE;
3482 case ICMP_ULT: return ICMP_UGE;
3483 case ICMP_UGE: return ICMP_ULT;
3484 case ICMP_ULE: return ICMP_UGT;
3485 case ICMP_SGT: return ICMP_SLE;
3486 case ICMP_SLT: return ICMP_SGE;
3487 case ICMP_SGE: return ICMP_SLT;
3488 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003489
Dan Gohman4e724382008-05-31 02:47:54 +00003490 case FCMP_OEQ: return FCMP_UNE;
3491 case FCMP_ONE: return FCMP_UEQ;
3492 case FCMP_OGT: return FCMP_ULE;
3493 case FCMP_OLT: return FCMP_UGE;
3494 case FCMP_OGE: return FCMP_ULT;
3495 case FCMP_OLE: return FCMP_UGT;
3496 case FCMP_UEQ: return FCMP_ONE;
3497 case FCMP_UNE: return FCMP_OEQ;
3498 case FCMP_UGT: return FCMP_OLE;
3499 case FCMP_ULT: return FCMP_OGE;
3500 case FCMP_UGE: return FCMP_OLT;
3501 case FCMP_ULE: return FCMP_OGT;
3502 case FCMP_ORD: return FCMP_UNO;
3503 case FCMP_UNO: return FCMP_ORD;
3504 case FCMP_TRUE: return FCMP_FALSE;
3505 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003506 }
3507}
3508
Reid Spencer266e42b2006-12-23 06:05:41 +00003509ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3510 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003511 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003512 case ICMP_EQ: case ICMP_NE:
3513 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3514 return pred;
3515 case ICMP_UGT: return ICMP_SGT;
3516 case ICMP_ULT: return ICMP_SLT;
3517 case ICMP_UGE: return ICMP_SGE;
3518 case ICMP_ULE: return ICMP_SLE;
3519 }
3520}
3521
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003522ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3523 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003524 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003525 case ICMP_EQ: case ICMP_NE:
3526 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3527 return pred;
3528 case ICMP_SGT: return ICMP_UGT;
3529 case ICMP_SLT: return ICMP_ULT;
3530 case ICMP_SGE: return ICMP_UGE;
3531 case ICMP_SLE: return ICMP_ULE;
3532 }
3533}
3534
Reid Spencer0286bc12007-02-28 22:00:54 +00003535/// Initialize a set of values that all satisfy the condition with C.
3536///
3537ConstantRange
3538ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3539 APInt Lower(C);
3540 APInt Upper(C);
3541 uint32_t BitWidth = C.getBitWidth();
3542 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003543 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003544 case ICmpInst::ICMP_EQ: ++Upper; break;
3545 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003546 case ICmpInst::ICMP_ULT:
3547 Lower = APInt::getMinValue(BitWidth);
3548 // Check for an empty-set condition.
3549 if (Lower == Upper)
3550 return ConstantRange(BitWidth, /*isFullSet=*/false);
3551 break;
3552 case ICmpInst::ICMP_SLT:
3553 Lower = APInt::getSignedMinValue(BitWidth);
3554 // Check for an empty-set condition.
3555 if (Lower == Upper)
3556 return ConstantRange(BitWidth, /*isFullSet=*/false);
3557 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003558 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003559 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003560 // Check for an empty-set condition.
3561 if (Lower == Upper)
3562 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003563 break;
3564 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003565 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003566 // Check for an empty-set condition.
3567 if (Lower == Upper)
3568 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003569 break;
3570 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003571 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003572 // Check for a full-set condition.
3573 if (Lower == Upper)
3574 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003575 break;
3576 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003577 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003578 // Check for a full-set condition.
3579 if (Lower == Upper)
3580 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003581 break;
3582 case ICmpInst::ICMP_UGE:
3583 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003584 // Check for a full-set condition.
3585 if (Lower == Upper)
3586 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003587 break;
3588 case ICmpInst::ICMP_SGE:
3589 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003590 // Check for a full-set condition.
3591 if (Lower == Upper)
3592 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003593 break;
3594 }
3595 return ConstantRange(Lower, Upper);
3596}
3597
Dan Gohman4e724382008-05-31 02:47:54 +00003598CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003599 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003600 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003601 case ICMP_EQ: case ICMP_NE:
3602 return pred;
3603 case ICMP_SGT: return ICMP_SLT;
3604 case ICMP_SLT: return ICMP_SGT;
3605 case ICMP_SGE: return ICMP_SLE;
3606 case ICMP_SLE: return ICMP_SGE;
3607 case ICMP_UGT: return ICMP_ULT;
3608 case ICMP_ULT: return ICMP_UGT;
3609 case ICMP_UGE: return ICMP_ULE;
3610 case ICMP_ULE: return ICMP_UGE;
3611
Reid Spencerd9436b62006-11-20 01:22:35 +00003612 case FCMP_FALSE: case FCMP_TRUE:
3613 case FCMP_OEQ: case FCMP_ONE:
3614 case FCMP_UEQ: case FCMP_UNE:
3615 case FCMP_ORD: case FCMP_UNO:
3616 return pred;
3617 case FCMP_OGT: return FCMP_OLT;
3618 case FCMP_OLT: return FCMP_OGT;
3619 case FCMP_OGE: return FCMP_OLE;
3620 case FCMP_OLE: return FCMP_OGE;
3621 case FCMP_UGT: return FCMP_ULT;
3622 case FCMP_ULT: return FCMP_UGT;
3623 case FCMP_UGE: return FCMP_ULE;
3624 case FCMP_ULE: return FCMP_UGE;
3625 }
3626}
3627
Sanjoy Das6e78b172015-10-22 19:57:34 +00003628CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3629 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3630
3631 switch (pred) {
3632 default:
3633 llvm_unreachable("Unknown predicate!");
3634 case CmpInst::ICMP_ULT:
3635 return CmpInst::ICMP_SLT;
3636 case CmpInst::ICMP_ULE:
3637 return CmpInst::ICMP_SLE;
3638 case CmpInst::ICMP_UGT:
3639 return CmpInst::ICMP_SGT;
3640 case CmpInst::ICMP_UGE:
3641 return CmpInst::ICMP_SGE;
3642 }
3643}
3644
Reid Spencer266e42b2006-12-23 06:05:41 +00003645bool CmpInst::isUnsigned(unsigned short predicate) {
3646 switch (predicate) {
3647 default: return false;
3648 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3649 case ICmpInst::ICMP_UGE: return true;
3650 }
3651}
3652
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003653bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003654 switch (predicate) {
3655 default: return false;
3656 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3657 case ICmpInst::ICMP_SGE: return true;
3658 }
3659}
3660
3661bool CmpInst::isOrdered(unsigned short predicate) {
3662 switch (predicate) {
3663 default: return false;
3664 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3665 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3666 case FCmpInst::FCMP_ORD: return true;
3667 }
3668}
3669
3670bool CmpInst::isUnordered(unsigned short predicate) {
3671 switch (predicate) {
3672 default: return false;
3673 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3674 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3675 case FCmpInst::FCMP_UNO: return true;
3676 }
3677}
3678
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003679bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3680 switch(predicate) {
3681 default: return false;
3682 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3683 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3684 }
3685}
3686
3687bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3688 switch(predicate) {
3689 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3690 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3691 default: return false;
3692 }
3693}
3694
3695
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003696//===----------------------------------------------------------------------===//
3697// SwitchInst Implementation
3698//===----------------------------------------------------------------------===//
3699
Chris Lattnerbaf00152010-11-17 05:41:46 +00003700void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3701 assert(Value && Default && NumReserved);
3702 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003703 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003704 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003705
Pete Coopereb31b682015-05-21 22:48:54 +00003706 Op<0>() = Value;
3707 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003708}
3709
Chris Lattner2195fc42007-02-24 00:55:48 +00003710/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3711/// switch on and a default destination. The number of additional cases can
3712/// be specified here to make memory allocation more efficient. This
3713/// constructor can also autoinsert before another instruction.
3714SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3715 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003716 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003717 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003718 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003719}
3720
3721/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3722/// switch on and a default destination. The number of additional cases can
3723/// be specified here to make memory allocation more efficient. This
3724/// constructor also autoinserts at the end of the specified BasicBlock.
3725SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3726 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003727 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003728 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003729 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003730}
3731
Misha Brukmanb1c93172005-04-21 23:48:37 +00003732SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003733 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003734 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003735 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003736 Use *OL = getOperandList();
3737 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003738 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003739 OL[i] = InOL[i];
3740 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003741 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003742 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003743}
3744
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003745
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003746/// addCase - Add an entry to the switch instruction...
3747///
Chris Lattner47ac1872005-02-24 05:32:09 +00003748void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003749 unsigned NewCaseIdx = getNumCases();
3750 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003751 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003752 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003753 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003754 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003755 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003756 CaseIt Case(this, NewCaseIdx);
3757 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003758 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003759}
3760
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003761/// removeCase - This method removes the specified case and its successor
3762/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003763void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003764 unsigned idx = i.getCaseIndex();
3765
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003766 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003767
3768 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003769 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003770
Jay Foad14277722011-02-01 09:22:34 +00003771 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003772 if (2 + (idx + 1) * 2 != NumOps) {
3773 OL[2 + idx * 2] = OL[NumOps - 2];
3774 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003775 }
3776
3777 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003778 OL[NumOps-2].set(nullptr);
3779 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003780 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003781}
3782
Jay Foade98f29d2011-04-01 08:00:58 +00003783/// growOperands - grow operands - This grows the operand list in response
3784/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003785///
Jay Foade98f29d2011-04-01 08:00:58 +00003786void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003787 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003788 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003789
3790 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003791 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003792}
3793
3794
3795BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3796 return getSuccessor(idx);
3797}
3798unsigned SwitchInst::getNumSuccessorsV() const {
3799 return getNumSuccessors();
3800}
3801void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3802 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003803}
Chris Lattnerf22be932004-10-15 23:52:53 +00003804
Chris Lattner3ed871f2009-10-27 19:13:16 +00003805//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003806// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807//===----------------------------------------------------------------------===//
3808
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003809void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003810 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003811 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003812 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003813 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003814 allocHungoffUses(ReservedSpace);
3815
Pete Coopereb31b682015-05-21 22:48:54 +00003816 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817}
3818
3819
Jay Foade98f29d2011-04-01 08:00:58 +00003820/// growOperands - grow operands - This grows the operand list in response
3821/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003822///
Jay Foade98f29d2011-04-01 08:00:58 +00003823void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003824 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003825 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003826
3827 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003828 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003829}
3830
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003831IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3832 Instruction *InsertBefore)
3833: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003834 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003835 init(Address, NumCases);
3836}
3837
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003838IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3839 BasicBlock *InsertAtEnd)
3840: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003841 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003842 init(Address, NumCases);
3843}
3844
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003845IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003846 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3847 nullptr, IBI.getNumOperands()) {
3848 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003849 Use *OL = getOperandList();
3850 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003851 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3852 OL[i] = InOL[i];
3853 SubclassOptionalData = IBI.SubclassOptionalData;
3854}
3855
Chris Lattner3ed871f2009-10-27 19:13:16 +00003856/// addDestination - Add a destination.
3857///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003858void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003859 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003860 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003861 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003862 // Initialize some new operands.
3863 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003864 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003865 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003866}
3867
3868/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003869/// indirectbr instruction.
3870void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003871 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3872
3873 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003874 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003875
3876 // Replace this value with the last one.
3877 OL[idx+1] = OL[NumOps-1];
3878
3879 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003880 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003881 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003882}
3883
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003884BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003885 return getSuccessor(idx);
3886}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003887unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003888 return getNumSuccessors();
3889}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003890void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003891 setSuccessor(idx, B);
3892}
3893
3894//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003895// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003896//===----------------------------------------------------------------------===//
3897
Chris Lattnerf22be932004-10-15 23:52:53 +00003898// Define these methods here so vtables don't get emitted into every translation
3899// unit that uses these classes.
3900
Pete Cooper75403d72015-06-24 20:22:23 +00003901GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003902 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003903}
3904
Pete Cooper75403d72015-06-24 20:22:23 +00003905BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003906 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003907}
3908
Pete Cooper75403d72015-06-24 20:22:23 +00003909FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003910 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003911}
3912
Pete Cooper75403d72015-06-24 20:22:23 +00003913ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003914 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003915}
3916
Pete Cooper75403d72015-06-24 20:22:23 +00003917ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003918 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919}
3920
Pete Cooper75403d72015-06-24 20:22:23 +00003921InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003922 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003923}
3924
Pete Cooper75403d72015-06-24 20:22:23 +00003925AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003926 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3927 (Value *)getOperand(0), getAlignment());
3928 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3929 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003930}
3931
Pete Cooper75403d72015-06-24 20:22:23 +00003932LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003933 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3934 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003938 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003939 getAlignment(), getOrdering(), getSynchScope());
3940
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941}
3942
Pete Cooper75403d72015-06-24 20:22:23 +00003943AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003944 AtomicCmpXchgInst *Result =
3945 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003946 getSuccessOrdering(), getFailureOrdering(),
3947 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003948 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003949 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003950 return Result;
3951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003954 AtomicRMWInst *Result =
3955 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3956 getOrdering(), getSynchScope());
3957 Result->setVolatile(isVolatile());
3958 return Result;
3959}
3960
Pete Cooper75403d72015-06-24 20:22:23 +00003961FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003962 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3963}
3964
Pete Cooper75403d72015-06-24 20:22:23 +00003965TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003966 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003967}
3968
Pete Cooper75403d72015-06-24 20:22:23 +00003969ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003970 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003971}
3972
Pete Cooper75403d72015-06-24 20:22:23 +00003973SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003974 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003975}
3976
Pete Cooper75403d72015-06-24 20:22:23 +00003977FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003979}
3980
Pete Cooper75403d72015-06-24 20:22:23 +00003981FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003982 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003983}
3984
Pete Cooper75403d72015-06-24 20:22:23 +00003985UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003986 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003987}
3988
Pete Cooper75403d72015-06-24 20:22:23 +00003989SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003990 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003991}
3992
Pete Cooper75403d72015-06-24 20:22:23 +00003993FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003994 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003995}
3996
Pete Cooper75403d72015-06-24 20:22:23 +00003997FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003998 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003999}
4000
Pete Cooper75403d72015-06-24 20:22:23 +00004001PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004002 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004003}
4004
Pete Cooper75403d72015-06-24 20:22:23 +00004005IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004006 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004007}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004008
Pete Cooper75403d72015-06-24 20:22:23 +00004009BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004010 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004011}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00004012
Pete Cooper75403d72015-06-24 20:22:23 +00004013AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004014 return new AddrSpaceCastInst(getOperand(0), getType());
4015}
4016
Pete Cooper75403d72015-06-24 20:22:23 +00004017CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004018 if (hasOperandBundles()) {
4019 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4020 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4021 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004022 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004023}
4024
Pete Cooper75403d72015-06-24 20:22:23 +00004025SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004026 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004027}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004028
Pete Cooper75403d72015-06-24 20:22:23 +00004029VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004030 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004031}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004032
Pete Cooper75403d72015-06-24 20:22:23 +00004033ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004034 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004035}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004036
Pete Cooper75403d72015-06-24 20:22:23 +00004037InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004038 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004039}
4040
Pete Cooper75403d72015-06-24 20:22:23 +00004041ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004042 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004043}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004044
Pete Cooper75403d72015-06-24 20:22:23 +00004045PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004046
Pete Cooper75403d72015-06-24 20:22:23 +00004047LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004048 return new LandingPadInst(*this);
4049}
4050
Pete Cooper75403d72015-06-24 20:22:23 +00004051ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004052 return new(getNumOperands()) ReturnInst(*this);
4053}
4054
Pete Cooper75403d72015-06-24 20:22:23 +00004055BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004056 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004057}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004058
Pete Cooper75403d72015-06-24 20:22:23 +00004059SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004060
Pete Cooper75403d72015-06-24 20:22:23 +00004061IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004062 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004063}
4064
Pete Cooper75403d72015-06-24 20:22:23 +00004065InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004066 if (hasOperandBundles()) {
4067 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4068 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4069 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004070 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004071}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004072
Pete Cooper75403d72015-06-24 20:22:23 +00004073ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004074
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00004075CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
4076 return new (getNumOperands()) CleanupEndPadInst(*this);
4077}
4078
David Majnemer654e1302015-07-31 17:58:14 +00004079CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4080 return new (getNumOperands()) CleanupReturnInst(*this);
4081}
4082
4083CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
4084 return new (getNumOperands()) CatchEndPadInst(*this);
4085}
4086
4087CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004088 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004089}
4090
4091CatchPadInst *CatchPadInst::cloneImpl() const {
4092 return new (getNumOperands()) CatchPadInst(*this);
4093}
4094
4095TerminatePadInst *TerminatePadInst::cloneImpl() const {
4096 return new (getNumOperands()) TerminatePadInst(*this);
4097}
4098
4099CleanupPadInst *CleanupPadInst::cloneImpl() const {
4100 return new (getNumOperands()) CleanupPadInst(*this);
4101}
4102
Pete Cooper75403d72015-06-24 20:22:23 +00004103UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004104 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004105 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004106}