blob: 79fb30e4734c33af754134ea5751da5444cb8e15 [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());
David Majnemer654e1302015-07-31 17:58:14 +0000687 if (Value *RetVal = CRI.getReturnValue())
688 setReturnValue(RetVal);
689 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
690 setUnwindDest(UnwindDest);
691}
692
693void CleanupReturnInst::init(Value *RetVal, BasicBlock *UnwindBB) {
694 SubclassOptionalData = 0;
695 if (UnwindBB)
696 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
697 if (RetVal)
698 setInstructionSubclassData(getSubclassDataFromInstruction() | 2);
699
700 if (UnwindBB)
701 setUnwindDest(UnwindBB);
702 if (RetVal)
703 setReturnValue(RetVal);
704}
705
706CleanupReturnInst::CleanupReturnInst(LLVMContext &C, Value *RetVal,
707 BasicBlock *UnwindBB, unsigned Values,
708 Instruction *InsertBefore)
709 : TerminatorInst(Type::getVoidTy(C), Instruction::CleanupRet,
710 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
711 Values, InsertBefore) {
712 init(RetVal, UnwindBB);
713}
714
715CleanupReturnInst::CleanupReturnInst(LLVMContext &C, Value *RetVal,
716 BasicBlock *UnwindBB, unsigned Values,
717 BasicBlock *InsertAtEnd)
718 : TerminatorInst(Type::getVoidTy(C), Instruction::CleanupRet,
719 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
720 Values, InsertAtEnd) {
721 init(RetVal, UnwindBB);
722}
723
724BasicBlock *CleanupReturnInst::getUnwindDest() const {
725 if (hasUnwindDest())
726 return cast<BasicBlock>(getOperand(getUnwindLabelOpIdx()));
727 return nullptr;
728}
729void CleanupReturnInst::setUnwindDest(BasicBlock *NewDest) {
730 assert(NewDest);
731 setOperand(getUnwindLabelOpIdx(), NewDest);
732}
733
734BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
735 assert(Idx == 0);
736 return getUnwindDest();
737}
738unsigned CleanupReturnInst::getNumSuccessorsV() const {
739 return getNumSuccessors();
740}
741void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
742 assert(Idx == 0);
743 setUnwindDest(B);
744}
745
746//===----------------------------------------------------------------------===//
747// CatchEndPadInst Implementation
748//===----------------------------------------------------------------------===//
749
750CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI)
751 : TerminatorInst(CRI.getType(), Instruction::CatchEndPad,
752 OperandTraits<CatchEndPadInst>::op_end(this) -
753 CRI.getNumOperands(),
754 CRI.getNumOperands()) {
755 SubclassOptionalData = CRI.SubclassOptionalData;
David Majnemereb518bd2015-08-04 08:21:40 +0000756 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000757 if (BasicBlock *UnwindDest = CRI.getUnwindDest())
758 setUnwindDest(UnwindDest);
759}
760
761void CatchEndPadInst::init(BasicBlock *UnwindBB) {
762 SubclassOptionalData = 0;
763 if (UnwindBB) {
764 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
765 setUnwindDest(UnwindBB);
766 }
767}
768
769CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000770 unsigned Values, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000771 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
772 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
773 Values, InsertBefore) {
774 init(UnwindBB);
775}
776
777CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB,
David Majnemer5c73c942015-08-13 22:11:40 +0000778 unsigned Values, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000779 : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad,
780 OperandTraits<CatchEndPadInst>::op_end(this) - Values,
781 Values, InsertAtEnd) {
782 init(UnwindBB);
783}
784
785BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const {
786 assert(Idx == 0);
787 return getUnwindDest();
788}
789unsigned CatchEndPadInst::getNumSuccessorsV() const {
790 return getNumSuccessors();
791}
792void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
793 assert(Idx == 0);
794 setUnwindDest(B);
795}
796
797//===----------------------------------------------------------------------===//
798// CatchReturnInst Implementation
799//===----------------------------------------------------------------------===//
800
801CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
802 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
803 OperandTraits<CatchReturnInst>::op_end(this) -
804 CRI.getNumOperands(),
805 CRI.getNumOperands()) {
806 Op<0>() = CRI.Op<0>();
807}
808
809CatchReturnInst::CatchReturnInst(BasicBlock *BB, Instruction *InsertBefore)
810 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
811 OperandTraits<CatchReturnInst>::op_begin(this), 1,
812 InsertBefore) {
813 Op<0>() = BB;
814}
815
816CatchReturnInst::CatchReturnInst(BasicBlock *BB, BasicBlock *InsertAtEnd)
817 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
818 OperandTraits<CatchReturnInst>::op_begin(this), 1,
819 InsertAtEnd) {
820 Op<0>() = BB;
821}
822
823BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
824 assert(Idx == 0);
825 return getSuccessor();
826}
827unsigned CatchReturnInst::getNumSuccessorsV() const {
828 return getNumSuccessors();
829}
830void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
831 assert(Idx == 0);
832 setSuccessor(B);
833}
834
835//===----------------------------------------------------------------------===//
836// CatchPadInst Implementation
837//===----------------------------------------------------------------------===//
838void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException,
David Majnemer5c73c942015-08-13 22:11:40 +0000839 ArrayRef<Value *> Args, const Twine &NameStr) {
David Majnemer654e1302015-07-31 17:58:14 +0000840 assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?");
841 Op<-2>() = IfNormal;
842 Op<-1>() = IfException;
843 std::copy(Args.begin(), Args.end(), op_begin());
844 setName(NameStr);
845}
846
847CatchPadInst::CatchPadInst(const CatchPadInst &CPI)
848 : TerminatorInst(CPI.getType(), Instruction::CatchPad,
849 OperandTraits<CatchPadInst>::op_end(this) -
850 CPI.getNumOperands(),
851 CPI.getNumOperands()) {
852 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
853}
854
855CatchPadInst::CatchPadInst(Type *RetTy, BasicBlock *IfNormal,
David Majnemer5c73c942015-08-13 22:11:40 +0000856 BasicBlock *IfException, ArrayRef<Value *> Args,
857 unsigned Values, const Twine &NameStr,
858 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000859 : TerminatorInst(RetTy, Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000860 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
861 InsertBefore) {
David Majnemer654e1302015-07-31 17:58:14 +0000862 init(IfNormal, IfException, Args, NameStr);
863}
864
865CatchPadInst::CatchPadInst(Type *RetTy, BasicBlock *IfNormal,
David Majnemer5c73c942015-08-13 22:11:40 +0000866 BasicBlock *IfException, ArrayRef<Value *> Args,
867 unsigned Values, const Twine &NameStr,
868 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000869 : TerminatorInst(RetTy, Instruction::CatchPad,
David Majnemer5c73c942015-08-13 22:11:40 +0000870 OperandTraits<CatchPadInst>::op_end(this) - Values, Values,
871 InsertAtEnd) {
David Majnemer654e1302015-07-31 17:58:14 +0000872 init(IfNormal, IfException, Args, NameStr);
873}
874
875BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const {
876 return getSuccessor(Idx);
877}
878unsigned CatchPadInst::getNumSuccessorsV() const {
879 return getNumSuccessors();
880}
881void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
882 return setSuccessor(Idx, B);
883}
884
885//===----------------------------------------------------------------------===//
886// TerminatePadInst Implementation
887//===----------------------------------------------------------------------===//
David Majnemer2a2b2422015-08-06 21:08:36 +0000888void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) {
David Majnemer654e1302015-07-31 17:58:14 +0000889 SubclassOptionalData = 0;
890 if (BB)
891 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
892 if (BB)
893 Op<-1>() = BB;
894 std::copy(Args.begin(), Args.end(), op_begin());
David Majnemer654e1302015-07-31 17:58:14 +0000895}
896
897TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
898 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
899 OperandTraits<TerminatePadInst>::op_end(this) -
900 TPI.getNumOperands(),
901 TPI.getNumOperands()) {
902 SubclassOptionalData = TPI.SubclassOptionalData;
David Majnemereb518bd2015-08-04 08:21:40 +0000903 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
David Majnemer654e1302015-07-31 17:58:14 +0000904 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
905}
906
907TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000908 ArrayRef<Value *> Args, unsigned Values,
909 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000910 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
911 OperandTraits<TerminatePadInst>::op_end(this) - Values,
912 Values, InsertBefore) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000913 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000914}
915
916TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB,
David Majnemer2a2b2422015-08-06 21:08:36 +0000917 ArrayRef<Value *> Args, unsigned Values,
918 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000919 : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad,
920 OperandTraits<TerminatePadInst>::op_end(this) - Values,
921 Values, InsertAtEnd) {
David Majnemer2a2b2422015-08-06 21:08:36 +0000922 init(BB, Args);
David Majnemer654e1302015-07-31 17:58:14 +0000923}
924
925BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
926 assert(Idx == 0);
927 return getUnwindDest();
928}
929unsigned TerminatePadInst::getNumSuccessorsV() const {
930 return getNumSuccessors();
931}
932void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
933 assert(Idx == 0);
934 return setUnwindDest(B);
935}
936
937//===----------------------------------------------------------------------===//
938// CleanupPadInst Implementation
939//===----------------------------------------------------------------------===//
940void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) {
941 assert(getNumOperands() == Args.size() && "NumOperands not set up?");
942 std::copy(Args.begin(), Args.end(), op_begin());
943 setName(NameStr);
944}
945
946CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI)
947 : Instruction(CPI.getType(), Instruction::CleanupPad,
948 OperandTraits<CleanupPadInst>::op_end(this) -
949 CPI.getNumOperands(),
950 CPI.getNumOperands()) {
951 std::copy(CPI.op_begin(), CPI.op_end(), op_begin());
952}
953
954CleanupPadInst::CleanupPadInst(Type *RetTy, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +0000955 const Twine &NameStr, Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000956 : Instruction(RetTy, Instruction::CleanupPad,
957 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
958 Args.size(), InsertBefore) {
959 init(Args, NameStr);
960}
961
962CleanupPadInst::CleanupPadInst(Type *RetTy, ArrayRef<Value *> Args,
David Majnemer5c73c942015-08-13 22:11:40 +0000963 const Twine &NameStr, BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000964 : Instruction(RetTy, Instruction::CleanupPad,
965 OperandTraits<CleanupPadInst>::op_end(this) - Args.size(),
966 Args.size(), InsertAtEnd) {
967 init(Args, NameStr);
968}
969
970//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000971// UnreachableInst Implementation
972//===----------------------------------------------------------------------===//
973
Owen Anderson55f1c092009-08-13 21:58:54 +0000974UnreachableInst::UnreachableInst(LLVMContext &Context,
975 Instruction *InsertBefore)
976 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000977 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000978}
Owen Anderson55f1c092009-08-13 21:58:54 +0000979UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
980 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000981 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000982}
983
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984unsigned UnreachableInst::getNumSuccessorsV() const {
985 return getNumSuccessors();
986}
987
988void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000989 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990}
991
992BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000993 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000994}
995
996//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000997// BranchInst Implementation
998//===----------------------------------------------------------------------===//
999
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000void BranchInst::AssertOK() {
1001 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001002 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001004}
1005
Chris Lattner2195fc42007-02-24 00:55:48 +00001006BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001007 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001008 OperandTraits<BranchInst>::op_end(this) - 1,
1009 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001010 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001011 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001012}
1013BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1014 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001015 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001016 OperandTraits<BranchInst>::op_end(this) - 3,
1017 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001018 Op<-1>() = IfTrue;
1019 Op<-2>() = IfFalse;
1020 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001021#ifndef NDEBUG
1022 AssertOK();
1023#endif
1024}
1025
1026BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001027 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001028 OperandTraits<BranchInst>::op_end(this) - 1,
1029 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001030 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001031 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001032}
1033
1034BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1035 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001036 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001037 OperandTraits<BranchInst>::op_end(this) - 3,
1038 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001039 Op<-1>() = IfTrue;
1040 Op<-2>() = IfFalse;
1041 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001042#ifndef NDEBUG
1043 AssertOK();
1044#endif
1045}
1046
1047
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001049 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1051 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001052 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001053 if (BI.getNumOperands() != 1) {
1054 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001055 Op<-3>() = BI.Op<-3>();
1056 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001058 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059}
1060
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001061void BranchInst::swapSuccessors() {
1062 assert(isConditional() &&
1063 "Cannot swap successors of an unconditional branch");
1064 Op<-1>().swap(Op<-2>());
1065
1066 // Update profile metadata if present and it matches our structural
1067 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001068 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001069 if (!ProfileData || ProfileData->getNumOperands() != 3)
1070 return;
1071
1072 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001073 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1074 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001075 setMetadata(LLVMContext::MD_prof,
1076 MDNode::get(ProfileData->getContext(), Ops));
1077}
1078
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001079BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1080 return getSuccessor(idx);
1081}
1082unsigned BranchInst::getNumSuccessorsV() const {
1083 return getNumSuccessors();
1084}
1085void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1086 setSuccessor(idx, B);
1087}
1088
1089
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001091// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001092//===----------------------------------------------------------------------===//
1093
Owen Andersonb6b25302009-07-14 23:09:55 +00001094static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001095 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001096 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001097 else {
1098 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001099 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001100 assert(Amt->getType()->isIntegerTy() &&
1101 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001102 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001103 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001104}
1105
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001106AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1107 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001108
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001109AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1110 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001111
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001112AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001113 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001114 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001115
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001116AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001117 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001118 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001119
Chris Lattner229907c2011-07-18 04:54:35 +00001120AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001121 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001122 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1123 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1124 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001125 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001126 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001127 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001128}
1129
Chris Lattner229907c2011-07-18 04:54:35 +00001130AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001131 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001132 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1133 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1134 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001135 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001136 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001137 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001138}
1139
Gordon Henriksen14a55692007-12-10 02:14:30 +00001140// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001141AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001142}
1143
Victor Hernandez8acf2952009-10-23 21:09:37 +00001144void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001145 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001146 assert(Align <= MaximumAlignment &&
1147 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001148 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1149 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001150 assert(getAlignment() == Align && "Alignment representation error!");
1151}
1152
Victor Hernandez8acf2952009-10-23 21:09:37 +00001153bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001154 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001155 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001156 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001157}
1158
Chris Lattner8b291e62008-11-26 02:54:17 +00001159/// isStaticAlloca - Return true if this alloca is in the entry block of the
1160/// function and is a constant size. If so, the code generator will fold it
1161/// into the prolog/epilog code, so it is basically free.
1162bool AllocaInst::isStaticAlloca() const {
1163 // Must be constant size.
1164 if (!isa<ConstantInt>(getArraySize())) return false;
1165
1166 // Must be in the entry block.
1167 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001168 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001169}
1170
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001171//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001172// LoadInst Implementation
1173//===----------------------------------------------------------------------===//
1174
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001175void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001176 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001177 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001178 assert(!(isAtomic() && getAlignment() == 0) &&
1179 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180}
1181
Daniel Dunbar4975db62009-07-25 04:41:11 +00001182LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001183 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184
Daniel Dunbar4975db62009-07-25 04:41:11 +00001185LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001186 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187
David Blaikie0c28fd72015-05-20 21:46:30 +00001188LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001190 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001191
1192LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1193 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001194 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001195
David Blaikieb7a029872015-04-17 19:56:21 +00001196LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001197 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001198 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001199 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001201LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001202 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001203 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001204}
1205
David Blaikie15d9a4c2015-04-06 20:59:48 +00001206LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001207 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001208 SynchronizationScope SynchScope, Instruction *InsertBef)
1209 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001210 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001211 setVolatile(isVolatile);
1212 setAlignment(Align);
1213 setAtomic(Order, SynchScope);
1214 AssertOK();
1215 setName(Name);
1216}
1217
1218LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1219 unsigned Align, AtomicOrdering Order,
1220 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001221 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001222 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001223 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001224 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001225 setAlignment(Align);
1226 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001227 AssertOK();
1228 setName(Name);
1229}
1230
Daniel Dunbar27096822009-08-11 18:11:15 +00001231LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1232 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1233 Load, Ptr, InsertBef) {
1234 setVolatile(false);
1235 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001236 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001237 AssertOK();
1238 if (Name && Name[0]) setName(Name);
1239}
1240
1241LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1242 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1243 Load, Ptr, InsertAE) {
1244 setVolatile(false);
1245 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001246 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001247 AssertOK();
1248 if (Name && Name[0]) setName(Name);
1249}
1250
David Blaikie0c28fd72015-05-20 21:46:30 +00001251LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001252 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001253 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1254 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001255 setVolatile(isVolatile);
1256 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001257 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001258 AssertOK();
1259 if (Name && Name[0]) setName(Name);
1260}
1261
1262LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1263 BasicBlock *InsertAE)
1264 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1265 Load, Ptr, InsertAE) {
1266 setVolatile(isVolatile);
1267 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001268 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001269 AssertOK();
1270 if (Name && Name[0]) setName(Name);
1271}
1272
Christopher Lamb84485702007-04-22 19:24:39 +00001273void LoadInst::setAlignment(unsigned Align) {
1274 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001275 assert(Align <= MaximumAlignment &&
1276 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001277 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001278 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001279 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001280}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001281
1282//===----------------------------------------------------------------------===//
1283// StoreInst Implementation
1284//===----------------------------------------------------------------------===//
1285
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001286void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001287 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001288 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001289 "Ptr must have pointer type!");
1290 assert(getOperand(0)->getType() ==
1291 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001292 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001293 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001294 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001295}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001298 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001299
1300StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001301 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001302
Misha Brukmanb1c93172005-04-21 23:48:37 +00001303StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001304 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001305 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001306
1307StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001308 BasicBlock *InsertAtEnd)
1309 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1310
1311StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1312 Instruction *InsertBefore)
1313 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1314 InsertBefore) {}
1315
1316StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1317 BasicBlock *InsertAtEnd)
1318 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1319 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001320
Misha Brukmanb1c93172005-04-21 23:48:37 +00001321StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001322 unsigned Align, AtomicOrdering Order,
1323 SynchronizationScope SynchScope,
1324 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001325 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001326 OperandTraits<StoreInst>::op_begin(this),
1327 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001328 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001329 Op<0>() = val;
1330 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001331 setVolatile(isVolatile);
1332 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001333 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001334 AssertOK();
1335}
1336
1337StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001338 unsigned Align, AtomicOrdering Order,
1339 SynchronizationScope SynchScope,
1340 BasicBlock *InsertAtEnd)
1341 : Instruction(Type::getVoidTy(val->getContext()), Store,
1342 OperandTraits<StoreInst>::op_begin(this),
1343 OperandTraits<StoreInst>::operands(this),
1344 InsertAtEnd) {
1345 Op<0>() = val;
1346 Op<1>() = addr;
1347 setVolatile(isVolatile);
1348 setAlignment(Align);
1349 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001350 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001351}
1352
Christopher Lamb84485702007-04-22 19:24:39 +00001353void StoreInst::setAlignment(unsigned Align) {
1354 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001355 assert(Align <= MaximumAlignment &&
1356 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001357 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001358 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001359 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001360}
1361
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001362//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001363// AtomicCmpXchgInst Implementation
1364//===----------------------------------------------------------------------===//
1365
1366void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001367 AtomicOrdering SuccessOrdering,
1368 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001369 SynchronizationScope SynchScope) {
1370 Op<0>() = Ptr;
1371 Op<1>() = Cmp;
1372 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001373 setSuccessOrdering(SuccessOrdering);
1374 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001375 setSynchScope(SynchScope);
1376
1377 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1378 "All operands must be non-null!");
1379 assert(getOperand(0)->getType()->isPointerTy() &&
1380 "Ptr must have pointer type!");
1381 assert(getOperand(1)->getType() ==
1382 cast<PointerType>(getOperand(0)->getType())->getElementType()
1383 && "Ptr must be a pointer to Cmp type!");
1384 assert(getOperand(2)->getType() ==
1385 cast<PointerType>(getOperand(0)->getType())->getElementType()
1386 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001387 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001388 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001389 assert(FailureOrdering != NotAtomic &&
1390 "AtomicCmpXchg instructions must be atomic!");
1391 assert(SuccessOrdering >= FailureOrdering &&
1392 "AtomicCmpXchg success ordering must be at least as strong as fail");
1393 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1394 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001395}
1396
1397AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001398 AtomicOrdering SuccessOrdering,
1399 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001400 SynchronizationScope SynchScope,
1401 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001402 : Instruction(
1403 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1404 nullptr),
1405 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1406 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001407 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001408}
1409
1410AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001411 AtomicOrdering SuccessOrdering,
1412 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001413 SynchronizationScope SynchScope,
1414 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001415 : Instruction(
1416 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1417 nullptr),
1418 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1419 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001420 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001421}
Tim Northover420a2162014-06-13 14:24:07 +00001422
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001423//===----------------------------------------------------------------------===//
1424// AtomicRMWInst Implementation
1425//===----------------------------------------------------------------------===//
1426
1427void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1428 AtomicOrdering Ordering,
1429 SynchronizationScope SynchScope) {
1430 Op<0>() = Ptr;
1431 Op<1>() = Val;
1432 setOperation(Operation);
1433 setOrdering(Ordering);
1434 setSynchScope(SynchScope);
1435
1436 assert(getOperand(0) && getOperand(1) &&
1437 "All operands must be non-null!");
1438 assert(getOperand(0)->getType()->isPointerTy() &&
1439 "Ptr must have pointer type!");
1440 assert(getOperand(1)->getType() ==
1441 cast<PointerType>(getOperand(0)->getType())->getElementType()
1442 && "Ptr must be a pointer to Val type!");
1443 assert(Ordering != NotAtomic &&
1444 "AtomicRMW instructions must be atomic!");
1445}
1446
1447AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1448 AtomicOrdering Ordering,
1449 SynchronizationScope SynchScope,
1450 Instruction *InsertBefore)
1451 : Instruction(Val->getType(), AtomicRMW,
1452 OperandTraits<AtomicRMWInst>::op_begin(this),
1453 OperandTraits<AtomicRMWInst>::operands(this),
1454 InsertBefore) {
1455 Init(Operation, Ptr, Val, Ordering, SynchScope);
1456}
1457
1458AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1459 AtomicOrdering Ordering,
1460 SynchronizationScope SynchScope,
1461 BasicBlock *InsertAtEnd)
1462 : Instruction(Val->getType(), AtomicRMW,
1463 OperandTraits<AtomicRMWInst>::op_begin(this),
1464 OperandTraits<AtomicRMWInst>::operands(this),
1465 InsertAtEnd) {
1466 Init(Operation, Ptr, Val, Ordering, SynchScope);
1467}
1468
1469//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001470// FenceInst Implementation
1471//===----------------------------------------------------------------------===//
1472
1473FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1474 SynchronizationScope SynchScope,
1475 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001476 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001477 setOrdering(Ordering);
1478 setSynchScope(SynchScope);
1479}
1480
1481FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1482 SynchronizationScope SynchScope,
1483 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001484 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001485 setOrdering(Ordering);
1486 setSynchScope(SynchScope);
1487}
1488
1489//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001490// GetElementPtrInst Implementation
1491//===----------------------------------------------------------------------===//
1492
Jay Foadd1b78492011-07-25 09:48:08 +00001493void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001494 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001495 assert(getNumOperands() == 1 + IdxList.size() &&
1496 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001497 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001498 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001499 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001500}
1501
Gabor Greiff6caff662008-05-10 08:32:32 +00001502GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001503 : Instruction(GEPI.getType(), GetElementPtr,
1504 OperandTraits<GetElementPtrInst>::op_end(this) -
1505 GEPI.getNumOperands(),
1506 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001507 SourceElementType(GEPI.SourceElementType),
1508 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001509 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001510 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001511}
1512
Chris Lattner6090a422009-03-09 04:46:40 +00001513/// getIndexedType - Returns the type of the element that would be accessed with
1514/// a gep instruction with the specified parameters.
1515///
1516/// The Idxs pointer should point to a continuous piece of memory containing the
1517/// indices, either as Value* or uint64_t.
1518///
1519/// A null type is returned if the indices are invalid for the specified
1520/// pointer type.
1521///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001522template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001523static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001524 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001525 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001526 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001527
Chris Lattner6090a422009-03-09 04:46:40 +00001528 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001529 // it cannot be 'stepped over'.
1530 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001531 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001532
Dan Gohman1ecaf452008-05-31 00:58:22 +00001533 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001534 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001535 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001536 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001537 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001538 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001539 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001540 }
Craig Topperc6207612014-04-09 06:08:46 +00001541 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001542}
1543
David Blaikied288fb82015-03-30 21:41:43 +00001544Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1545 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001546}
1547
David Blaikied288fb82015-03-30 21:41:43 +00001548Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001549 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001550 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001551}
1552
David Blaikied288fb82015-03-30 21:41:43 +00001553Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1554 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001555}
1556
Chris Lattner45f15572007-04-14 00:12:57 +00001557/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1558/// zeros. If so, the result pointer and the first operand have the same
1559/// value, just potentially different types.
1560bool GetElementPtrInst::hasAllZeroIndices() const {
1561 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1562 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1563 if (!CI->isZero()) return false;
1564 } else {
1565 return false;
1566 }
1567 }
1568 return true;
1569}
1570
Chris Lattner27058292007-04-27 20:35:56 +00001571/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1572/// constant integers. If so, the result pointer and the first operand have
1573/// a constant offset between them.
1574bool GetElementPtrInst::hasAllConstantIndices() const {
1575 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1576 if (!isa<ConstantInt>(getOperand(i)))
1577 return false;
1578 }
1579 return true;
1580}
1581
Dan Gohman1b849082009-09-07 23:54:19 +00001582void GetElementPtrInst::setIsInBounds(bool B) {
1583 cast<GEPOperator>(this)->setIsInBounds(B);
1584}
Chris Lattner45f15572007-04-14 00:12:57 +00001585
Nick Lewycky28a5f252009-09-27 21:33:04 +00001586bool GetElementPtrInst::isInBounds() const {
1587 return cast<GEPOperator>(this)->isInBounds();
1588}
1589
Chandler Carruth1e140532012-12-11 10:29:10 +00001590bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1591 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001592 // Delegate to the generic GEPOperator implementation.
1593 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001594}
1595
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001597// ExtractElementInst Implementation
1598//===----------------------------------------------------------------------===//
1599
1600ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001601 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001602 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001603 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001604 ExtractElement,
1605 OperandTraits<ExtractElementInst>::op_begin(this),
1606 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001607 assert(isValidOperands(Val, Index) &&
1608 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001609 Op<0>() = Val;
1610 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001611 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001612}
1613
1614ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001615 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001616 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001617 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001618 ExtractElement,
1619 OperandTraits<ExtractElementInst>::op_begin(this),
1620 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001621 assert(isValidOperands(Val, Index) &&
1622 "Invalid extractelement instruction operands!");
1623
Gabor Greif2d3024d2008-05-26 21:33:52 +00001624 Op<0>() = Val;
1625 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001626 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001627}
1628
Chris Lattner65511ff2006-10-05 06:24:58 +00001629
Chris Lattner54865b32006-04-08 04:05:48 +00001630bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001631 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001632 return false;
1633 return true;
1634}
1635
1636
Robert Bocchino23004482006-01-10 19:05:34 +00001637//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001638// InsertElementInst Implementation
1639//===----------------------------------------------------------------------===//
1640
Chris Lattner54865b32006-04-08 04:05:48 +00001641InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001642 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001643 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001644 : Instruction(Vec->getType(), InsertElement,
1645 OperandTraits<InsertElementInst>::op_begin(this),
1646 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001647 assert(isValidOperands(Vec, Elt, Index) &&
1648 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001649 Op<0>() = Vec;
1650 Op<1>() = Elt;
1651 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001652 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001653}
1654
Chris Lattner54865b32006-04-08 04:05:48 +00001655InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001656 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001657 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001658 : Instruction(Vec->getType(), InsertElement,
1659 OperandTraits<InsertElementInst>::op_begin(this),
1660 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001661 assert(isValidOperands(Vec, Elt, Index) &&
1662 "Invalid insertelement instruction operands!");
1663
Gabor Greif2d3024d2008-05-26 21:33:52 +00001664 Op<0>() = Vec;
1665 Op<1>() = Elt;
1666 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001667 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001668}
1669
Chris Lattner54865b32006-04-08 04:05:48 +00001670bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1671 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001672 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001673 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001674
Reid Spencerd84d35b2007-02-15 02:26:10 +00001675 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001676 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001677
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001678 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001679 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001680 return true;
1681}
1682
1683
Robert Bocchinoca27f032006-01-17 20:07:22 +00001684//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001685// ShuffleVectorInst Implementation
1686//===----------------------------------------------------------------------===//
1687
1688ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001689 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001690 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001691: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001692 cast<VectorType>(Mask->getType())->getNumElements()),
1693 ShuffleVector,
1694 OperandTraits<ShuffleVectorInst>::op_begin(this),
1695 OperandTraits<ShuffleVectorInst>::operands(this),
1696 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001697 assert(isValidOperands(V1, V2, Mask) &&
1698 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001699 Op<0>() = V1;
1700 Op<1>() = V2;
1701 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001702 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001703}
1704
1705ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001706 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001707 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001708: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1709 cast<VectorType>(Mask->getType())->getNumElements()),
1710 ShuffleVector,
1711 OperandTraits<ShuffleVectorInst>::op_begin(this),
1712 OperandTraits<ShuffleVectorInst>::operands(this),
1713 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714 assert(isValidOperands(V1, V2, Mask) &&
1715 "Invalid shuffle vector instruction operands!");
1716
Gabor Greif2d3024d2008-05-26 21:33:52 +00001717 Op<0>() = V1;
1718 Op<1>() = V2;
1719 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001720 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001721}
1722
Mon P Wang25f01062008-11-10 04:46:22 +00001723bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001724 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001725 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001726 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001727 return false;
1728
Chris Lattner1dcb6542012-01-25 23:49:49 +00001729 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001730 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001731 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001732 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001733
1734 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001735 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1736 return true;
1737
Nate Begeman60a31c32010-08-13 00:16:46 +00001738 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001739 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001740 for (Value *Op : MV->operands()) {
1741 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001742 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001743 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001744 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001745 return false;
1746 }
1747 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001748 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001749 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001750
1751 if (const ConstantDataSequential *CDS =
1752 dyn_cast<ConstantDataSequential>(Mask)) {
1753 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1754 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1755 if (CDS->getElementAsInteger(i) >= V1Size*2)
1756 return false;
1757 return true;
1758 }
1759
1760 // The bitcode reader can create a place holder for a forward reference
1761 // used as the shuffle mask. When this occurs, the shuffle mask will
1762 // fall into this case and fail. To avoid this error, do this bit of
1763 // ugliness to allow such a mask pass.
1764 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1765 if (CE->getOpcode() == Instruction::UserOp1)
1766 return true;
1767
1768 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001769}
1770
Chris Lattnerf724e342008-03-02 05:28:33 +00001771/// getMaskValue - Return the index from the shuffle mask for the specified
1772/// output result. This is either -1 if the element is undef or a number less
1773/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001774int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1775 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1776 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001777 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001778 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001779 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001780 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001781 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001782}
1783
Chris Lattner1dcb6542012-01-25 23:49:49 +00001784/// getShuffleMask - Return the full mask for this instruction, where each
1785/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001786void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1787 SmallVectorImpl<int> &Result) {
1788 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001789
Chris Lattnercf129702012-01-26 02:51:13 +00001790 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001791 for (unsigned i = 0; i != NumElts; ++i)
1792 Result.push_back(CDS->getElementAsInteger(i));
1793 return;
1794 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001795 for (unsigned i = 0; i != NumElts; ++i) {
1796 Constant *C = Mask->getAggregateElement(i);
1797 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001798 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001799 }
1800}
1801
1802
Dan Gohman12fce772008-05-15 19:50:34 +00001803//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001804// InsertValueInst Class
1805//===----------------------------------------------------------------------===//
1806
Jay Foad57aa6362011-07-13 10:26:04 +00001807void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1808 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001809 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001810
1811 // There's no fundamental reason why we require at least one index
1812 // (other than weirdness with &*IdxBegin being invalid; see
1813 // getelementptr's init routine for example). But there's no
1814 // present need to support it.
1815 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1816
1817 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001818 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001819 Op<0>() = Agg;
1820 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001821
Jay Foad57aa6362011-07-13 10:26:04 +00001822 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001823 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001824}
1825
1826InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001827 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001828 OperandTraits<InsertValueInst>::op_begin(this), 2),
1829 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001830 Op<0>() = IVI.getOperand(0);
1831 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001832 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001833}
1834
1835//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001836// ExtractValueInst Class
1837//===----------------------------------------------------------------------===//
1838
Jay Foad57aa6362011-07-13 10:26:04 +00001839void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001840 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001841
Jay Foad57aa6362011-07-13 10:26:04 +00001842 // There's no fundamental reason why we require at least one index.
1843 // But there's no present need to support it.
1844 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001845
Jay Foad57aa6362011-07-13 10:26:04 +00001846 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001847 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001848}
1849
1850ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001851 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001852 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001853 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001854}
1855
Dan Gohman12fce772008-05-15 19:50:34 +00001856// getIndexedType - Returns the type of the element that would be extracted
1857// with an extractvalue instruction with the specified parameters.
1858//
1859// A null type is returned if the indices are invalid for the specified
1860// pointer type.
1861//
Chris Lattner229907c2011-07-18 04:54:35 +00001862Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001863 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001864 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001865 // We can't use CompositeType::indexValid(Index) here.
1866 // indexValid() always returns true for arrays because getelementptr allows
1867 // out-of-bounds indices. Since we don't allow those for extractvalue and
1868 // insertvalue we need to check array indexing manually.
1869 // Since the only other types we can index into are struct types it's just
1870 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001871 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001872 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001873 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001874 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001875 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001876 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001877 } else {
1878 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001879 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001880 }
1881
1882 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001883 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001884 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001885}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001886
1887//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001888// BinaryOperator Class
1889//===----------------------------------------------------------------------===//
1890
Chris Lattner2195fc42007-02-24 00:55:48 +00001891BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001892 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001893 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001894 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001895 OperandTraits<BinaryOperator>::op_begin(this),
1896 OperandTraits<BinaryOperator>::operands(this),
1897 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001898 Op<0>() = S1;
1899 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001900 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001901 setName(Name);
1902}
1903
1904BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001905 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001906 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001907 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001908 OperandTraits<BinaryOperator>::op_begin(this),
1909 OperandTraits<BinaryOperator>::operands(this),
1910 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001911 Op<0>() = S1;
1912 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001913 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001914 setName(Name);
1915}
1916
1917
1918void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001919 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001920 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001921 assert(LHS->getType() == RHS->getType() &&
1922 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001923#ifndef NDEBUG
1924 switch (iType) {
1925 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001926 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001927 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001928 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001929 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001930 "Tried to create an integer operation on a non-integer type!");
1931 break;
1932 case FAdd: case FSub:
1933 case FMul:
1934 assert(getType() == LHS->getType() &&
1935 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001936 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001937 "Tried to create a floating-point operation on a "
1938 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001939 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001940 case UDiv:
1941 case SDiv:
1942 assert(getType() == LHS->getType() &&
1943 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001944 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001945 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001946 "Incorrect operand type (not integer) for S/UDIV");
1947 break;
1948 case FDiv:
1949 assert(getType() == LHS->getType() &&
1950 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001951 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001952 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001953 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001954 case URem:
1955 case SRem:
1956 assert(getType() == LHS->getType() &&
1957 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001958 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001959 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001960 "Incorrect operand type (not integer) for S/UREM");
1961 break;
1962 case FRem:
1963 assert(getType() == LHS->getType() &&
1964 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001965 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001966 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001967 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001968 case Shl:
1969 case LShr:
1970 case AShr:
1971 assert(getType() == LHS->getType() &&
1972 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001973 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001974 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001975 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001976 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001977 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001978 case And: case Or:
1979 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001980 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001981 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001982 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001983 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001984 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001985 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001986 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 default:
1988 break;
1989 }
1990#endif
1991}
1992
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001993BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001994 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001995 Instruction *InsertBefore) {
1996 assert(S1->getType() == S2->getType() &&
1997 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001998 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001999}
2000
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002001BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002002 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002003 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002005 InsertAtEnd->getInstList().push_back(Res);
2006 return Res;
2007}
2008
Dan Gohman5476cfd2009-08-12 16:23:25 +00002009BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002010 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002011 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002012 return new BinaryOperator(Instruction::Sub,
2013 zero, Op,
2014 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002015}
2016
Dan Gohman5476cfd2009-08-12 16:23:25 +00002017BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002018 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002019 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002020 return new BinaryOperator(Instruction::Sub,
2021 zero, Op,
2022 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002023}
2024
Dan Gohman4ab44202009-12-18 02:58:50 +00002025BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2026 Instruction *InsertBefore) {
2027 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2028 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2029}
2030
2031BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2032 BasicBlock *InsertAtEnd) {
2033 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2034 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2035}
2036
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002037BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2038 Instruction *InsertBefore) {
2039 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2040 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2041}
2042
2043BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2044 BasicBlock *InsertAtEnd) {
2045 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2046 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
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 Instruction *InsertBefore) {
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, InsertBefore);
2054}
2055
Dan Gohman5476cfd2009-08-12 16:23:25 +00002056BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002057 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002058 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002059 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002060 Op->getType(), Name, InsertAtEnd);
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 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002065 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002066 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002067 Op->getType(), Name, InsertBefore);
2068}
2069
Dan Gohman5476cfd2009-08-12 16:23:25 +00002070BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002071 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002072 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002073 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002074 Op->getType(), Name, InsertAtEnd);
2075}
2076
2077
2078// isConstantAllOnes - Helper function for several functions below
2079static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002080 if (const Constant *C = dyn_cast<Constant>(V))
2081 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002082 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083}
2084
Owen Andersonbb2501b2009-07-13 22:18:28 +00002085bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002086 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2087 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002088 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2089 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002090 return false;
2091}
2092
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002093bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002094 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2095 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002096 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2097 if (!IgnoreZeroSign)
2098 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2099 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2100 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002101 return false;
2102}
2103
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104bool BinaryOperator::isNot(const Value *V) {
2105 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2106 return (Bop->getOpcode() == Instruction::Xor &&
2107 (isConstantAllOnes(Bop->getOperand(1)) ||
2108 isConstantAllOnes(Bop->getOperand(0))));
2109 return false;
2110}
2111
Chris Lattner2c7d1772005-04-24 07:28:37 +00002112Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002113 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002114}
2115
Chris Lattner2c7d1772005-04-24 07:28:37 +00002116const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2117 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118}
2119
Dan Gohmana5b96452009-06-04 22:49:04 +00002120Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002121 return cast<BinaryOperator>(BinOp)->getOperand(1);
2122}
2123
2124const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2125 return getFNegArgument(const_cast<Value*>(BinOp));
2126}
2127
Chris Lattner2c7d1772005-04-24 07:28:37 +00002128Value *BinaryOperator::getNotArgument(Value *BinOp) {
2129 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2130 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2131 Value *Op0 = BO->getOperand(0);
2132 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002133 if (isConstantAllOnes(Op0)) return Op1;
2134
2135 assert(isConstantAllOnes(Op1));
2136 return Op0;
2137}
2138
Chris Lattner2c7d1772005-04-24 07:28:37 +00002139const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2140 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002141}
2142
2143
2144// swapOperands - Exchange the two operands to this instruction. This
2145// instruction is safe to use on any binary instruction and does not
2146// modify the semantics of the instruction. If the instruction is
2147// order dependent (SetLT f.e.) the opcode is changed.
2148//
2149bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002150 if (!isCommutative())
2151 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002152 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002153 return false;
2154}
2155
Dan Gohman1b849082009-09-07 23:54:19 +00002156void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2157 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2158}
2159
2160void BinaryOperator::setHasNoSignedWrap(bool b) {
2161 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2162}
2163
2164void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002165 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002166}
2167
Nick Lewycky28a5f252009-09-27 21:33:04 +00002168bool BinaryOperator::hasNoUnsignedWrap() const {
2169 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2170}
2171
2172bool BinaryOperator::hasNoSignedWrap() const {
2173 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2174}
2175
2176bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002177 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002178}
2179
Sanjay Patela982d992014-09-03 01:06:50 +00002180void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002181 // Copy the wrapping flags.
2182 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2183 setHasNoSignedWrap(OB->hasNoSignedWrap());
2184 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2185 }
2186
2187 // Copy the exact flag.
2188 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2189 setIsExact(PE->isExact());
2190
2191 // Copy the fast-math flags.
2192 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002193 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002194}
2195
Sanjay Patela982d992014-09-03 01:06:50 +00002196void BinaryOperator::andIRFlags(const Value *V) {
2197 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2198 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2199 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2200 }
2201
2202 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2203 setIsExact(isExact() & PE->isExact());
2204
2205 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2206 FastMathFlags FM = getFastMathFlags();
2207 FM &= FP->getFastMathFlags();
2208 copyFastMathFlags(FM);
2209 }
2210}
2211
2212
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002213//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002214// FPMathOperator Class
2215//===----------------------------------------------------------------------===//
2216
2217/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2218/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002219/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002220float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002221 const MDNode *MD =
2222 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002223 if (!MD)
2224 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002225 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002226 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002227}
2228
2229
2230//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002231// CastInst Class
2232//===----------------------------------------------------------------------===//
2233
David Blaikie3a15e142011-12-01 08:00:17 +00002234void CastInst::anchor() {}
2235
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236// Just determine if this cast only deals with integral->integral conversion.
2237bool CastInst::isIntegerCast() const {
2238 switch (getOpcode()) {
2239 default: return false;
2240 case Instruction::ZExt:
2241 case Instruction::SExt:
2242 case Instruction::Trunc:
2243 return true;
2244 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002245 return getOperand(0)->getType()->isIntegerTy() &&
2246 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002248}
2249
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250bool CastInst::isLosslessCast() const {
2251 // Only BitCast can be lossless, exit fast if we're not BitCast
2252 if (getOpcode() != Instruction::BitCast)
2253 return false;
2254
2255 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002256 Type* SrcTy = getOperand(0)->getType();
2257 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258 if (SrcTy == DstTy)
2259 return true;
2260
Reid Spencer8d9336d2006-12-31 05:26:44 +00002261 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002262 if (SrcTy->isPointerTy())
2263 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 return false; // Other types have no identity values
2265}
2266
2267/// This function determines if the CastInst does not require any bits to be
2268/// changed in order to effect the cast. Essentially, it identifies cases where
2269/// no code gen is necessary for the cast, hence the name no-op cast. For
2270/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002271/// # bitcast i32* %x to i8*
2272/// # bitcast <2 x i32> %x to <4 x i16>
2273/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002274/// @brief Determine if the described cast is a no-op.
2275bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002276 Type *SrcTy,
2277 Type *DestTy,
2278 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002279 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002280 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281 case Instruction::Trunc:
2282 case Instruction::ZExt:
2283 case Instruction::SExt:
2284 case Instruction::FPTrunc:
2285 case Instruction::FPExt:
2286 case Instruction::UIToFP:
2287 case Instruction::SIToFP:
2288 case Instruction::FPToUI:
2289 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002290 case Instruction::AddrSpaceCast:
2291 // TODO: Target informations may give a more accurate answer here.
2292 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 case Instruction::BitCast:
2294 return true; // BitCast never modifies bits.
2295 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002296 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002297 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002299 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002300 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 }
2302}
2303
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002304/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002305bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002306 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2307}
2308
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002309bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002310 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002311 if (getOpcode() == Instruction::PtrToInt)
2312 PtrOpTy = getOperand(0)->getType();
2313 else if (getOpcode() == Instruction::IntToPtr)
2314 PtrOpTy = getType();
2315
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002316 Type *IntPtrTy =
2317 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002318
2319 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2320}
2321
2322/// This function determines if a pair of casts can be eliminated and what
2323/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324/// instructions like this:
2325/// * %F = firstOpcode SrcTy %x to MidTy
2326/// * %S = secondOpcode MidTy %F to DstTy
2327/// The function returns a resultOpcode so these two casts can be replaced with:
2328/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2329/// If no such cast is permited, the function returns 0.
2330unsigned CastInst::isEliminableCastPair(
2331 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002332 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2333 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 // Define the 144 possibilities for these two cast instructions. The values
2335 // in this matrix determine what to do in a given situation and select the
2336 // case in the switch below. The rows correspond to firstOp, the columns
2337 // correspond to secondOp. In looking at the table below, keep in mind
2338 // the following cast properties:
2339 //
2340 // Size Compare Source Destination
2341 // Operator Src ? Size Type Sign Type Sign
2342 // -------- ------------ ------------------- ---------------------
2343 // TRUNC > Integer Any Integral Any
2344 // ZEXT < Integral Unsigned Integer Any
2345 // SEXT < Integral Signed Integer Any
2346 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002347 // FPTOSI n/a FloatPt n/a Integral Signed
2348 // UITOFP n/a Integral Unsigned FloatPt n/a
2349 // SITOFP n/a Integral Signed FloatPt n/a
2350 // FPTRUNC > FloatPt n/a FloatPt n/a
2351 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 // PTRTOINT n/a Pointer n/a Integral Unsigned
2353 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002354 // BITCAST = FirstClass n/a FirstClass n/a
2355 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002356 //
2357 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002358 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2359 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002360 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002361 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002362 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002363 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002364 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2366 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002367 // T F F U S F F P I B A -+
2368 // R Z S P P I I T P 2 N T S |
2369 // U E E 2 2 2 2 R E I T C C +- secondOp
2370 // N X X U S F F N X N 2 V V |
2371 // C T T I I P P C T T P T T -+
2372 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002373 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002374 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2375 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2376 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2377 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2378 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002379 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2381 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2382 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2383 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2384 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002386
Chris Lattner25eea4d2010-07-12 01:19:22 +00002387 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002388 // merging. However, bitcast of A->B->A are allowed.
2389 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2390 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2391 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2392
2393 // Check if any of the bitcasts convert scalars<->vectors.
2394 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2395 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2396 // Unless we are bitcasing to the original type, disallow optimizations.
2397 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398
2399 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2400 [secondOp-Instruction::CastOpsBegin];
2401 switch (ElimCase) {
2402 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 return 0;
2405 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002406 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 return firstOp;
2408 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002409 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 return secondOp;
2411 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002412 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002413 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002414 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002415 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 return firstOp;
2417 return 0;
2418 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002419 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002420 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002421 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return firstOp;
2423 return 0;
2424 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002426 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002427 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return secondOp;
2429 return 0;
2430 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002431 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002432 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002433 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 return secondOp;
2435 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002436 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002437 // Cannot simplify if address spaces are different!
2438 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2439 return 0;
2440
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002441 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002442 // We can still fold this without knowing the actual sizes as long we
2443 // know that the intermediate pointer is the largest possible
2444 // pointer size.
2445 // FIXME: Is this always true?
2446 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002447 return Instruction::BitCast;
2448
2449 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002450 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002451 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002452 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 if (MidSize >= PtrSize)
2454 return Instruction::BitCast;
2455 return 0;
2456 }
2457 case 8: {
2458 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2459 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2460 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002461 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2462 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 if (SrcSize == DstSize)
2464 return Instruction::BitCast;
2465 else if (SrcSize < DstSize)
2466 return firstOp;
2467 return secondOp;
2468 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002469 case 9:
2470 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 return Instruction::ZExt;
2472 case 10:
2473 // fpext followed by ftrunc is allowed if the bit size returned to is
2474 // the same as the original, in which case its just a bitcast
2475 if (SrcTy == DstTy)
2476 return Instruction::BitCast;
2477 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002478 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002480 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002481 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002482 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002483 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2484 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 if (SrcSize <= PtrSize && SrcSize == DstSize)
2486 return Instruction::BitCast;
2487 return 0;
2488 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002489 case 12: {
2490 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2491 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2492 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2493 return Instruction::AddrSpaceCast;
2494 return Instruction::BitCast;
2495 }
2496 case 13:
2497 // FIXME: this state can be merged with (1), but the following assert
2498 // is useful to check the correcteness of the sequence due to semantic
2499 // change of bitcast.
2500 assert(
2501 SrcTy->isPtrOrPtrVectorTy() &&
2502 MidTy->isPtrOrPtrVectorTy() &&
2503 DstTy->isPtrOrPtrVectorTy() &&
2504 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2505 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2506 "Illegal addrspacecast, bitcast sequence!");
2507 // Allowed, use first cast's opcode
2508 return firstOp;
2509 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002510 // bitcast, addrspacecast -> addrspacecast if the element type of
2511 // bitcast's source is the same as that of addrspacecast's destination.
2512 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2513 return Instruction::AddrSpaceCast;
2514 return 0;
2515
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002516 case 15:
2517 // FIXME: this state can be merged with (1), but the following assert
2518 // is useful to check the correcteness of the sequence due to semantic
2519 // change of bitcast.
2520 assert(
2521 SrcTy->isIntOrIntVectorTy() &&
2522 MidTy->isPtrOrPtrVectorTy() &&
2523 DstTy->isPtrOrPtrVectorTy() &&
2524 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2525 "Illegal inttoptr, bitcast sequence!");
2526 // Allowed, use first cast's opcode
2527 return firstOp;
2528 case 16:
2529 // FIXME: this state can be merged with (2), but the following assert
2530 // is useful to check the correcteness of the sequence due to semantic
2531 // change of bitcast.
2532 assert(
2533 SrcTy->isPtrOrPtrVectorTy() &&
2534 MidTy->isPtrOrPtrVectorTy() &&
2535 DstTy->isIntOrIntVectorTy() &&
2536 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2537 "Illegal bitcast, ptrtoint sequence!");
2538 // Allowed, use second cast's opcode
2539 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002540 case 17:
2541 // (sitofp (zext x)) -> (uitofp x)
2542 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002544 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002546 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 default:
Craig Topperc514b542012-02-05 22:14:15 +00002548 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550}
2551
Chris Lattner229907c2011-07-18 04:54:35 +00002552CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002553 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002554 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555 // Construct and return the appropriate CastInst subclass
2556 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002557 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2558 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2559 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2560 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2561 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2562 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2563 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2564 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2565 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2566 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2567 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2568 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2569 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2570 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572}
2573
Chris Lattner229907c2011-07-18 04:54:35 +00002574CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002575 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002576 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 // Construct and return the appropriate CastInst subclass
2578 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002579 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2580 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2581 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2582 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2583 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2584 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2585 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2586 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2587 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2588 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2589 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2590 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2591 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2592 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
Chris Lattner229907c2011-07-18 04:54:35 +00002596CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002598 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002599 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002600 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2601 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002602}
2603
Chris Lattner229907c2011-07-18 04:54:35 +00002604CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002605 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002606 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002607 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002608 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2609 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002610}
2611
Chris Lattner229907c2011-07-18 04:54:35 +00002612CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002613 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002614 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002615 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002616 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2617 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002618}
2619
Chris Lattner229907c2011-07-18 04:54:35 +00002620CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002622 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002623 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002624 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2625 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002626}
2627
Chris Lattner229907c2011-07-18 04:54:35 +00002628CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002629 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002630 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002631 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002632 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2633 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002634}
2635
Chris Lattner229907c2011-07-18 04:54:35 +00002636CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002638 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002640 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2641 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002642}
2643
Chris Lattner229907c2011-07-18 04:54:35 +00002644CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002646 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002647 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2648 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2649 "Invalid cast");
2650 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002651 assert((!Ty->isVectorTy() ||
2652 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002653 "Invalid cast");
2654
Matt Arsenault065ced92013-07-31 00:17:33 +00002655 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002656 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002657
Matt Arsenault740980e2014-07-14 17:24:35 +00002658 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002659}
2660
2661/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002662CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2663 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002664 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002665 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2666 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002667 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002668 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002669 assert((!Ty->isVectorTy() ||
2670 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002671 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002672
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002673 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002674 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002675
Matt Arsenault740980e2014-07-14 17:24:35 +00002676 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2677}
2678
2679CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2680 Value *S, Type *Ty,
2681 const Twine &Name,
2682 BasicBlock *InsertAtEnd) {
2683 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2684 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2685
2686 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2687 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2688
2689 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2690}
2691
2692CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2693 Value *S, Type *Ty,
2694 const Twine &Name,
2695 Instruction *InsertBefore) {
2696 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2697 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2698
2699 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002700 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2701
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002702 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002703}
2704
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002705CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2706 const Twine &Name,
2707 Instruction *InsertBefore) {
2708 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2709 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2710 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2711 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2712
2713 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2714}
2715
Matt Arsenault740980e2014-07-14 17:24:35 +00002716CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002717 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002718 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002719 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002720 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002721 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2722 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002723 Instruction::CastOps opcode =
2724 (SrcBits == DstBits ? Instruction::BitCast :
2725 (SrcBits > DstBits ? Instruction::Trunc :
2726 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002727 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002728}
2729
Chris Lattner229907c2011-07-18 04:54:35 +00002730CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002731 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002732 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002733 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002734 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002735 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2736 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002737 Instruction::CastOps opcode =
2738 (SrcBits == DstBits ? Instruction::BitCast :
2739 (SrcBits > DstBits ? Instruction::Trunc :
2740 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002741 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002742}
2743
Chris Lattner229907c2011-07-18 04:54:35 +00002744CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002745 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002746 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002747 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002748 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002749 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2750 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002751 Instruction::CastOps opcode =
2752 (SrcBits == DstBits ? Instruction::BitCast :
2753 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002754 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002755}
2756
Chris Lattner229907c2011-07-18 04:54:35 +00002757CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002758 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002759 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002760 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002761 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002762 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2763 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002764 Instruction::CastOps opcode =
2765 (SrcBits == DstBits ? Instruction::BitCast :
2766 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002767 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002768}
2769
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002770// Check whether it is valid to call getCastOpcode for these types.
2771// This routine must be kept in sync with getCastOpcode.
2772bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2773 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2774 return false;
2775
2776 if (SrcTy == DestTy)
2777 return true;
2778
2779 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2780 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2781 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2782 // An element by element cast. Valid if casting the elements is valid.
2783 SrcTy = SrcVecTy->getElementType();
2784 DestTy = DestVecTy->getElementType();
2785 }
2786
2787 // Get the bit sizes, we'll need these
2788 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2789 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2790
2791 // Run through the possibilities ...
2792 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002793 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002794 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002795 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002796 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002797 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002798 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002799 // Casting from something else
2800 return SrcTy->isPointerTy();
2801 }
2802 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2803 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002804 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002805 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002806 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002807 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002808 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002809 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002810 return false;
2811 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002812 if (DestTy->isVectorTy()) // Casting to vector
2813 return DestBits == SrcBits;
2814 if (DestTy->isPointerTy()) { // Casting to pointer
2815 if (SrcTy->isPointerTy()) // Casting from pointer
2816 return true;
2817 return SrcTy->isIntegerTy(); // Casting from integral
2818 }
2819 if (DestTy->isX86_MMXTy()) {
2820 if (SrcTy->isVectorTy())
2821 return DestBits == SrcBits; // 64-bit vector to MMX
2822 return false;
2823 } // Casting to something else
2824 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002825}
2826
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002827bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2828 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2829 return false;
2830
2831 if (SrcTy == DestTy)
2832 return true;
2833
2834 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2835 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2836 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2837 // An element by element cast. Valid if casting the elements is valid.
2838 SrcTy = SrcVecTy->getElementType();
2839 DestTy = DestVecTy->getElementType();
2840 }
2841 }
2842 }
2843
2844 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2845 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2846 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2847 }
2848 }
2849
2850 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2851 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2852
2853 // Could still have vectors of pointers if the number of elements doesn't
2854 // match
2855 if (SrcBits == 0 || DestBits == 0)
2856 return false;
2857
2858 if (SrcBits != DestBits)
2859 return false;
2860
2861 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2862 return false;
2863
2864 return true;
2865}
2866
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002867bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002868 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002869 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2870 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002871 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002872 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2873 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002874 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002875
2876 return isBitCastable(SrcTy, DestTy);
2877}
2878
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002879// Provide a way to get a "cast" where the cast opcode is inferred from the
2880// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002881// logic in the castIsValid function below. This axiom should hold:
2882// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2883// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002884// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002885// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002886Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002887CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002888 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2889 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002890
Duncan Sands55e50902008-01-06 10:12:28 +00002891 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2892 "Only first class types are castable!");
2893
Duncan Sandsa8514532011-05-18 07:13:41 +00002894 if (SrcTy == DestTy)
2895 return BitCast;
2896
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002897 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002898 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2899 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002900 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2901 // An element by element cast. Find the appropriate opcode based on the
2902 // element types.
2903 SrcTy = SrcVecTy->getElementType();
2904 DestTy = DestVecTy->getElementType();
2905 }
2906
2907 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002908 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2909 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002910
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002912 if (DestTy->isIntegerTy()) { // Casting to integral
2913 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914 if (DestBits < SrcBits)
2915 return Trunc; // int -> smaller int
2916 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002917 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002918 return SExt; // signed -> SEXT
2919 else
2920 return ZExt; // unsigned -> ZEXT
2921 } else {
2922 return BitCast; // Same size, No-op cast
2923 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002924 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002925 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926 return FPToSI; // FP -> sint
2927 else
2928 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002929 } else if (SrcTy->isVectorTy()) {
2930 assert(DestBits == SrcBits &&
2931 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002932 return BitCast; // Same size, no-op cast
2933 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002934 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935 "Casting from a value that is not first-class type");
2936 return PtrToInt; // ptr -> int
2937 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002938 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2939 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002940 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 return SIToFP; // sint -> FP
2942 else
2943 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002944 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 if (DestBits < SrcBits) {
2946 return FPTrunc; // FP -> smaller FP
2947 } else if (DestBits > SrcBits) {
2948 return FPExt; // FP -> larger FP
2949 } else {
2950 return BitCast; // same size, no-op cast
2951 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002952 } else if (SrcTy->isVectorTy()) {
2953 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002954 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002955 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002957 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002958 } else if (DestTy->isVectorTy()) {
2959 assert(DestBits == SrcBits &&
2960 "Illegal cast to vector (wrong type or size)");
2961 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002962 } else if (DestTy->isPointerTy()) {
2963 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002964 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2965 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002966 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002967 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002968 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002970 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002971 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002972 if (SrcTy->isVectorTy()) {
2973 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002974 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002975 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002976 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002978 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979}
2980
2981//===----------------------------------------------------------------------===//
2982// CastInst SubClass Constructors
2983//===----------------------------------------------------------------------===//
2984
2985/// Check that the construction parameters for a CastInst are correct. This
2986/// could be broken out into the separate constructors but it is useful to have
2987/// it in one place and to eliminate the redundant code for getting the sizes
2988/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002989bool
Chris Lattner229907c2011-07-18 04:54:35 +00002990CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991
2992 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002993 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002994
Chris Lattner37bc78a2010-01-26 21:51:43 +00002995 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2996 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997 return false;
2998
2999 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003000 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3001 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003002
Duncan Sands7f646562011-05-18 09:21:57 +00003003 // If these are vector types, get the lengths of the vectors (using zero for
3004 // scalar types means that checking that vector lengths match also checks that
3005 // scalars are not being converted to vectors or vectors to scalars).
3006 unsigned SrcLength = SrcTy->isVectorTy() ?
3007 cast<VectorType>(SrcTy)->getNumElements() : 0;
3008 unsigned DstLength = DstTy->isVectorTy() ?
3009 cast<VectorType>(DstTy)->getNumElements() : 0;
3010
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011 // Switch on the opcode provided
3012 switch (op) {
3013 default: return false; // This is an input error
3014 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003015 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3016 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003018 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3019 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003020 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003021 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3022 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003024 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3025 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003026 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003027 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3028 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003031 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3032 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003035 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3036 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003037 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003038 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003039 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003040 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3041 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3042 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003043 return SrcTy->getScalarType()->isPointerTy() &&
3044 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003046 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003047 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003048 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3049 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3050 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003051 return SrcTy->getScalarType()->isIntegerTy() &&
3052 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003053 case Instruction::BitCast: {
3054 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3055 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3056
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 // BitCast implies a no-op cast of type only. No bits change.
3058 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003059 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 return false;
3061
Alp Tokerf907b892013-12-05 05:44:44 +00003062 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003063 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003064 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003065 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3066
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003067 // If both are pointers then the address spaces must match.
3068 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3069 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003070
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003071 // A vector of pointers must have the same number of elements.
3072 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3073 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3074 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3075
3076 return false;
3077 }
3078
3079 return true;
3080 }
3081 case Instruction::AddrSpaceCast: {
3082 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3083 if (!SrcPtrTy)
3084 return false;
3085
3086 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3087 if (!DstPtrTy)
3088 return false;
3089
3090 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3091 return false;
3092
3093 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3094 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3095 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3096
3097 return false;
3098 }
3099
3100 return true;
3101 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003102 }
3103}
3104
3105TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003106 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003107) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003108 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003109}
3110
3111TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003112 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003114 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003115}
3116
3117ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003118 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003120 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003121}
3122
3123ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003124 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003126 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127}
3128SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003129 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003130) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003131 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132}
3133
Jeff Cohencc08c832006-12-02 02:22:01 +00003134SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003135 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003137 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138}
3139
3140FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003141 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003143 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003144}
3145
3146FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003147 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003148) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003149 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150}
3151
3152FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003153 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003155 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156}
3157
3158FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003159 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003161 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162}
3163
3164UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003190) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193
3194FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
3200FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
3205
3206FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003207 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210}
3211
3212PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003213 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216}
3217
3218PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003219 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222}
3223
3224IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003225 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228}
3229
3230IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003231 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234}
3235
3236BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003237 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240}
3241
3242BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003243 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003247
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003248AddrSpaceCastInst::AddrSpaceCastInst(
3249 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3250) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3252}
3253
3254AddrSpaceCastInst::AddrSpaceCastInst(
3255 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3256) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3258}
3259
Chris Lattnerf16dc002006-09-17 19:29:56 +00003260//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003261// CmpInst Classes
3262//===----------------------------------------------------------------------===//
3263
Craig Topper3186c012012-09-23 02:12:10 +00003264void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003265
Chris Lattner229907c2011-07-18 04:54:35 +00003266CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003267 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003268 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003269 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003270 OperandTraits<CmpInst>::op_begin(this),
3271 OperandTraits<CmpInst>::operands(this),
3272 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003273 Op<0>() = LHS;
3274 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003275 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003276 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003277}
Gabor Greiff6caff662008-05-10 08:32:32 +00003278
Chris Lattner229907c2011-07-18 04:54:35 +00003279CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003280 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003281 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003282 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003283 OperandTraits<CmpInst>::op_begin(this),
3284 OperandTraits<CmpInst>::operands(this),
3285 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003286 Op<0>() = LHS;
3287 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003288 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003289 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003290}
3291
3292CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003293CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003295 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003296 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003297 if (InsertBefore)
3298 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3299 S1, S2, Name);
3300 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003301 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003302 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003303 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003304
3305 if (InsertBefore)
3306 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3307 S1, S2, Name);
3308 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003309 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003311}
3312
3313CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003314CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003315 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003316 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3318 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003319 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003320 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3321 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003322}
3323
3324void CmpInst::swapOperands() {
3325 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3326 IC->swapOperands();
3327 else
3328 cast<FCmpInst>(this)->swapOperands();
3329}
3330
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003331bool CmpInst::isCommutative() const {
3332 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003333 return IC->isCommutative();
3334 return cast<FCmpInst>(this)->isCommutative();
3335}
3336
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003337bool CmpInst::isEquality() const {
3338 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003339 return IC->isEquality();
3340 return cast<FCmpInst>(this)->isEquality();
3341}
3342
3343
Dan Gohman4e724382008-05-31 02:47:54 +00003344CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003345 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003346 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003347 case ICMP_EQ: return ICMP_NE;
3348 case ICMP_NE: return ICMP_EQ;
3349 case ICMP_UGT: return ICMP_ULE;
3350 case ICMP_ULT: return ICMP_UGE;
3351 case ICMP_UGE: return ICMP_ULT;
3352 case ICMP_ULE: return ICMP_UGT;
3353 case ICMP_SGT: return ICMP_SLE;
3354 case ICMP_SLT: return ICMP_SGE;
3355 case ICMP_SGE: return ICMP_SLT;
3356 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003357
Dan Gohman4e724382008-05-31 02:47:54 +00003358 case FCMP_OEQ: return FCMP_UNE;
3359 case FCMP_ONE: return FCMP_UEQ;
3360 case FCMP_OGT: return FCMP_ULE;
3361 case FCMP_OLT: return FCMP_UGE;
3362 case FCMP_OGE: return FCMP_ULT;
3363 case FCMP_OLE: return FCMP_UGT;
3364 case FCMP_UEQ: return FCMP_ONE;
3365 case FCMP_UNE: return FCMP_OEQ;
3366 case FCMP_UGT: return FCMP_OLE;
3367 case FCMP_ULT: return FCMP_OGE;
3368 case FCMP_UGE: return FCMP_OLT;
3369 case FCMP_ULE: return FCMP_OGT;
3370 case FCMP_ORD: return FCMP_UNO;
3371 case FCMP_UNO: return FCMP_ORD;
3372 case FCMP_TRUE: return FCMP_FALSE;
3373 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003374 }
3375}
3376
Reid Spencer266e42b2006-12-23 06:05:41 +00003377ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3378 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003379 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003380 case ICMP_EQ: case ICMP_NE:
3381 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3382 return pred;
3383 case ICMP_UGT: return ICMP_SGT;
3384 case ICMP_ULT: return ICMP_SLT;
3385 case ICMP_UGE: return ICMP_SGE;
3386 case ICMP_ULE: return ICMP_SLE;
3387 }
3388}
3389
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003390ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3391 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003392 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003393 case ICMP_EQ: case ICMP_NE:
3394 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3395 return pred;
3396 case ICMP_SGT: return ICMP_UGT;
3397 case ICMP_SLT: return ICMP_ULT;
3398 case ICMP_SGE: return ICMP_UGE;
3399 case ICMP_SLE: return ICMP_ULE;
3400 }
3401}
3402
Reid Spencer0286bc12007-02-28 22:00:54 +00003403/// Initialize a set of values that all satisfy the condition with C.
3404///
3405ConstantRange
3406ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3407 APInt Lower(C);
3408 APInt Upper(C);
3409 uint32_t BitWidth = C.getBitWidth();
3410 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003411 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003412 case ICmpInst::ICMP_EQ: ++Upper; break;
3413 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003414 case ICmpInst::ICMP_ULT:
3415 Lower = APInt::getMinValue(BitWidth);
3416 // Check for an empty-set condition.
3417 if (Lower == Upper)
3418 return ConstantRange(BitWidth, /*isFullSet=*/false);
3419 break;
3420 case ICmpInst::ICMP_SLT:
3421 Lower = APInt::getSignedMinValue(BitWidth);
3422 // Check for an empty-set condition.
3423 if (Lower == Upper)
3424 return ConstantRange(BitWidth, /*isFullSet=*/false);
3425 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003426 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003427 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003428 // Check for an empty-set condition.
3429 if (Lower == Upper)
3430 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003431 break;
3432 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003433 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003434 // Check for an empty-set condition.
3435 if (Lower == Upper)
3436 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003437 break;
3438 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003439 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003440 // Check for a full-set condition.
3441 if (Lower == Upper)
3442 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003443 break;
3444 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003445 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003446 // Check for a full-set condition.
3447 if (Lower == Upper)
3448 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003449 break;
3450 case ICmpInst::ICMP_UGE:
3451 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003452 // Check for a full-set condition.
3453 if (Lower == Upper)
3454 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003455 break;
3456 case ICmpInst::ICMP_SGE:
3457 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003458 // Check for a full-set condition.
3459 if (Lower == Upper)
3460 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003461 break;
3462 }
3463 return ConstantRange(Lower, Upper);
3464}
3465
Dan Gohman4e724382008-05-31 02:47:54 +00003466CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003467 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003468 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003469 case ICMP_EQ: case ICMP_NE:
3470 return pred;
3471 case ICMP_SGT: return ICMP_SLT;
3472 case ICMP_SLT: return ICMP_SGT;
3473 case ICMP_SGE: return ICMP_SLE;
3474 case ICMP_SLE: return ICMP_SGE;
3475 case ICMP_UGT: return ICMP_ULT;
3476 case ICMP_ULT: return ICMP_UGT;
3477 case ICMP_UGE: return ICMP_ULE;
3478 case ICMP_ULE: return ICMP_UGE;
3479
Reid Spencerd9436b62006-11-20 01:22:35 +00003480 case FCMP_FALSE: case FCMP_TRUE:
3481 case FCMP_OEQ: case FCMP_ONE:
3482 case FCMP_UEQ: case FCMP_UNE:
3483 case FCMP_ORD: case FCMP_UNO:
3484 return pred;
3485 case FCMP_OGT: return FCMP_OLT;
3486 case FCMP_OLT: return FCMP_OGT;
3487 case FCMP_OGE: return FCMP_OLE;
3488 case FCMP_OLE: return FCMP_OGE;
3489 case FCMP_UGT: return FCMP_ULT;
3490 case FCMP_ULT: return FCMP_UGT;
3491 case FCMP_UGE: return FCMP_ULE;
3492 case FCMP_ULE: return FCMP_UGE;
3493 }
3494}
3495
Reid Spencer266e42b2006-12-23 06:05:41 +00003496bool CmpInst::isUnsigned(unsigned short predicate) {
3497 switch (predicate) {
3498 default: return false;
3499 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3500 case ICmpInst::ICMP_UGE: return true;
3501 }
3502}
3503
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003504bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003505 switch (predicate) {
3506 default: return false;
3507 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3508 case ICmpInst::ICMP_SGE: return true;
3509 }
3510}
3511
3512bool CmpInst::isOrdered(unsigned short predicate) {
3513 switch (predicate) {
3514 default: return false;
3515 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3516 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3517 case FCmpInst::FCMP_ORD: return true;
3518 }
3519}
3520
3521bool CmpInst::isUnordered(unsigned short predicate) {
3522 switch (predicate) {
3523 default: return false;
3524 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3525 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3526 case FCmpInst::FCMP_UNO: return true;
3527 }
3528}
3529
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003530bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3531 switch(predicate) {
3532 default: return false;
3533 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3534 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3535 }
3536}
3537
3538bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3539 switch(predicate) {
3540 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3541 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3542 default: return false;
3543 }
3544}
3545
3546
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003547//===----------------------------------------------------------------------===//
3548// SwitchInst Implementation
3549//===----------------------------------------------------------------------===//
3550
Chris Lattnerbaf00152010-11-17 05:41:46 +00003551void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3552 assert(Value && Default && NumReserved);
3553 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003554 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003555 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003556
Pete Coopereb31b682015-05-21 22:48:54 +00003557 Op<0>() = Value;
3558 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003559}
3560
Chris Lattner2195fc42007-02-24 00:55:48 +00003561/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3562/// switch on and a default destination. The number of additional cases can
3563/// be specified here to make memory allocation more efficient. This
3564/// constructor can also autoinsert before another instruction.
3565SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3566 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003567 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003568 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003569 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003570}
3571
3572/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3573/// switch on and a default destination. The number of additional cases can
3574/// be specified here to make memory allocation more efficient. This
3575/// constructor also autoinserts at the end of the specified BasicBlock.
3576SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3577 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003578 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003579 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003580 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003581}
3582
Misha Brukmanb1c93172005-04-21 23:48:37 +00003583SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003584 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003585 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003586 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003587 Use *OL = getOperandList();
3588 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003589 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003590 OL[i] = InOL[i];
3591 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003592 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003593 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003594}
3595
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003596
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003597/// addCase - Add an entry to the switch instruction...
3598///
Chris Lattner47ac1872005-02-24 05:32:09 +00003599void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003600 unsigned NewCaseIdx = getNumCases();
3601 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003602 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003603 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003604 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003605 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003606 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003607 CaseIt Case(this, NewCaseIdx);
3608 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003609 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003610}
3611
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003612/// removeCase - This method removes the specified case and its successor
3613/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003614void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003615 unsigned idx = i.getCaseIndex();
3616
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003617 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003618
3619 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003620 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003621
Jay Foad14277722011-02-01 09:22:34 +00003622 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003623 if (2 + (idx + 1) * 2 != NumOps) {
3624 OL[2 + idx * 2] = OL[NumOps - 2];
3625 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003626 }
3627
3628 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003629 OL[NumOps-2].set(nullptr);
3630 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003631 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003632}
3633
Jay Foade98f29d2011-04-01 08:00:58 +00003634/// growOperands - grow operands - This grows the operand list in response
3635/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003636///
Jay Foade98f29d2011-04-01 08:00:58 +00003637void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003638 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003639 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003640
3641 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003642 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003643}
3644
3645
3646BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3647 return getSuccessor(idx);
3648}
3649unsigned SwitchInst::getNumSuccessorsV() const {
3650 return getNumSuccessors();
3651}
3652void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3653 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003654}
Chris Lattnerf22be932004-10-15 23:52:53 +00003655
Chris Lattner3ed871f2009-10-27 19:13:16 +00003656//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003657// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003658//===----------------------------------------------------------------------===//
3659
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003660void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003661 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003662 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003663 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003664 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003665 allocHungoffUses(ReservedSpace);
3666
Pete Coopereb31b682015-05-21 22:48:54 +00003667 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003668}
3669
3670
Jay Foade98f29d2011-04-01 08:00:58 +00003671/// growOperands - grow operands - This grows the operand list in response
3672/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003673///
Jay Foade98f29d2011-04-01 08:00:58 +00003674void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003675 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003676 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003677
3678 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003679 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003680}
3681
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003682IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3683 Instruction *InsertBefore)
3684: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003685 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003686 init(Address, NumCases);
3687}
3688
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003689IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3690 BasicBlock *InsertAtEnd)
3691: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003692 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003693 init(Address, NumCases);
3694}
3695
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003696IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003697 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3698 nullptr, IBI.getNumOperands()) {
3699 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003700 Use *OL = getOperandList();
3701 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003702 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3703 OL[i] = InOL[i];
3704 SubclassOptionalData = IBI.SubclassOptionalData;
3705}
3706
Chris Lattner3ed871f2009-10-27 19:13:16 +00003707/// addDestination - Add a destination.
3708///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003709void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003710 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003712 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003713 // Initialize some new operands.
3714 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003715 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003716 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003717}
3718
3719/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003720/// indirectbr instruction.
3721void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003722 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3723
3724 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003725 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003726
3727 // Replace this value with the last one.
3728 OL[idx+1] = OL[NumOps-1];
3729
3730 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003731 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003732 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003733}
3734
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003735BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003736 return getSuccessor(idx);
3737}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003738unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739 return getNumSuccessors();
3740}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003741void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742 setSuccessor(idx, B);
3743}
3744
3745//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003746// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003747//===----------------------------------------------------------------------===//
3748
Chris Lattnerf22be932004-10-15 23:52:53 +00003749// Define these methods here so vtables don't get emitted into every translation
3750// unit that uses these classes.
3751
Pete Cooper75403d72015-06-24 20:22:23 +00003752GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003753 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003754}
3755
Pete Cooper75403d72015-06-24 20:22:23 +00003756BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003757 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003758}
3759
Pete Cooper75403d72015-06-24 20:22:23 +00003760FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003761 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003762}
3763
Pete Cooper75403d72015-06-24 20:22:23 +00003764ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003765 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003766}
3767
Pete Cooper75403d72015-06-24 20:22:23 +00003768ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003769 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003770}
3771
Pete Cooper75403d72015-06-24 20:22:23 +00003772InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003773 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003774}
3775
Pete Cooper75403d72015-06-24 20:22:23 +00003776AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003777 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3778 (Value *)getOperand(0), getAlignment());
3779 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3780 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003781}
3782
Pete Cooper75403d72015-06-24 20:22:23 +00003783LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003784 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3785 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003786}
3787
Pete Cooper75403d72015-06-24 20:22:23 +00003788StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003789 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003790 getAlignment(), getOrdering(), getSynchScope());
3791
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003792}
3793
Pete Cooper75403d72015-06-24 20:22:23 +00003794AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003795 AtomicCmpXchgInst *Result =
3796 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003797 getSuccessOrdering(), getFailureOrdering(),
3798 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003799 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003800 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003801 return Result;
3802}
3803
Pete Cooper75403d72015-06-24 20:22:23 +00003804AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003805 AtomicRMWInst *Result =
3806 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3807 getOrdering(), getSynchScope());
3808 Result->setVolatile(isVolatile());
3809 return Result;
3810}
3811
Pete Cooper75403d72015-06-24 20:22:23 +00003812FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003813 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3814}
3815
Pete Cooper75403d72015-06-24 20:22:23 +00003816TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003817 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003818}
3819
Pete Cooper75403d72015-06-24 20:22:23 +00003820ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003821 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003822}
3823
Pete Cooper75403d72015-06-24 20:22:23 +00003824SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003825 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003826}
3827
Pete Cooper75403d72015-06-24 20:22:23 +00003828FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003829 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003830}
3831
Pete Cooper75403d72015-06-24 20:22:23 +00003832FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003833 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003834}
3835
Pete Cooper75403d72015-06-24 20:22:23 +00003836UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003837 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003838}
3839
Pete Cooper75403d72015-06-24 20:22:23 +00003840SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003841 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003842}
3843
Pete Cooper75403d72015-06-24 20:22:23 +00003844FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003845 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003846}
3847
Pete Cooper75403d72015-06-24 20:22:23 +00003848FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003849 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003850}
3851
Pete Cooper75403d72015-06-24 20:22:23 +00003852PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003853 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003854}
3855
Pete Cooper75403d72015-06-24 20:22:23 +00003856IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003857 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003858}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003859
Pete Cooper75403d72015-06-24 20:22:23 +00003860BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003861 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003862}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003863
Pete Cooper75403d72015-06-24 20:22:23 +00003864AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003865 return new AddrSpaceCastInst(getOperand(0), getType());
3866}
3867
Pete Cooper75403d72015-06-24 20:22:23 +00003868CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003869 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003870}
3871
Pete Cooper75403d72015-06-24 20:22:23 +00003872SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003873 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003874}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003875
Pete Cooper75403d72015-06-24 20:22:23 +00003876VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003877 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003878}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003879
Pete Cooper75403d72015-06-24 20:22:23 +00003880ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003881 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003882}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003883
Pete Cooper75403d72015-06-24 20:22:23 +00003884InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003885 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003886}
3887
Pete Cooper75403d72015-06-24 20:22:23 +00003888ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003889 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003890}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003891
Pete Cooper75403d72015-06-24 20:22:23 +00003892PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003893
Pete Cooper75403d72015-06-24 20:22:23 +00003894LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003895 return new LandingPadInst(*this);
3896}
3897
Pete Cooper75403d72015-06-24 20:22:23 +00003898ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003899 return new(getNumOperands()) ReturnInst(*this);
3900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003903 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003904}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905
Pete Cooper75403d72015-06-24 20:22:23 +00003906SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003907
Pete Cooper75403d72015-06-24 20:22:23 +00003908IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003909 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003910}
3911
Pete Cooper75403d72015-06-24 20:22:23 +00003912InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003913 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003914}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003915
Pete Cooper75403d72015-06-24 20:22:23 +00003916ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003917
David Majnemer654e1302015-07-31 17:58:14 +00003918CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3919 return new (getNumOperands()) CleanupReturnInst(*this);
3920}
3921
3922CatchEndPadInst *CatchEndPadInst::cloneImpl() const {
3923 return new (getNumOperands()) CatchEndPadInst(*this);
3924}
3925
3926CatchReturnInst *CatchReturnInst::cloneImpl() const {
3927 return new (1) CatchReturnInst(*this);
3928}
3929
3930CatchPadInst *CatchPadInst::cloneImpl() const {
3931 return new (getNumOperands()) CatchPadInst(*this);
3932}
3933
3934TerminatePadInst *TerminatePadInst::cloneImpl() const {
3935 return new (getNumOperands()) TerminatePadInst(*this);
3936}
3937
3938CleanupPadInst *CleanupPadInst::cloneImpl() const {
3939 return new (getNumOperands()) CleanupPadInst(*this);
3940}
3941
Pete Cooper75403d72015-06-24 20:22:23 +00003942UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003943 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003944 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945}