blob: e79fa415ffb209138393121e90fd10f95b520a3e [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,
229 const Twine &NameStr) {
230 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000231 assert(getNumOperands() == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000232 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000233
Jay Foad5bd375a2011-07-15 08:37:34 +0000234#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000235 assert((Args.size() == FTy->getNumParams() ||
236 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000237 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000238
239 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000240 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000241 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000242 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000243#endif
244
245 std::copy(Args.begin(), Args.end(), op_begin());
246 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247}
248
Jay Foad5bd375a2011-07-15 08:37:34 +0000249void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 FTy =
251 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000252 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000255 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000256
257 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258}
259
Daniel Dunbar4975db62009-07-25 04:41:11 +0000260CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000261 Instruction *InsertBefore)
262 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
263 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000264 Instruction::Call,
265 OperandTraits<CallInst>::op_end(this) - 1,
266 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000267 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268}
269
Daniel Dunbar4975db62009-07-25 04:41:11 +0000270CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 BasicBlock *InsertAtEnd)
272 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
273 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000274 Instruction::Call,
275 OperandTraits<CallInst>::op_end(this) - 1,
276 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000277 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278}
279
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000281 : Instruction(CI.getType(), Instruction::Call,
282 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
283 CI.getNumOperands()),
284 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000285 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000286 setCallingConv(CI.getCallingConv());
287
Jay Foad5bd375a2011-07-15 08:37:34 +0000288 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000289 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290}
291
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000292void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000293 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000294 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000295 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000296}
297
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000298void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
299 AttributeSet PAL = getAttributes();
300 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
301 setAttributes(PAL);
302}
303
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000304void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000305 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000306 AttrBuilder B(attr);
307 LLVMContext &Context = getContext();
308 PAL = PAL.removeAttributes(Context, i,
309 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000310 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000311}
312
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000313void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
314 AttributeSet PAL = getAttributes();
315 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
316 setAttributes(PAL);
317}
318
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000319void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
320 AttributeSet PAL = getAttributes();
321 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
322 setAttributes(PAL);
323}
324
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000325bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000326 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000327 return true;
328 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000329 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000330 return false;
331}
332
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000333/// IsConstantOne - Return true only if val is constant int 1
334static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000335 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000336 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
337 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000338}
339
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000340static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000341 BasicBlock *InsertAtEnd, Type *IntPtrTy,
342 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000343 Value *ArraySize, Function *MallocF,
344 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000345 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000346 "createMalloc needs either InsertBefore or InsertAtEnd");
347
348 // malloc(type) becomes:
349 // bitcast (i8* malloc(typeSize)) to type*
350 // malloc(type, arraySize) becomes:
351 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000352 if (!ArraySize)
353 ArraySize = ConstantInt::get(IntPtrTy, 1);
354 else if (ArraySize->getType() != IntPtrTy) {
355 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000356 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
357 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000358 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000359 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
360 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000361 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000362
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000363 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000364 if (IsConstantOne(AllocSize)) {
365 AllocSize = ArraySize; // Operand * 1 = Operand
366 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
367 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
368 false /*ZExt*/);
369 // Malloc arg is constant product of type size and array size
370 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
371 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000372 // Multiply type size by the array size...
373 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000374 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
375 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000377 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
378 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000379 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000380 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000381
Victor Hernandez788eaab2009-09-18 19:20:02 +0000382 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000383 // Create the call to Malloc.
384 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
385 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000386 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000387 Value *MallocFunc = MallocF;
388 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000389 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000390 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000391 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000392 CallInst *MCall = nullptr;
393 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000394 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000395 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000396 Result = MCall;
397 if (Result->getType() != AllocPtrType)
398 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000399 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000400 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000401 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 Result = MCall;
403 if (Result->getType() != AllocPtrType) {
404 InsertAtEnd->getInstList().push_back(MCall);
405 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000406 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000407 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000408 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000409 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000410 if (Function *F = dyn_cast<Function>(MallocFunc)) {
411 MCall->setCallingConv(F->getCallingConv());
412 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
413 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000414 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000415
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000416 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000417}
418
419/// CreateMalloc - Generate the IR for a call to malloc:
420/// 1. Compute the malloc call's argument as the specified type's size,
421/// possibly multiplied by the array size if the array size is not
422/// constant 1.
423/// 2. Call malloc with that argument.
424/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000425Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000426 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000428 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000429 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000430 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000431 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000432}
433
434/// CreateMalloc - Generate the IR for a call to malloc:
435/// 1. Compute the malloc call's argument as the specified type's size,
436/// possibly multiplied by the array size if the array size is not
437/// constant 1.
438/// 2. Call malloc with that argument.
439/// 3. Bitcast the result of the malloc call to the specified type.
440/// Note: This function does not add the bitcast to the basic block, that is the
441/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000442Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000443 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000444 Value *AllocSize, Value *ArraySize,
445 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000446 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000447 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000448}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000449
Victor Hernandeze2971492009-10-24 04:23:03 +0000450static Instruction* createFree(Value* Source, Instruction *InsertBefore,
451 BasicBlock *InsertAtEnd) {
452 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
453 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000454 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000455 "Can not free something of nonpointer type!");
456
457 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
458 Module* M = BB->getParent()->getParent();
459
Chris Lattner229907c2011-07-18 04:54:35 +0000460 Type *VoidTy = Type::getVoidTy(M->getContext());
461 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000462 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000463 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000464 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000465 Value *PtrCast = Source;
466 if (InsertBefore) {
467 if (Source->getType() != IntPtrTy)
468 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
469 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
470 } else {
471 if (Source->getType() != IntPtrTy)
472 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
473 Result = CallInst::Create(FreeFunc, PtrCast, "");
474 }
475 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000476 if (Function *F = dyn_cast<Function>(FreeFunc))
477 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000478
479 return Result;
480}
481
482/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000483Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000484 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000485}
486
487/// CreateFree - Generate the IR for a call to the builtin free function.
488/// Note: This function does not add the call to the basic block, that is the
489/// responsibility of the caller.
490Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000491 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000492 assert(FreeCall && "CreateFree did not create a CallInst");
493 return FreeCall;
494}
495
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496//===----------------------------------------------------------------------===//
497// InvokeInst Implementation
498//===----------------------------------------------------------------------===//
499
David Blaikie3e807092015-05-13 18:35:26 +0000500void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
501 BasicBlock *IfException, ArrayRef<Value *> Args,
502 const Twine &NameStr) {
503 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000504
Pete Cooperb4eede22015-06-12 17:48:10 +0000505 assert(getNumOperands() == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000506 Op<-3>() = Fn;
507 Op<-2>() = IfNormal;
508 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000509
510#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000511 assert(((Args.size() == FTy->getNumParams()) ||
512 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000513 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000514
Jay Foad5bd375a2011-07-15 08:37:34 +0000515 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000516 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000517 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000518 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000519#endif
520
521 std::copy(Args.begin(), Args.end(), op_begin());
522 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000523}
524
Misha Brukmanb1c93172005-04-21 23:48:37 +0000525InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000526 : TerminatorInst(II.getType(), Instruction::Invoke,
527 OperandTraits<InvokeInst>::op_end(this) -
528 II.getNumOperands(),
529 II.getNumOperands()),
530 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000531 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000532 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000533 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000534}
535
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
537 return getSuccessor(idx);
538}
539unsigned InvokeInst::getNumSuccessorsV() const {
540 return getNumSuccessors();
541}
542void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
543 return setSuccessor(idx, B);
544}
545
Michael Gottesman41748d72013-06-27 00:25:01 +0000546bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000547 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000548 return true;
549 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000550 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000551 return false;
552}
553
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000554bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000555 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000556 return true;
557 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000558 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000559 return false;
560}
561
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000562void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000563 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000564 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000565 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000566}
567
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000568void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000569 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000570 AttrBuilder B(attr);
571 PAL = PAL.removeAttributes(getContext(), i,
572 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000573 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000574}
575
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000576void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
577 AttributeSet PAL = getAttributes();
578 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
579 setAttributes(PAL);
580}
581
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000582void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
583 AttributeSet PAL = getAttributes();
584 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
585 setAttributes(PAL);
586}
587
Bill Wendlingfae14752011-08-12 20:24:12 +0000588LandingPadInst *InvokeInst::getLandingPadInst() const {
589 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
590}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000591
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000592//===----------------------------------------------------------------------===//
593// ReturnInst Implementation
594//===----------------------------------------------------------------------===//
595
Chris Lattner2195fc42007-02-24 00:55:48 +0000596ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000597 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000598 OperandTraits<ReturnInst>::op_end(this) -
599 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000600 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000601 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000602 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000603 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000604}
605
Owen Anderson55f1c092009-08-13 21:58:54 +0000606ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
607 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000608 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
609 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000610 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000611 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000612}
Owen Anderson55f1c092009-08-13 21:58:54 +0000613ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
614 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000615 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
616 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000617 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000618 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000619}
Owen Anderson55f1c092009-08-13 21:58:54 +0000620ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
621 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000622 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000623}
624
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625unsigned ReturnInst::getNumSuccessorsV() const {
626 return getNumSuccessors();
627}
628
Devang Patelae682fb2008-02-26 17:56:20 +0000629/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
630/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000632 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000635BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000636 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637}
638
Devang Patel59643e52008-02-23 00:35:18 +0000639ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000640}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000642//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000643// ResumeInst Implementation
644//===----------------------------------------------------------------------===//
645
646ResumeInst::ResumeInst(const ResumeInst &RI)
647 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
648 OperandTraits<ResumeInst>::op_begin(this), 1) {
649 Op<0>() = RI.Op<0>();
650}
651
652ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
653 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
654 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
655 Op<0>() = Exn;
656}
657
658ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
659 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
660 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
661 Op<0>() = Exn;
662}
663
664unsigned ResumeInst::getNumSuccessorsV() const {
665 return getNumSuccessors();
666}
667
668void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
669 llvm_unreachable("ResumeInst has no successors!");
670}
671
672BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
673 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000674}
675
676//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000677// CleanupReturnInst Implementation
678//===----------------------------------------------------------------------===//
679
680CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
681 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
682 OperandTraits<CleanupReturnInst>::op_end(this) -
683 CRI.getNumOperands(),
684 CRI.getNumOperands()) {
685 SubclassOptionalData = CRI.SubclassOptionalData;
David Majnemereb518bd2015-08-04 08:21:40 +0000686 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000687 Op<-1>() = CRI.Op<-1>();
688 if (CRI.hasUnwindDest())
689 Op<-2>() = CRI.Op<-2>();
David Majnemer654e1302015-07-31 17:58:14 +0000690}
691
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000692void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000693 SubclassOptionalData = 0;
694 if (UnwindBB)
695 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000696
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000697 Op<-1>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000698 if (UnwindBB)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000699 Op<-2>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000700}
701
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000702CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000703 BasicBlock *UnwindBB, unsigned Values,
704 Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000705 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
706 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000707 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
708 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000709 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000710}
711
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000712CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000713 BasicBlock *UnwindBB, unsigned Values,
714 BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000715 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
716 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000717 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
718 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000719 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000720}
721
722BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
723 assert(Idx == 0);
724 return getUnwindDest();
725}
726unsigned CleanupReturnInst::getNumSuccessorsV() const {
727 return getNumSuccessors();
728}
729void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
730 assert(Idx == 0);
731 setUnwindDest(B);
732}
733
734//===----------------------------------------------------------------------===//
735// CatchEndPadInst Implementation
736//===----------------------------------------------------------------------===//
737
738CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
739 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
740 OperandTraits<CatchEndPadInst>::op_end(this) -
741 CRI.getNumOperands(),
742 CRI.getNumOperands()) {
743 SubclassOptionalData = CRI.SubclassOptionalData;
David Majnemereb518bd2015-08-04 08:21:40 +0000744 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000745 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
746 setUnwindDest(UnwindDest);
747}
748
749void CatchEndPadInst::init(BasicBlock *UnwindBB) {
750 SubclassOptionalData = 0;
751 if (UnwindBB) {
752 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
753 setUnwindDest(UnwindBB);
754 }
755}
756
757CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000758 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000759 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
760 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
761 Values, InsertBefore) {
762 init(UnwindBB);
763}
764
765CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000766 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000767 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
768 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
769 Values, InsertAtEnd) {
770 init(UnwindBB);
771}
772
773BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
774 assert(Idx == 0);
775 return getUnwindDest();
776}
777unsigned CatchEndPadInst::getNumSuccessorsV() const {
778 return getNumSuccessors();
779}
780void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
781 assert(Idx == 0);
782 setUnwindDest(B);
783}
784
785//===----------------------------------------------------------------------===//
786// CatchReturnInst Implementation
787//===----------------------------------------------------------------------===//
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000788void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) {
789 Op<0>() = CatchPad;
790 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000791}
David Majnemer654e1302015-07-31 17:58:14 +0000792
793CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
794 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000795 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
796 Op<0>() = CRI.Op<0>();
797 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000798}
799
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000800CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000801 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000802 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000803 OperandTraits<CatchReturnInst>::op_begin(this), 2,
804 InsertBefore) {
805 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000806}
807
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000808CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000809 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000810 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000811 OperandTraits<CatchReturnInst>::op_begin(this), 2,
812 InsertAtEnd) {
813 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000814}
815
816BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
817 assert(Idx == 0);
818 return getSuccessor();
819}
820unsigned CatchReturnInst::getNumSuccessorsV() const {
821 return getNumSuccessors();
822}
823void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
824 assert(Idx == 0);
825 setSuccessor(B);
826}
827
828//===----------------------------------------------------------------------===//
829// CatchPadInst Implementation
830//===----------------------------------------------------------------------===//
831void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000832 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000833 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
834 Op<-2>() = IfNormal;
835 Op<-1>() = IfException;
836 std::copy(Args.begin(), Args.end(), op_begin());
837 setName(NameStr);
838}
839
840CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
841 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
842 OperandTraits<CatchPadInst>::op_end(this) -
843 CPI.getNumOperands(),
844 CPI.getNumOperands()) {
845 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
846}
847
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000848CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
849 ArrayRef<Value *> Args, unsigned Values,
850 const Twine &NameStr, Instruction *InsertBefore)
851 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
852 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000853 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
854 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000855 init(IfNormal, IfException, Args, NameStr);
856}
857
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000858CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
859 ArrayRef<Value *> Args, unsigned Values,
860 const Twine &NameStr, BasicBlock *InsertAtEnd)
861 : TerminatorInst(Type::getTokenTy(IfNormal->getContext()),
862 Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000863 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
864 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000865 init(IfNormal, IfException, Args, NameStr);
866}
867
868BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
869 return getSuccessor(Idx);
870}
871unsigned CatchPadInst::getNumSuccessorsV() const {
872 return getNumSuccessors();
873}
874void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
875 return setSuccessor(Idx, B);
876}
877
878//===----------------------------------------------------------------------===//
879// TerminatePadInst Implementation
880//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000881void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000882 SubclassOptionalData = 0;
883 if (BB)
884 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
885 if (BB)
886 Op<-1>() = BB;
887 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +0000888}
889
890TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
891 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
892 OperandTraits<TerminatePadInst>::op_end(this) -
893 TPI.getNumOperands(),
894 TPI.getNumOperands()) {
895 SubclassOptionalData = TPI.SubclassOptionalData;
David Majnemereb518bd2015-08-04 08:21:40 +0000896 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000897 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
898}
899
900TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000901 ArrayRef<Value *> Args, unsigned Values,
902 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000903 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
904 OperandTraits<TerminatePadInst>::op_end(this) - Values,
905 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000906 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000907}
908
909TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000910 ArrayRef<Value *> Args, unsigned Values,
911 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000912 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
913 OperandTraits<TerminatePadInst>::op_end(this) - Values,
914 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000915 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000916}
917
918BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
919 assert(Idx == 0);
920 return getUnwindDest();
921}
922unsigned TerminatePadInst::getNumSuccessorsV() const {
923 return getNumSuccessors();
924}
925void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
926 assert(Idx == 0);
927 return setUnwindDest(B);
928}
929
930//===----------------------------------------------------------------------===//
931// CleanupPadInst Implementation
932//===----------------------------------------------------------------------===//
933void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
934 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
935 std::copy(Args.begin(), Args.end(), op_begin());
936 setName(NameStr);
937}
938
939CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
940 : Instruction(CPI.getType(), Instruction::CleanupPad,
941 OperandTraits<CleanupPadInst>::op_end(this) -
942 CPI.getNumOperands(),
943 CPI.getNumOperands()) {
944 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
945}
946
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000947CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +0000948 const Twine &NameStr, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000949 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000950 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
951 Args.size(), InsertBefore) {
952 init(Args, NameStr);
953}
954
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000955CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +0000956 const Twine &NameStr, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000957 : Instruction(Type::getTokenTy(C), Instruction::CleanupPad,
David Majnemer654e1302015-07-31 17:58:14 +0000958 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
959 Args.size(), InsertAtEnd) {
960 init(Args, NameStr);
961}
962
963//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000964// UnreachableInst Implementation
965//===----------------------------------------------------------------------===//
966
Owen Anderson55f1c092009-08-13 21:58:54 +0000967UnreachableInst::UnreachableInst(LLVMContext &Context,
968 Instruction *InsertBefore)
969 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000970 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000971}
Owen Anderson55f1c092009-08-13 21:58:54 +0000972UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
973 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000974 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000975}
976
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977unsigned UnreachableInst::getNumSuccessorsV() const {
978 return getNumSuccessors();
979}
980
981void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000982 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000983}
984
985BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000986 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000987}
988
989//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000990// BranchInst Implementation
991//===----------------------------------------------------------------------===//
992
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993void BranchInst::AssertOK() {
994 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000995 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000997}
998
Chris Lattner2195fc42007-02-24 00:55:48 +0000999BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001000 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001001 OperandTraits<BranchInst>::op_end(this) - 1,
1002 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001003 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001004 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001005}
1006BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1007 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001008 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001009 OperandTraits<BranchInst>::op_end(this) - 3,
1010 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001011 Op<-1>() = IfTrue;
1012 Op<-2>() = IfFalse;
1013 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001014#ifndef NDEBUG
1015 AssertOK();
1016#endif
1017}
1018
1019BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001020 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001021 OperandTraits<BranchInst>::op_end(this) - 1,
1022 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001023 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001024 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001025}
1026
1027BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1028 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001029 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001030 OperandTraits<BranchInst>::op_end(this) - 3,
1031 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001032 Op<-1>() = IfTrue;
1033 Op<-2>() = IfFalse;
1034 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001035#ifndef NDEBUG
1036 AssertOK();
1037#endif
1038}
1039
1040
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001041BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001042 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001043 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1044 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001045 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046 if (BI.getNumOperands() != 1) {
1047 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001048 Op<-3>() = BI.Op<-3>();
1049 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001051 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001052}
1053
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001054void BranchInst::swapSuccessors() {
1055 assert(isConditional() &&
1056 "Cannot swap successors of an unconditional branch");
1057 Op<-1>().swap(Op<-2>());
1058
1059 // Update profile metadata if present and it matches our structural
1060 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001061 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001062 if (!ProfileData || ProfileData->getNumOperands() != 3)
1063 return;
1064
1065 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001066 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1067 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001068 setMetadata(LLVMContext::MD_prof,
1069 MDNode::get(ProfileData->getContext(), Ops));
1070}
1071
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1073 return getSuccessor(idx);
1074}
1075unsigned BranchInst::getNumSuccessorsV() const {
1076 return getNumSuccessors();
1077}
1078void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1079 setSuccessor(idx, B);
1080}
1081
1082
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001084// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085//===----------------------------------------------------------------------===//
1086
Owen Andersonb6b25302009-07-14 23:09:55 +00001087static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001088 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001089 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001090 else {
1091 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001092 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001093 assert(Amt->getType()->isIntegerTy() &&
1094 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001095 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001096 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001097}
1098
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001099AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1100 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001101
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001102AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1103 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001104
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001105AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001106 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001107 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001108
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001109AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001110 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001111 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001112
Chris Lattner229907c2011-07-18 04:54:35 +00001113AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001114 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001115 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1116 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1117 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001118 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001119 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001120 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001121}
1122
Chris Lattner229907c2011-07-18 04:54:35 +00001123AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001124 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001125 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1126 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1127 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001128 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001129 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001130 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131}
1132
Gordon Henriksen14a55692007-12-10 02:14:30 +00001133// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001134AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001135}
1136
Victor Hernandez8acf2952009-10-23 21:09:37 +00001137void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001138 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001139 assert(Align <= MaximumAlignment &&
1140 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001141 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1142 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001143 assert(getAlignment() == Align && "Alignment representation error!");
1144}
1145
Victor Hernandez8acf2952009-10-23 21:09:37 +00001146bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001147 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001148 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001149 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001150}
1151
Chris Lattner8b291e62008-11-26 02:54:17 +00001152/// isStaticAlloca - Return true if this alloca is in the entry block of the
1153/// function and is a constant size. If so, the code generator will fold it
1154/// into the prolog/epilog code, so it is basically free.
1155bool AllocaInst::isStaticAlloca() const {
1156 // Must be constant size.
1157 if (!isa<ConstantInt>(getArraySize())) return false;
1158
1159 // Must be in the entry block.
1160 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001161 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001162}
1163
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001164//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001165// LoadInst Implementation
1166//===----------------------------------------------------------------------===//
1167
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001168void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001169 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001170 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001171 assert(!(isAtomic() && getAlignment() == 0) &&
1172 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001173}
1174
Daniel Dunbar4975db62009-07-25 04:41:11 +00001175LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001176 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001177
Daniel Dunbar4975db62009-07-25 04:41:11 +00001178LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001179 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180
David Blaikie0c28fd72015-05-20 21:46:30 +00001181LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001182 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001183 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001184
1185LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1186 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001187 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001188
David Blaikieb7a029872015-04-17 19:56:21 +00001189LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001190 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001191 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001192 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001194LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001195 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001196 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001197}
1198
David Blaikie15d9a4c2015-04-06 20:59:48 +00001199LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001200 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001201 SynchronizationScope SynchScope, Instruction *InsertBef)
1202 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001203 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001204 setVolatile(isVolatile);
1205 setAlignment(Align);
1206 setAtomic(Order, SynchScope);
1207 AssertOK();
1208 setName(Name);
1209}
1210
1211LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1212 unsigned Align, AtomicOrdering Order,
1213 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001215 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001216 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001217 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001218 setAlignment(Align);
1219 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001220 AssertOK();
1221 setName(Name);
1222}
1223
Daniel Dunbar27096822009-08-11 18:11:15 +00001224LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1225 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1226 Load, Ptr, InsertBef) {
1227 setVolatile(false);
1228 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001229 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001230 AssertOK();
1231 if (Name && Name[0]) setName(Name);
1232}
1233
1234LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1235 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1236 Load, Ptr, InsertAE) {
1237 setVolatile(false);
1238 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001239 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001240 AssertOK();
1241 if (Name && Name[0]) setName(Name);
1242}
1243
David Blaikie0c28fd72015-05-20 21:46:30 +00001244LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001245 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001246 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1247 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001248 setVolatile(isVolatile);
1249 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001250 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001251 AssertOK();
1252 if (Name && Name[0]) setName(Name);
1253}
1254
1255LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1256 BasicBlock *InsertAE)
1257 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1258 Load, Ptr, InsertAE) {
1259 setVolatile(isVolatile);
1260 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001261 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001262 AssertOK();
1263 if (Name && Name[0]) setName(Name);
1264}
1265
Christopher Lamb84485702007-04-22 19:24:39 +00001266void LoadInst::setAlignment(unsigned Align) {
1267 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001268 assert(Align <= MaximumAlignment &&
1269 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001270 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001271 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001272 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001273}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001274
1275//===----------------------------------------------------------------------===//
1276// StoreInst Implementation
1277//===----------------------------------------------------------------------===//
1278
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001279void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001280 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001281 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001282 "Ptr must have pointer type!");
1283 assert(getOperand(0)->getType() ==
1284 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001285 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001286 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001287 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001288}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001289
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001290StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001291 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001292
1293StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001294 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001295
Misha Brukmanb1c93172005-04-21 23:48:37 +00001296StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001298 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001299
1300StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001301 BasicBlock *InsertAtEnd)
1302 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1303
1304StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1305 Instruction *InsertBefore)
1306 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1307 InsertBefore) {}
1308
1309StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1310 BasicBlock *InsertAtEnd)
1311 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1312 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001313
Misha Brukmanb1c93172005-04-21 23:48:37 +00001314StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001315 unsigned Align, AtomicOrdering Order,
1316 SynchronizationScope SynchScope,
1317 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001318 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001319 OperandTraits<StoreInst>::op_begin(this),
1320 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001321 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001322 Op<0>() = val;
1323 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001324 setVolatile(isVolatile);
1325 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001326 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001327 AssertOK();
1328}
1329
1330StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001331 unsigned Align, AtomicOrdering Order,
1332 SynchronizationScope SynchScope,
1333 BasicBlock *InsertAtEnd)
1334 : Instruction(Type::getVoidTy(val->getContext()), Store,
1335 OperandTraits<StoreInst>::op_begin(this),
1336 OperandTraits<StoreInst>::operands(this),
1337 InsertAtEnd) {
1338 Op<0>() = val;
1339 Op<1>() = addr;
1340 setVolatile(isVolatile);
1341 setAlignment(Align);
1342 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001343 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001344}
1345
Christopher Lamb84485702007-04-22 19:24:39 +00001346void StoreInst::setAlignment(unsigned Align) {
1347 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001348 assert(Align <= MaximumAlignment &&
1349 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001350 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001351 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001352 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001353}
1354
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001356// AtomicCmpXchgInst Implementation
1357//===----------------------------------------------------------------------===//
1358
1359void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001360 AtomicOrdering SuccessOrdering,
1361 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001362 SynchronizationScope SynchScope) {
1363 Op<0>() = Ptr;
1364 Op<1>() = Cmp;
1365 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001366 setSuccessOrdering(SuccessOrdering);
1367 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001368 setSynchScope(SynchScope);
1369
1370 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1371 "All operands must be non-null!");
1372 assert(getOperand(0)->getType()->isPointerTy() &&
1373 "Ptr must have pointer type!");
1374 assert(getOperand(1)->getType() ==
1375 cast<PointerType>(getOperand(0)->getType())->getElementType()
1376 && "Ptr must be a pointer to Cmp type!");
1377 assert(getOperand(2)->getType() ==
1378 cast<PointerType>(getOperand(0)->getType())->getElementType()
1379 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001380 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001381 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001382 assert(FailureOrdering != NotAtomic &&
1383 "AtomicCmpXchg instructions must be atomic!");
1384 assert(SuccessOrdering >= FailureOrdering &&
1385 "AtomicCmpXchg success ordering must be at least as strong as fail");
1386 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1387 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001388}
1389
1390AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001391 AtomicOrdering SuccessOrdering,
1392 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001393 SynchronizationScope SynchScope,
1394 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001395 : Instruction(
1396 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1397 nullptr),
1398 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1399 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001400 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001401}
1402
1403AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001404 AtomicOrdering SuccessOrdering,
1405 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001406 SynchronizationScope SynchScope,
1407 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001408 : Instruction(
1409 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1410 nullptr),
1411 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1412 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001413 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001414}
Tim Northover420a2162014-06-13 14:24:07 +00001415
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001416//===----------------------------------------------------------------------===//
1417// AtomicRMWInst Implementation
1418//===----------------------------------------------------------------------===//
1419
1420void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1421 AtomicOrdering Ordering,
1422 SynchronizationScope SynchScope) {
1423 Op<0>() = Ptr;
1424 Op<1>() = Val;
1425 setOperation(Operation);
1426 setOrdering(Ordering);
1427 setSynchScope(SynchScope);
1428
1429 assert(getOperand(0) && getOperand(1) &&
1430 "All operands must be non-null!");
1431 assert(getOperand(0)->getType()->isPointerTy() &&
1432 "Ptr must have pointer type!");
1433 assert(getOperand(1)->getType() ==
1434 cast<PointerType>(getOperand(0)->getType())->getElementType()
1435 && "Ptr must be a pointer to Val type!");
1436 assert(Ordering != NotAtomic &&
1437 "AtomicRMW instructions must be atomic!");
1438}
1439
1440AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1441 AtomicOrdering Ordering,
1442 SynchronizationScope SynchScope,
1443 Instruction *InsertBefore)
1444 : Instruction(Val->getType(), AtomicRMW,
1445 OperandTraits<AtomicRMWInst>::op_begin(this),
1446 OperandTraits<AtomicRMWInst>::operands(this),
1447 InsertBefore) {
1448 Init(Operation, Ptr, Val, Ordering, SynchScope);
1449}
1450
1451AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1452 AtomicOrdering Ordering,
1453 SynchronizationScope SynchScope,
1454 BasicBlock *InsertAtEnd)
1455 : Instruction(Val->getType(), AtomicRMW,
1456 OperandTraits<AtomicRMWInst>::op_begin(this),
1457 OperandTraits<AtomicRMWInst>::operands(this),
1458 InsertAtEnd) {
1459 Init(Operation, Ptr, Val, Ordering, SynchScope);
1460}
1461
1462//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001463// FenceInst Implementation
1464//===----------------------------------------------------------------------===//
1465
1466FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1467 SynchronizationScope SynchScope,
1468 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001469 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001470 setOrdering(Ordering);
1471 setSynchScope(SynchScope);
1472}
1473
1474FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1475 SynchronizationScope SynchScope,
1476 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001477 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001478 setOrdering(Ordering);
1479 setSynchScope(SynchScope);
1480}
1481
1482//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483// GetElementPtrInst Implementation
1484//===----------------------------------------------------------------------===//
1485
Jay Foadd1b78492011-07-25 09:48:08 +00001486void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001487 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001488 assert(getNumOperands() == 1 + IdxList.size() &&
1489 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001490 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001491 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001492 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001493}
1494
Gabor Greiff6caff662008-05-10 08:32:32 +00001495GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001496 : Instruction(GEPI.getType(), GetElementPtr,
1497 OperandTraits<GetElementPtrInst>::op_end(this) -
1498 GEPI.getNumOperands(),
1499 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001500 SourceElementType(GEPI.SourceElementType),
1501 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001502 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001503 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001504}
1505
Chris Lattner6090a422009-03-09 04:46:40 +00001506/// getIndexedType - Returns the type of the element that would be accessed with
1507/// a gep instruction with the specified parameters.
1508///
1509/// The Idxs pointer should point to a continuous piece of memory containing the
1510/// indices, either as Value* or uint64_t.
1511///
1512/// A null type is returned if the indices are invalid for the specified
1513/// pointer type.
1514///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001515template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001516static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001517 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001518 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001519 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001520
Chris Lattner6090a422009-03-09 04:46:40 +00001521 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001522 // it cannot be 'stepped over'.
1523 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001524 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001525
Dan Gohman1ecaf452008-05-31 00:58:22 +00001526 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001527 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001528 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001529 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001530 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001531 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001532 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001533 }
Craig Topperc6207612014-04-09 06:08:46 +00001534 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001535}
1536
David Blaikied288fb82015-03-30 21:41:43 +00001537Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1538 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001539}
1540
David Blaikied288fb82015-03-30 21:41:43 +00001541Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001542 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001543 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001544}
1545
David Blaikied288fb82015-03-30 21:41:43 +00001546Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1547 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001548}
1549
Chris Lattner45f15572007-04-14 00:12:57 +00001550/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1551/// zeros. If so, the result pointer and the first operand have the same
1552/// value, just potentially different types.
1553bool GetElementPtrInst::hasAllZeroIndices() const {
1554 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1555 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1556 if (!CI->isZero()) return false;
1557 } else {
1558 return false;
1559 }
1560 }
1561 return true;
1562}
1563
Chris Lattner27058292007-04-27 20:35:56 +00001564/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1565/// constant integers. If so, the result pointer and the first operand have
1566/// a constant offset between them.
1567bool GetElementPtrInst::hasAllConstantIndices() const {
1568 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1569 if (!isa<ConstantInt>(getOperand(i)))
1570 return false;
1571 }
1572 return true;
1573}
1574
Dan Gohman1b849082009-09-07 23:54:19 +00001575void GetElementPtrInst::setIsInBounds(bool B) {
1576 cast<GEPOperator>(this)->setIsInBounds(B);
1577}
Chris Lattner45f15572007-04-14 00:12:57 +00001578
Nick Lewycky28a5f252009-09-27 21:33:04 +00001579bool GetElementPtrInst::isInBounds() const {
1580 return cast<GEPOperator>(this)->isInBounds();
1581}
1582
Chandler Carruth1e140532012-12-11 10:29:10 +00001583bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1584 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001585 // Delegate to the generic GEPOperator implementation.
1586 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001587}
1588
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001590// ExtractElementInst Implementation
1591//===----------------------------------------------------------------------===//
1592
1593ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001594 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001595 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001596 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001597 ExtractElement,
1598 OperandTraits<ExtractElementInst>::op_begin(this),
1599 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001600 assert(isValidOperands(Val, Index) &&
1601 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001602 Op<0>() = Val;
1603 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001604 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001605}
1606
1607ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001608 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001609 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001611 ExtractElement,
1612 OperandTraits<ExtractElementInst>::op_begin(this),
1613 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001614 assert(isValidOperands(Val, Index) &&
1615 "Invalid extractelement instruction operands!");
1616
Gabor Greif2d3024d2008-05-26 21:33:52 +00001617 Op<0>() = Val;
1618 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001619 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001620}
1621
Chris Lattner65511ff2006-10-05 06:24:58 +00001622
Chris Lattner54865b32006-04-08 04:05:48 +00001623bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001624 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001625 return false;
1626 return true;
1627}
1628
1629
Robert Bocchino23004482006-01-10 19:05:34 +00001630//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001631// InsertElementInst Implementation
1632//===----------------------------------------------------------------------===//
1633
Chris Lattner54865b32006-04-08 04:05:48 +00001634InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001635 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001636 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001637 : Instruction(Vec->getType(), InsertElement,
1638 OperandTraits<InsertElementInst>::op_begin(this),
1639 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001640 assert(isValidOperands(Vec, Elt, Index) &&
1641 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001642 Op<0>() = Vec;
1643 Op<1>() = Elt;
1644 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001645 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001646}
1647
Chris Lattner54865b32006-04-08 04:05:48 +00001648InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001649 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001650 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001651 : Instruction(Vec->getType(), InsertElement,
1652 OperandTraits<InsertElementInst>::op_begin(this),
1653 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001654 assert(isValidOperands(Vec, Elt, Index) &&
1655 "Invalid insertelement instruction operands!");
1656
Gabor Greif2d3024d2008-05-26 21:33:52 +00001657 Op<0>() = Vec;
1658 Op<1>() = Elt;
1659 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001660 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001661}
1662
Chris Lattner54865b32006-04-08 04:05:48 +00001663bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1664 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001665 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001666 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001667
Reid Spencerd84d35b2007-02-15 02:26:10 +00001668 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001669 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001670
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001671 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001672 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001673 return true;
1674}
1675
1676
Robert Bocchinoca27f032006-01-17 20:07:22 +00001677//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001678// ShuffleVectorInst Implementation
1679//===----------------------------------------------------------------------===//
1680
1681ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001682 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001683 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001684: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001685 cast<VectorType>(Mask->getType())->getNumElements()),
1686 ShuffleVector,
1687 OperandTraits<ShuffleVectorInst>::op_begin(this),
1688 OperandTraits<ShuffleVectorInst>::operands(this),
1689 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001690 assert(isValidOperands(V1, V2, Mask) &&
1691 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001692 Op<0>() = V1;
1693 Op<1>() = V2;
1694 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001695 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001696}
1697
1698ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001699 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001700 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001701: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1702 cast<VectorType>(Mask->getType())->getNumElements()),
1703 ShuffleVector,
1704 OperandTraits<ShuffleVectorInst>::op_begin(this),
1705 OperandTraits<ShuffleVectorInst>::operands(this),
1706 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001707 assert(isValidOperands(V1, V2, Mask) &&
1708 "Invalid shuffle vector instruction operands!");
1709
Gabor Greif2d3024d2008-05-26 21:33:52 +00001710 Op<0>() = V1;
1711 Op<1>() = V2;
1712 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001713 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714}
1715
Mon P Wang25f01062008-11-10 04:46:22 +00001716bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001717 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001718 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001719 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001720 return false;
1721
Chris Lattner1dcb6542012-01-25 23:49:49 +00001722 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001723 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001724 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001725 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001726
1727 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001728 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1729 return true;
1730
Nate Begeman60a31c32010-08-13 00:16:46 +00001731 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001732 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001733 for (Value *Op : MV->operands()) {
1734 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001735 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001736 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001737 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001738 return false;
1739 }
1740 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001741 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001742 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001743
1744 if (const ConstantDataSequential *CDS =
1745 dyn_cast<ConstantDataSequential>(Mask)) {
1746 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1747 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1748 if (CDS->getElementAsInteger(i) >= V1Size*2)
1749 return false;
1750 return true;
1751 }
1752
1753 // The bitcode reader can create a place holder for a forward reference
1754 // used as the shuffle mask. When this occurs, the shuffle mask will
1755 // fall into this case and fail. To avoid this error, do this bit of
1756 // ugliness to allow such a mask pass.
1757 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1758 if (CE->getOpcode() == Instruction::UserOp1)
1759 return true;
1760
1761 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001762}
1763
Chris Lattnerf724e342008-03-02 05:28:33 +00001764/// getMaskValue - Return the index from the shuffle mask for the specified
1765/// output result. This is either -1 if the element is undef or a number less
1766/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001767int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1768 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1769 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001770 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001771 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001772 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001773 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001774 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001775}
1776
Chris Lattner1dcb6542012-01-25 23:49:49 +00001777/// getShuffleMask - Return the full mask for this instruction, where each
1778/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001779void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1780 SmallVectorImpl<int> &Result) {
1781 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001782
Chris Lattnercf129702012-01-26 02:51:13 +00001783 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001784 for (unsigned i = 0; i != NumElts; ++i)
1785 Result.push_back(CDS->getElementAsInteger(i));
1786 return;
1787 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001788 for (unsigned i = 0; i != NumElts; ++i) {
1789 Constant *C = Mask->getAggregateElement(i);
1790 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001791 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001792 }
1793}
1794
1795
Dan Gohman12fce772008-05-15 19:50:34 +00001796//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001797// InsertValueInst Class
1798//===----------------------------------------------------------------------===//
1799
Jay Foad57aa6362011-07-13 10:26:04 +00001800void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1801 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001802 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001803
1804 // There's no fundamental reason why we require at least one index
1805 // (other than weirdness with &*IdxBegin being invalid; see
1806 // getelementptr's init routine for example). But there's no
1807 // present need to support it.
1808 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1809
1810 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001811 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001812 Op<0>() = Agg;
1813 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001814
Jay Foad57aa6362011-07-13 10:26:04 +00001815 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001816 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001817}
1818
1819InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001820 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001821 OperandTraits<InsertValueInst>::op_begin(this), 2),
1822 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001823 Op<0>() = IVI.getOperand(0);
1824 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001825 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001826}
1827
1828//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001829// ExtractValueInst Class
1830//===----------------------------------------------------------------------===//
1831
Jay Foad57aa6362011-07-13 10:26:04 +00001832void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001833 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001834
Jay Foad57aa6362011-07-13 10:26:04 +00001835 // There's no fundamental reason why we require at least one index.
1836 // But there's no present need to support it.
1837 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001838
Jay Foad57aa6362011-07-13 10:26:04 +00001839 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001840 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001841}
1842
1843ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001844 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001845 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001846 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001847}
1848
Dan Gohman12fce772008-05-15 19:50:34 +00001849// getIndexedType - Returns the type of the element that would be extracted
1850// with an extractvalue instruction with the specified parameters.
1851//
1852// A null type is returned if the indices are invalid for the specified
1853// pointer type.
1854//
Chris Lattner229907c2011-07-18 04:54:35 +00001855Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001856 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001857 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001858 // We can't use CompositeType::indexValid(Index) here.
1859 // indexValid() always returns true for arrays because getelementptr allows
1860 // out-of-bounds indices. Since we don't allow those for extractvalue and
1861 // insertvalue we need to check array indexing manually.
1862 // Since the only other types we can index into are struct types it's just
1863 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001864 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001865 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001866 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001867 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001868 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001869 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001870 } else {
1871 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001872 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001873 }
1874
1875 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001876 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001877 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001878}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001879
1880//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001881// BinaryOperator Class
1882//===----------------------------------------------------------------------===//
1883
Chris Lattner2195fc42007-02-24 00:55:48 +00001884BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001885 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001886 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001887 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001888 OperandTraits<BinaryOperator>::op_begin(this),
1889 OperandTraits<BinaryOperator>::operands(this),
1890 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001891 Op<0>() = S1;
1892 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001893 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001894 setName(Name);
1895}
1896
1897BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001898 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001899 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001900 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001901 OperandTraits<BinaryOperator>::op_begin(this),
1902 OperandTraits<BinaryOperator>::operands(this),
1903 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001904 Op<0>() = S1;
1905 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001906 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001907 setName(Name);
1908}
1909
1910
1911void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001912 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001913 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001914 assert(LHS->getType() == RHS->getType() &&
1915 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001916#ifndef NDEBUG
1917 switch (iType) {
1918 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001919 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001920 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001921 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001922 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001923 "Tried to create an integer operation on a non-integer type!");
1924 break;
1925 case FAdd: case FSub:
1926 case FMul:
1927 assert(getType() == LHS->getType() &&
1928 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001929 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001930 "Tried to create a floating-point operation on a "
1931 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001932 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001933 case UDiv:
1934 case SDiv:
1935 assert(getType() == LHS->getType() &&
1936 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001937 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001938 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001939 "Incorrect operand type (not integer) for S/UDIV");
1940 break;
1941 case FDiv:
1942 assert(getType() == LHS->getType() &&
1943 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001944 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001945 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001946 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001947 case URem:
1948 case SRem:
1949 assert(getType() == LHS->getType() &&
1950 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001951 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001952 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001953 "Incorrect operand type (not integer) for S/UREM");
1954 break;
1955 case FRem:
1956 assert(getType() == LHS->getType() &&
1957 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001958 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001959 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001960 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001961 case Shl:
1962 case LShr:
1963 case AShr:
1964 assert(getType() == LHS->getType() &&
1965 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001966 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001967 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001968 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001969 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001970 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001971 case And: case Or:
1972 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001973 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001974 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001975 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001976 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001977 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001978 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001979 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001980 default:
1981 break;
1982 }
1983#endif
1984}
1985
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001987 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001988 Instruction *InsertBefore) {
1989 assert(S1->getType() == S2->getType() &&
1990 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001991 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001995 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001996 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001997 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001998 InsertAtEnd->getInstList().push_back(Res);
1999 return Res;
2000}
2001
Dan Gohman5476cfd2009-08-12 16:23:25 +00002002BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002003 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002004 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002005 return new BinaryOperator(Instruction::Sub,
2006 zero, Op,
2007 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002008}
2009
Dan Gohman5476cfd2009-08-12 16:23:25 +00002010BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002011 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002012 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002013 return new BinaryOperator(Instruction::Sub,
2014 zero, Op,
2015 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002016}
2017
Dan Gohman4ab44202009-12-18 02:58:50 +00002018BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2019 Instruction *InsertBefore) {
2020 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2021 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2022}
2023
2024BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2025 BasicBlock *InsertAtEnd) {
2026 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2027 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2028}
2029
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002030BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2031 Instruction *InsertBefore) {
2032 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2033 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2034}
2035
2036BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2037 BasicBlock *InsertAtEnd) {
2038 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2039 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2040}
2041
Dan Gohman5476cfd2009-08-12 16:23:25 +00002042BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002043 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002044 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002045 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002046 Op->getType(), Name, InsertBefore);
2047}
2048
Dan Gohman5476cfd2009-08-12 16:23:25 +00002049BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002050 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002051 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002052 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002053 Op->getType(), Name, InsertAtEnd);
2054}
2055
Dan Gohman5476cfd2009-08-12 16:23:25 +00002056BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002057 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002058 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002059 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002060 Op->getType(), Name, InsertBefore);
2061}
2062
Dan Gohman5476cfd2009-08-12 16:23:25 +00002063BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002064 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002065 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002066 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002067 Op->getType(), Name, InsertAtEnd);
2068}
2069
2070
2071// isConstantAllOnes - Helper function for several functions below
2072static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002073 if (const Constant *C = dyn_cast<Constant>(V))
2074 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002075 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076}
2077
Owen Andersonbb2501b2009-07-13 22:18:28 +00002078bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002079 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2080 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002081 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2082 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083 return false;
2084}
2085
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002086bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002087 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2088 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002089 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2090 if (!IgnoreZeroSign)
2091 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2092 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2093 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002094 return false;
2095}
2096
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002097bool BinaryOperator::isNot(const Value *V) {
2098 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2099 return (Bop->getOpcode() == Instruction::Xor &&
2100 (isConstantAllOnes(Bop->getOperand(1)) ||
2101 isConstantAllOnes(Bop->getOperand(0))));
2102 return false;
2103}
2104
Chris Lattner2c7d1772005-04-24 07:28:37 +00002105Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002106 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002107}
2108
Chris Lattner2c7d1772005-04-24 07:28:37 +00002109const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2110 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002111}
2112
Dan Gohmana5b96452009-06-04 22:49:04 +00002113Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002114 return cast<BinaryOperator>(BinOp)->getOperand(1);
2115}
2116
2117const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2118 return getFNegArgument(const_cast<Value*>(BinOp));
2119}
2120
Chris Lattner2c7d1772005-04-24 07:28:37 +00002121Value *BinaryOperator::getNotArgument(Value *BinOp) {
2122 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2123 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2124 Value *Op0 = BO->getOperand(0);
2125 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 if (isConstantAllOnes(Op0)) return Op1;
2127
2128 assert(isConstantAllOnes(Op1));
2129 return Op0;
2130}
2131
Chris Lattner2c7d1772005-04-24 07:28:37 +00002132const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2133 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134}
2135
2136
2137// swapOperands - Exchange the two operands to this instruction. This
2138// instruction is safe to use on any binary instruction and does not
2139// modify the semantics of the instruction. If the instruction is
2140// order dependent (SetLT f.e.) the opcode is changed.
2141//
2142bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002143 if (!isCommutative())
2144 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002145 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002146 return false;
2147}
2148
Dan Gohman1b849082009-09-07 23:54:19 +00002149void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2150 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2151}
2152
2153void BinaryOperator::setHasNoSignedWrap(bool b) {
2154 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2155}
2156
2157void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002158 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002159}
2160
Nick Lewycky28a5f252009-09-27 21:33:04 +00002161bool BinaryOperator::hasNoUnsignedWrap() const {
2162 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2163}
2164
2165bool BinaryOperator::hasNoSignedWrap() const {
2166 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2167}
2168
2169bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002170 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002171}
2172
Sanjay Patela982d992014-09-03 01:06:50 +00002173void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002174 // Copy the wrapping flags.
2175 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2176 setHasNoSignedWrap(OB->hasNoSignedWrap());
2177 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2178 }
2179
2180 // Copy the exact flag.
2181 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2182 setIsExact(PE->isExact());
2183
2184 // Copy the fast-math flags.
2185 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002186 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002187}
2188
Sanjay Patela982d992014-09-03 01:06:50 +00002189void BinaryOperator::andIRFlags(const Value *V) {
2190 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2191 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2192 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2193 }
2194
2195 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2196 setIsExact(isExact() & PE->isExact());
2197
2198 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2199 FastMathFlags FM = getFastMathFlags();
2200 FM &= FP->getFastMathFlags();
2201 copyFastMathFlags(FM);
2202 }
2203}
2204
2205
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002206//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002207// FPMathOperator Class
2208//===----------------------------------------------------------------------===//
2209
2210/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2211/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002212/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002213float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002214 const MDNode *MD =
2215 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002216 if (!MD)
2217 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002218 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002219 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002220}
2221
2222
2223//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002224// CastInst Class
2225//===----------------------------------------------------------------------===//
2226
David Blaikie3a15e142011-12-01 08:00:17 +00002227void CastInst::anchor() {}
2228
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229// Just determine if this cast only deals with integral->integral conversion.
2230bool CastInst::isIntegerCast() const {
2231 switch (getOpcode()) {
2232 default: return false;
2233 case Instruction::ZExt:
2234 case Instruction::SExt:
2235 case Instruction::Trunc:
2236 return true;
2237 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002238 return getOperand(0)->getType()->isIntegerTy() &&
2239 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002241}
2242
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243bool CastInst::isLosslessCast() const {
2244 // Only BitCast can be lossless, exit fast if we're not BitCast
2245 if (getOpcode() != Instruction::BitCast)
2246 return false;
2247
2248 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002249 Type* SrcTy = getOperand(0)->getType();
2250 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 if (SrcTy == DstTy)
2252 return true;
2253
Reid Spencer8d9336d2006-12-31 05:26:44 +00002254 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002255 if (SrcTy->isPointerTy())
2256 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 return false; // Other types have no identity values
2258}
2259
2260/// This function determines if the CastInst does not require any bits to be
2261/// changed in order to effect the cast. Essentially, it identifies cases where
2262/// no code gen is necessary for the cast, hence the name no-op cast. For
2263/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002264/// # bitcast i32* %x to i8*
2265/// # bitcast <2 x i32> %x to <4 x i16>
2266/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002267/// @brief Determine if the described cast is a no-op.
2268bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002269 Type *SrcTy,
2270 Type *DestTy,
2271 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002272 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002273 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274 case Instruction::Trunc:
2275 case Instruction::ZExt:
2276 case Instruction::SExt:
2277 case Instruction::FPTrunc:
2278 case Instruction::FPExt:
2279 case Instruction::UIToFP:
2280 case Instruction::SIToFP:
2281 case Instruction::FPToUI:
2282 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002283 case Instruction::AddrSpaceCast:
2284 // TODO: Target informations may give a more accurate answer here.
2285 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002286 case Instruction::BitCast:
2287 return true; // BitCast never modifies bits.
2288 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002289 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002290 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002292 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002293 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 }
2295}
2296
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002297/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002298bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002299 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2300}
2301
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002302bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002303 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002304 if (getOpcode() == Instruction::PtrToInt)
2305 PtrOpTy = getOperand(0)->getType();
2306 else if (getOpcode() == Instruction::IntToPtr)
2307 PtrOpTy = getType();
2308
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002309 Type *IntPtrTy =
2310 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002311
2312 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2313}
2314
2315/// This function determines if a pair of casts can be eliminated and what
2316/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317/// instructions like this:
2318/// * %F = firstOpcode SrcTy %x to MidTy
2319/// * %S = secondOpcode MidTy %F to DstTy
2320/// The function returns a resultOpcode so these two casts can be replaced with:
2321/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2322/// If no such cast is permited, the function returns 0.
2323unsigned CastInst::isEliminableCastPair(
2324 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002325 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2326 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002327 // Define the 144 possibilities for these two cast instructions. The values
2328 // in this matrix determine what to do in a given situation and select the
2329 // case in the switch below. The rows correspond to firstOp, the columns
2330 // correspond to secondOp. In looking at the table below, keep in mind
2331 // the following cast properties:
2332 //
2333 // Size Compare Source Destination
2334 // Operator Src ? Size Type Sign Type Sign
2335 // -------- ------------ ------------------- ---------------------
2336 // TRUNC > Integer Any Integral Any
2337 // ZEXT < Integral Unsigned Integer Any
2338 // SEXT < Integral Signed Integer Any
2339 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002340 // FPTOSI n/a FloatPt n/a Integral Signed
2341 // UITOFP n/a Integral Unsigned FloatPt n/a
2342 // SITOFP n/a Integral Signed FloatPt n/a
2343 // FPTRUNC > FloatPt n/a FloatPt n/a
2344 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 // PTRTOINT n/a Pointer n/a Integral Unsigned
2346 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002347 // BITCAST = FirstClass n/a FirstClass n/a
2348 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002349 //
2350 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002351 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2352 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002353 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002354 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002355 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002356 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002357 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2359 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002360 // T F F U S F F P I B A -+
2361 // R Z S P P I I T P 2 N T S |
2362 // U E E 2 2 2 2 R E I T C C +- secondOp
2363 // N X X U S F F N X N 2 V V |
2364 // C T T I I P P C T T P T T -+
2365 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002366 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002367 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2368 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2369 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2370 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2371 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002372 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002373 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2374 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2375 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2376 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2377 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002379
Chris Lattner25eea4d2010-07-12 01:19:22 +00002380 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002381 // merging. However, bitcast of A->B->A are allowed.
2382 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2383 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2384 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2385
2386 // Check if any of the bitcasts convert scalars<->vectors.
2387 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2388 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2389 // Unless we are bitcasing to the original type, disallow optimizations.
2390 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391
2392 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2393 [secondOp-Instruction::CastOpsBegin];
2394 switch (ElimCase) {
2395 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002396 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 return 0;
2398 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 return firstOp;
2401 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002402 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 return secondOp;
2404 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002406 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002407 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002408 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409 return firstOp;
2410 return 0;
2411 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002412 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002413 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002414 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 return firstOp;
2416 return 0;
2417 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002418 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002419 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002420 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 return secondOp;
2422 return 0;
2423 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002424 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002425 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002426 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 return secondOp;
2428 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002429 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002430 // Cannot simplify if address spaces are different!
2431 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2432 return 0;
2433
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002434 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002435 // We can still fold this without knowing the actual sizes as long we
2436 // know that the intermediate pointer is the largest possible
2437 // pointer size.
2438 // FIXME: Is this always true?
2439 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002440 return Instruction::BitCast;
2441
2442 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002443 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002444 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002445 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 if (MidSize >= PtrSize)
2447 return Instruction::BitCast;
2448 return 0;
2449 }
2450 case 8: {
2451 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2452 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2453 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002454 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2455 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 if (SrcSize == DstSize)
2457 return Instruction::BitCast;
2458 else if (SrcSize < DstSize)
2459 return firstOp;
2460 return secondOp;
2461 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 case 9:
2463 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 return Instruction::ZExt;
2465 case 10:
2466 // fpext followed by ftrunc is allowed if the bit size returned to is
2467 // the same as the original, in which case its just a bitcast
2468 if (SrcTy == DstTy)
2469 return Instruction::BitCast;
2470 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002471 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002473 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002474 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002475 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002476 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2477 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 if (SrcSize <= PtrSize && SrcSize == DstSize)
2479 return Instruction::BitCast;
2480 return 0;
2481 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002482 case 12: {
2483 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2484 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2485 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2486 return Instruction::AddrSpaceCast;
2487 return Instruction::BitCast;
2488 }
2489 case 13:
2490 // FIXME: this state can be merged with (1), but the following assert
2491 // is useful to check the correcteness of the sequence due to semantic
2492 // change of bitcast.
2493 assert(
2494 SrcTy->isPtrOrPtrVectorTy() &&
2495 MidTy->isPtrOrPtrVectorTy() &&
2496 DstTy->isPtrOrPtrVectorTy() &&
2497 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2498 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2499 "Illegal addrspacecast, bitcast sequence!");
2500 // Allowed, use first cast's opcode
2501 return firstOp;
2502 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002503 // bitcast, addrspacecast -> addrspacecast if the element type of
2504 // bitcast's source is the same as that of addrspacecast's destination.
2505 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2506 return Instruction::AddrSpaceCast;
2507 return 0;
2508
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002509 case 15:
2510 // FIXME: this state can be merged with (1), but the following assert
2511 // is useful to check the correcteness of the sequence due to semantic
2512 // change of bitcast.
2513 assert(
2514 SrcTy->isIntOrIntVectorTy() &&
2515 MidTy->isPtrOrPtrVectorTy() &&
2516 DstTy->isPtrOrPtrVectorTy() &&
2517 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2518 "Illegal inttoptr, bitcast sequence!");
2519 // Allowed, use first cast's opcode
2520 return firstOp;
2521 case 16:
2522 // FIXME: this state can be merged with (2), but the following assert
2523 // is useful to check the correcteness of the sequence due to semantic
2524 // change of bitcast.
2525 assert(
2526 SrcTy->isPtrOrPtrVectorTy() &&
2527 MidTy->isPtrOrPtrVectorTy() &&
2528 DstTy->isIntOrIntVectorTy() &&
2529 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2530 "Illegal bitcast, ptrtoint sequence!");
2531 // Allowed, use second cast's opcode
2532 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002533 case 17:
2534 // (sitofp (zext x)) -> (uitofp x)
2535 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002539 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 default:
Craig Topperc514b542012-02-05 22:14:15 +00002541 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543}
2544
Chris Lattner229907c2011-07-18 04:54:35 +00002545CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002546 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002547 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 // Construct and return the appropriate CastInst subclass
2549 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002550 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2551 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2552 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2553 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2554 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2555 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2556 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2557 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2558 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2559 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2560 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2561 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2562 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2563 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566
Chris Lattner229907c2011-07-18 04:54:35 +00002567CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002568 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002569 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570 // Construct and return the appropriate CastInst subclass
2571 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002572 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2573 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2574 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2575 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2576 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2577 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2578 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2579 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2580 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2581 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2582 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2583 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2584 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2585 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
Chris Lattner229907c2011-07-18 04:54:35 +00002589CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002590 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002591 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002592 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002593 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2594 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002595}
2596
Chris Lattner229907c2011-07-18 04:54:35 +00002597CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002598 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002599 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002600 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002601 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2602 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002603}
2604
Chris Lattner229907c2011-07-18 04:54:35 +00002605CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002606 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002607 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002608 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002609 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2610 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002611}
2612
Chris Lattner229907c2011-07-18 04:54:35 +00002613CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002614 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002615 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002616 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002617 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2618 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002619}
2620
Chris Lattner229907c2011-07-18 04:54:35 +00002621CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002623 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002624 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002625 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2626 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002627}
2628
Chris Lattner229907c2011-07-18 04:54:35 +00002629CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002630 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002631 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002632 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002633 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2634 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002635}
2636
Chris Lattner229907c2011-07-18 04:54:35 +00002637CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002638 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002639 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002640 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2641 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2642 "Invalid cast");
2643 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002644 assert((!Ty->isVectorTy() ||
2645 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002646 "Invalid cast");
2647
Matt Arsenault065ced92013-07-31 00:17:33 +00002648 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002649 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002650
Matt Arsenault740980e2014-07-14 17:24:35 +00002651 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002652}
2653
2654/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002655CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2656 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002657 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002658 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2659 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002660 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002661 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002662 assert((!Ty->isVectorTy() ||
2663 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002664 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002665
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002666 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002667 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002668
Matt Arsenault740980e2014-07-14 17:24:35 +00002669 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2670}
2671
2672CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2673 Value *S, Type *Ty,
2674 const Twine &Name,
2675 BasicBlock *InsertAtEnd) {
2676 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2677 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2678
2679 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2680 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2681
2682 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2683}
2684
2685CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2686 Value *S, Type *Ty,
2687 const Twine &Name,
2688 Instruction *InsertBefore) {
2689 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2690 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2691
2692 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002693 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2694
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002695 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002696}
2697
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002698CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2699 const Twine &Name,
2700 Instruction *InsertBefore) {
2701 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2702 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2703 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2704 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2705
2706 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2707}
2708
Matt Arsenault740980e2014-07-14 17:24:35 +00002709CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002710 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002711 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002712 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002713 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002714 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2715 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002716 Instruction::CastOps opcode =
2717 (SrcBits == DstBits ? Instruction::BitCast :
2718 (SrcBits > DstBits ? Instruction::Trunc :
2719 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002720 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002721}
2722
Chris Lattner229907c2011-07-18 04:54:35 +00002723CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002724 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002725 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002726 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002727 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002728 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2729 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002730 Instruction::CastOps opcode =
2731 (SrcBits == DstBits ? Instruction::BitCast :
2732 (SrcBits > DstBits ? Instruction::Trunc :
2733 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002734 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002735}
2736
Chris Lattner229907c2011-07-18 04:54:35 +00002737CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002738 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002739 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002740 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002741 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002742 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2743 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002744 Instruction::CastOps opcode =
2745 (SrcBits == DstBits ? Instruction::BitCast :
2746 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002747 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002748}
2749
Chris Lattner229907c2011-07-18 04:54:35 +00002750CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002751 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002752 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002753 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002754 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002755 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2756 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002757 Instruction::CastOps opcode =
2758 (SrcBits == DstBits ? Instruction::BitCast :
2759 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002760 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002761}
2762
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002763// Check whether it is valid to call getCastOpcode for these types.
2764// This routine must be kept in sync with getCastOpcode.
2765bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2766 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2767 return false;
2768
2769 if (SrcTy == DestTy)
2770 return true;
2771
2772 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2773 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2774 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2775 // An element by element cast. Valid if casting the elements is valid.
2776 SrcTy = SrcVecTy->getElementType();
2777 DestTy = DestVecTy->getElementType();
2778 }
2779
2780 // Get the bit sizes, we'll need these
2781 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2782 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2783
2784 // Run through the possibilities ...
2785 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002786 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002787 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002788 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002789 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002790 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002791 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002792 // Casting from something else
2793 return SrcTy->isPointerTy();
2794 }
2795 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2796 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002797 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002798 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002799 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002800 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002801 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002802 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002803 return false;
2804 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002805 if (DestTy->isVectorTy()) // Casting to vector
2806 return DestBits == SrcBits;
2807 if (DestTy->isPointerTy()) { // Casting to pointer
2808 if (SrcTy->isPointerTy()) // Casting from pointer
2809 return true;
2810 return SrcTy->isIntegerTy(); // Casting from integral
2811 }
2812 if (DestTy->isX86_MMXTy()) {
2813 if (SrcTy->isVectorTy())
2814 return DestBits == SrcBits; // 64-bit vector to MMX
2815 return false;
2816 } // Casting to something else
2817 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002818}
2819
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002820bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2821 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2822 return false;
2823
2824 if (SrcTy == DestTy)
2825 return true;
2826
2827 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2828 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2829 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2830 // An element by element cast. Valid if casting the elements is valid.
2831 SrcTy = SrcVecTy->getElementType();
2832 DestTy = DestVecTy->getElementType();
2833 }
2834 }
2835 }
2836
2837 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2838 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2839 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2840 }
2841 }
2842
2843 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2844 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2845
2846 // Could still have vectors of pointers if the number of elements doesn't
2847 // match
2848 if (SrcBits == 0 || DestBits == 0)
2849 return false;
2850
2851 if (SrcBits != DestBits)
2852 return false;
2853
2854 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2855 return false;
2856
2857 return true;
2858}
2859
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002860bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002861 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002862 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2863 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002864 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002865 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2866 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002867 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002868
2869 return isBitCastable(SrcTy, DestTy);
2870}
2871
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002872// Provide a way to get a "cast" where the cast opcode is inferred from the
2873// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002874// logic in the castIsValid function below. This axiom should hold:
2875// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2876// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002878// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002879Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002880CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002881 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2882 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883
Duncan Sands55e50902008-01-06 10:12:28 +00002884 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2885 "Only first class types are castable!");
2886
Duncan Sandsa8514532011-05-18 07:13:41 +00002887 if (SrcTy == DestTy)
2888 return BitCast;
2889
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002890 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002891 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2892 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002893 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2894 // An element by element cast. Find the appropriate opcode based on the
2895 // element types.
2896 SrcTy = SrcVecTy->getElementType();
2897 DestTy = DestVecTy->getElementType();
2898 }
2899
2900 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002901 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2902 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002903
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002905 if (DestTy->isIntegerTy()) { // Casting to integral
2906 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907 if (DestBits < SrcBits)
2908 return Trunc; // int -> smaller int
2909 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002910 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911 return SExt; // signed -> SEXT
2912 else
2913 return ZExt; // unsigned -> ZEXT
2914 } else {
2915 return BitCast; // Same size, No-op cast
2916 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002917 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002918 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919 return FPToSI; // FP -> sint
2920 else
2921 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002922 } else if (SrcTy->isVectorTy()) {
2923 assert(DestBits == SrcBits &&
2924 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925 return BitCast; // Same size, no-op cast
2926 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002927 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928 "Casting from a value that is not first-class type");
2929 return PtrToInt; // ptr -> int
2930 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002931 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2932 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002933 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002934 return SIToFP; // sint -> FP
2935 else
2936 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002937 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938 if (DestBits < SrcBits) {
2939 return FPTrunc; // FP -> smaller FP
2940 } else if (DestBits > SrcBits) {
2941 return FPExt; // FP -> larger FP
2942 } else {
2943 return BitCast; // same size, no-op cast
2944 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002945 } else if (SrcTy->isVectorTy()) {
2946 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002947 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002948 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002950 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002951 } else if (DestTy->isVectorTy()) {
2952 assert(DestBits == SrcBits &&
2953 "Illegal cast to vector (wrong type or size)");
2954 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002955 } else if (DestTy->isPointerTy()) {
2956 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002957 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2958 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002960 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002962 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002963 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002964 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002965 if (SrcTy->isVectorTy()) {
2966 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002967 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002968 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002969 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002970 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002971 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002972}
2973
2974//===----------------------------------------------------------------------===//
2975// CastInst SubClass Constructors
2976//===----------------------------------------------------------------------===//
2977
2978/// Check that the construction parameters for a CastInst are correct. This
2979/// could be broken out into the separate constructors but it is useful to have
2980/// it in one place and to eliminate the redundant code for getting the sizes
2981/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002982bool
Chris Lattner229907c2011-07-18 04:54:35 +00002983CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984
2985 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002986 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002987
Chris Lattner37bc78a2010-01-26 21:51:43 +00002988 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2989 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002990 return false;
2991
2992 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002993 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2994 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995
Duncan Sands7f646562011-05-18 09:21:57 +00002996 // If these are vector types, get the lengths of the vectors (using zero for
2997 // scalar types means that checking that vector lengths match also checks that
2998 // scalars are not being converted to vectors or vectors to scalars).
2999 unsigned SrcLength = SrcTy->isVectorTy() ?
3000 cast<VectorType>(SrcTy)->getNumElements() : 0;
3001 unsigned DstLength = DstTy->isVectorTy() ?
3002 cast<VectorType>(DstTy)->getNumElements() : 0;
3003
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003004 // Switch on the opcode provided
3005 switch (op) {
3006 default: return false; // This is an input error
3007 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003008 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3009 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003010 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003011 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3012 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003014 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3015 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003017 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3018 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003019 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003020 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3021 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003022 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003024 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3025 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003026 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003028 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3029 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003031 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003032 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003033 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3034 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3035 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003036 return SrcTy->getScalarType()->isPointerTy() &&
3037 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003039 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003040 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003041 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3042 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3043 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003044 return SrcTy->getScalarType()->isIntegerTy() &&
3045 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003046 case Instruction::BitCast: {
3047 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3048 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3049
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050 // BitCast implies a no-op cast of type only. No bits change.
3051 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003052 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 return false;
3054
Alp Tokerf907b892013-12-05 05:44:44 +00003055 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003056 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003057 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003058 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3059
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003060 // If both are pointers then the address spaces must match.
3061 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3062 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003063
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003064 // A vector of pointers must have the same number of elements.
3065 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3066 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3067 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3068
3069 return false;
3070 }
3071
3072 return true;
3073 }
3074 case Instruction::AddrSpaceCast: {
3075 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3076 if (!SrcPtrTy)
3077 return false;
3078
3079 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3080 if (!DstPtrTy)
3081 return false;
3082
3083 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3084 return false;
3085
3086 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3087 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3088 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3089
3090 return false;
3091 }
3092
3093 return true;
3094 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 }
3096}
3097
3098TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003099 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003101 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003102}
3103
3104TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003105 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003107 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003108}
3109
3110ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003111 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003112) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003113 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003114}
3115
3116ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003117 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003118) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003119 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003120}
3121SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003122 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003124 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125}
3126
Jeff Cohencc08c832006-12-02 02:22:01 +00003127SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003128 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003130 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131}
3132
3133FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003134 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003136 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137}
3138
3139FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003140 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003141) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143}
3144
3145FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003146 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003147) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003148 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149}
3150
3151FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003152 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003153) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003154 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155}
3156
3157UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003158 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003160 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161}
3162
3163UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003164 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003165) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003166 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167}
3168
3169SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003170 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003172 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173}
3174
3175SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003176 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003177) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003178 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179}
3180
3181FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003182 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003183) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003184 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185}
3186
3187FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003188 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003190 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191}
3192
3193FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003194 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003196 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197}
3198
3199FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003200 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003202 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203}
3204
3205PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003206 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003208 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209}
3210
3211PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003213) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215}
3216
3217IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003218 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003219) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221}
3222
3223IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003224 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003227}
3228
3229BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003230 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233}
3234
3235BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003236 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003238 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003240
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003241AddrSpaceCastInst::AddrSpaceCastInst(
3242 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3243) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3244 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3245}
3246
3247AddrSpaceCastInst::AddrSpaceCastInst(
3248 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3249) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3250 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3251}
3252
Chris Lattnerf16dc002006-09-17 19:29:56 +00003253//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003254// CmpInst Classes
3255//===----------------------------------------------------------------------===//
3256
Craig Topper3186c012012-09-23 02:12:10 +00003257void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003258
Chris Lattner229907c2011-07-18 04:54:35 +00003259CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003260 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003261 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003262 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003263 OperandTraits<CmpInst>::op_begin(this),
3264 OperandTraits<CmpInst>::operands(this),
3265 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003266 Op<0>() = LHS;
3267 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003268 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003269 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003270}
Gabor Greiff6caff662008-05-10 08:32:32 +00003271
Chris Lattner229907c2011-07-18 04:54:35 +00003272CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003273 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003274 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003275 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003276 OperandTraits<CmpInst>::op_begin(this),
3277 OperandTraits<CmpInst>::operands(this),
3278 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003279 Op<0>() = LHS;
3280 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003281 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003282 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003283}
3284
3285CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003286CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003288 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003289 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290 if (InsertBefore)
3291 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3292 S1, S2, Name);
3293 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003294 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003295 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003296 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003297
3298 if (InsertBefore)
3299 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3300 S1, S2, Name);
3301 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003302 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003304}
3305
3306CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003307CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003308 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003309 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3311 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003312 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003313 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3314 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003315}
3316
3317void CmpInst::swapOperands() {
3318 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3319 IC->swapOperands();
3320 else
3321 cast<FCmpInst>(this)->swapOperands();
3322}
3323
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003324bool CmpInst::isCommutative() const {
3325 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003326 return IC->isCommutative();
3327 return cast<FCmpInst>(this)->isCommutative();
3328}
3329
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003330bool CmpInst::isEquality() const {
3331 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003332 return IC->isEquality();
3333 return cast<FCmpInst>(this)->isEquality();
3334}
3335
3336
Dan Gohman4e724382008-05-31 02:47:54 +00003337CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003338 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003339 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003340 case ICMP_EQ: return ICMP_NE;
3341 case ICMP_NE: return ICMP_EQ;
3342 case ICMP_UGT: return ICMP_ULE;
3343 case ICMP_ULT: return ICMP_UGE;
3344 case ICMP_UGE: return ICMP_ULT;
3345 case ICMP_ULE: return ICMP_UGT;
3346 case ICMP_SGT: return ICMP_SLE;
3347 case ICMP_SLT: return ICMP_SGE;
3348 case ICMP_SGE: return ICMP_SLT;
3349 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003350
Dan Gohman4e724382008-05-31 02:47:54 +00003351 case FCMP_OEQ: return FCMP_UNE;
3352 case FCMP_ONE: return FCMP_UEQ;
3353 case FCMP_OGT: return FCMP_ULE;
3354 case FCMP_OLT: return FCMP_UGE;
3355 case FCMP_OGE: return FCMP_ULT;
3356 case FCMP_OLE: return FCMP_UGT;
3357 case FCMP_UEQ: return FCMP_ONE;
3358 case FCMP_UNE: return FCMP_OEQ;
3359 case FCMP_UGT: return FCMP_OLE;
3360 case FCMP_ULT: return FCMP_OGE;
3361 case FCMP_UGE: return FCMP_OLT;
3362 case FCMP_ULE: return FCMP_OGT;
3363 case FCMP_ORD: return FCMP_UNO;
3364 case FCMP_UNO: return FCMP_ORD;
3365 case FCMP_TRUE: return FCMP_FALSE;
3366 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003367 }
3368}
3369
Reid Spencer266e42b2006-12-23 06:05:41 +00003370ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3371 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003372 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003373 case ICMP_EQ: case ICMP_NE:
3374 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3375 return pred;
3376 case ICMP_UGT: return ICMP_SGT;
3377 case ICMP_ULT: return ICMP_SLT;
3378 case ICMP_UGE: return ICMP_SGE;
3379 case ICMP_ULE: return ICMP_SLE;
3380 }
3381}
3382
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003383ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3384 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003385 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003386 case ICMP_EQ: case ICMP_NE:
3387 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3388 return pred;
3389 case ICMP_SGT: return ICMP_UGT;
3390 case ICMP_SLT: return ICMP_ULT;
3391 case ICMP_SGE: return ICMP_UGE;
3392 case ICMP_SLE: return ICMP_ULE;
3393 }
3394}
3395
Reid Spencer0286bc12007-02-28 22:00:54 +00003396/// Initialize a set of values that all satisfy the condition with C.
3397///
3398ConstantRange
3399ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3400 APInt Lower(C);
3401 APInt Upper(C);
3402 uint32_t BitWidth = C.getBitWidth();
3403 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003404 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003405 case ICmpInst::ICMP_EQ: ++Upper; break;
3406 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003407 case ICmpInst::ICMP_ULT:
3408 Lower = APInt::getMinValue(BitWidth);
3409 // Check for an empty-set condition.
3410 if (Lower == Upper)
3411 return ConstantRange(BitWidth, /*isFullSet=*/false);
3412 break;
3413 case ICmpInst::ICMP_SLT:
3414 Lower = APInt::getSignedMinValue(BitWidth);
3415 // Check for an empty-set condition.
3416 if (Lower == Upper)
3417 return ConstantRange(BitWidth, /*isFullSet=*/false);
3418 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003419 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003420 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003421 // Check for an empty-set condition.
3422 if (Lower == Upper)
3423 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003424 break;
3425 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003426 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003427 // Check for an empty-set condition.
3428 if (Lower == Upper)
3429 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003430 break;
3431 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003432 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003433 // Check for a full-set condition.
3434 if (Lower == Upper)
3435 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003436 break;
3437 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003438 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003439 // Check for a full-set condition.
3440 if (Lower == Upper)
3441 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003442 break;
3443 case ICmpInst::ICMP_UGE:
3444 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003445 // Check for a full-set condition.
3446 if (Lower == Upper)
3447 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003448 break;
3449 case ICmpInst::ICMP_SGE:
3450 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003451 // Check for a full-set condition.
3452 if (Lower == Upper)
3453 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003454 break;
3455 }
3456 return ConstantRange(Lower, Upper);
3457}
3458
Dan Gohman4e724382008-05-31 02:47:54 +00003459CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003460 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003461 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003462 case ICMP_EQ: case ICMP_NE:
3463 return pred;
3464 case ICMP_SGT: return ICMP_SLT;
3465 case ICMP_SLT: return ICMP_SGT;
3466 case ICMP_SGE: return ICMP_SLE;
3467 case ICMP_SLE: return ICMP_SGE;
3468 case ICMP_UGT: return ICMP_ULT;
3469 case ICMP_ULT: return ICMP_UGT;
3470 case ICMP_UGE: return ICMP_ULE;
3471 case ICMP_ULE: return ICMP_UGE;
3472
Reid Spencerd9436b62006-11-20 01:22:35 +00003473 case FCMP_FALSE: case FCMP_TRUE:
3474 case FCMP_OEQ: case FCMP_ONE:
3475 case FCMP_UEQ: case FCMP_UNE:
3476 case FCMP_ORD: case FCMP_UNO:
3477 return pred;
3478 case FCMP_OGT: return FCMP_OLT;
3479 case FCMP_OLT: return FCMP_OGT;
3480 case FCMP_OGE: return FCMP_OLE;
3481 case FCMP_OLE: return FCMP_OGE;
3482 case FCMP_UGT: return FCMP_ULT;
3483 case FCMP_ULT: return FCMP_UGT;
3484 case FCMP_UGE: return FCMP_ULE;
3485 case FCMP_ULE: return FCMP_UGE;
3486 }
3487}
3488
Reid Spencer266e42b2006-12-23 06:05:41 +00003489bool CmpInst::isUnsigned(unsigned short predicate) {
3490 switch (predicate) {
3491 default: return false;
3492 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3493 case ICmpInst::ICMP_UGE: return true;
3494 }
3495}
3496
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003497bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003498 switch (predicate) {
3499 default: return false;
3500 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3501 case ICmpInst::ICMP_SGE: return true;
3502 }
3503}
3504
3505bool CmpInst::isOrdered(unsigned short predicate) {
3506 switch (predicate) {
3507 default: return false;
3508 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3509 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3510 case FCmpInst::FCMP_ORD: return true;
3511 }
3512}
3513
3514bool CmpInst::isUnordered(unsigned short predicate) {
3515 switch (predicate) {
3516 default: return false;
3517 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3518 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3519 case FCmpInst::FCMP_UNO: return true;
3520 }
3521}
3522
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003523bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3524 switch(predicate) {
3525 default: return false;
3526 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3527 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3528 }
3529}
3530
3531bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3532 switch(predicate) {
3533 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3534 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3535 default: return false;
3536 }
3537}
3538
3539
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003540//===----------------------------------------------------------------------===//
3541// SwitchInst Implementation
3542//===----------------------------------------------------------------------===//
3543
Chris Lattnerbaf00152010-11-17 05:41:46 +00003544void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3545 assert(Value && Default && NumReserved);
3546 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003547 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003548 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003549
Pete Coopereb31b682015-05-21 22:48:54 +00003550 Op<0>() = Value;
3551 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003552}
3553
Chris Lattner2195fc42007-02-24 00:55:48 +00003554/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3555/// switch on and a default destination. The number of additional cases can
3556/// be specified here to make memory allocation more efficient. This
3557/// constructor can also autoinsert before another instruction.
3558SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3559 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003560 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003561 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003562 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003563}
3564
3565/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3566/// switch on and a default destination. The number of additional cases can
3567/// be specified here to make memory allocation more efficient. This
3568/// constructor also autoinserts at the end of the specified BasicBlock.
3569SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3570 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003571 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003572 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003573 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003574}
3575
Misha Brukmanb1c93172005-04-21 23:48:37 +00003576SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003577 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003578 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003579 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003580 Use *OL = getOperandList();
3581 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003582 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003583 OL[i] = InOL[i];
3584 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003585 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003586 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003587}
3588
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003589
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003590/// addCase - Add an entry to the switch instruction...
3591///
Chris Lattner47ac1872005-02-24 05:32:09 +00003592void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003593 unsigned NewCaseIdx = getNumCases();
3594 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003595 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003596 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003597 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003598 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003599 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003600 CaseIt Case(this, NewCaseIdx);
3601 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003602 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003603}
3604
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003605/// removeCase - This method removes the specified case and its successor
3606/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003607void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003608 unsigned idx = i.getCaseIndex();
3609
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003610 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003611
3612 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003613 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003614
Jay Foad14277722011-02-01 09:22:34 +00003615 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003616 if (2 + (idx + 1) * 2 != NumOps) {
3617 OL[2 + idx * 2] = OL[NumOps - 2];
3618 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003619 }
3620
3621 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003622 OL[NumOps-2].set(nullptr);
3623 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003624 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003625}
3626
Jay Foade98f29d2011-04-01 08:00:58 +00003627/// growOperands - grow operands - This grows the operand list in response
3628/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003629///
Jay Foade98f29d2011-04-01 08:00:58 +00003630void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003631 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003632 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003633
3634 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003635 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003636}
3637
3638
3639BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3640 return getSuccessor(idx);
3641}
3642unsigned SwitchInst::getNumSuccessorsV() const {
3643 return getNumSuccessors();
3644}
3645void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3646 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003647}
Chris Lattnerf22be932004-10-15 23:52:53 +00003648
Chris Lattner3ed871f2009-10-27 19:13:16 +00003649//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003650// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003651//===----------------------------------------------------------------------===//
3652
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003653void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003654 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003655 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003656 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003657 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003658 allocHungoffUses(ReservedSpace);
3659
Pete Coopereb31b682015-05-21 22:48:54 +00003660 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003661}
3662
3663
Jay Foade98f29d2011-04-01 08:00:58 +00003664/// growOperands - grow operands - This grows the operand list in response
3665/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003666///
Jay Foade98f29d2011-04-01 08:00:58 +00003667void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003668 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003669 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003670
3671 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003672 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003673}
3674
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003675IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3676 Instruction *InsertBefore)
3677: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003678 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003679 init(Address, NumCases);
3680}
3681
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003682IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3683 BasicBlock *InsertAtEnd)
3684: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003685 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003686 init(Address, NumCases);
3687}
3688
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003689IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003690 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3691 nullptr, IBI.getNumOperands()) {
3692 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003693 Use *OL = getOperandList();
3694 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003695 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3696 OL[i] = InOL[i];
3697 SubclassOptionalData = IBI.SubclassOptionalData;
3698}
3699
Chris Lattner3ed871f2009-10-27 19:13:16 +00003700/// addDestination - Add a destination.
3701///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003702void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003703 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003704 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003705 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003706 // Initialize some new operands.
3707 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003708 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003709 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003710}
3711
3712/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003713/// indirectbr instruction.
3714void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003715 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3716
3717 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003718 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003719
3720 // Replace this value with the last one.
3721 OL[idx+1] = OL[NumOps-1];
3722
3723 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003724 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003725 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003726}
3727
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003728BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003729 return getSuccessor(idx);
3730}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003731unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003732 return getNumSuccessors();
3733}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003734void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003735 setSuccessor(idx, B);
3736}
3737
3738//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003739// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003740//===----------------------------------------------------------------------===//
3741
Chris Lattnerf22be932004-10-15 23:52:53 +00003742// Define these methods here so vtables don't get emitted into every translation
3743// unit that uses these classes.
3744
Pete Cooper75403d72015-06-24 20:22:23 +00003745GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003746 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003747}
3748
Pete Cooper75403d72015-06-24 20:22:23 +00003749BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003750 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003751}
3752
Pete Cooper75403d72015-06-24 20:22:23 +00003753FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003754 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003755}
3756
Pete Cooper75403d72015-06-24 20:22:23 +00003757ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003758 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003759}
3760
Pete Cooper75403d72015-06-24 20:22:23 +00003761ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003762 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003763}
3764
Pete Cooper75403d72015-06-24 20:22:23 +00003765InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003766 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003767}
3768
Pete Cooper75403d72015-06-24 20:22:23 +00003769AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003770 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3771 (Value *)getOperand(0), getAlignment());
3772 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3773 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003774}
3775
Pete Cooper75403d72015-06-24 20:22:23 +00003776LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003777 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3778 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003779}
3780
Pete Cooper75403d72015-06-24 20:22:23 +00003781StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003782 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003783 getAlignment(), getOrdering(), getSynchScope());
3784
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003785}
3786
Pete Cooper75403d72015-06-24 20:22:23 +00003787AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003788 AtomicCmpXchgInst *Result =
3789 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003790 getSuccessOrdering(), getFailureOrdering(),
3791 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003792 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003793 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003794 return Result;
3795}
3796
Pete Cooper75403d72015-06-24 20:22:23 +00003797AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003798 AtomicRMWInst *Result =
3799 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3800 getOrdering(), getSynchScope());
3801 Result->setVolatile(isVolatile());
3802 return Result;
3803}
3804
Pete Cooper75403d72015-06-24 20:22:23 +00003805FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003806 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3807}
3808
Pete Cooper75403d72015-06-24 20:22:23 +00003809TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003810 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003811}
3812
Pete Cooper75403d72015-06-24 20:22:23 +00003813ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003814 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003815}
3816
Pete Cooper75403d72015-06-24 20:22:23 +00003817SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003818 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003819}
3820
Pete Cooper75403d72015-06-24 20:22:23 +00003821FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003822 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003823}
3824
Pete Cooper75403d72015-06-24 20:22:23 +00003825FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003826 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003830 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003831}
3832
Pete Cooper75403d72015-06-24 20:22:23 +00003833SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003834 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003835}
3836
Pete Cooper75403d72015-06-24 20:22:23 +00003837FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003838 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003839}
3840
Pete Cooper75403d72015-06-24 20:22:23 +00003841FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003842 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003843}
3844
Pete Cooper75403d72015-06-24 20:22:23 +00003845PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003846 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003847}
3848
Pete Cooper75403d72015-06-24 20:22:23 +00003849IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003850 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003851}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003852
Pete Cooper75403d72015-06-24 20:22:23 +00003853BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003854 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003855}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003856
Pete Cooper75403d72015-06-24 20:22:23 +00003857AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003858 return new AddrSpaceCastInst(getOperand(0), getType());
3859}
3860
Pete Cooper75403d72015-06-24 20:22:23 +00003861CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003862 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003863}
3864
Pete Cooper75403d72015-06-24 20:22:23 +00003865SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003866 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003867}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003868
Pete Cooper75403d72015-06-24 20:22:23 +00003869VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003870 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003871}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003872
Pete Cooper75403d72015-06-24 20:22:23 +00003873ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003874 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003875}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876
Pete Cooper75403d72015-06-24 20:22:23 +00003877InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003878 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003879}
3880
Pete Cooper75403d72015-06-24 20:22:23 +00003881ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003882 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003883}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003884
Pete Cooper75403d72015-06-24 20:22:23 +00003885PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003886
Pete Cooper75403d72015-06-24 20:22:23 +00003887LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003888 return new LandingPadInst(*this);
3889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new(getNumOperands()) ReturnInst(*this);
3893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003896 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003897}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003898
Pete Cooper75403d72015-06-24 20:22:23 +00003899SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003900
Pete Cooper75403d72015-06-24 20:22:23 +00003901IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003902 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003903}
3904
Pete Cooper75403d72015-06-24 20:22:23 +00003905InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003906 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003907}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003908
Pete Cooper75403d72015-06-24 20:22:23 +00003909ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003910
David Majnemer654e1302015-07-31 17:58:14 +00003911CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3912 return new (getNumOperands()) CleanupReturnInst(*this);
3913}
3914
3915CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
3916 return new (getNumOperands()) CatchEndPadInst(*this);
3917}
3918
3919CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003920 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003921}
3922
3923CatchPadInst *CatchPadInst::cloneImpl() const {
3924 return new (getNumOperands()) CatchPadInst(*this);
3925}
3926
3927TerminatePadInst *TerminatePadInst::cloneImpl() const {
3928 return new (getNumOperands()) TerminatePadInst(*this);
3929}
3930
3931CleanupPadInst *CleanupPadInst::cloneImpl() const {
3932 return new (getNumOperands()) CleanupPadInst(*this);
3933}
3934
Pete Cooper75403d72015-06-24 20:22:23 +00003935UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003936 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003937 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003938}