blob: dff31bee093595ffef6a997866333e0feeacfc4d [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";
65
Chris Lattner229907c2011-07-18 04:54:35 +000066 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000067 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000070 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000071 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000072 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
Craig Topperc6207612014-04-09 06:08:46 +000079 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000080}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000088 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
89 ReservedSpace(PN.getNumOperands()) {
90 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000091 std::copy(PN.op_begin(), PN.op_end(), op_begin());
92 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000093 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094}
95
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096// removeIncomingValue - Remove an incoming value. This is useful if a
97// predecessor basic block is deleted.
98Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +000099 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000100
101 // Move everything after this operand down.
102 //
103 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000106 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
107 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108
109 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000110 Op<-1>().set(nullptr);
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112
113 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000116 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117 eraseFromParent();
118 }
119 return Removed;
120}
121
Jay Foade98f29d2011-04-01 08:00:58 +0000122/// growOperands - grow operands - This grows the operand list in response
123/// to a push_back style of operation. This grows the number of ops by 1.5
124/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125///
Jay Foade98f29d2011-04-01 08:00:58 +0000126void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000127 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000128 unsigned NumOps = e + e / 2;
129 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
130
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000132 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133}
134
Nate Begemanb3923212005-08-04 23:24:19 +0000135/// hasConstantValue - If the specified PHI node always merges together the same
136/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000137Value *PHINode::hasConstantValue() const {
138 // Exploit the fact that phi nodes always have at least one entry.
139 Value *ConstantValue = getIncomingValue(0);
140 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000141 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
142 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000143 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000144 // The case where the first value is this PHI.
145 ConstantValue = getIncomingValue(i);
146 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000147 if (ConstantValue == this)
148 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000149 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000150}
151
Bill Wendlingfae14752011-08-12 20:24:12 +0000152//===----------------------------------------------------------------------===//
153// LandingPadInst Implementation
154//===----------------------------------------------------------------------===//
155
156LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
157 unsigned NumReservedValues, const Twine &NameStr,
158 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +0000159 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000160 init(PersonalityFn, 1 + NumReservedValues, NameStr);
161}
162
163LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
164 unsigned NumReservedValues, const Twine &NameStr,
165 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +0000166 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000167 init(PersonalityFn, 1 + NumReservedValues, NameStr);
168}
169
170LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000171 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
172 LP.getNumOperands()),
173 ReservedSpace(LP.getNumOperands()) {
174 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000175 Use *OL = getOperandList();
176 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000177 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
178 OL[I] = InOL[I];
179
180 setCleanup(LP.isCleanup());
181}
182
Bill Wendlingfae14752011-08-12 20:24:12 +0000183LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
184 unsigned NumReservedClauses,
185 const Twine &NameStr,
186 Instruction *InsertBefore) {
187 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
188 InsertBefore);
189}
190
191LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
192 unsigned NumReservedClauses,
193 const Twine &NameStr,
194 BasicBlock *InsertAtEnd) {
195 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
196 InsertAtEnd);
197}
198
199void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
200 const Twine &NameStr) {
201 ReservedSpace = NumReservedValues;
202 NumOperands = 1;
Pete Cooper3fc30402015-06-10 22:38:46 +0000203 allocHungoffUses(ReservedSpace);
Pete Coopereb31b682015-05-21 22:48:54 +0000204 Op<0>() = PersFn;
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 setName(NameStr);
206 setCleanup(false);
207}
208
209/// growOperands - grow operands - This grows the operand list in response to a
210/// push_back style of operation. This grows the number of ops by 2 times.
211void LandingPadInst::growOperands(unsigned Size) {
212 unsigned e = getNumOperands();
213 if (ReservedSpace >= e + Size) return;
214 ReservedSpace = (e + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000215 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000216}
217
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000218void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000219 unsigned OpNo = getNumOperands();
220 growOperands(1);
221 assert(OpNo < ReservedSpace && "Growing didn't work!");
222 ++NumOperands;
Pete Cooper74510a42015-06-12 17:48:05 +0000223 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000224}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225
226//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227// CallInst Implementation
228//===----------------------------------------------------------------------===//
229
Gordon Henriksen14a55692007-12-10 02:14:30 +0000230CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000231}
232
David Blaikie348de692015-04-23 21:36:23 +0000233void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
234 const Twine &NameStr) {
235 this->FTy = FTy;
Jay Foad5bd375a2011-07-15 08:37:34 +0000236 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000237 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238
Jay Foad5bd375a2011-07-15 08:37:34 +0000239#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000240 assert((Args.size() == FTy->getNumParams() ||
241 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000242 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000243
244 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000245 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000246 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000247 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000248#endif
249
250 std::copy(Args.begin(), Args.end(), op_begin());
251 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252}
253
Jay Foad5bd375a2011-07-15 08:37:34 +0000254void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000255 FTy =
256 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Gabor Greiff6caff662008-05-10 08:32:32 +0000257 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000258 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000260 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000261
262 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263}
264
Daniel Dunbar4975db62009-07-25 04:41:11 +0000265CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266 Instruction *InsertBefore)
267 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
268 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000269 Instruction::Call,
270 OperandTraits<CallInst>::op_end(this) - 1,
271 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Daniel Dunbar4975db62009-07-25 04:41:11 +0000275CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 BasicBlock *InsertAtEnd)
277 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
278 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000279 Instruction::Call,
280 OperandTraits<CallInst>::op_end(this) - 1,
281 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000282 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283}
284
Misha Brukmanb1c93172005-04-21 23:48:37 +0000285CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000286 : Instruction(CI.getType(), Instruction::Call,
287 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
288 CI.getNumOperands()),
289 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000290 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000291 setCallingConv(CI.getCallingConv());
292
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000294 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295}
296
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000297void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000298 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000299 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000300 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000301}
302
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000303void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000304 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000305 AttrBuilder B(attr);
306 LLVMContext &Context = getContext();
307 PAL = PAL.removeAttributes(Context, i,
308 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000309 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000310}
311
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000312void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
313 AttributeSet PAL = getAttributes();
314 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
315 setAttributes(PAL);
316}
317
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000318void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
319 AttributeSet PAL = getAttributes();
320 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
321 setAttributes(PAL);
322}
323
Michael Gottesman41748d72013-06-27 00:25:01 +0000324bool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000325 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000326 return true;
327 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000328 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000329 return false;
330}
331
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000332bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000333 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000334 return true;
335 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000336 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000337 return false;
338}
339
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000340/// IsConstantOne - Return true only if val is constant int 1
341static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000342 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000343 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
344 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000345}
346
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000347static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000348 BasicBlock *InsertAtEnd, Type *IntPtrTy,
349 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000350 Value *ArraySize, Function *MallocF,
351 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000352 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000353 "createMalloc needs either InsertBefore or InsertAtEnd");
354
355 // malloc(type) becomes:
356 // bitcast (i8* malloc(typeSize)) to type*
357 // malloc(type, arraySize) becomes:
358 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000359 if (!ArraySize)
360 ArraySize = ConstantInt::get(IntPtrTy, 1);
361 else if (ArraySize->getType() != IntPtrTy) {
362 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000363 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
364 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000365 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000366 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
367 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000368 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000369
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000370 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371 if (IsConstantOne(AllocSize)) {
372 AllocSize = ArraySize; // Operand * 1 = Operand
373 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
374 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
375 false /*ZExt*/);
376 // Malloc arg is constant product of type size and array size
377 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
378 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000379 // Multiply type size by the array size...
380 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000381 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
382 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000383 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000384 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
385 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000386 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000387 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000388
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000390 // Create the call to Malloc.
391 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
392 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000393 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000394 Value *MallocFunc = MallocF;
395 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000396 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000397 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000398 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000399 CallInst *MCall = nullptr;
400 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000401 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000402 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000403 Result = MCall;
404 if (Result->getType() != AllocPtrType)
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, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000407 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000408 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000409 Result = MCall;
410 if (Result->getType() != AllocPtrType) {
411 InsertAtEnd->getInstList().push_back(MCall);
412 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000413 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000414 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000415 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000417 if (Function *F = dyn_cast<Function>(MallocFunc)) {
418 MCall->setCallingConv(F->getCallingConv());
419 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
420 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000421 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000422
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000423 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000424}
425
426/// CreateMalloc - Generate the IR for a call to malloc:
427/// 1. Compute the malloc call's argument as the specified type's size,
428/// possibly multiplied by the array size if the array size is not
429/// constant 1.
430/// 2. Call malloc with that argument.
431/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000432Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000433 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000434 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000435 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000436 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000437 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000438 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000439}
440
441/// CreateMalloc - Generate the IR for a call to malloc:
442/// 1. Compute the malloc call's argument as the specified type's size,
443/// possibly multiplied by the array size if the array size is not
444/// constant 1.
445/// 2. Call malloc with that argument.
446/// 3. Bitcast the result of the malloc call to the specified type.
447/// Note: This function does not add the bitcast to the basic block, that is the
448/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000449Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000450 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000451 Value *AllocSize, Value *ArraySize,
452 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000453 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000454 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000456
Victor Hernandeze2971492009-10-24 04:23:03 +0000457static Instruction* createFree(Value* Source, Instruction *InsertBefore,
458 BasicBlock *InsertAtEnd) {
459 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
460 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000461 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000462 "Can not free something of nonpointer type!");
463
464 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
465 Module* M = BB->getParent()->getParent();
466
Chris Lattner229907c2011-07-18 04:54:35 +0000467 Type *VoidTy = Type::getVoidTy(M->getContext());
468 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000469 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000470 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000471 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000472 Value *PtrCast = Source;
473 if (InsertBefore) {
474 if (Source->getType() != IntPtrTy)
475 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
476 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
477 } else {
478 if (Source->getType() != IntPtrTy)
479 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
480 Result = CallInst::Create(FreeFunc, PtrCast, "");
481 }
482 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000483 if (Function *F = dyn_cast<Function>(FreeFunc))
484 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000485
486 return Result;
487}
488
489/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000490Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000491 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000492}
493
494/// CreateFree - Generate the IR for a call to the builtin free function.
495/// Note: This function does not add the call to the basic block, that is the
496/// responsibility of the caller.
497Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000498 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000499 assert(FreeCall && "CreateFree did not create a CallInst");
500 return FreeCall;
501}
502
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000503//===----------------------------------------------------------------------===//
504// InvokeInst Implementation
505//===----------------------------------------------------------------------===//
506
David Blaikie3e807092015-05-13 18:35:26 +0000507void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
508 BasicBlock *IfException, ArrayRef<Value *> Args,
509 const Twine &NameStr) {
510 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000511
Jay Foad5bd375a2011-07-15 08:37:34 +0000512 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000513 Op<-3>() = Fn;
514 Op<-2>() = IfNormal;
515 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000516
517#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000518 assert(((Args.size() == FTy->getNumParams()) ||
519 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000520 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000521
Jay Foad5bd375a2011-07-15 08:37:34 +0000522 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000523 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000524 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000525 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000526#endif
527
528 std::copy(Args.begin(), Args.end(), op_begin());
529 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530}
531
Misha Brukmanb1c93172005-04-21 23:48:37 +0000532InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000533 : TerminatorInst(II.getType(), Instruction::Invoke,
534 OperandTraits<InvokeInst>::op_end(this) -
535 II.getNumOperands(),
536 II.getNumOperands()),
537 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000538 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000539 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000540 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541}
542
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
544 return getSuccessor(idx);
545}
546unsigned InvokeInst::getNumSuccessorsV() const {
547 return getNumSuccessors();
548}
549void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
550 return setSuccessor(idx, B);
551}
552
Michael Gottesman41748d72013-06-27 00:25:01 +0000553bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000554 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000555 return true;
556 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000557 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000558 return false;
559}
560
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000561bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000562 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000563 return true;
564 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000565 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000566 return false;
567}
568
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000569void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000570 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000571 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000572 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000573}
574
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000575void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000576 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000577 AttrBuilder B(attr);
578 PAL = PAL.removeAttributes(getContext(), i,
579 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000580 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000581}
582
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000583void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
584 AttributeSet PAL = getAttributes();
585 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
586 setAttributes(PAL);
587}
588
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000589void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
590 AttributeSet PAL = getAttributes();
591 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
592 setAttributes(PAL);
593}
594
Bill Wendlingfae14752011-08-12 20:24:12 +0000595LandingPadInst *InvokeInst::getLandingPadInst() const {
596 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
597}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000598
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599//===----------------------------------------------------------------------===//
600// ReturnInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattner2195fc42007-02-24 00:55:48 +0000603ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000604 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000605 OperandTraits<ReturnInst>::op_end(this) -
606 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000607 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000608 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000609 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000610 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000611}
612
Owen Anderson55f1c092009-08-13 21:58:54 +0000613ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
614 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000615 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
616 InsertBefore) {
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 &C, Value *retVal, BasicBlock *InsertAtEnd)
621 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000622 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
623 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000624 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000625 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000626}
Owen Anderson55f1c092009-08-13 21:58:54 +0000627ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
628 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000629 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000630}
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632unsigned ReturnInst::getNumSuccessorsV() const {
633 return getNumSuccessors();
634}
635
Devang Patelae682fb2008-02-26 17:56:20 +0000636/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
637/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000639 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000640}
641
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000643 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000644}
645
Devang Patel59643e52008-02-23 00:35:18 +0000646ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000647}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000650// ResumeInst Implementation
651//===----------------------------------------------------------------------===//
652
653ResumeInst::ResumeInst(const ResumeInst &RI)
654 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
655 OperandTraits<ResumeInst>::op_begin(this), 1) {
656 Op<0>() = RI.Op<0>();
657}
658
659ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
660 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
661 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
662 Op<0>() = Exn;
663}
664
665ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
666 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
667 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
668 Op<0>() = Exn;
669}
670
671unsigned ResumeInst::getNumSuccessorsV() const {
672 return getNumSuccessors();
673}
674
675void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
676 llvm_unreachable("ResumeInst has no successors!");
677}
678
679BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
680 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000681}
682
683//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000684// UnreachableInst Implementation
685//===----------------------------------------------------------------------===//
686
Owen Anderson55f1c092009-08-13 21:58:54 +0000687UnreachableInst::UnreachableInst(LLVMContext &Context,
688 Instruction *InsertBefore)
689 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000690 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000691}
Owen Anderson55f1c092009-08-13 21:58:54 +0000692UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
693 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000694 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000695}
696
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697unsigned UnreachableInst::getNumSuccessorsV() const {
698 return getNumSuccessors();
699}
700
701void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000702 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703}
704
705BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000706 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000707}
708
709//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000710// BranchInst Implementation
711//===----------------------------------------------------------------------===//
712
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713void BranchInst::AssertOK() {
714 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000715 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717}
718
Chris Lattner2195fc42007-02-24 00:55:48 +0000719BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000720 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000721 OperandTraits<BranchInst>::op_end(this) - 1,
722 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000723 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000724 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000725}
726BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
727 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000728 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000729 OperandTraits<BranchInst>::op_end(this) - 3,
730 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000731 Op<-1>() = IfTrue;
732 Op<-2>() = IfFalse;
733 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000734#ifndef NDEBUG
735 AssertOK();
736#endif
737}
738
739BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000740 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000741 OperandTraits<BranchInst>::op_end(this) - 1,
742 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000743 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000744 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000745}
746
747BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
748 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000749 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000750 OperandTraits<BranchInst>::op_end(this) - 3,
751 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000752 Op<-1>() = IfTrue;
753 Op<-2>() = IfFalse;
754 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755#ifndef NDEBUG
756 AssertOK();
757#endif
758}
759
760
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000761BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000762 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000763 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
764 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000765 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766 if (BI.getNumOperands() != 1) {
767 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000768 Op<-3>() = BI.Op<-3>();
769 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000771 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000772}
773
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000774void BranchInst::swapSuccessors() {
775 assert(isConditional() &&
776 "Cannot swap successors of an unconditional branch");
777 Op<-1>().swap(Op<-2>());
778
779 // Update profile metadata if present and it matches our structural
780 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000781 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000782 if (!ProfileData || ProfileData->getNumOperands() != 3)
783 return;
784
785 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000786 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
787 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000788 setMetadata(LLVMContext::MD_prof,
789 MDNode::get(ProfileData->getContext(), Ops));
790}
791
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
793 return getSuccessor(idx);
794}
795unsigned BranchInst::getNumSuccessorsV() const {
796 return getNumSuccessors();
797}
798void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
799 setSuccessor(idx, B);
800}
801
802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000804// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805//===----------------------------------------------------------------------===//
806
Owen Andersonb6b25302009-07-14 23:09:55 +0000807static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000809 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000810 else {
811 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000812 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000813 assert(Amt->getType()->isIntegerTy() &&
814 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000815 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000816 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817}
818
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000819AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
820 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000822AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
823 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000824
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000825AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000826 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000827 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000828
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000829AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000830 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000831 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000832
Chris Lattner229907c2011-07-18 04:54:35 +0000833AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000835 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
836 getAISize(Ty->getContext(), ArraySize), InsertBefore),
837 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000838 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000839 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000840 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841}
842
Chris Lattner229907c2011-07-18 04:54:35 +0000843AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000844 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000845 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
846 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
847 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000848 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000849 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Gordon Henriksen14a55692007-12-10 02:14:30 +0000853// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000854AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000855}
856
Victor Hernandez8acf2952009-10-23 21:09:37 +0000857void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000858 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000859 assert(Align <= MaximumAlignment &&
860 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +0000861 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
862 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +0000863 assert(getAlignment() == Align && "Alignment representation error!");
864}
865
Victor Hernandez8acf2952009-10-23 21:09:37 +0000866bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000867 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000868 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000869 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000870}
871
Chris Lattner8b291e62008-11-26 02:54:17 +0000872/// isStaticAlloca - Return true if this alloca is in the entry block of the
873/// function and is a constant size. If so, the code generator will fold it
874/// into the prolog/epilog code, so it is basically free.
875bool AllocaInst::isStaticAlloca() const {
876 // Must be constant size.
877 if (!isa<ConstantInt>(getArraySize())) return false;
878
879 // Must be in the entry block.
880 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +0000881 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +0000882}
883
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885// LoadInst Implementation
886//===----------------------------------------------------------------------===//
887
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000888void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000889 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000890 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000891 assert(!(isAtomic() && getAlignment() == 0) &&
892 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893}
894
Daniel Dunbar4975db62009-07-25 04:41:11 +0000895LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000896 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000897
Daniel Dunbar4975db62009-07-25 04:41:11 +0000898LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000899 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900
David Blaikie0c28fd72015-05-20 21:46:30 +0000901LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000903 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +0000904
905LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
906 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000907 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +0000908
David Blaikieb7a029872015-04-17 19:56:21 +0000909LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000910 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +0000911 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000912 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000913
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000914LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000915 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000916 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +0000917}
918
David Blaikie15d9a4c2015-04-06 20:59:48 +0000919LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +0000920 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +0000921 SynchronizationScope SynchScope, Instruction *InsertBef)
922 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +0000923 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +0000924 setVolatile(isVolatile);
925 setAlignment(Align);
926 setAtomic(Order, SynchScope);
927 AssertOK();
928 setName(Name);
929}
930
931LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
932 unsigned Align, AtomicOrdering Order,
933 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000934 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000936 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000938 setAlignment(Align);
939 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000940 AssertOK();
941 setName(Name);
942}
943
Daniel Dunbar27096822009-08-11 18:11:15 +0000944LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
945 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
946 Load, Ptr, InsertBef) {
947 setVolatile(false);
948 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000949 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000950 AssertOK();
951 if (Name && Name[0]) setName(Name);
952}
953
954LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
955 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
956 Load, Ptr, InsertAE) {
957 setVolatile(false);
958 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000959 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000960 AssertOK();
961 if (Name && Name[0]) setName(Name);
962}
963
David Blaikie0c28fd72015-05-20 21:46:30 +0000964LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +0000965 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000966 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
967 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +0000968 setVolatile(isVolatile);
969 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000970 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000971 AssertOK();
972 if (Name && Name[0]) setName(Name);
973}
974
975LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
976 BasicBlock *InsertAE)
977 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
978 Load, Ptr, InsertAE) {
979 setVolatile(isVolatile);
980 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000981 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000982 AssertOK();
983 if (Name && Name[0]) setName(Name);
984}
985
Christopher Lamb84485702007-04-22 19:24:39 +0000986void LoadInst::setAlignment(unsigned Align) {
987 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000988 assert(Align <= MaximumAlignment &&
989 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +0000990 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000991 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000992 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000993}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994
995//===----------------------------------------------------------------------===//
996// StoreInst Implementation
997//===----------------------------------------------------------------------===//
998
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001000 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001001 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002 "Ptr must have pointer type!");
1003 assert(getOperand(0)->getType() ==
1004 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001005 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001006 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001007 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001008}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001009
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001011 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012
1013StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001014 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001015
Misha Brukmanb1c93172005-04-21 23:48:37 +00001016StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001017 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001018 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001019
1020StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001021 BasicBlock *InsertAtEnd)
1022 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1023
1024StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1025 Instruction *InsertBefore)
1026 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1027 InsertBefore) {}
1028
1029StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1030 BasicBlock *InsertAtEnd)
1031 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1032 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001033
Misha Brukmanb1c93172005-04-21 23:48:37 +00001034StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001035 unsigned Align, AtomicOrdering Order,
1036 SynchronizationScope SynchScope,
1037 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001038 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001039 OperandTraits<StoreInst>::op_begin(this),
1040 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001041 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001042 Op<0>() = val;
1043 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001044 setVolatile(isVolatile);
1045 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001046 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001047 AssertOK();
1048}
1049
1050StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001051 unsigned Align, AtomicOrdering Order,
1052 SynchronizationScope SynchScope,
1053 BasicBlock *InsertAtEnd)
1054 : Instruction(Type::getVoidTy(val->getContext()), Store,
1055 OperandTraits<StoreInst>::op_begin(this),
1056 OperandTraits<StoreInst>::operands(this),
1057 InsertAtEnd) {
1058 Op<0>() = val;
1059 Op<1>() = addr;
1060 setVolatile(isVolatile);
1061 setAlignment(Align);
1062 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001063 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064}
1065
Christopher Lamb84485702007-04-22 19:24:39 +00001066void StoreInst::setAlignment(unsigned Align) {
1067 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001068 assert(Align <= MaximumAlignment &&
1069 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001070 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001071 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001072 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001073}
1074
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001076// AtomicCmpXchgInst Implementation
1077//===----------------------------------------------------------------------===//
1078
1079void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001080 AtomicOrdering SuccessOrdering,
1081 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001082 SynchronizationScope SynchScope) {
1083 Op<0>() = Ptr;
1084 Op<1>() = Cmp;
1085 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001086 setSuccessOrdering(SuccessOrdering);
1087 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001088 setSynchScope(SynchScope);
1089
1090 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1091 "All operands must be non-null!");
1092 assert(getOperand(0)->getType()->isPointerTy() &&
1093 "Ptr must have pointer type!");
1094 assert(getOperand(1)->getType() ==
1095 cast<PointerType>(getOperand(0)->getType())->getElementType()
1096 && "Ptr must be a pointer to Cmp type!");
1097 assert(getOperand(2)->getType() ==
1098 cast<PointerType>(getOperand(0)->getType())->getElementType()
1099 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001100 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001101 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001102 assert(FailureOrdering != NotAtomic &&
1103 "AtomicCmpXchg instructions must be atomic!");
1104 assert(SuccessOrdering >= FailureOrdering &&
1105 "AtomicCmpXchg success ordering must be at least as strong as fail");
1106 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1107 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001108}
1109
1110AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001111 AtomicOrdering SuccessOrdering,
1112 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001113 SynchronizationScope SynchScope,
1114 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001115 : Instruction(
1116 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1117 nullptr),
1118 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1119 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001120 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001121}
1122
1123AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001124 AtomicOrdering SuccessOrdering,
1125 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001126 SynchronizationScope SynchScope,
1127 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001128 : Instruction(
1129 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1130 nullptr),
1131 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1132 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001133 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001134}
Tim Northover420a2162014-06-13 14:24:07 +00001135
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001136//===----------------------------------------------------------------------===//
1137// AtomicRMWInst Implementation
1138//===----------------------------------------------------------------------===//
1139
1140void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1141 AtomicOrdering Ordering,
1142 SynchronizationScope SynchScope) {
1143 Op<0>() = Ptr;
1144 Op<1>() = Val;
1145 setOperation(Operation);
1146 setOrdering(Ordering);
1147 setSynchScope(SynchScope);
1148
1149 assert(getOperand(0) && getOperand(1) &&
1150 "All operands must be non-null!");
1151 assert(getOperand(0)->getType()->isPointerTy() &&
1152 "Ptr must have pointer type!");
1153 assert(getOperand(1)->getType() ==
1154 cast<PointerType>(getOperand(0)->getType())->getElementType()
1155 && "Ptr must be a pointer to Val type!");
1156 assert(Ordering != NotAtomic &&
1157 "AtomicRMW instructions must be atomic!");
1158}
1159
1160AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1161 AtomicOrdering Ordering,
1162 SynchronizationScope SynchScope,
1163 Instruction *InsertBefore)
1164 : Instruction(Val->getType(), AtomicRMW,
1165 OperandTraits<AtomicRMWInst>::op_begin(this),
1166 OperandTraits<AtomicRMWInst>::operands(this),
1167 InsertBefore) {
1168 Init(Operation, Ptr, Val, Ordering, SynchScope);
1169}
1170
1171AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1172 AtomicOrdering Ordering,
1173 SynchronizationScope SynchScope,
1174 BasicBlock *InsertAtEnd)
1175 : Instruction(Val->getType(), AtomicRMW,
1176 OperandTraits<AtomicRMWInst>::op_begin(this),
1177 OperandTraits<AtomicRMWInst>::operands(this),
1178 InsertAtEnd) {
1179 Init(Operation, Ptr, Val, Ordering, SynchScope);
1180}
1181
1182//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001183// FenceInst Implementation
1184//===----------------------------------------------------------------------===//
1185
1186FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1187 SynchronizationScope SynchScope,
1188 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001189 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001190 setOrdering(Ordering);
1191 setSynchScope(SynchScope);
1192}
1193
1194FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1195 SynchronizationScope SynchScope,
1196 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001197 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001198 setOrdering(Ordering);
1199 setSynchScope(SynchScope);
1200}
1201
1202//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001203// GetElementPtrInst Implementation
1204//===----------------------------------------------------------------------===//
1205
Jay Foadd1b78492011-07-25 09:48:08 +00001206void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001207 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001208 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001209 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001210 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001211 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001212}
1213
Gabor Greiff6caff662008-05-10 08:32:32 +00001214GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001215 : Instruction(GEPI.getType(), GetElementPtr,
1216 OperandTraits<GetElementPtrInst>::op_end(this) -
1217 GEPI.getNumOperands(),
1218 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001219 SourceElementType(GEPI.SourceElementType),
1220 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001221 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001222 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001223}
1224
Chris Lattner6090a422009-03-09 04:46:40 +00001225/// getIndexedType - Returns the type of the element that would be accessed with
1226/// a gep instruction with the specified parameters.
1227///
1228/// The Idxs pointer should point to a continuous piece of memory containing the
1229/// indices, either as Value* or uint64_t.
1230///
1231/// A null type is returned if the indices are invalid for the specified
1232/// pointer type.
1233///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001234template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001235static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001236 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001237 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001238 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001239
Chris Lattner6090a422009-03-09 04:46:40 +00001240 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001241 // it cannot be 'stepped over'.
1242 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001243 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001244
Dan Gohman1ecaf452008-05-31 00:58:22 +00001245 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001246 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001247 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001248 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001249 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001250 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001251 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001252 }
Craig Topperc6207612014-04-09 06:08:46 +00001253 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001254}
1255
David Blaikied288fb82015-03-30 21:41:43 +00001256Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1257 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001258}
1259
David Blaikied288fb82015-03-30 21:41:43 +00001260Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001261 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001262 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001263}
1264
David Blaikied288fb82015-03-30 21:41:43 +00001265Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1266 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001267}
1268
Chris Lattner45f15572007-04-14 00:12:57 +00001269/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1270/// zeros. If so, the result pointer and the first operand have the same
1271/// value, just potentially different types.
1272bool GetElementPtrInst::hasAllZeroIndices() const {
1273 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1274 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1275 if (!CI->isZero()) return false;
1276 } else {
1277 return false;
1278 }
1279 }
1280 return true;
1281}
1282
Chris Lattner27058292007-04-27 20:35:56 +00001283/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1284/// constant integers. If so, the result pointer and the first operand have
1285/// a constant offset between them.
1286bool GetElementPtrInst::hasAllConstantIndices() const {
1287 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1288 if (!isa<ConstantInt>(getOperand(i)))
1289 return false;
1290 }
1291 return true;
1292}
1293
Dan Gohman1b849082009-09-07 23:54:19 +00001294void GetElementPtrInst::setIsInBounds(bool B) {
1295 cast<GEPOperator>(this)->setIsInBounds(B);
1296}
Chris Lattner45f15572007-04-14 00:12:57 +00001297
Nick Lewycky28a5f252009-09-27 21:33:04 +00001298bool GetElementPtrInst::isInBounds() const {
1299 return cast<GEPOperator>(this)->isInBounds();
1300}
1301
Chandler Carruth1e140532012-12-11 10:29:10 +00001302bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1303 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001304 // Delegate to the generic GEPOperator implementation.
1305 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001306}
1307
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001308//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001309// ExtractElementInst Implementation
1310//===----------------------------------------------------------------------===//
1311
1312ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001313 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001314 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001316 ExtractElement,
1317 OperandTraits<ExtractElementInst>::op_begin(this),
1318 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001319 assert(isValidOperands(Val, Index) &&
1320 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001321 Op<0>() = Val;
1322 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001323 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001324}
1325
1326ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001327 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001328 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001329 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001330 ExtractElement,
1331 OperandTraits<ExtractElementInst>::op_begin(this),
1332 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001333 assert(isValidOperands(Val, Index) &&
1334 "Invalid extractelement instruction operands!");
1335
Gabor Greif2d3024d2008-05-26 21:33:52 +00001336 Op<0>() = Val;
1337 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001338 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001339}
1340
Chris Lattner65511ff2006-10-05 06:24:58 +00001341
Chris Lattner54865b32006-04-08 04:05:48 +00001342bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001343 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001344 return false;
1345 return true;
1346}
1347
1348
Robert Bocchino23004482006-01-10 19:05:34 +00001349//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001350// InsertElementInst Implementation
1351//===----------------------------------------------------------------------===//
1352
Chris Lattner54865b32006-04-08 04:05:48 +00001353InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001354 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001355 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001356 : Instruction(Vec->getType(), InsertElement,
1357 OperandTraits<InsertElementInst>::op_begin(this),
1358 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001359 assert(isValidOperands(Vec, Elt, Index) &&
1360 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001361 Op<0>() = Vec;
1362 Op<1>() = Elt;
1363 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001364 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001365}
1366
Chris Lattner54865b32006-04-08 04:05:48 +00001367InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001368 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001369 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001370 : Instruction(Vec->getType(), InsertElement,
1371 OperandTraits<InsertElementInst>::op_begin(this),
1372 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001373 assert(isValidOperands(Vec, Elt, Index) &&
1374 "Invalid insertelement instruction operands!");
1375
Gabor Greif2d3024d2008-05-26 21:33:52 +00001376 Op<0>() = Vec;
1377 Op<1>() = Elt;
1378 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001379 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001380}
1381
Chris Lattner54865b32006-04-08 04:05:48 +00001382bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1383 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001384 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001385 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001386
Reid Spencerd84d35b2007-02-15 02:26:10 +00001387 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001388 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001389
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001390 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001391 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001392 return true;
1393}
1394
1395
Robert Bocchinoca27f032006-01-17 20:07:22 +00001396//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001397// ShuffleVectorInst Implementation
1398//===----------------------------------------------------------------------===//
1399
1400ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001401 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001403: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001404 cast<VectorType>(Mask->getType())->getNumElements()),
1405 ShuffleVector,
1406 OperandTraits<ShuffleVectorInst>::op_begin(this),
1407 OperandTraits<ShuffleVectorInst>::operands(this),
1408 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001409 assert(isValidOperands(V1, V2, Mask) &&
1410 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001411 Op<0>() = V1;
1412 Op<1>() = V2;
1413 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001414 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001415}
1416
1417ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001418 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001420: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1421 cast<VectorType>(Mask->getType())->getNumElements()),
1422 ShuffleVector,
1423 OperandTraits<ShuffleVectorInst>::op_begin(this),
1424 OperandTraits<ShuffleVectorInst>::operands(this),
1425 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001426 assert(isValidOperands(V1, V2, Mask) &&
1427 "Invalid shuffle vector instruction operands!");
1428
Gabor Greif2d3024d2008-05-26 21:33:52 +00001429 Op<0>() = V1;
1430 Op<1>() = V2;
1431 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001432 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001433}
1434
Mon P Wang25f01062008-11-10 04:46:22 +00001435bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001436 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001437 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001438 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001439 return false;
1440
Chris Lattner1dcb6542012-01-25 23:49:49 +00001441 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001442 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001443 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001444 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001445
1446 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001447 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1448 return true;
1449
Nate Begeman60a31c32010-08-13 00:16:46 +00001450 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001451 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001452 for (Value *Op : MV->operands()) {
1453 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001454 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001455 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001456 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001457 return false;
1458 }
1459 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001460 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001461 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001462
1463 if (const ConstantDataSequential *CDS =
1464 dyn_cast<ConstantDataSequential>(Mask)) {
1465 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1466 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1467 if (CDS->getElementAsInteger(i) >= V1Size*2)
1468 return false;
1469 return true;
1470 }
1471
1472 // The bitcode reader can create a place holder for a forward reference
1473 // used as the shuffle mask. When this occurs, the shuffle mask will
1474 // fall into this case and fail. To avoid this error, do this bit of
1475 // ugliness to allow such a mask pass.
1476 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1477 if (CE->getOpcode() == Instruction::UserOp1)
1478 return true;
1479
1480 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001481}
1482
Chris Lattnerf724e342008-03-02 05:28:33 +00001483/// getMaskValue - Return the index from the shuffle mask for the specified
1484/// output result. This is either -1 if the element is undef or a number less
1485/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001486int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1487 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1488 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001489 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001490 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001491 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001492 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001493 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001494}
1495
Chris Lattner1dcb6542012-01-25 23:49:49 +00001496/// getShuffleMask - Return the full mask for this instruction, where each
1497/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001498void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1499 SmallVectorImpl<int> &Result) {
1500 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001501
Chris Lattnercf129702012-01-26 02:51:13 +00001502 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001503 for (unsigned i = 0; i != NumElts; ++i)
1504 Result.push_back(CDS->getElementAsInteger(i));
1505 return;
1506 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001507 for (unsigned i = 0; i != NumElts; ++i) {
1508 Constant *C = Mask->getAggregateElement(i);
1509 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001510 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001511 }
1512}
1513
1514
Dan Gohman12fce772008-05-15 19:50:34 +00001515//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001516// InsertValueInst Class
1517//===----------------------------------------------------------------------===//
1518
Jay Foad57aa6362011-07-13 10:26:04 +00001519void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1520 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001521 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001522
1523 // There's no fundamental reason why we require at least one index
1524 // (other than weirdness with &*IdxBegin being invalid; see
1525 // getelementptr's init routine for example). But there's no
1526 // present need to support it.
1527 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1528
1529 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001530 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001531 Op<0>() = Agg;
1532 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001533
Jay Foad57aa6362011-07-13 10:26:04 +00001534 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001535 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001536}
1537
1538InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001539 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001540 OperandTraits<InsertValueInst>::op_begin(this), 2),
1541 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001542 Op<0>() = IVI.getOperand(0);
1543 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001544 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001545}
1546
1547//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001548// ExtractValueInst Class
1549//===----------------------------------------------------------------------===//
1550
Jay Foad57aa6362011-07-13 10:26:04 +00001551void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001552 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001553
Jay Foad57aa6362011-07-13 10:26:04 +00001554 // There's no fundamental reason why we require at least one index.
1555 // But there's no present need to support it.
1556 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001557
Jay Foad57aa6362011-07-13 10:26:04 +00001558 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001559 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001560}
1561
1562ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001563 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001564 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001565 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001566}
1567
Dan Gohman12fce772008-05-15 19:50:34 +00001568// getIndexedType - Returns the type of the element that would be extracted
1569// with an extractvalue instruction with the specified parameters.
1570//
1571// A null type is returned if the indices are invalid for the specified
1572// pointer type.
1573//
Chris Lattner229907c2011-07-18 04:54:35 +00001574Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001575 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001576 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001577 // We can't use CompositeType::indexValid(Index) here.
1578 // indexValid() always returns true for arrays because getelementptr allows
1579 // out-of-bounds indices. Since we don't allow those for extractvalue and
1580 // insertvalue we need to check array indexing manually.
1581 // Since the only other types we can index into are struct types it's just
1582 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001583 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001584 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001585 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001586 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001587 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001588 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001589 } else {
1590 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001591 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001592 }
1593
1594 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001595 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001596 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001597}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001598
1599//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600// BinaryOperator Class
1601//===----------------------------------------------------------------------===//
1602
Chris Lattner2195fc42007-02-24 00:55:48 +00001603BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001604 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001605 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001606 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001607 OperandTraits<BinaryOperator>::op_begin(this),
1608 OperandTraits<BinaryOperator>::operands(this),
1609 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001610 Op<0>() = S1;
1611 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001612 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001613 setName(Name);
1614}
1615
1616BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001617 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001618 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001619 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001620 OperandTraits<BinaryOperator>::op_begin(this),
1621 OperandTraits<BinaryOperator>::operands(this),
1622 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001623 Op<0>() = S1;
1624 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001625 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001626 setName(Name);
1627}
1628
1629
1630void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001631 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001632 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001633 assert(LHS->getType() == RHS->getType() &&
1634 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635#ifndef NDEBUG
1636 switch (iType) {
1637 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001638 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001639 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001641 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001642 "Tried to create an integer operation on a non-integer type!");
1643 break;
1644 case FAdd: case FSub:
1645 case FMul:
1646 assert(getType() == LHS->getType() &&
1647 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001648 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001649 "Tried to create a floating-point operation on a "
1650 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001651 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001652 case UDiv:
1653 case SDiv:
1654 assert(getType() == LHS->getType() &&
1655 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001656 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001657 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001658 "Incorrect operand type (not integer) for S/UDIV");
1659 break;
1660 case FDiv:
1661 assert(getType() == LHS->getType() &&
1662 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001663 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001664 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001665 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001666 case URem:
1667 case SRem:
1668 assert(getType() == LHS->getType() &&
1669 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001670 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001671 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001672 "Incorrect operand type (not integer) for S/UREM");
1673 break;
1674 case FRem:
1675 assert(getType() == LHS->getType() &&
1676 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001677 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001678 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001679 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001680 case Shl:
1681 case LShr:
1682 case AShr:
1683 assert(getType() == LHS->getType() &&
1684 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001685 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001686 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001687 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001688 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001689 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001690 case And: case Or:
1691 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001692 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001694 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001695 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001696 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001697 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001698 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001699 default:
1700 break;
1701 }
1702#endif
1703}
1704
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001705BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001706 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001707 Instruction *InsertBefore) {
1708 assert(S1->getType() == S2->getType() &&
1709 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001710 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001711}
1712
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001713BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001714 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001715 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001716 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001717 InsertAtEnd->getInstList().push_back(Res);
1718 return Res;
1719}
1720
Dan Gohman5476cfd2009-08-12 16:23:25 +00001721BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001722 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001723 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001724 return new BinaryOperator(Instruction::Sub,
1725 zero, Op,
1726 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001727}
1728
Dan Gohman5476cfd2009-08-12 16:23:25 +00001729BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001730 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001731 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001732 return new BinaryOperator(Instruction::Sub,
1733 zero, Op,
1734 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735}
1736
Dan Gohman4ab44202009-12-18 02:58:50 +00001737BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1738 Instruction *InsertBefore) {
1739 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1740 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1741}
1742
1743BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1744 BasicBlock *InsertAtEnd) {
1745 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1746 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1747}
1748
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001749BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1750 Instruction *InsertBefore) {
1751 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1752 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1753}
1754
1755BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1756 BasicBlock *InsertAtEnd) {
1757 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1758 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1759}
1760
Dan Gohman5476cfd2009-08-12 16:23:25 +00001761BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001762 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001763 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001764 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001765 Op->getType(), Name, InsertBefore);
1766}
1767
Dan Gohman5476cfd2009-08-12 16:23:25 +00001768BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001769 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001770 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001771 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001772 Op->getType(), Name, InsertAtEnd);
1773}
1774
Dan Gohman5476cfd2009-08-12 16:23:25 +00001775BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001777 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001778 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001779 Op->getType(), Name, InsertBefore);
1780}
1781
Dan Gohman5476cfd2009-08-12 16:23:25 +00001782BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001783 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001784 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001785 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001786 Op->getType(), Name, InsertAtEnd);
1787}
1788
1789
1790// isConstantAllOnes - Helper function for several functions below
1791static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001792 if (const Constant *C = dyn_cast<Constant>(V))
1793 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001794 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795}
1796
Owen Andersonbb2501b2009-07-13 22:18:28 +00001797bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001798 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1799 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001800 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1801 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001802 return false;
1803}
1804
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001805bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001806 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1807 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001808 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1809 if (!IgnoreZeroSign)
1810 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1811 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1812 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001813 return false;
1814}
1815
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816bool BinaryOperator::isNot(const Value *V) {
1817 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1818 return (Bop->getOpcode() == Instruction::Xor &&
1819 (isConstantAllOnes(Bop->getOperand(1)) ||
1820 isConstantAllOnes(Bop->getOperand(0))));
1821 return false;
1822}
1823
Chris Lattner2c7d1772005-04-24 07:28:37 +00001824Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001825 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001826}
1827
Chris Lattner2c7d1772005-04-24 07:28:37 +00001828const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1829 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830}
1831
Dan Gohmana5b96452009-06-04 22:49:04 +00001832Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001833 return cast<BinaryOperator>(BinOp)->getOperand(1);
1834}
1835
1836const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1837 return getFNegArgument(const_cast<Value*>(BinOp));
1838}
1839
Chris Lattner2c7d1772005-04-24 07:28:37 +00001840Value *BinaryOperator::getNotArgument(Value *BinOp) {
1841 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1842 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1843 Value *Op0 = BO->getOperand(0);
1844 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845 if (isConstantAllOnes(Op0)) return Op1;
1846
1847 assert(isConstantAllOnes(Op1));
1848 return Op0;
1849}
1850
Chris Lattner2c7d1772005-04-24 07:28:37 +00001851const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1852 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001853}
1854
1855
1856// swapOperands - Exchange the two operands to this instruction. This
1857// instruction is safe to use on any binary instruction and does not
1858// modify the semantics of the instruction. If the instruction is
1859// order dependent (SetLT f.e.) the opcode is changed.
1860//
1861bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001862 if (!isCommutative())
1863 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001864 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865 return false;
1866}
1867
Dan Gohman1b849082009-09-07 23:54:19 +00001868void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1869 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1870}
1871
1872void BinaryOperator::setHasNoSignedWrap(bool b) {
1873 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1874}
1875
1876void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001877 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001878}
1879
Nick Lewycky28a5f252009-09-27 21:33:04 +00001880bool BinaryOperator::hasNoUnsignedWrap() const {
1881 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1882}
1883
1884bool BinaryOperator::hasNoSignedWrap() const {
1885 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1886}
1887
1888bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001889 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001890}
1891
Sanjay Patela982d992014-09-03 01:06:50 +00001892void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001893 // Copy the wrapping flags.
1894 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1895 setHasNoSignedWrap(OB->hasNoSignedWrap());
1896 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
1897 }
1898
1899 // Copy the exact flag.
1900 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1901 setIsExact(PE->isExact());
1902
1903 // Copy the fast-math flags.
1904 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00001905 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001906}
1907
Sanjay Patela982d992014-09-03 01:06:50 +00001908void BinaryOperator::andIRFlags(const Value *V) {
1909 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1910 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
1911 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
1912 }
1913
1914 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1915 setIsExact(isExact() & PE->isExact());
1916
1917 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
1918 FastMathFlags FM = getFastMathFlags();
1919 FM &= FP->getFastMathFlags();
1920 copyFastMathFlags(FM);
1921 }
1922}
1923
1924
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001925//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00001926// FPMathOperator Class
1927//===----------------------------------------------------------------------===//
1928
1929/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
1930/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00001931/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00001932float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001933 const MDNode *MD =
1934 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00001935 if (!MD)
1936 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001937 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00001938 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00001939}
1940
1941
1942//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001943// CastInst Class
1944//===----------------------------------------------------------------------===//
1945
David Blaikie3a15e142011-12-01 08:00:17 +00001946void CastInst::anchor() {}
1947
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948// Just determine if this cast only deals with integral->integral conversion.
1949bool CastInst::isIntegerCast() const {
1950 switch (getOpcode()) {
1951 default: return false;
1952 case Instruction::ZExt:
1953 case Instruction::SExt:
1954 case Instruction::Trunc:
1955 return true;
1956 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001957 return getOperand(0)->getType()->isIntegerTy() &&
1958 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001960}
1961
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962bool CastInst::isLosslessCast() const {
1963 // Only BitCast can be lossless, exit fast if we're not BitCast
1964 if (getOpcode() != Instruction::BitCast)
1965 return false;
1966
1967 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001968 Type* SrcTy = getOperand(0)->getType();
1969 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 if (SrcTy == DstTy)
1971 return true;
1972
Reid Spencer8d9336d2006-12-31 05:26:44 +00001973 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001974 if (SrcTy->isPointerTy())
1975 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 return false; // Other types have no identity values
1977}
1978
1979/// This function determines if the CastInst does not require any bits to be
1980/// changed in order to effect the cast. Essentially, it identifies cases where
1981/// no code gen is necessary for the cast, hence the name no-op cast. For
1982/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001983/// # bitcast i32* %x to i8*
1984/// # bitcast <2 x i32> %x to <4 x i16>
1985/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001986/// @brief Determine if the described cast is a no-op.
1987bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001988 Type *SrcTy,
1989 Type *DestTy,
1990 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001991 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001992 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993 case Instruction::Trunc:
1994 case Instruction::ZExt:
1995 case Instruction::SExt:
1996 case Instruction::FPTrunc:
1997 case Instruction::FPExt:
1998 case Instruction::UIToFP:
1999 case Instruction::SIToFP:
2000 case Instruction::FPToUI:
2001 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002002 case Instruction::AddrSpaceCast:
2003 // TODO: Target informations may give a more accurate answer here.
2004 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 case Instruction::BitCast:
2006 return true; // BitCast never modifies bits.
2007 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002008 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002009 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002011 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002012 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 }
2014}
2015
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002016/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002017bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002018 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2019}
2020
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002021bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002022 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002023 if (getOpcode() == Instruction::PtrToInt)
2024 PtrOpTy = getOperand(0)->getType();
2025 else if (getOpcode() == Instruction::IntToPtr)
2026 PtrOpTy = getType();
2027
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002028 Type *IntPtrTy =
2029 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002030
2031 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2032}
2033
2034/// This function determines if a pair of casts can be eliminated and what
2035/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002036/// instructions like this:
2037/// * %F = firstOpcode SrcTy %x to MidTy
2038/// * %S = secondOpcode MidTy %F to DstTy
2039/// The function returns a resultOpcode so these two casts can be replaced with:
2040/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2041/// If no such cast is permited, the function returns 0.
2042unsigned CastInst::isEliminableCastPair(
2043 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002044 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2045 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 // Define the 144 possibilities for these two cast instructions. The values
2047 // in this matrix determine what to do in a given situation and select the
2048 // case in the switch below. The rows correspond to firstOp, the columns
2049 // correspond to secondOp. In looking at the table below, keep in mind
2050 // the following cast properties:
2051 //
2052 // Size Compare Source Destination
2053 // Operator Src ? Size Type Sign Type Sign
2054 // -------- ------------ ------------------- ---------------------
2055 // TRUNC > Integer Any Integral Any
2056 // ZEXT < Integral Unsigned Integer Any
2057 // SEXT < Integral Signed Integer Any
2058 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002059 // FPTOSI n/a FloatPt n/a Integral Signed
2060 // UITOFP n/a Integral Unsigned FloatPt n/a
2061 // SITOFP n/a Integral Signed FloatPt n/a
2062 // FPTRUNC > FloatPt n/a FloatPt n/a
2063 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002064 // PTRTOINT n/a Pointer n/a Integral Unsigned
2065 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002066 // BITCAST = FirstClass n/a FirstClass n/a
2067 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002068 //
2069 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002070 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2071 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002072 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002073 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002074 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002075 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002076 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2078 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002079 // T F F U S F F P I B A -+
2080 // R Z S P P I I T P 2 N T S |
2081 // U E E 2 2 2 2 R E I T C C +- secondOp
2082 // N X X U S F F N X N 2 V V |
2083 // C T T I I P P C T T P T T -+
2084 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002085 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002086 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2087 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2088 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2089 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2090 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002091 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002092 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2093 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2094 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2095 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2096 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002097 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002098
Chris Lattner25eea4d2010-07-12 01:19:22 +00002099 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002100 // merging. However, bitcast of A->B->A are allowed.
2101 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2102 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2103 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2104
2105 // Check if any of the bitcasts convert scalars<->vectors.
2106 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2107 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2108 // Unless we are bitcasing to the original type, disallow optimizations.
2109 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110
2111 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2112 [secondOp-Instruction::CastOpsBegin];
2113 switch (ElimCase) {
2114 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002115 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002116 return 0;
2117 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002118 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 return firstOp;
2120 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002121 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122 return secondOp;
2123 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002124 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002125 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002126 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002127 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128 return firstOp;
2129 return 0;
2130 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002131 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002132 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002133 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134 return firstOp;
2135 return 0;
2136 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002137 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002138 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002139 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140 return secondOp;
2141 return 0;
2142 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002143 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002144 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002145 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146 return secondOp;
2147 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002148 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002149 // Cannot simplify if address spaces are different!
2150 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2151 return 0;
2152
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002153 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002154 // We can still fold this without knowing the actual sizes as long we
2155 // know that the intermediate pointer is the largest possible
2156 // pointer size.
2157 // FIXME: Is this always true?
2158 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002159 return Instruction::BitCast;
2160
2161 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002162 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002163 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002164 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 if (MidSize >= PtrSize)
2166 return Instruction::BitCast;
2167 return 0;
2168 }
2169 case 8: {
2170 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2171 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2172 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002173 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2174 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175 if (SrcSize == DstSize)
2176 return Instruction::BitCast;
2177 else if (SrcSize < DstSize)
2178 return firstOp;
2179 return secondOp;
2180 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002181 case 9:
2182 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 return Instruction::ZExt;
2184 case 10:
2185 // fpext followed by ftrunc is allowed if the bit size returned to is
2186 // the same as the original, in which case its just a bitcast
2187 if (SrcTy == DstTy)
2188 return Instruction::BitCast;
2189 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002190 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002192 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002193 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002194 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002195 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2196 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 if (SrcSize <= PtrSize && SrcSize == DstSize)
2198 return Instruction::BitCast;
2199 return 0;
2200 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002201 case 12: {
2202 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2203 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2204 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2205 return Instruction::AddrSpaceCast;
2206 return Instruction::BitCast;
2207 }
2208 case 13:
2209 // FIXME: this state can be merged with (1), but the following assert
2210 // is useful to check the correcteness of the sequence due to semantic
2211 // change of bitcast.
2212 assert(
2213 SrcTy->isPtrOrPtrVectorTy() &&
2214 MidTy->isPtrOrPtrVectorTy() &&
2215 DstTy->isPtrOrPtrVectorTy() &&
2216 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2217 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2218 "Illegal addrspacecast, bitcast sequence!");
2219 // Allowed, use first cast's opcode
2220 return firstOp;
2221 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002222 // bitcast, addrspacecast -> addrspacecast if the element type of
2223 // bitcast's source is the same as that of addrspacecast's destination.
2224 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2225 return Instruction::AddrSpaceCast;
2226 return 0;
2227
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002228 case 15:
2229 // FIXME: this state can be merged with (1), but the following assert
2230 // is useful to check the correcteness of the sequence due to semantic
2231 // change of bitcast.
2232 assert(
2233 SrcTy->isIntOrIntVectorTy() &&
2234 MidTy->isPtrOrPtrVectorTy() &&
2235 DstTy->isPtrOrPtrVectorTy() &&
2236 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2237 "Illegal inttoptr, bitcast sequence!");
2238 // Allowed, use first cast's opcode
2239 return firstOp;
2240 case 16:
2241 // FIXME: this state can be merged with (2), but the following assert
2242 // is useful to check the correcteness of the sequence due to semantic
2243 // change of bitcast.
2244 assert(
2245 SrcTy->isPtrOrPtrVectorTy() &&
2246 MidTy->isPtrOrPtrVectorTy() &&
2247 DstTy->isIntOrIntVectorTy() &&
2248 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2249 "Illegal bitcast, ptrtoint sequence!");
2250 // Allowed, use second cast's opcode
2251 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002252 case 17:
2253 // (sitofp (zext x)) -> (uitofp x)
2254 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002256 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002258 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259 default:
Craig Topperc514b542012-02-05 22:14:15 +00002260 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262}
2263
Chris Lattner229907c2011-07-18 04:54:35 +00002264CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002265 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002266 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267 // Construct and return the appropriate CastInst subclass
2268 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002269 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2270 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2271 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2272 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2273 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2274 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2275 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2276 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2277 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2278 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2279 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2280 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2281 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2282 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284}
2285
Chris Lattner229907c2011-07-18 04:54:35 +00002286CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002287 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002288 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 // Construct and return the appropriate CastInst subclass
2290 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002291 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2292 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2293 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2294 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2295 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2296 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2297 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2298 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2299 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2300 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2301 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2302 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2303 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2304 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306}
2307
Chris Lattner229907c2011-07-18 04:54:35 +00002308CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002309 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002310 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002311 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002312 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2313 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002314}
2315
Chris Lattner229907c2011-07-18 04:54:35 +00002316CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002317 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002318 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002319 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002320 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2321 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002322}
2323
Chris Lattner229907c2011-07-18 04:54:35 +00002324CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002325 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002326 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002327 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002328 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2329 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002330}
2331
Chris Lattner229907c2011-07-18 04:54:35 +00002332CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002333 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002334 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002335 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002336 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2337 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002338}
2339
Chris Lattner229907c2011-07-18 04:54:35 +00002340CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002341 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002342 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002343 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002344 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2345 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002346}
2347
Chris Lattner229907c2011-07-18 04:54:35 +00002348CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002349 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002350 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002352 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2353 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002354}
2355
Chris Lattner229907c2011-07-18 04:54:35 +00002356CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002357 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002358 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002359 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2360 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2361 "Invalid cast");
2362 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002363 assert((!Ty->isVectorTy() ||
2364 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002365 "Invalid cast");
2366
Matt Arsenault065ced92013-07-31 00:17:33 +00002367 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002368 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002369
Matt Arsenault740980e2014-07-14 17:24:35 +00002370 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002371}
2372
2373/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002374CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2375 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002376 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002377 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2378 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002379 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002380 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002381 assert((!Ty->isVectorTy() ||
2382 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002383 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002384
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002385 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002386 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002387
Matt Arsenault740980e2014-07-14 17:24:35 +00002388 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2389}
2390
2391CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2392 Value *S, Type *Ty,
2393 const Twine &Name,
2394 BasicBlock *InsertAtEnd) {
2395 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2396 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2397
2398 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2399 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2400
2401 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2402}
2403
2404CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2405 Value *S, Type *Ty,
2406 const Twine &Name,
2407 Instruction *InsertBefore) {
2408 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2409 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2410
2411 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002412 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2413
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002414 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002415}
2416
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002417CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2418 const Twine &Name,
2419 Instruction *InsertBefore) {
2420 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2421 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2422 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2423 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2424
2425 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2426}
2427
Matt Arsenault740980e2014-07-14 17:24:35 +00002428CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002429 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002430 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002431 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002432 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002433 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2434 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002435 Instruction::CastOps opcode =
2436 (SrcBits == DstBits ? Instruction::BitCast :
2437 (SrcBits > DstBits ? Instruction::Trunc :
2438 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002439 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002440}
2441
Chris Lattner229907c2011-07-18 04:54:35 +00002442CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002443 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002444 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002445 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002446 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002447 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2448 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002449 Instruction::CastOps opcode =
2450 (SrcBits == DstBits ? Instruction::BitCast :
2451 (SrcBits > DstBits ? Instruction::Trunc :
2452 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002453 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002454}
2455
Chris Lattner229907c2011-07-18 04:54:35 +00002456CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002457 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002458 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002459 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002460 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002461 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2462 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002463 Instruction::CastOps opcode =
2464 (SrcBits == DstBits ? Instruction::BitCast :
2465 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002466 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002467}
2468
Chris Lattner229907c2011-07-18 04:54:35 +00002469CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002470 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002471 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002472 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002473 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002474 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2475 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002476 Instruction::CastOps opcode =
2477 (SrcBits == DstBits ? Instruction::BitCast :
2478 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002479 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002480}
2481
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002482// Check whether it is valid to call getCastOpcode for these types.
2483// This routine must be kept in sync with getCastOpcode.
2484bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2485 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2486 return false;
2487
2488 if (SrcTy == DestTy)
2489 return true;
2490
2491 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2492 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2493 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2494 // An element by element cast. Valid if casting the elements is valid.
2495 SrcTy = SrcVecTy->getElementType();
2496 DestTy = DestVecTy->getElementType();
2497 }
2498
2499 // Get the bit sizes, we'll need these
2500 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2501 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2502
2503 // Run through the possibilities ...
2504 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002505 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002506 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002507 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002508 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002509 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002510 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002511 // Casting from something else
2512 return SrcTy->isPointerTy();
2513 }
2514 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2515 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002516 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002517 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002518 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002519 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002520 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002521 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002522 return false;
2523 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002524 if (DestTy->isVectorTy()) // Casting to vector
2525 return DestBits == SrcBits;
2526 if (DestTy->isPointerTy()) { // Casting to pointer
2527 if (SrcTy->isPointerTy()) // Casting from pointer
2528 return true;
2529 return SrcTy->isIntegerTy(); // Casting from integral
2530 }
2531 if (DestTy->isX86_MMXTy()) {
2532 if (SrcTy->isVectorTy())
2533 return DestBits == SrcBits; // 64-bit vector to MMX
2534 return false;
2535 } // Casting to something else
2536 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002537}
2538
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002539bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2540 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2541 return false;
2542
2543 if (SrcTy == DestTy)
2544 return true;
2545
2546 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2547 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2548 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2549 // An element by element cast. Valid if casting the elements is valid.
2550 SrcTy = SrcVecTy->getElementType();
2551 DestTy = DestVecTy->getElementType();
2552 }
2553 }
2554 }
2555
2556 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2557 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2558 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2559 }
2560 }
2561
2562 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2563 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2564
2565 // Could still have vectors of pointers if the number of elements doesn't
2566 // match
2567 if (SrcBits == 0 || DestBits == 0)
2568 return false;
2569
2570 if (SrcBits != DestBits)
2571 return false;
2572
2573 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2574 return false;
2575
2576 return true;
2577}
2578
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002579bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002580 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002581 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2582 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002583 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002584 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2585 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002586 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002587
2588 return isBitCastable(SrcTy, DestTy);
2589}
2590
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002591// Provide a way to get a "cast" where the cast opcode is inferred from the
2592// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593// logic in the castIsValid function below. This axiom should hold:
2594// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2595// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002597// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002599CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002600 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2601 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602
Duncan Sands55e50902008-01-06 10:12:28 +00002603 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2604 "Only first class types are castable!");
2605
Duncan Sandsa8514532011-05-18 07:13:41 +00002606 if (SrcTy == DestTy)
2607 return BitCast;
2608
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002609 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002610 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2611 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002612 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2613 // An element by element cast. Find the appropriate opcode based on the
2614 // element types.
2615 SrcTy = SrcVecTy->getElementType();
2616 DestTy = DestVecTy->getElementType();
2617 }
2618
2619 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002620 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2621 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002622
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002624 if (DestTy->isIntegerTy()) { // Casting to integral
2625 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626 if (DestBits < SrcBits)
2627 return Trunc; // int -> smaller int
2628 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002629 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630 return SExt; // signed -> SEXT
2631 else
2632 return ZExt; // unsigned -> ZEXT
2633 } else {
2634 return BitCast; // Same size, No-op cast
2635 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002636 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002637 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638 return FPToSI; // FP -> sint
2639 else
2640 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002641 } else if (SrcTy->isVectorTy()) {
2642 assert(DestBits == SrcBits &&
2643 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644 return BitCast; // Same size, no-op cast
2645 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002646 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647 "Casting from a value that is not first-class type");
2648 return PtrToInt; // ptr -> int
2649 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002650 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2651 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002652 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 return SIToFP; // sint -> FP
2654 else
2655 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002656 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 if (DestBits < SrcBits) {
2658 return FPTrunc; // FP -> smaller FP
2659 } else if (DestBits > SrcBits) {
2660 return FPExt; // FP -> larger FP
2661 } else {
2662 return BitCast; // same size, no-op cast
2663 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002664 } else if (SrcTy->isVectorTy()) {
2665 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002666 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002667 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002669 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002670 } else if (DestTy->isVectorTy()) {
2671 assert(DestBits == SrcBits &&
2672 "Illegal cast to vector (wrong type or size)");
2673 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002674 } else if (DestTy->isPointerTy()) {
2675 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002676 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2677 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002679 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002682 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002683 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002684 if (SrcTy->isVectorTy()) {
2685 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002686 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002687 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002688 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002690 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691}
2692
2693//===----------------------------------------------------------------------===//
2694// CastInst SubClass Constructors
2695//===----------------------------------------------------------------------===//
2696
2697/// Check that the construction parameters for a CastInst are correct. This
2698/// could be broken out into the separate constructors but it is useful to have
2699/// it in one place and to eliminate the redundant code for getting the sizes
2700/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002701bool
Chris Lattner229907c2011-07-18 04:54:35 +00002702CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703
2704 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002705 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002706
Chris Lattner37bc78a2010-01-26 21:51:43 +00002707 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2708 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709 return false;
2710
2711 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002712 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2713 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002714
Duncan Sands7f646562011-05-18 09:21:57 +00002715 // If these are vector types, get the lengths of the vectors (using zero for
2716 // scalar types means that checking that vector lengths match also checks that
2717 // scalars are not being converted to vectors or vectors to scalars).
2718 unsigned SrcLength = SrcTy->isVectorTy() ?
2719 cast<VectorType>(SrcTy)->getNumElements() : 0;
2720 unsigned DstLength = DstTy->isVectorTy() ?
2721 cast<VectorType>(DstTy)->getNumElements() : 0;
2722
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723 // Switch on the opcode provided
2724 switch (op) {
2725 default: return false; // This is an input error
2726 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002727 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2728 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002730 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2731 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002732 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002733 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2734 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002736 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2737 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002738 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002739 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2740 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002743 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2744 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002747 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2748 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002749 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002750 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002751 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002752 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2753 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2754 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002755 return SrcTy->getScalarType()->isPointerTy() &&
2756 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002758 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002759 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002760 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2761 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2762 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002763 return SrcTy->getScalarType()->isIntegerTy() &&
2764 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002765 case Instruction::BitCast: {
2766 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2767 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2768
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769 // BitCast implies a no-op cast of type only. No bits change.
2770 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002771 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772 return false;
2773
Alp Tokerf907b892013-12-05 05:44:44 +00002774 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002775 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002776 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002777 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2778
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002779 // If both are pointers then the address spaces must match.
2780 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2781 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002782
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002783 // A vector of pointers must have the same number of elements.
2784 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2785 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2786 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2787
2788 return false;
2789 }
2790
2791 return true;
2792 }
2793 case Instruction::AddrSpaceCast: {
2794 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2795 if (!SrcPtrTy)
2796 return false;
2797
2798 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2799 if (!DstPtrTy)
2800 return false;
2801
2802 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2803 return false;
2804
2805 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2806 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2807 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2808
2809 return false;
2810 }
2811
2812 return true;
2813 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814 }
2815}
2816
2817TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002818 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002820 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002821}
2822
2823TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002824 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002827}
2828
2829ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002830 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002833}
2834
2835ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002836 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002838 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002839}
2840SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002841 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002842) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002843 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002844}
2845
Jeff Cohencc08c832006-12-02 02:22:01 +00002846SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002847 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002848) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002849 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002850}
2851
2852FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002853 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002854) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002855 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002856}
2857
2858FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002859 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002860) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002861 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002862}
2863
2864FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002865 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002866) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002867 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002868}
2869
2870FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002871 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002872) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002873 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002874}
2875
2876UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002877 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002878) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002879 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002880}
2881
2882UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002883 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002884) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002885 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002886}
2887
2888SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002889 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002890) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002891 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002892}
2893
2894SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002895 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002896) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002897 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002898}
2899
2900FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002901 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002902) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002903 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904}
2905
2906FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002907 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002908) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002909 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910}
2911
2912FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002913 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002915 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002916}
2917
2918FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002919 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002920) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002921 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922}
2923
2924PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002925 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002927 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928}
2929
2930PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002931 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002932) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002933 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002934}
2935
2936IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002937 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002939 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002940}
2941
2942IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002943 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002944) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002945 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002946}
2947
2948BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002949 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002951 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002952}
2953
2954BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002955 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002957 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002958}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002959
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002960AddrSpaceCastInst::AddrSpaceCastInst(
2961 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2962) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
2963 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2964}
2965
2966AddrSpaceCastInst::AddrSpaceCastInst(
2967 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2968) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
2969 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2970}
2971
Chris Lattnerf16dc002006-09-17 19:29:56 +00002972//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002973// CmpInst Classes
2974//===----------------------------------------------------------------------===//
2975
Craig Topper3186c012012-09-23 02:12:10 +00002976void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002977
Chris Lattner229907c2011-07-18 04:54:35 +00002978CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002979 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002980 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002981 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002982 OperandTraits<CmpInst>::op_begin(this),
2983 OperandTraits<CmpInst>::operands(this),
2984 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002985 Op<0>() = LHS;
2986 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002987 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002988 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002989}
Gabor Greiff6caff662008-05-10 08:32:32 +00002990
Chris Lattner229907c2011-07-18 04:54:35 +00002991CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002992 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002993 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002994 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002995 OperandTraits<CmpInst>::op_begin(this),
2996 OperandTraits<CmpInst>::operands(this),
2997 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002998 Op<0>() = LHS;
2999 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003000 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003001 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003002}
3003
3004CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003005CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003006 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003007 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003008 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003009 if (InsertBefore)
3010 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3011 S1, S2, Name);
3012 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003013 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003014 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003015 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003016
3017 if (InsertBefore)
3018 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3019 S1, S2, Name);
3020 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003021 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003022 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003023}
3024
3025CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003026CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003027 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003028 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003029 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3030 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003031 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003032 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3033 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003034}
3035
3036void CmpInst::swapOperands() {
3037 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3038 IC->swapOperands();
3039 else
3040 cast<FCmpInst>(this)->swapOperands();
3041}
3042
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003043bool CmpInst::isCommutative() const {
3044 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003045 return IC->isCommutative();
3046 return cast<FCmpInst>(this)->isCommutative();
3047}
3048
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003049bool CmpInst::isEquality() const {
3050 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003051 return IC->isEquality();
3052 return cast<FCmpInst>(this)->isEquality();
3053}
3054
3055
Dan Gohman4e724382008-05-31 02:47:54 +00003056CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003057 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003058 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003059 case ICMP_EQ: return ICMP_NE;
3060 case ICMP_NE: return ICMP_EQ;
3061 case ICMP_UGT: return ICMP_ULE;
3062 case ICMP_ULT: return ICMP_UGE;
3063 case ICMP_UGE: return ICMP_ULT;
3064 case ICMP_ULE: return ICMP_UGT;
3065 case ICMP_SGT: return ICMP_SLE;
3066 case ICMP_SLT: return ICMP_SGE;
3067 case ICMP_SGE: return ICMP_SLT;
3068 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003069
Dan Gohman4e724382008-05-31 02:47:54 +00003070 case FCMP_OEQ: return FCMP_UNE;
3071 case FCMP_ONE: return FCMP_UEQ;
3072 case FCMP_OGT: return FCMP_ULE;
3073 case FCMP_OLT: return FCMP_UGE;
3074 case FCMP_OGE: return FCMP_ULT;
3075 case FCMP_OLE: return FCMP_UGT;
3076 case FCMP_UEQ: return FCMP_ONE;
3077 case FCMP_UNE: return FCMP_OEQ;
3078 case FCMP_UGT: return FCMP_OLE;
3079 case FCMP_ULT: return FCMP_OGE;
3080 case FCMP_UGE: return FCMP_OLT;
3081 case FCMP_ULE: return FCMP_OGT;
3082 case FCMP_ORD: return FCMP_UNO;
3083 case FCMP_UNO: return FCMP_ORD;
3084 case FCMP_TRUE: return FCMP_FALSE;
3085 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003086 }
3087}
3088
Reid Spencer266e42b2006-12-23 06:05:41 +00003089ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3090 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003091 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003092 case ICMP_EQ: case ICMP_NE:
3093 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3094 return pred;
3095 case ICMP_UGT: return ICMP_SGT;
3096 case ICMP_ULT: return ICMP_SLT;
3097 case ICMP_UGE: return ICMP_SGE;
3098 case ICMP_ULE: return ICMP_SLE;
3099 }
3100}
3101
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003102ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3103 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003104 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003105 case ICMP_EQ: case ICMP_NE:
3106 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3107 return pred;
3108 case ICMP_SGT: return ICMP_UGT;
3109 case ICMP_SLT: return ICMP_ULT;
3110 case ICMP_SGE: return ICMP_UGE;
3111 case ICMP_SLE: return ICMP_ULE;
3112 }
3113}
3114
Reid Spencer0286bc12007-02-28 22:00:54 +00003115/// Initialize a set of values that all satisfy the condition with C.
3116///
3117ConstantRange
3118ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3119 APInt Lower(C);
3120 APInt Upper(C);
3121 uint32_t BitWidth = C.getBitWidth();
3122 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003123 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003124 case ICmpInst::ICMP_EQ: ++Upper; break;
3125 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003126 case ICmpInst::ICMP_ULT:
3127 Lower = APInt::getMinValue(BitWidth);
3128 // Check for an empty-set condition.
3129 if (Lower == Upper)
3130 return ConstantRange(BitWidth, /*isFullSet=*/false);
3131 break;
3132 case ICmpInst::ICMP_SLT:
3133 Lower = APInt::getSignedMinValue(BitWidth);
3134 // Check for an empty-set condition.
3135 if (Lower == Upper)
3136 return ConstantRange(BitWidth, /*isFullSet=*/false);
3137 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003138 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003139 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003140 // Check for an empty-set condition.
3141 if (Lower == Upper)
3142 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003143 break;
3144 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003145 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003146 // Check for an empty-set condition.
3147 if (Lower == Upper)
3148 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003149 break;
3150 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003151 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003152 // Check for a full-set condition.
3153 if (Lower == Upper)
3154 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003155 break;
3156 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003157 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003158 // Check for a full-set condition.
3159 if (Lower == Upper)
3160 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003161 break;
3162 case ICmpInst::ICMP_UGE:
3163 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003164 // Check for a full-set condition.
3165 if (Lower == Upper)
3166 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003167 break;
3168 case ICmpInst::ICMP_SGE:
3169 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003170 // Check for a full-set condition.
3171 if (Lower == Upper)
3172 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003173 break;
3174 }
3175 return ConstantRange(Lower, Upper);
3176}
3177
Dan Gohman4e724382008-05-31 02:47:54 +00003178CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003179 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003180 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003181 case ICMP_EQ: case ICMP_NE:
3182 return pred;
3183 case ICMP_SGT: return ICMP_SLT;
3184 case ICMP_SLT: return ICMP_SGT;
3185 case ICMP_SGE: return ICMP_SLE;
3186 case ICMP_SLE: return ICMP_SGE;
3187 case ICMP_UGT: return ICMP_ULT;
3188 case ICMP_ULT: return ICMP_UGT;
3189 case ICMP_UGE: return ICMP_ULE;
3190 case ICMP_ULE: return ICMP_UGE;
3191
Reid Spencerd9436b62006-11-20 01:22:35 +00003192 case FCMP_FALSE: case FCMP_TRUE:
3193 case FCMP_OEQ: case FCMP_ONE:
3194 case FCMP_UEQ: case FCMP_UNE:
3195 case FCMP_ORD: case FCMP_UNO:
3196 return pred;
3197 case FCMP_OGT: return FCMP_OLT;
3198 case FCMP_OLT: return FCMP_OGT;
3199 case FCMP_OGE: return FCMP_OLE;
3200 case FCMP_OLE: return FCMP_OGE;
3201 case FCMP_UGT: return FCMP_ULT;
3202 case FCMP_ULT: return FCMP_UGT;
3203 case FCMP_UGE: return FCMP_ULE;
3204 case FCMP_ULE: return FCMP_UGE;
3205 }
3206}
3207
Reid Spencer266e42b2006-12-23 06:05:41 +00003208bool CmpInst::isUnsigned(unsigned short predicate) {
3209 switch (predicate) {
3210 default: return false;
3211 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3212 case ICmpInst::ICMP_UGE: return true;
3213 }
3214}
3215
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003216bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003217 switch (predicate) {
3218 default: return false;
3219 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3220 case ICmpInst::ICMP_SGE: return true;
3221 }
3222}
3223
3224bool CmpInst::isOrdered(unsigned short predicate) {
3225 switch (predicate) {
3226 default: return false;
3227 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3228 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3229 case FCmpInst::FCMP_ORD: return true;
3230 }
3231}
3232
3233bool CmpInst::isUnordered(unsigned short predicate) {
3234 switch (predicate) {
3235 default: return false;
3236 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3237 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3238 case FCmpInst::FCMP_UNO: return true;
3239 }
3240}
3241
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003242bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3243 switch(predicate) {
3244 default: return false;
3245 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3246 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3247 }
3248}
3249
3250bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3251 switch(predicate) {
3252 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3253 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3254 default: return false;
3255 }
3256}
3257
3258
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003259//===----------------------------------------------------------------------===//
3260// SwitchInst Implementation
3261//===----------------------------------------------------------------------===//
3262
Chris Lattnerbaf00152010-11-17 05:41:46 +00003263void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3264 assert(Value && Default && NumReserved);
3265 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003266 NumOperands = 2;
Pete Cooper3fc30402015-06-10 22:38:46 +00003267 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003268
Pete Coopereb31b682015-05-21 22:48:54 +00003269 Op<0>() = Value;
3270 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003271}
3272
Chris Lattner2195fc42007-02-24 00:55:48 +00003273/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3274/// switch on and a default destination. The number of additional cases can
3275/// be specified here to make memory allocation more efficient. This
3276/// constructor can also autoinsert before another instruction.
3277SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3278 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003279 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003280 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003281 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003282}
3283
3284/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3285/// switch on and a default destination. The number of additional cases can
3286/// be specified here to make memory allocation more efficient. This
3287/// constructor also autoinserts at the end of the specified BasicBlock.
3288SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3289 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003290 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003291 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003292 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003293}
3294
Misha Brukmanb1c93172005-04-21 23:48:37 +00003295SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003296 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003297 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3298 NumOperands = SI.getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003299 Use *OL = getOperandList();
3300 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003301 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003302 OL[i] = InOL[i];
3303 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003304 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003305 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003306}
3307
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003308
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003309/// addCase - Add an entry to the switch instruction...
3310///
Chris Lattner47ac1872005-02-24 05:32:09 +00003311void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003312 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003313 unsigned OpNo = NumOperands;
3314 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003315 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003316 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003317 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003318 NumOperands = OpNo+2;
Bob Wilsone4077362013-09-09 19:14:35 +00003319 CaseIt Case(this, NewCaseIdx);
3320 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003321 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003322}
3323
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003324/// removeCase - This method removes the specified case and its successor
3325/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003326void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003327 unsigned idx = i.getCaseIndex();
3328
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003329 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003330
3331 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003332 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003333
Jay Foad14277722011-02-01 09:22:34 +00003334 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003335 if (2 + (idx + 1) * 2 != NumOps) {
3336 OL[2 + idx * 2] = OL[NumOps - 2];
3337 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003338 }
3339
3340 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003341 OL[NumOps-2].set(nullptr);
3342 OL[NumOps-2+1].set(nullptr);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003343 NumOperands = NumOps-2;
3344}
3345
Jay Foade98f29d2011-04-01 08:00:58 +00003346/// growOperands - grow operands - This grows the operand list in response
3347/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003348///
Jay Foade98f29d2011-04-01 08:00:58 +00003349void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003350 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003351 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003352
3353 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003354 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003355}
3356
3357
3358BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3359 return getSuccessor(idx);
3360}
3361unsigned SwitchInst::getNumSuccessorsV() const {
3362 return getNumSuccessors();
3363}
3364void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3365 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003366}
Chris Lattnerf22be932004-10-15 23:52:53 +00003367
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003369// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003370//===----------------------------------------------------------------------===//
3371
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003372void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003373 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003374 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003375 ReservedSpace = 1+NumDests;
3376 NumOperands = 1;
Pete Cooper3fc30402015-06-10 22:38:46 +00003377 allocHungoffUses(ReservedSpace);
3378
Pete Coopereb31b682015-05-21 22:48:54 +00003379 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003380}
3381
3382
Jay Foade98f29d2011-04-01 08:00:58 +00003383/// growOperands - grow operands - This grows the operand list in response
3384/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003385///
Jay Foade98f29d2011-04-01 08:00:58 +00003386void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003387 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003388 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003389
3390 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003391 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003392}
3393
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003394IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3395 Instruction *InsertBefore)
3396: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003397 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003398 init(Address, NumCases);
3399}
3400
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003401IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3402 BasicBlock *InsertAtEnd)
3403: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003404 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003405 init(Address, NumCases);
3406}
3407
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003408IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003409 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3410 nullptr, IBI.getNumOperands()) {
3411 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003412 Use *OL = getOperandList();
3413 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003414 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3415 OL[i] = InOL[i];
3416 SubclassOptionalData = IBI.SubclassOptionalData;
3417}
3418
Chris Lattner3ed871f2009-10-27 19:13:16 +00003419/// addDestination - Add a destination.
3420///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003421void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003422 unsigned OpNo = NumOperands;
3423 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003424 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003425 // Initialize some new operands.
3426 assert(OpNo < ReservedSpace && "Growing didn't work!");
3427 NumOperands = OpNo+1;
Pete Cooper74510a42015-06-12 17:48:05 +00003428 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003429}
3430
3431/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003432/// indirectbr instruction.
3433void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003434 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3435
3436 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003437 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003438
3439 // Replace this value with the last one.
3440 OL[idx+1] = OL[NumOps-1];
3441
3442 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003443 OL[NumOps-1].set(nullptr);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003444 NumOperands = NumOps-1;
3445}
3446
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003447BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003448 return getSuccessor(idx);
3449}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003450unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003451 return getNumSuccessors();
3452}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003453void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003454 setSuccessor(idx, B);
3455}
3456
3457//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003458// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003459//===----------------------------------------------------------------------===//
3460
Chris Lattnerf22be932004-10-15 23:52:53 +00003461// Define these methods here so vtables don't get emitted into every translation
3462// unit that uses these classes.
3463
Devang Patel11cf3f42009-10-27 22:16:29 +00003464GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3465 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003466}
3467
Devang Patel11cf3f42009-10-27 22:16:29 +00003468BinaryOperator *BinaryOperator::clone_impl() const {
3469 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003470}
3471
Devang Patel11cf3f42009-10-27 22:16:29 +00003472FCmpInst* FCmpInst::clone_impl() const {
3473 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003474}
3475
Devang Patel11cf3f42009-10-27 22:16:29 +00003476ICmpInst* ICmpInst::clone_impl() const {
3477 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003478}
3479
Devang Patel11cf3f42009-10-27 22:16:29 +00003480ExtractValueInst *ExtractValueInst::clone_impl() const {
3481 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003482}
3483
Devang Patel11cf3f42009-10-27 22:16:29 +00003484InsertValueInst *InsertValueInst::clone_impl() const {
3485 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003486}
3487
Devang Patel11cf3f42009-10-27 22:16:29 +00003488AllocaInst *AllocaInst::clone_impl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003489 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3490 (Value *)getOperand(0), getAlignment());
3491 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3492 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003493}
3494
Devang Patel11cf3f42009-10-27 22:16:29 +00003495LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003496 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3497 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003498}
3499
Devang Patel11cf3f42009-10-27 22:16:29 +00003500StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003501 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003502 getAlignment(), getOrdering(), getSynchScope());
3503
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003504}
3505
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003506AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3507 AtomicCmpXchgInst *Result =
3508 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003509 getSuccessOrdering(), getFailureOrdering(),
3510 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003511 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003512 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003513 return Result;
3514}
3515
3516AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3517 AtomicRMWInst *Result =
3518 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3519 getOrdering(), getSynchScope());
3520 Result->setVolatile(isVolatile());
3521 return Result;
3522}
3523
Eli Friedmanfee02c62011-07-25 23:16:38 +00003524FenceInst *FenceInst::clone_impl() const {
3525 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3526}
3527
Devang Patel11cf3f42009-10-27 22:16:29 +00003528TruncInst *TruncInst::clone_impl() const {
3529 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003530}
3531
Devang Patel11cf3f42009-10-27 22:16:29 +00003532ZExtInst *ZExtInst::clone_impl() const {
3533 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003534}
3535
Devang Patel11cf3f42009-10-27 22:16:29 +00003536SExtInst *SExtInst::clone_impl() const {
3537 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003538}
3539
Devang Patel11cf3f42009-10-27 22:16:29 +00003540FPTruncInst *FPTruncInst::clone_impl() const {
3541 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003542}
3543
Devang Patel11cf3f42009-10-27 22:16:29 +00003544FPExtInst *FPExtInst::clone_impl() const {
3545 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003546}
3547
Devang Patel11cf3f42009-10-27 22:16:29 +00003548UIToFPInst *UIToFPInst::clone_impl() const {
3549 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003550}
3551
Devang Patel11cf3f42009-10-27 22:16:29 +00003552SIToFPInst *SIToFPInst::clone_impl() const {
3553 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003554}
3555
Devang Patel11cf3f42009-10-27 22:16:29 +00003556FPToUIInst *FPToUIInst::clone_impl() const {
3557 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003558}
3559
Devang Patel11cf3f42009-10-27 22:16:29 +00003560FPToSIInst *FPToSIInst::clone_impl() const {
3561 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003562}
3563
Devang Patel11cf3f42009-10-27 22:16:29 +00003564PtrToIntInst *PtrToIntInst::clone_impl() const {
3565 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003566}
3567
Devang Patel11cf3f42009-10-27 22:16:29 +00003568IntToPtrInst *IntToPtrInst::clone_impl() const {
3569 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003570}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003571
Devang Patel11cf3f42009-10-27 22:16:29 +00003572BitCastInst *BitCastInst::clone_impl() const {
3573 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003574}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003575
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003576AddrSpaceCastInst *AddrSpaceCastInst::clone_impl() const {
3577 return new AddrSpaceCastInst(getOperand(0), getType());
3578}
3579
Devang Patel11cf3f42009-10-27 22:16:29 +00003580CallInst *CallInst::clone_impl() const {
3581 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003582}
3583
Devang Patel11cf3f42009-10-27 22:16:29 +00003584SelectInst *SelectInst::clone_impl() const {
3585 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003586}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003587
Devang Patel11cf3f42009-10-27 22:16:29 +00003588VAArgInst *VAArgInst::clone_impl() const {
3589 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003590}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003591
Devang Patel11cf3f42009-10-27 22:16:29 +00003592ExtractElementInst *ExtractElementInst::clone_impl() const {
3593 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003594}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003595
Devang Patel11cf3f42009-10-27 22:16:29 +00003596InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003597 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003598}
3599
Devang Patel11cf3f42009-10-27 22:16:29 +00003600ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003601 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003602}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003603
Devang Patel11cf3f42009-10-27 22:16:29 +00003604PHINode *PHINode::clone_impl() const {
3605 return new PHINode(*this);
3606}
3607
Bill Wendlingfae14752011-08-12 20:24:12 +00003608LandingPadInst *LandingPadInst::clone_impl() const {
3609 return new LandingPadInst(*this);
3610}
3611
Devang Patel11cf3f42009-10-27 22:16:29 +00003612ReturnInst *ReturnInst::clone_impl() const {
3613 return new(getNumOperands()) ReturnInst(*this);
3614}
3615
3616BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003617 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003618}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003619
Devang Patel11cf3f42009-10-27 22:16:29 +00003620SwitchInst *SwitchInst::clone_impl() const {
3621 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003622}
3623
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003624IndirectBrInst *IndirectBrInst::clone_impl() const {
3625 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003626}
3627
3628
Devang Patel11cf3f42009-10-27 22:16:29 +00003629InvokeInst *InvokeInst::clone_impl() const {
3630 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003631}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003632
Bill Wendlingf891bf82011-07-31 06:30:59 +00003633ResumeInst *ResumeInst::clone_impl() const {
3634 return new(1) ResumeInst(*this);
3635}
3636
Devang Patel11cf3f42009-10-27 22:16:29 +00003637UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003638 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003639 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003640}