blob: 73b966f808edd351c98c2bd8e718af223a72403d [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());
Bill Wendlingfae14752011-08-12 20:24:12 +0000175 Use *OL = OperandList, *InOL = LP.OperandList;
176 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
177 OL[I] = InOL[I];
178
179 setCleanup(LP.isCleanup());
180}
181
Bill Wendlingfae14752011-08-12 20:24:12 +0000182LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
183 unsigned NumReservedClauses,
184 const Twine &NameStr,
185 Instruction *InsertBefore) {
186 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
187 InsertBefore);
188}
189
190LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
191 unsigned NumReservedClauses,
192 const Twine &NameStr,
193 BasicBlock *InsertAtEnd) {
194 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
195 InsertAtEnd);
196}
197
198void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
199 const Twine &NameStr) {
200 ReservedSpace = NumReservedValues;
201 NumOperands = 1;
Pete Cooper3fc30402015-06-10 22:38:46 +0000202 allocHungoffUses(ReservedSpace);
Pete Coopereb31b682015-05-21 22:48:54 +0000203 Op<0>() = PersFn;
Bill Wendlingfae14752011-08-12 20:24:12 +0000204 setName(NameStr);
205 setCleanup(false);
206}
207
208/// growOperands - grow operands - This grows the operand list in response to a
209/// push_back style of operation. This grows the number of ops by 2 times.
210void LandingPadInst::growOperands(unsigned Size) {
211 unsigned e = getNumOperands();
212 if (ReservedSpace >= e + Size) return;
213 ReservedSpace = (e + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000214 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000215}
216
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000217void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000218 unsigned OpNo = getNumOperands();
219 growOperands(1);
220 assert(OpNo < ReservedSpace && "Growing didn't work!");
221 ++NumOperands;
222 OperandList[OpNo] = Val;
223}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224
225//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226// CallInst Implementation
227//===----------------------------------------------------------------------===//
228
Gordon Henriksen14a55692007-12-10 02:14:30 +0000229CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230}
231
David Blaikie348de692015-04-23 21:36:23 +0000232void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
233 const Twine &NameStr) {
234 this->FTy = FTy;
Jay Foad5bd375a2011-07-15 08:37:34 +0000235 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000236 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237
Jay Foad5bd375a2011-07-15 08:37:34 +0000238#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000239 assert((Args.size() == FTy->getNumParams() ||
240 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000241 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000242
243 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000244 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000245 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000246 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000247#endif
248
249 std::copy(Args.begin(), Args.end(), op_begin());
250 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251}
252
Jay Foad5bd375a2011-07-15 08:37:34 +0000253void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000254 FTy =
255 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Gabor Greiff6caff662008-05-10 08:32:32 +0000256 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000257 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000260
261 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262}
263
Daniel Dunbar4975db62009-07-25 04:41:11 +0000264CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 Instruction *InsertBefore)
266 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
267 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000268 Instruction::Call,
269 OperandTraits<CallInst>::op_end(this) - 1,
270 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000271 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Daniel Dunbar4975db62009-07-25 04:41:11 +0000274CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275 BasicBlock *InsertAtEnd)
276 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
277 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000278 Instruction::Call,
279 OperandTraits<CallInst>::op_end(this) - 1,
280 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000281 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282}
283
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000285 : Instruction(CI.getType(), Instruction::Call,
286 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
287 CI.getNumOperands()),
288 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000289 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000290 setCallingConv(CI.getCallingConv());
291
Jay Foad5bd375a2011-07-15 08:37:34 +0000292 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000293 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000296void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000297 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000298 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000299 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000300}
301
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000302void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000303 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000304 AttrBuilder B(attr);
305 LLVMContext &Context = getContext();
306 PAL = PAL.removeAttributes(Context, i,
307 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000308 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000309}
310
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000311void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
312 AttributeSet PAL = getAttributes();
313 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
314 setAttributes(PAL);
315}
316
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000317void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
318 AttributeSet PAL = getAttributes();
319 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
320 setAttributes(PAL);
321}
322
Michael Gottesman41748d72013-06-27 00:25:01 +0000323bool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000324 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000325 return true;
326 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000327 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000328 return false;
329}
330
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000331bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000332 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000333 return true;
334 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000335 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000336 return false;
337}
338
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000339/// IsConstantOne - Return true only if val is constant int 1
340static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000341 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000342 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
343 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000344}
345
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000346static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000347 BasicBlock *InsertAtEnd, Type *IntPtrTy,
348 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000349 Value *ArraySize, Function *MallocF,
350 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000351 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000352 "createMalloc needs either InsertBefore or InsertAtEnd");
353
354 // malloc(type) becomes:
355 // bitcast (i8* malloc(typeSize)) to type*
356 // malloc(type, arraySize) becomes:
357 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000358 if (!ArraySize)
359 ArraySize = ConstantInt::get(IntPtrTy, 1);
360 else if (ArraySize->getType() != IntPtrTy) {
361 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000362 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
363 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000364 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000365 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
366 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000367 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000368
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000369 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000370 if (IsConstantOne(AllocSize)) {
371 AllocSize = ArraySize; // Operand * 1 = Operand
372 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
373 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
374 false /*ZExt*/);
375 // Malloc arg is constant product of type size and array size
376 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
377 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 // Multiply type size by the array size...
379 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000380 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
381 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000382 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000383 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
384 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000385 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000386 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387
Victor Hernandez788eaab2009-09-18 19:20:02 +0000388 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389 // Create the call to Malloc.
390 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
391 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000392 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000393 Value *MallocFunc = MallocF;
394 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000395 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000396 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000397 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000398 CallInst *MCall = nullptr;
399 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000400 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000401 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 Result = MCall;
403 if (Result->getType() != AllocPtrType)
404 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000406 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000407 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000408 Result = MCall;
409 if (Result->getType() != AllocPtrType) {
410 InsertAtEnd->getInstList().push_back(MCall);
411 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000412 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000413 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000414 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000415 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000416 if (Function *F = dyn_cast<Function>(MallocFunc)) {
417 MCall->setCallingConv(F->getCallingConv());
418 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
419 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000420 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000422 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423}
424
425/// CreateMalloc - Generate the IR for a call to malloc:
426/// 1. Compute the malloc call's argument as the specified type's size,
427/// possibly multiplied by the array size if the array size is not
428/// constant 1.
429/// 2. Call malloc with that argument.
430/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000431Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000432 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000433 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000434 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000435 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000436 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000437 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438}
439
440/// CreateMalloc - Generate the IR for a call to malloc:
441/// 1. Compute the malloc call's argument as the specified type's size,
442/// possibly multiplied by the array size if the array size is not
443/// constant 1.
444/// 2. Call malloc with that argument.
445/// 3. Bitcast the result of the malloc call to the specified type.
446/// Note: This function does not add the bitcast to the basic block, that is the
447/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000448Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000449 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000450 Value *AllocSize, Value *ArraySize,
451 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000452 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000453 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000454}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000455
Victor Hernandeze2971492009-10-24 04:23:03 +0000456static Instruction* createFree(Value* Source, Instruction *InsertBefore,
457 BasicBlock *InsertAtEnd) {
458 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
459 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000460 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000461 "Can not free something of nonpointer type!");
462
463 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
464 Module* M = BB->getParent()->getParent();
465
Chris Lattner229907c2011-07-18 04:54:35 +0000466 Type *VoidTy = Type::getVoidTy(M->getContext());
467 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000468 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000469 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000470 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000471 Value *PtrCast = Source;
472 if (InsertBefore) {
473 if (Source->getType() != IntPtrTy)
474 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
475 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
476 } else {
477 if (Source->getType() != IntPtrTy)
478 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
479 Result = CallInst::Create(FreeFunc, PtrCast, "");
480 }
481 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000482 if (Function *F = dyn_cast<Function>(FreeFunc))
483 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000484
485 return Result;
486}
487
488/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000489Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000490 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000491}
492
493/// CreateFree - Generate the IR for a call to the builtin free function.
494/// Note: This function does not add the call to the basic block, that is the
495/// responsibility of the caller.
496Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000497 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000498 assert(FreeCall && "CreateFree did not create a CallInst");
499 return FreeCall;
500}
501
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502//===----------------------------------------------------------------------===//
503// InvokeInst Implementation
504//===----------------------------------------------------------------------===//
505
David Blaikie3e807092015-05-13 18:35:26 +0000506void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
507 BasicBlock *IfException, ArrayRef<Value *> Args,
508 const Twine &NameStr) {
509 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000510
Jay Foad5bd375a2011-07-15 08:37:34 +0000511 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000512 Op<-3>() = Fn;
513 Op<-2>() = IfNormal;
514 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000515
516#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000517 assert(((Args.size() == FTy->getNumParams()) ||
518 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000519 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000520
Jay Foad5bd375a2011-07-15 08:37:34 +0000521 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000522 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000523 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000524 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000525#endif
526
527 std::copy(Args.begin(), Args.end(), op_begin());
528 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000529}
530
Misha Brukmanb1c93172005-04-21 23:48:37 +0000531InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000532 : TerminatorInst(II.getType(), Instruction::Invoke,
533 OperandTraits<InvokeInst>::op_end(this) -
534 II.getNumOperands(),
535 II.getNumOperands()),
536 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000537 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000538 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000539 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540}
541
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
543 return getSuccessor(idx);
544}
545unsigned InvokeInst::getNumSuccessorsV() const {
546 return getNumSuccessors();
547}
548void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
549 return setSuccessor(idx, B);
550}
551
Michael Gottesman41748d72013-06-27 00:25:01 +0000552bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000553 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000554 return true;
555 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000556 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000557 return false;
558}
559
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000560bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000561 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000562 return true;
563 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000564 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000565 return false;
566}
567
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000568void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000569 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000570 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000571 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000572}
573
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000574void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000575 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000576 AttrBuilder B(attr);
577 PAL = PAL.removeAttributes(getContext(), i,
578 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000579 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000580}
581
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000582void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
583 AttributeSet PAL = getAttributes();
584 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
585 setAttributes(PAL);
586}
587
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000588void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
589 AttributeSet PAL = getAttributes();
590 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
591 setAttributes(PAL);
592}
593
Bill Wendlingfae14752011-08-12 20:24:12 +0000594LandingPadInst *InvokeInst::getLandingPadInst() const {
595 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
596}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000597
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000598//===----------------------------------------------------------------------===//
599// ReturnInst Implementation
600//===----------------------------------------------------------------------===//
601
Chris Lattner2195fc42007-02-24 00:55:48 +0000602ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000603 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000604 OperandTraits<ReturnInst>::op_end(this) -
605 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000606 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000607 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000608 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000609 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000610}
611
Owen Anderson55f1c092009-08-13 21:58:54 +0000612ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
613 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000614 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
615 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000616 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000617 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000618}
Owen Anderson55f1c092009-08-13 21:58:54 +0000619ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
620 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
622 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000623 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000624 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
Owen Anderson55f1c092009-08-13 21:58:54 +0000626ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
627 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000629}
630
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631unsigned ReturnInst::getNumSuccessorsV() const {
632 return getNumSuccessors();
633}
634
Devang Patelae682fb2008-02-26 17:56:20 +0000635/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
636/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000638 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639}
640
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000642 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643}
644
Devang Patel59643e52008-02-23 00:35:18 +0000645ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000646}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000649// ResumeInst Implementation
650//===----------------------------------------------------------------------===//
651
652ResumeInst::ResumeInst(const ResumeInst &RI)
653 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
654 OperandTraits<ResumeInst>::op_begin(this), 1) {
655 Op<0>() = RI.Op<0>();
656}
657
658ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
659 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
660 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
661 Op<0>() = Exn;
662}
663
664ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
665 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
666 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
667 Op<0>() = Exn;
668}
669
670unsigned ResumeInst::getNumSuccessorsV() const {
671 return getNumSuccessors();
672}
673
674void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
675 llvm_unreachable("ResumeInst has no successors!");
676}
677
678BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
679 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000680}
681
682//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000683// UnreachableInst Implementation
684//===----------------------------------------------------------------------===//
685
Owen Anderson55f1c092009-08-13 21:58:54 +0000686UnreachableInst::UnreachableInst(LLVMContext &Context,
687 Instruction *InsertBefore)
688 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000689 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000690}
Owen Anderson55f1c092009-08-13 21:58:54 +0000691UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
692 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000693 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000694}
695
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696unsigned UnreachableInst::getNumSuccessorsV() const {
697 return getNumSuccessors();
698}
699
700void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000701 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702}
703
704BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000705 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000706}
707
708//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709// BranchInst Implementation
710//===----------------------------------------------------------------------===//
711
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712void BranchInst::AssertOK() {
713 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000714 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716}
717
Chris Lattner2195fc42007-02-24 00:55:48 +0000718BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000719 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000720 OperandTraits<BranchInst>::op_end(this) - 1,
721 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000722 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000723 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000724}
725BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
726 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000727 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000728 OperandTraits<BranchInst>::op_end(this) - 3,
729 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000730 Op<-1>() = IfTrue;
731 Op<-2>() = IfFalse;
732 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000733#ifndef NDEBUG
734 AssertOK();
735#endif
736}
737
738BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000740 OperandTraits<BranchInst>::op_end(this) - 1,
741 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000742 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000743 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000744}
745
746BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
747 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000748 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000749 OperandTraits<BranchInst>::op_end(this) - 3,
750 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000751 Op<-1>() = IfTrue;
752 Op<-2>() = IfFalse;
753 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000754#ifndef NDEBUG
755 AssertOK();
756#endif
757}
758
759
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000761 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000762 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
763 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000764 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765 if (BI.getNumOperands() != 1) {
766 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000767 Op<-3>() = BI.Op<-3>();
768 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000770 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771}
772
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000773void BranchInst::swapSuccessors() {
774 assert(isConditional() &&
775 "Cannot swap successors of an unconditional branch");
776 Op<-1>().swap(Op<-2>());
777
778 // Update profile metadata if present and it matches our structural
779 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000780 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000781 if (!ProfileData || ProfileData->getNumOperands() != 3)
782 return;
783
784 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000785 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
786 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000787 setMetadata(LLVMContext::MD_prof,
788 MDNode::get(ProfileData->getContext(), Ops));
789}
790
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
792 return getSuccessor(idx);
793}
794unsigned BranchInst::getNumSuccessorsV() const {
795 return getNumSuccessors();
796}
797void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
798 setSuccessor(idx, B);
799}
800
801
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000803// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804//===----------------------------------------------------------------------===//
805
Owen Andersonb6b25302009-07-14 23:09:55 +0000806static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000808 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000809 else {
810 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000811 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000812 assert(Amt->getType()->isIntegerTy() &&
813 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000814 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000815 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816}
817
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000818AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
819 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000821AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
822 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000823
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000824AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000825 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000826 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000827
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000828AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000829 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000830 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000831
Chris Lattner229907c2011-07-18 04:54:35 +0000832AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000833 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000834 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
835 getAISize(Ty->getContext(), ArraySize), InsertBefore),
836 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000837 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000838 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000839 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840}
841
Chris Lattner229907c2011-07-18 04:54:35 +0000842AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000844 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
845 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
846 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000847 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000848 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000849 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000850}
851
Gordon Henriksen14a55692007-12-10 02:14:30 +0000852// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000853AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000854}
855
Victor Hernandez8acf2952009-10-23 21:09:37 +0000856void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000857 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000858 assert(Align <= MaximumAlignment &&
859 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +0000860 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
861 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +0000862 assert(getAlignment() == Align && "Alignment representation error!");
863}
864
Victor Hernandez8acf2952009-10-23 21:09:37 +0000865bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000866 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000867 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000868 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000869}
870
Chris Lattner8b291e62008-11-26 02:54:17 +0000871/// isStaticAlloca - Return true if this alloca is in the entry block of the
872/// function and is a constant size. If so, the code generator will fold it
873/// into the prolog/epilog code, so it is basically free.
874bool AllocaInst::isStaticAlloca() const {
875 // Must be constant size.
876 if (!isa<ConstantInt>(getArraySize())) return false;
877
878 // Must be in the entry block.
879 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +0000880 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +0000881}
882
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884// LoadInst Implementation
885//===----------------------------------------------------------------------===//
886
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000888 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000890 assert(!(isAtomic() && getAlignment() == 0) &&
891 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892}
893
Daniel Dunbar4975db62009-07-25 04:41:11 +0000894LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000895 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896
Daniel Dunbar4975db62009-07-25 04:41:11 +0000897LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000898 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899
David Blaikie0c28fd72015-05-20 21:46:30 +0000900LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000902 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +0000903
904LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
905 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000906 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +0000907
David Blaikieb7a029872015-04-17 19:56:21 +0000908LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000909 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +0000910 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000911 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000913LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000914 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000915 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +0000916}
917
David Blaikie15d9a4c2015-04-06 20:59:48 +0000918LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +0000919 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +0000920 SynchronizationScope SynchScope, Instruction *InsertBef)
921 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +0000922 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +0000923 setVolatile(isVolatile);
924 setAlignment(Align);
925 setAtomic(Order, SynchScope);
926 AssertOK();
927 setName(Name);
928}
929
930LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
931 unsigned Align, AtomicOrdering Order,
932 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000935 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000936 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000937 setAlignment(Align);
938 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000939 AssertOK();
940 setName(Name);
941}
942
Daniel Dunbar27096822009-08-11 18:11:15 +0000943LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
944 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
945 Load, Ptr, InsertBef) {
946 setVolatile(false);
947 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000948 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000949 AssertOK();
950 if (Name && Name[0]) setName(Name);
951}
952
953LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
954 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
955 Load, Ptr, InsertAE) {
956 setVolatile(false);
957 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000958 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000959 AssertOK();
960 if (Name && Name[0]) setName(Name);
961}
962
David Blaikie0c28fd72015-05-20 21:46:30 +0000963LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +0000964 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000965 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
966 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +0000967 setVolatile(isVolatile);
968 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000969 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000970 AssertOK();
971 if (Name && Name[0]) setName(Name);
972}
973
974LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
975 BasicBlock *InsertAE)
976 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
977 Load, Ptr, InsertAE) {
978 setVolatile(isVolatile);
979 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000980 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000981 AssertOK();
982 if (Name && Name[0]) setName(Name);
983}
984
Christopher Lamb84485702007-04-22 19:24:39 +0000985void LoadInst::setAlignment(unsigned Align) {
986 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000987 assert(Align <= MaximumAlignment &&
988 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +0000989 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000990 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000991 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000992}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993
994//===----------------------------------------------------------------------===//
995// StoreInst Implementation
996//===----------------------------------------------------------------------===//
997
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000999 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001000 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001001 "Ptr must have pointer type!");
1002 assert(getOperand(0)->getType() ==
1003 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001004 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001005 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001006 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001007}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001009StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001010 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001011
1012StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001013 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014
Misha Brukmanb1c93172005-04-21 23:48:37 +00001015StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001017 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001018
1019StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001020 BasicBlock *InsertAtEnd)
1021 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1022
1023StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1024 Instruction *InsertBefore)
1025 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1026 InsertBefore) {}
1027
1028StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1029 BasicBlock *InsertAtEnd)
1030 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1031 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001034 unsigned Align, AtomicOrdering Order,
1035 SynchronizationScope SynchScope,
1036 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001037 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001038 OperandTraits<StoreInst>::op_begin(this),
1039 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001040 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001041 Op<0>() = val;
1042 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001043 setVolatile(isVolatile);
1044 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001045 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001046 AssertOK();
1047}
1048
1049StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001050 unsigned Align, AtomicOrdering Order,
1051 SynchronizationScope SynchScope,
1052 BasicBlock *InsertAtEnd)
1053 : Instruction(Type::getVoidTy(val->getContext()), Store,
1054 OperandTraits<StoreInst>::op_begin(this),
1055 OperandTraits<StoreInst>::operands(this),
1056 InsertAtEnd) {
1057 Op<0>() = val;
1058 Op<1>() = addr;
1059 setVolatile(isVolatile);
1060 setAlignment(Align);
1061 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063}
1064
Christopher Lamb84485702007-04-22 19:24:39 +00001065void StoreInst::setAlignment(unsigned Align) {
1066 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001067 assert(Align <= MaximumAlignment &&
1068 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001069 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001070 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001071 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001072}
1073
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001074//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001075// AtomicCmpXchgInst Implementation
1076//===----------------------------------------------------------------------===//
1077
1078void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001079 AtomicOrdering SuccessOrdering,
1080 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001081 SynchronizationScope SynchScope) {
1082 Op<0>() = Ptr;
1083 Op<1>() = Cmp;
1084 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001085 setSuccessOrdering(SuccessOrdering);
1086 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001087 setSynchScope(SynchScope);
1088
1089 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1090 "All operands must be non-null!");
1091 assert(getOperand(0)->getType()->isPointerTy() &&
1092 "Ptr must have pointer type!");
1093 assert(getOperand(1)->getType() ==
1094 cast<PointerType>(getOperand(0)->getType())->getElementType()
1095 && "Ptr must be a pointer to Cmp type!");
1096 assert(getOperand(2)->getType() ==
1097 cast<PointerType>(getOperand(0)->getType())->getElementType()
1098 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001099 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001100 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001101 assert(FailureOrdering != NotAtomic &&
1102 "AtomicCmpXchg instructions must be atomic!");
1103 assert(SuccessOrdering >= FailureOrdering &&
1104 "AtomicCmpXchg success ordering must be at least as strong as fail");
1105 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1106 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001107}
1108
1109AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001110 AtomicOrdering SuccessOrdering,
1111 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001112 SynchronizationScope SynchScope,
1113 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001114 : Instruction(
1115 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1116 nullptr),
1117 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1118 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001119 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001120}
1121
1122AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001123 AtomicOrdering SuccessOrdering,
1124 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001125 SynchronizationScope SynchScope,
1126 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001127 : Instruction(
1128 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1129 nullptr),
1130 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1131 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001132 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001133}
Tim Northover420a2162014-06-13 14:24:07 +00001134
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001135//===----------------------------------------------------------------------===//
1136// AtomicRMWInst Implementation
1137//===----------------------------------------------------------------------===//
1138
1139void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1140 AtomicOrdering Ordering,
1141 SynchronizationScope SynchScope) {
1142 Op<0>() = Ptr;
1143 Op<1>() = Val;
1144 setOperation(Operation);
1145 setOrdering(Ordering);
1146 setSynchScope(SynchScope);
1147
1148 assert(getOperand(0) && getOperand(1) &&
1149 "All operands must be non-null!");
1150 assert(getOperand(0)->getType()->isPointerTy() &&
1151 "Ptr must have pointer type!");
1152 assert(getOperand(1)->getType() ==
1153 cast<PointerType>(getOperand(0)->getType())->getElementType()
1154 && "Ptr must be a pointer to Val type!");
1155 assert(Ordering != NotAtomic &&
1156 "AtomicRMW instructions must be atomic!");
1157}
1158
1159AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1160 AtomicOrdering Ordering,
1161 SynchronizationScope SynchScope,
1162 Instruction *InsertBefore)
1163 : Instruction(Val->getType(), AtomicRMW,
1164 OperandTraits<AtomicRMWInst>::op_begin(this),
1165 OperandTraits<AtomicRMWInst>::operands(this),
1166 InsertBefore) {
1167 Init(Operation, Ptr, Val, Ordering, SynchScope);
1168}
1169
1170AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1171 AtomicOrdering Ordering,
1172 SynchronizationScope SynchScope,
1173 BasicBlock *InsertAtEnd)
1174 : Instruction(Val->getType(), AtomicRMW,
1175 OperandTraits<AtomicRMWInst>::op_begin(this),
1176 OperandTraits<AtomicRMWInst>::operands(this),
1177 InsertAtEnd) {
1178 Init(Operation, Ptr, Val, Ordering, SynchScope);
1179}
1180
1181//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001182// FenceInst Implementation
1183//===----------------------------------------------------------------------===//
1184
1185FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1186 SynchronizationScope SynchScope,
1187 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001188 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001189 setOrdering(Ordering);
1190 setSynchScope(SynchScope);
1191}
1192
1193FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1194 SynchronizationScope SynchScope,
1195 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001196 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001197 setOrdering(Ordering);
1198 setSynchScope(SynchScope);
1199}
1200
1201//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202// GetElementPtrInst Implementation
1203//===----------------------------------------------------------------------===//
1204
Jay Foadd1b78492011-07-25 09:48:08 +00001205void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001206 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001207 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001208 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001209 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001210 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001211}
1212
Gabor Greiff6caff662008-05-10 08:32:32 +00001213GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001214 : Instruction(GEPI.getType(), GetElementPtr,
1215 OperandTraits<GetElementPtrInst>::op_end(this) -
1216 GEPI.getNumOperands(),
1217 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001218 SourceElementType(GEPI.SourceElementType),
1219 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001220 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001221 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001222}
1223
Chris Lattner6090a422009-03-09 04:46:40 +00001224/// getIndexedType - Returns the type of the element that would be accessed with
1225/// a gep instruction with the specified parameters.
1226///
1227/// The Idxs pointer should point to a continuous piece of memory containing the
1228/// indices, either as Value* or uint64_t.
1229///
1230/// A null type is returned if the indices are invalid for the specified
1231/// pointer type.
1232///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001233template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001234static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001235 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001236 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001237 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001238
Chris Lattner6090a422009-03-09 04:46:40 +00001239 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001240 // it cannot be 'stepped over'.
1241 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001242 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001243
Dan Gohman1ecaf452008-05-31 00:58:22 +00001244 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001245 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001246 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001247 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001248 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001249 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001250 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001251 }
Craig Topperc6207612014-04-09 06:08:46 +00001252 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001253}
1254
David Blaikied288fb82015-03-30 21:41:43 +00001255Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1256 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001257}
1258
David Blaikied288fb82015-03-30 21:41:43 +00001259Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001260 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001261 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001262}
1263
David Blaikied288fb82015-03-30 21:41:43 +00001264Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1265 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001266}
1267
Chris Lattner45f15572007-04-14 00:12:57 +00001268/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1269/// zeros. If so, the result pointer and the first operand have the same
1270/// value, just potentially different types.
1271bool GetElementPtrInst::hasAllZeroIndices() const {
1272 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1273 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1274 if (!CI->isZero()) return false;
1275 } else {
1276 return false;
1277 }
1278 }
1279 return true;
1280}
1281
Chris Lattner27058292007-04-27 20:35:56 +00001282/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1283/// constant integers. If so, the result pointer and the first operand have
1284/// a constant offset between them.
1285bool GetElementPtrInst::hasAllConstantIndices() const {
1286 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1287 if (!isa<ConstantInt>(getOperand(i)))
1288 return false;
1289 }
1290 return true;
1291}
1292
Dan Gohman1b849082009-09-07 23:54:19 +00001293void GetElementPtrInst::setIsInBounds(bool B) {
1294 cast<GEPOperator>(this)->setIsInBounds(B);
1295}
Chris Lattner45f15572007-04-14 00:12:57 +00001296
Nick Lewycky28a5f252009-09-27 21:33:04 +00001297bool GetElementPtrInst::isInBounds() const {
1298 return cast<GEPOperator>(this)->isInBounds();
1299}
1300
Chandler Carruth1e140532012-12-11 10:29:10 +00001301bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1302 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001303 // Delegate to the generic GEPOperator implementation.
1304 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001305}
1306
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001307//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001308// ExtractElementInst Implementation
1309//===----------------------------------------------------------------------===//
1310
1311ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001312 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001314 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001315 ExtractElement,
1316 OperandTraits<ExtractElementInst>::op_begin(this),
1317 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001318 assert(isValidOperands(Val, Index) &&
1319 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001320 Op<0>() = Val;
1321 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001322 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001323}
1324
1325ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001326 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001327 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001328 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001329 ExtractElement,
1330 OperandTraits<ExtractElementInst>::op_begin(this),
1331 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001332 assert(isValidOperands(Val, Index) &&
1333 "Invalid extractelement instruction operands!");
1334
Gabor Greif2d3024d2008-05-26 21:33:52 +00001335 Op<0>() = Val;
1336 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001337 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001338}
1339
Chris Lattner65511ff2006-10-05 06:24:58 +00001340
Chris Lattner54865b32006-04-08 04:05:48 +00001341bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001342 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001343 return false;
1344 return true;
1345}
1346
1347
Robert Bocchino23004482006-01-10 19:05:34 +00001348//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001349// InsertElementInst Implementation
1350//===----------------------------------------------------------------------===//
1351
Chris Lattner54865b32006-04-08 04:05:48 +00001352InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001353 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001355 : Instruction(Vec->getType(), InsertElement,
1356 OperandTraits<InsertElementInst>::op_begin(this),
1357 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001358 assert(isValidOperands(Vec, Elt, Index) &&
1359 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001360 Op<0>() = Vec;
1361 Op<1>() = Elt;
1362 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001363 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001364}
1365
Chris Lattner54865b32006-04-08 04:05:48 +00001366InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001367 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001368 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001369 : Instruction(Vec->getType(), InsertElement,
1370 OperandTraits<InsertElementInst>::op_begin(this),
1371 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001372 assert(isValidOperands(Vec, Elt, Index) &&
1373 "Invalid insertelement instruction operands!");
1374
Gabor Greif2d3024d2008-05-26 21:33:52 +00001375 Op<0>() = Vec;
1376 Op<1>() = Elt;
1377 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001378 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001379}
1380
Chris Lattner54865b32006-04-08 04:05:48 +00001381bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1382 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001383 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001384 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001385
Reid Spencerd84d35b2007-02-15 02:26:10 +00001386 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001387 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001388
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001389 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001390 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001391 return true;
1392}
1393
1394
Robert Bocchinoca27f032006-01-17 20:07:22 +00001395//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001396// ShuffleVectorInst Implementation
1397//===----------------------------------------------------------------------===//
1398
1399ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001400 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001402: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001403 cast<VectorType>(Mask->getType())->getNumElements()),
1404 ShuffleVector,
1405 OperandTraits<ShuffleVectorInst>::op_begin(this),
1406 OperandTraits<ShuffleVectorInst>::operands(this),
1407 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001408 assert(isValidOperands(V1, V2, Mask) &&
1409 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001410 Op<0>() = V1;
1411 Op<1>() = V2;
1412 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001413 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001414}
1415
1416ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001417 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001419: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1420 cast<VectorType>(Mask->getType())->getNumElements()),
1421 ShuffleVector,
1422 OperandTraits<ShuffleVectorInst>::op_begin(this),
1423 OperandTraits<ShuffleVectorInst>::operands(this),
1424 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001425 assert(isValidOperands(V1, V2, Mask) &&
1426 "Invalid shuffle vector instruction operands!");
1427
Gabor Greif2d3024d2008-05-26 21:33:52 +00001428 Op<0>() = V1;
1429 Op<1>() = V2;
1430 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001431 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001432}
1433
Mon P Wang25f01062008-11-10 04:46:22 +00001434bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001435 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001436 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001437 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001438 return false;
1439
Chris Lattner1dcb6542012-01-25 23:49:49 +00001440 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001441 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001442 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001443 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001444
1445 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001446 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1447 return true;
1448
Nate Begeman60a31c32010-08-13 00:16:46 +00001449 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001450 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001451 for (Value *Op : MV->operands()) {
1452 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001453 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001454 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001455 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001456 return false;
1457 }
1458 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001459 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001460 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001461
1462 if (const ConstantDataSequential *CDS =
1463 dyn_cast<ConstantDataSequential>(Mask)) {
1464 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1465 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1466 if (CDS->getElementAsInteger(i) >= V1Size*2)
1467 return false;
1468 return true;
1469 }
1470
1471 // The bitcode reader can create a place holder for a forward reference
1472 // used as the shuffle mask. When this occurs, the shuffle mask will
1473 // fall into this case and fail. To avoid this error, do this bit of
1474 // ugliness to allow such a mask pass.
1475 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1476 if (CE->getOpcode() == Instruction::UserOp1)
1477 return true;
1478
1479 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001480}
1481
Chris Lattnerf724e342008-03-02 05:28:33 +00001482/// getMaskValue - Return the index from the shuffle mask for the specified
1483/// output result. This is either -1 if the element is undef or a number less
1484/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001485int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1486 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1487 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001488 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001489 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001490 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001491 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001492 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001493}
1494
Chris Lattner1dcb6542012-01-25 23:49:49 +00001495/// getShuffleMask - Return the full mask for this instruction, where each
1496/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001497void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1498 SmallVectorImpl<int> &Result) {
1499 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001500
Chris Lattnercf129702012-01-26 02:51:13 +00001501 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001502 for (unsigned i = 0; i != NumElts; ++i)
1503 Result.push_back(CDS->getElementAsInteger(i));
1504 return;
1505 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001506 for (unsigned i = 0; i != NumElts; ++i) {
1507 Constant *C = Mask->getAggregateElement(i);
1508 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001509 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001510 }
1511}
1512
1513
Dan Gohman12fce772008-05-15 19:50:34 +00001514//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001515// InsertValueInst Class
1516//===----------------------------------------------------------------------===//
1517
Jay Foad57aa6362011-07-13 10:26:04 +00001518void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1519 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001520 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001521
1522 // There's no fundamental reason why we require at least one index
1523 // (other than weirdness with &*IdxBegin being invalid; see
1524 // getelementptr's init routine for example). But there's no
1525 // present need to support it.
1526 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1527
1528 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001529 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 Op<0>() = Agg;
1531 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001532
Jay Foad57aa6362011-07-13 10:26:04 +00001533 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001534 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001535}
1536
1537InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001538 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001539 OperandTraits<InsertValueInst>::op_begin(this), 2),
1540 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001541 Op<0>() = IVI.getOperand(0);
1542 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001543 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001544}
1545
1546//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001547// ExtractValueInst Class
1548//===----------------------------------------------------------------------===//
1549
Jay Foad57aa6362011-07-13 10:26:04 +00001550void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001551 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001552
Jay Foad57aa6362011-07-13 10:26:04 +00001553 // There's no fundamental reason why we require at least one index.
1554 // But there's no present need to support it.
1555 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001556
Jay Foad57aa6362011-07-13 10:26:04 +00001557 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001558 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001559}
1560
1561ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001562 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001563 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001564 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001565}
1566
Dan Gohman12fce772008-05-15 19:50:34 +00001567// getIndexedType - Returns the type of the element that would be extracted
1568// with an extractvalue instruction with the specified parameters.
1569//
1570// A null type is returned if the indices are invalid for the specified
1571// pointer type.
1572//
Chris Lattner229907c2011-07-18 04:54:35 +00001573Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001574 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001575 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001576 // We can't use CompositeType::indexValid(Index) here.
1577 // indexValid() always returns true for arrays because getelementptr allows
1578 // out-of-bounds indices. Since we don't allow those for extractvalue and
1579 // insertvalue we need to check array indexing manually.
1580 // Since the only other types we can index into are struct types it's just
1581 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001582 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001583 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001584 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001585 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001586 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001587 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001588 } else {
1589 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001590 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001591 }
1592
1593 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001594 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001595 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001596}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001597
1598//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001599// BinaryOperator Class
1600//===----------------------------------------------------------------------===//
1601
Chris Lattner2195fc42007-02-24 00:55:48 +00001602BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001603 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001604 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001605 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001606 OperandTraits<BinaryOperator>::op_begin(this),
1607 OperandTraits<BinaryOperator>::operands(this),
1608 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001609 Op<0>() = S1;
1610 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001611 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001612 setName(Name);
1613}
1614
1615BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001616 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001617 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001618 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001619 OperandTraits<BinaryOperator>::op_begin(this),
1620 OperandTraits<BinaryOperator>::operands(this),
1621 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001622 Op<0>() = S1;
1623 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001624 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001625 setName(Name);
1626}
1627
1628
1629void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001630 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001631 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001632 assert(LHS->getType() == RHS->getType() &&
1633 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634#ifndef NDEBUG
1635 switch (iType) {
1636 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001637 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001638 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001639 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001640 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001641 "Tried to create an integer operation on a non-integer type!");
1642 break;
1643 case FAdd: case FSub:
1644 case FMul:
1645 assert(getType() == LHS->getType() &&
1646 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001647 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001648 "Tried to create a floating-point operation on a "
1649 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001650 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001651 case UDiv:
1652 case SDiv:
1653 assert(getType() == LHS->getType() &&
1654 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001655 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001656 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001657 "Incorrect operand type (not integer) for S/UDIV");
1658 break;
1659 case FDiv:
1660 assert(getType() == LHS->getType() &&
1661 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001662 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001663 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001664 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001665 case URem:
1666 case SRem:
1667 assert(getType() == LHS->getType() &&
1668 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001669 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001670 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001671 "Incorrect operand type (not integer) for S/UREM");
1672 break;
1673 case FRem:
1674 assert(getType() == LHS->getType() &&
1675 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001676 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001677 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001678 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001679 case Shl:
1680 case LShr:
1681 case AShr:
1682 assert(getType() == LHS->getType() &&
1683 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001684 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001685 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001686 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001687 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001688 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 case And: case Or:
1690 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001691 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001693 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001694 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001695 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001696 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001698 default:
1699 break;
1700 }
1701#endif
1702}
1703
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001704BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001705 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706 Instruction *InsertBefore) {
1707 assert(S1->getType() == S2->getType() &&
1708 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001709 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001710}
1711
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001712BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001713 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001714 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001715 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001716 InsertAtEnd->getInstList().push_back(Res);
1717 return Res;
1718}
1719
Dan Gohman5476cfd2009-08-12 16:23:25 +00001720BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001721 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001722 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001723 return new BinaryOperator(Instruction::Sub,
1724 zero, Op,
1725 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001726}
1727
Dan Gohman5476cfd2009-08-12 16:23:25 +00001728BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001729 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001730 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001731 return new BinaryOperator(Instruction::Sub,
1732 zero, Op,
1733 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734}
1735
Dan Gohman4ab44202009-12-18 02:58:50 +00001736BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1737 Instruction *InsertBefore) {
1738 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1739 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1740}
1741
1742BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1743 BasicBlock *InsertAtEnd) {
1744 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1745 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1746}
1747
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001748BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1749 Instruction *InsertBefore) {
1750 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1751 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1752}
1753
1754BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1755 BasicBlock *InsertAtEnd) {
1756 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1757 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1758}
1759
Dan Gohman5476cfd2009-08-12 16:23:25 +00001760BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001761 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001762 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001763 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001764 Op->getType(), Name, InsertBefore);
1765}
1766
Dan Gohman5476cfd2009-08-12 16:23:25 +00001767BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001768 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001769 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001770 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001771 Op->getType(), Name, InsertAtEnd);
1772}
1773
Dan Gohman5476cfd2009-08-12 16:23:25 +00001774BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001776 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001777 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778 Op->getType(), Name, InsertBefore);
1779}
1780
Dan Gohman5476cfd2009-08-12 16:23:25 +00001781BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001782 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001783 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001784 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001785 Op->getType(), Name, InsertAtEnd);
1786}
1787
1788
1789// isConstantAllOnes - Helper function for several functions below
1790static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001791 if (const Constant *C = dyn_cast<Constant>(V))
1792 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001793 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001794}
1795
Owen Andersonbb2501b2009-07-13 22:18:28 +00001796bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001797 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1798 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001799 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1800 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801 return false;
1802}
1803
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001804bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001805 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1806 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001807 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1808 if (!IgnoreZeroSign)
1809 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1810 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1811 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001812 return false;
1813}
1814
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815bool BinaryOperator::isNot(const Value *V) {
1816 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1817 return (Bop->getOpcode() == Instruction::Xor &&
1818 (isConstantAllOnes(Bop->getOperand(1)) ||
1819 isConstantAllOnes(Bop->getOperand(0))));
1820 return false;
1821}
1822
Chris Lattner2c7d1772005-04-24 07:28:37 +00001823Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001824 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001825}
1826
Chris Lattner2c7d1772005-04-24 07:28:37 +00001827const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1828 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001829}
1830
Dan Gohmana5b96452009-06-04 22:49:04 +00001831Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001832 return cast<BinaryOperator>(BinOp)->getOperand(1);
1833}
1834
1835const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1836 return getFNegArgument(const_cast<Value*>(BinOp));
1837}
1838
Chris Lattner2c7d1772005-04-24 07:28:37 +00001839Value *BinaryOperator::getNotArgument(Value *BinOp) {
1840 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1841 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1842 Value *Op0 = BO->getOperand(0);
1843 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844 if (isConstantAllOnes(Op0)) return Op1;
1845
1846 assert(isConstantAllOnes(Op1));
1847 return Op0;
1848}
1849
Chris Lattner2c7d1772005-04-24 07:28:37 +00001850const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1851 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001852}
1853
1854
1855// swapOperands - Exchange the two operands to this instruction. This
1856// instruction is safe to use on any binary instruction and does not
1857// modify the semantics of the instruction. If the instruction is
1858// order dependent (SetLT f.e.) the opcode is changed.
1859//
1860bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001861 if (!isCommutative())
1862 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001863 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001864 return false;
1865}
1866
Dan Gohman1b849082009-09-07 23:54:19 +00001867void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1868 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1869}
1870
1871void BinaryOperator::setHasNoSignedWrap(bool b) {
1872 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1873}
1874
1875void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001876 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001877}
1878
Nick Lewycky28a5f252009-09-27 21:33:04 +00001879bool BinaryOperator::hasNoUnsignedWrap() const {
1880 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1881}
1882
1883bool BinaryOperator::hasNoSignedWrap() const {
1884 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1885}
1886
1887bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001888 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001889}
1890
Sanjay Patela982d992014-09-03 01:06:50 +00001891void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001892 // Copy the wrapping flags.
1893 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1894 setHasNoSignedWrap(OB->hasNoSignedWrap());
1895 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
1896 }
1897
1898 // Copy the exact flag.
1899 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1900 setIsExact(PE->isExact());
1901
1902 // Copy the fast-math flags.
1903 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00001904 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001905}
1906
Sanjay Patela982d992014-09-03 01:06:50 +00001907void BinaryOperator::andIRFlags(const Value *V) {
1908 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1909 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
1910 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
1911 }
1912
1913 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1914 setIsExact(isExact() & PE->isExact());
1915
1916 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
1917 FastMathFlags FM = getFastMathFlags();
1918 FM &= FP->getFastMathFlags();
1919 copyFastMathFlags(FM);
1920 }
1921}
1922
1923
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001924//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00001925// FPMathOperator Class
1926//===----------------------------------------------------------------------===//
1927
1928/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
1929/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00001930/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00001931float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001932 const MDNode *MD =
1933 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00001934 if (!MD)
1935 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001936 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00001937 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00001938}
1939
1940
1941//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001942// CastInst Class
1943//===----------------------------------------------------------------------===//
1944
David Blaikie3a15e142011-12-01 08:00:17 +00001945void CastInst::anchor() {}
1946
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947// Just determine if this cast only deals with integral->integral conversion.
1948bool CastInst::isIntegerCast() const {
1949 switch (getOpcode()) {
1950 default: return false;
1951 case Instruction::ZExt:
1952 case Instruction::SExt:
1953 case Instruction::Trunc:
1954 return true;
1955 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001956 return getOperand(0)->getType()->isIntegerTy() &&
1957 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001959}
1960
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961bool CastInst::isLosslessCast() const {
1962 // Only BitCast can be lossless, exit fast if we're not BitCast
1963 if (getOpcode() != Instruction::BitCast)
1964 return false;
1965
1966 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001967 Type* SrcTy = getOperand(0)->getType();
1968 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 if (SrcTy == DstTy)
1970 return true;
1971
Reid Spencer8d9336d2006-12-31 05:26:44 +00001972 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001973 if (SrcTy->isPointerTy())
1974 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975 return false; // Other types have no identity values
1976}
1977
1978/// This function determines if the CastInst does not require any bits to be
1979/// changed in order to effect the cast. Essentially, it identifies cases where
1980/// no code gen is necessary for the cast, hence the name no-op cast. For
1981/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001982/// # bitcast i32* %x to i8*
1983/// # bitcast <2 x i32> %x to <4 x i16>
1984/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001985/// @brief Determine if the described cast is a no-op.
1986bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001987 Type *SrcTy,
1988 Type *DestTy,
1989 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001990 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001991 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992 case Instruction::Trunc:
1993 case Instruction::ZExt:
1994 case Instruction::SExt:
1995 case Instruction::FPTrunc:
1996 case Instruction::FPExt:
1997 case Instruction::UIToFP:
1998 case Instruction::SIToFP:
1999 case Instruction::FPToUI:
2000 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002001 case Instruction::AddrSpaceCast:
2002 // TODO: Target informations may give a more accurate answer here.
2003 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 case Instruction::BitCast:
2005 return true; // BitCast never modifies bits.
2006 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002007 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002008 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002010 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002011 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002012 }
2013}
2014
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002015/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002016bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002017 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2018}
2019
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002020bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002021 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002022 if (getOpcode() == Instruction::PtrToInt)
2023 PtrOpTy = getOperand(0)->getType();
2024 else if (getOpcode() == Instruction::IntToPtr)
2025 PtrOpTy = getType();
2026
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002027 Type *IntPtrTy =
2028 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002029
2030 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2031}
2032
2033/// This function determines if a pair of casts can be eliminated and what
2034/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035/// instructions like this:
2036/// * %F = firstOpcode SrcTy %x to MidTy
2037/// * %S = secondOpcode MidTy %F to DstTy
2038/// The function returns a resultOpcode so these two casts can be replaced with:
2039/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2040/// If no such cast is permited, the function returns 0.
2041unsigned CastInst::isEliminableCastPair(
2042 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002043 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2044 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 // Define the 144 possibilities for these two cast instructions. The values
2046 // in this matrix determine what to do in a given situation and select the
2047 // case in the switch below. The rows correspond to firstOp, the columns
2048 // correspond to secondOp. In looking at the table below, keep in mind
2049 // the following cast properties:
2050 //
2051 // Size Compare Source Destination
2052 // Operator Src ? Size Type Sign Type Sign
2053 // -------- ------------ ------------------- ---------------------
2054 // TRUNC > Integer Any Integral Any
2055 // ZEXT < Integral Unsigned Integer Any
2056 // SEXT < Integral Signed Integer Any
2057 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002058 // FPTOSI n/a FloatPt n/a Integral Signed
2059 // UITOFP n/a Integral Unsigned FloatPt n/a
2060 // SITOFP n/a Integral Signed FloatPt n/a
2061 // FPTRUNC > FloatPt n/a FloatPt n/a
2062 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 // PTRTOINT n/a Pointer n/a Integral Unsigned
2064 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002065 // BITCAST = FirstClass n/a FirstClass n/a
2066 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002067 //
2068 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002069 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2070 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002071 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002072 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002073 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002074 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002075 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2077 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002078 // T F F U S F F P I B A -+
2079 // R Z S P P I I T P 2 N T S |
2080 // U E E 2 2 2 2 R E I T C C +- secondOp
2081 // N X X U S F F N X N 2 V V |
2082 // C T T I I P P C T T P T T -+
2083 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002084 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002085 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2086 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2087 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2088 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2089 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002090 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002091 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2092 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2093 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2094 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2095 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002097
Chris Lattner25eea4d2010-07-12 01:19:22 +00002098 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002099 // merging. However, bitcast of A->B->A are allowed.
2100 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2101 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2102 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2103
2104 // Check if any of the bitcasts convert scalars<->vectors.
2105 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2106 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2107 // Unless we are bitcasing to the original type, disallow optimizations.
2108 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109
2110 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2111 [secondOp-Instruction::CastOpsBegin];
2112 switch (ElimCase) {
2113 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002114 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 return 0;
2116 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002117 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118 return firstOp;
2119 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002120 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 return secondOp;
2122 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002123 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002124 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002125 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002126 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127 return firstOp;
2128 return 0;
2129 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002130 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002131 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002132 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133 return firstOp;
2134 return 0;
2135 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002136 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002137 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002138 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002139 return secondOp;
2140 return 0;
2141 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002142 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002143 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002144 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002145 return secondOp;
2146 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002147 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002148 // Cannot simplify if address spaces are different!
2149 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2150 return 0;
2151
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002152 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002153 // We can still fold this without knowing the actual sizes as long we
2154 // know that the intermediate pointer is the largest possible
2155 // pointer size.
2156 // FIXME: Is this always true?
2157 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002158 return Instruction::BitCast;
2159
2160 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002161 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002162 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002163 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 if (MidSize >= PtrSize)
2165 return Instruction::BitCast;
2166 return 0;
2167 }
2168 case 8: {
2169 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2170 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2171 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002172 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2173 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 if (SrcSize == DstSize)
2175 return Instruction::BitCast;
2176 else if (SrcSize < DstSize)
2177 return firstOp;
2178 return secondOp;
2179 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002180 case 9:
2181 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 return Instruction::ZExt;
2183 case 10:
2184 // fpext followed by ftrunc is allowed if the bit size returned to is
2185 // the same as the original, in which case its just a bitcast
2186 if (SrcTy == DstTy)
2187 return Instruction::BitCast;
2188 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002189 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002191 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002192 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002193 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002194 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2195 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 if (SrcSize <= PtrSize && SrcSize == DstSize)
2197 return Instruction::BitCast;
2198 return 0;
2199 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002200 case 12: {
2201 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2202 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2203 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2204 return Instruction::AddrSpaceCast;
2205 return Instruction::BitCast;
2206 }
2207 case 13:
2208 // FIXME: this state can be merged with (1), but the following assert
2209 // is useful to check the correcteness of the sequence due to semantic
2210 // change of bitcast.
2211 assert(
2212 SrcTy->isPtrOrPtrVectorTy() &&
2213 MidTy->isPtrOrPtrVectorTy() &&
2214 DstTy->isPtrOrPtrVectorTy() &&
2215 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2216 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2217 "Illegal addrspacecast, bitcast sequence!");
2218 // Allowed, use first cast's opcode
2219 return firstOp;
2220 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002221 // bitcast, addrspacecast -> addrspacecast if the element type of
2222 // bitcast's source is the same as that of addrspacecast's destination.
2223 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2224 return Instruction::AddrSpaceCast;
2225 return 0;
2226
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002227 case 15:
2228 // FIXME: this state can be merged with (1), but the following assert
2229 // is useful to check the correcteness of the sequence due to semantic
2230 // change of bitcast.
2231 assert(
2232 SrcTy->isIntOrIntVectorTy() &&
2233 MidTy->isPtrOrPtrVectorTy() &&
2234 DstTy->isPtrOrPtrVectorTy() &&
2235 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2236 "Illegal inttoptr, bitcast sequence!");
2237 // Allowed, use first cast's opcode
2238 return firstOp;
2239 case 16:
2240 // FIXME: this state can be merged with (2), but the following assert
2241 // is useful to check the correcteness of the sequence due to semantic
2242 // change of bitcast.
2243 assert(
2244 SrcTy->isPtrOrPtrVectorTy() &&
2245 MidTy->isPtrOrPtrVectorTy() &&
2246 DstTy->isIntOrIntVectorTy() &&
2247 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2248 "Illegal bitcast, ptrtoint sequence!");
2249 // Allowed, use second cast's opcode
2250 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002251 case 17:
2252 // (sitofp (zext x)) -> (uitofp x)
2253 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002255 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002257 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258 default:
Craig Topperc514b542012-02-05 22:14:15 +00002259 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261}
2262
Chris Lattner229907c2011-07-18 04:54:35 +00002263CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002264 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002265 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266 // Construct and return the appropriate CastInst subclass
2267 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002268 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2269 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2270 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2271 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2272 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2273 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2274 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2275 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2276 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2277 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2278 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2279 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2280 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2281 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283}
2284
Chris Lattner229907c2011-07-18 04:54:35 +00002285CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002286 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002287 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288 // Construct and return the appropriate CastInst subclass
2289 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002290 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2291 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2292 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2293 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2294 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2295 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2296 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2297 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2298 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2299 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2300 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2301 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2302 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2303 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305}
2306
Chris Lattner229907c2011-07-18 04:54:35 +00002307CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002308 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002309 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002310 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002311 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2312 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002313}
2314
Chris Lattner229907c2011-07-18 04:54:35 +00002315CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002316 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002317 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002318 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002319 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2320 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002321}
2322
Chris Lattner229907c2011-07-18 04:54:35 +00002323CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002324 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002325 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002326 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002327 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2328 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002329}
2330
Chris Lattner229907c2011-07-18 04:54:35 +00002331CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002332 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002333 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002334 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002335 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2336 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002337}
2338
Chris Lattner229907c2011-07-18 04:54:35 +00002339CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002340 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002341 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002342 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002343 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2344 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002345}
2346
Chris Lattner229907c2011-07-18 04:54:35 +00002347CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002348 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002349 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002350 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002351 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2352 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002353}
2354
Chris Lattner229907c2011-07-18 04:54:35 +00002355CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002356 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002357 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002358 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2359 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2360 "Invalid cast");
2361 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002362 assert((!Ty->isVectorTy() ||
2363 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002364 "Invalid cast");
2365
Matt Arsenault065ced92013-07-31 00:17:33 +00002366 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002367 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002368
Matt Arsenault740980e2014-07-14 17:24:35 +00002369 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002370}
2371
2372/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002373CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2374 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002375 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002376 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2377 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002378 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002379 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002380 assert((!Ty->isVectorTy() ||
2381 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002382 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002383
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002384 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002385 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002386
Matt Arsenault740980e2014-07-14 17:24:35 +00002387 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2388}
2389
2390CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2391 Value *S, Type *Ty,
2392 const Twine &Name,
2393 BasicBlock *InsertAtEnd) {
2394 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2395 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2396
2397 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2398 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2399
2400 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2401}
2402
2403CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2404 Value *S, Type *Ty,
2405 const Twine &Name,
2406 Instruction *InsertBefore) {
2407 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2408 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2409
2410 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002411 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2412
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002413 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002414}
2415
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002416CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2417 const Twine &Name,
2418 Instruction *InsertBefore) {
2419 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2420 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2421 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2422 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2423
2424 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2425}
2426
Matt Arsenault740980e2014-07-14 17:24:35 +00002427CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002428 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002429 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002430 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002431 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002432 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2433 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002434 Instruction::CastOps opcode =
2435 (SrcBits == DstBits ? Instruction::BitCast :
2436 (SrcBits > DstBits ? Instruction::Trunc :
2437 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002438 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002439}
2440
Chris Lattner229907c2011-07-18 04:54:35 +00002441CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002442 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002443 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002444 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002445 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002446 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2447 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002448 Instruction::CastOps opcode =
2449 (SrcBits == DstBits ? Instruction::BitCast :
2450 (SrcBits > DstBits ? Instruction::Trunc :
2451 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002452 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002453}
2454
Chris Lattner229907c2011-07-18 04:54:35 +00002455CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002456 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002457 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002458 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002459 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002460 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2461 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002462 Instruction::CastOps opcode =
2463 (SrcBits == DstBits ? Instruction::BitCast :
2464 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002465 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002466}
2467
Chris Lattner229907c2011-07-18 04:54:35 +00002468CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002469 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002470 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002471 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002472 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002473 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2474 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002475 Instruction::CastOps opcode =
2476 (SrcBits == DstBits ? Instruction::BitCast :
2477 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002478 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002479}
2480
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002481// Check whether it is valid to call getCastOpcode for these types.
2482// This routine must be kept in sync with getCastOpcode.
2483bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2484 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2485 return false;
2486
2487 if (SrcTy == DestTy)
2488 return true;
2489
2490 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2491 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2492 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2493 // An element by element cast. Valid if casting the elements is valid.
2494 SrcTy = SrcVecTy->getElementType();
2495 DestTy = DestVecTy->getElementType();
2496 }
2497
2498 // Get the bit sizes, we'll need these
2499 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2500 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2501
2502 // Run through the possibilities ...
2503 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002504 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002505 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002506 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002507 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002508 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002509 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002510 // Casting from something else
2511 return SrcTy->isPointerTy();
2512 }
2513 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2514 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002515 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002516 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002517 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002518 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002519 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002520 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002521 return false;
2522 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002523 if (DestTy->isVectorTy()) // Casting to vector
2524 return DestBits == SrcBits;
2525 if (DestTy->isPointerTy()) { // Casting to pointer
2526 if (SrcTy->isPointerTy()) // Casting from pointer
2527 return true;
2528 return SrcTy->isIntegerTy(); // Casting from integral
2529 }
2530 if (DestTy->isX86_MMXTy()) {
2531 if (SrcTy->isVectorTy())
2532 return DestBits == SrcBits; // 64-bit vector to MMX
2533 return false;
2534 } // Casting to something else
2535 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002536}
2537
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002538bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2539 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2540 return false;
2541
2542 if (SrcTy == DestTy)
2543 return true;
2544
2545 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2546 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2547 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2548 // An element by element cast. Valid if casting the elements is valid.
2549 SrcTy = SrcVecTy->getElementType();
2550 DestTy = DestVecTy->getElementType();
2551 }
2552 }
2553 }
2554
2555 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2556 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2557 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2558 }
2559 }
2560
2561 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2562 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2563
2564 // Could still have vectors of pointers if the number of elements doesn't
2565 // match
2566 if (SrcBits == 0 || DestBits == 0)
2567 return false;
2568
2569 if (SrcBits != DestBits)
2570 return false;
2571
2572 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2573 return false;
2574
2575 return true;
2576}
2577
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002578bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002579 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002580 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2581 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002582 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002583 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2584 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002585 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002586
2587 return isBitCastable(SrcTy, DestTy);
2588}
2589
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002590// Provide a way to get a "cast" where the cast opcode is inferred from the
2591// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002592// logic in the castIsValid function below. This axiom should hold:
2593// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2594// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002596// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002598CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002599 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2600 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601
Duncan Sands55e50902008-01-06 10:12:28 +00002602 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2603 "Only first class types are castable!");
2604
Duncan Sandsa8514532011-05-18 07:13:41 +00002605 if (SrcTy == DestTy)
2606 return BitCast;
2607
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002608 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002609 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2610 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002611 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2612 // An element by element cast. Find the appropriate opcode based on the
2613 // element types.
2614 SrcTy = SrcVecTy->getElementType();
2615 DestTy = DestVecTy->getElementType();
2616 }
2617
2618 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002619 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2620 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002621
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002623 if (DestTy->isIntegerTy()) { // Casting to integral
2624 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625 if (DestBits < SrcBits)
2626 return Trunc; // int -> smaller int
2627 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002628 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629 return SExt; // signed -> SEXT
2630 else
2631 return ZExt; // unsigned -> ZEXT
2632 } else {
2633 return BitCast; // Same size, No-op cast
2634 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002635 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002636 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 return FPToSI; // FP -> sint
2638 else
2639 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002640 } else if (SrcTy->isVectorTy()) {
2641 assert(DestBits == SrcBits &&
2642 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643 return BitCast; // Same size, no-op cast
2644 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002645 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646 "Casting from a value that is not first-class type");
2647 return PtrToInt; // ptr -> int
2648 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002649 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2650 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002651 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 return SIToFP; // sint -> FP
2653 else
2654 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002655 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656 if (DestBits < SrcBits) {
2657 return FPTrunc; // FP -> smaller FP
2658 } else if (DestBits > SrcBits) {
2659 return FPExt; // FP -> larger FP
2660 } else {
2661 return BitCast; // same size, no-op cast
2662 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002663 } else if (SrcTy->isVectorTy()) {
2664 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002665 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002666 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002668 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002669 } else if (DestTy->isVectorTy()) {
2670 assert(DestBits == SrcBits &&
2671 "Illegal cast to vector (wrong type or size)");
2672 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002673 } else if (DestTy->isPointerTy()) {
2674 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002675 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2676 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002678 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002681 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002682 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002683 if (SrcTy->isVectorTy()) {
2684 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002685 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002686 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002687 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002689 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002690}
2691
2692//===----------------------------------------------------------------------===//
2693// CastInst SubClass Constructors
2694//===----------------------------------------------------------------------===//
2695
2696/// Check that the construction parameters for a CastInst are correct. This
2697/// could be broken out into the separate constructors but it is useful to have
2698/// it in one place and to eliminate the redundant code for getting the sizes
2699/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002700bool
Chris Lattner229907c2011-07-18 04:54:35 +00002701CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702
2703 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002704 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002705
Chris Lattner37bc78a2010-01-26 21:51:43 +00002706 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2707 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708 return false;
2709
2710 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002711 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2712 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713
Duncan Sands7f646562011-05-18 09:21:57 +00002714 // If these are vector types, get the lengths of the vectors (using zero for
2715 // scalar types means that checking that vector lengths match also checks that
2716 // scalars are not being converted to vectors or vectors to scalars).
2717 unsigned SrcLength = SrcTy->isVectorTy() ?
2718 cast<VectorType>(SrcTy)->getNumElements() : 0;
2719 unsigned DstLength = DstTy->isVectorTy() ?
2720 cast<VectorType>(DstTy)->getNumElements() : 0;
2721
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722 // Switch on the opcode provided
2723 switch (op) {
2724 default: return false; // This is an input error
2725 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002726 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2727 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002729 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2730 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002731 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002732 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2733 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002735 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2736 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002738 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2739 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002742 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2743 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002744 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002746 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2747 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002749 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002750 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002751 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2752 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2753 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002754 return SrcTy->getScalarType()->isPointerTy() &&
2755 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002756 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002757 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002758 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002759 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2760 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2761 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002762 return SrcTy->getScalarType()->isIntegerTy() &&
2763 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002764 case Instruction::BitCast: {
2765 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2766 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2767
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002768 // BitCast implies a no-op cast of type only. No bits change.
2769 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002770 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 return false;
2772
Alp Tokerf907b892013-12-05 05:44:44 +00002773 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002774 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002775 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002776 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2777
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002778 // If both are pointers then the address spaces must match.
2779 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2780 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002781
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002782 // A vector of pointers must have the same number of elements.
2783 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2784 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2785 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2786
2787 return false;
2788 }
2789
2790 return true;
2791 }
2792 case Instruction::AddrSpaceCast: {
2793 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2794 if (!SrcPtrTy)
2795 return false;
2796
2797 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2798 if (!DstPtrTy)
2799 return false;
2800
2801 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2802 return false;
2803
2804 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2805 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2806 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2807
2808 return false;
2809 }
2810
2811 return true;
2812 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813 }
2814}
2815
2816TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002817 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002819 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002820}
2821
2822TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002823 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002825 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826}
2827
2828ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002829 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002831 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002832}
2833
2834ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002835 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002836) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002837 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002838}
2839SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002840 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843}
2844
Jeff Cohencc08c832006-12-02 02:22:01 +00002845SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002846 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849}
2850
2851FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002852 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002854 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002855}
2856
2857FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002858 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002859) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002860 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002861}
2862
2863FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002864 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002866 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002867}
2868
2869FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002870 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002872 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873}
2874
2875UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002876 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002878 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002879}
2880
2881UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002882 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002884 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885}
2886
2887SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002888 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002890 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891}
2892
2893SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002894 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002896 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002897}
2898
2899FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002900 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002902 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903}
2904
2905FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002906 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002908 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002909}
2910
2911FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002912 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002914 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915}
2916
2917FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002918 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002920 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002921}
2922
2923PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002924 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002926 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927}
2928
2929PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002930 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002932 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933}
2934
2935IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002936 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002938 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939}
2940
2941IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002942 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002944 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945}
2946
2947BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002948 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002950 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951}
2952
2953BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002954 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002956 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002957}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002958
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002959AddrSpaceCastInst::AddrSpaceCastInst(
2960 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2961) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
2962 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2963}
2964
2965AddrSpaceCastInst::AddrSpaceCastInst(
2966 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2967) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
2968 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2969}
2970
Chris Lattnerf16dc002006-09-17 19:29:56 +00002971//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002972// CmpInst Classes
2973//===----------------------------------------------------------------------===//
2974
Craig Topper3186c012012-09-23 02:12:10 +00002975void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002976
Chris Lattner229907c2011-07-18 04:54:35 +00002977CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002978 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002979 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002980 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002981 OperandTraits<CmpInst>::op_begin(this),
2982 OperandTraits<CmpInst>::operands(this),
2983 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002984 Op<0>() = LHS;
2985 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002986 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002987 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002988}
Gabor Greiff6caff662008-05-10 08:32:32 +00002989
Chris Lattner229907c2011-07-18 04:54:35 +00002990CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002991 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002992 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002993 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002994 OperandTraits<CmpInst>::op_begin(this),
2995 OperandTraits<CmpInst>::operands(this),
2996 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002997 Op<0>() = LHS;
2998 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002999 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003000 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003001}
3002
3003CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003004CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003005 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003006 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003007 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003008 if (InsertBefore)
3009 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3010 S1, S2, Name);
3011 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003012 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003014 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003015
3016 if (InsertBefore)
3017 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3018 S1, S2, Name);
3019 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003020 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003021 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003022}
3023
3024CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003025CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003026 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003027 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003028 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3029 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003030 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003031 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3032 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003033}
3034
3035void CmpInst::swapOperands() {
3036 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3037 IC->swapOperands();
3038 else
3039 cast<FCmpInst>(this)->swapOperands();
3040}
3041
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003042bool CmpInst::isCommutative() const {
3043 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003044 return IC->isCommutative();
3045 return cast<FCmpInst>(this)->isCommutative();
3046}
3047
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003048bool CmpInst::isEquality() const {
3049 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003050 return IC->isEquality();
3051 return cast<FCmpInst>(this)->isEquality();
3052}
3053
3054
Dan Gohman4e724382008-05-31 02:47:54 +00003055CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003056 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003057 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003058 case ICMP_EQ: return ICMP_NE;
3059 case ICMP_NE: return ICMP_EQ;
3060 case ICMP_UGT: return ICMP_ULE;
3061 case ICMP_ULT: return ICMP_UGE;
3062 case ICMP_UGE: return ICMP_ULT;
3063 case ICMP_ULE: return ICMP_UGT;
3064 case ICMP_SGT: return ICMP_SLE;
3065 case ICMP_SLT: return ICMP_SGE;
3066 case ICMP_SGE: return ICMP_SLT;
3067 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003068
Dan Gohman4e724382008-05-31 02:47:54 +00003069 case FCMP_OEQ: return FCMP_UNE;
3070 case FCMP_ONE: return FCMP_UEQ;
3071 case FCMP_OGT: return FCMP_ULE;
3072 case FCMP_OLT: return FCMP_UGE;
3073 case FCMP_OGE: return FCMP_ULT;
3074 case FCMP_OLE: return FCMP_UGT;
3075 case FCMP_UEQ: return FCMP_ONE;
3076 case FCMP_UNE: return FCMP_OEQ;
3077 case FCMP_UGT: return FCMP_OLE;
3078 case FCMP_ULT: return FCMP_OGE;
3079 case FCMP_UGE: return FCMP_OLT;
3080 case FCMP_ULE: return FCMP_OGT;
3081 case FCMP_ORD: return FCMP_UNO;
3082 case FCMP_UNO: return FCMP_ORD;
3083 case FCMP_TRUE: return FCMP_FALSE;
3084 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003085 }
3086}
3087
Reid Spencer266e42b2006-12-23 06:05:41 +00003088ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3089 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003090 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003091 case ICMP_EQ: case ICMP_NE:
3092 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3093 return pred;
3094 case ICMP_UGT: return ICMP_SGT;
3095 case ICMP_ULT: return ICMP_SLT;
3096 case ICMP_UGE: return ICMP_SGE;
3097 case ICMP_ULE: return ICMP_SLE;
3098 }
3099}
3100
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003101ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3102 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003103 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003104 case ICMP_EQ: case ICMP_NE:
3105 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3106 return pred;
3107 case ICMP_SGT: return ICMP_UGT;
3108 case ICMP_SLT: return ICMP_ULT;
3109 case ICMP_SGE: return ICMP_UGE;
3110 case ICMP_SLE: return ICMP_ULE;
3111 }
3112}
3113
Reid Spencer0286bc12007-02-28 22:00:54 +00003114/// Initialize a set of values that all satisfy the condition with C.
3115///
3116ConstantRange
3117ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3118 APInt Lower(C);
3119 APInt Upper(C);
3120 uint32_t BitWidth = C.getBitWidth();
3121 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003122 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003123 case ICmpInst::ICMP_EQ: ++Upper; break;
3124 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003125 case ICmpInst::ICMP_ULT:
3126 Lower = APInt::getMinValue(BitWidth);
3127 // Check for an empty-set condition.
3128 if (Lower == Upper)
3129 return ConstantRange(BitWidth, /*isFullSet=*/false);
3130 break;
3131 case ICmpInst::ICMP_SLT:
3132 Lower = APInt::getSignedMinValue(BitWidth);
3133 // Check for an empty-set condition.
3134 if (Lower == Upper)
3135 return ConstantRange(BitWidth, /*isFullSet=*/false);
3136 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003137 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003138 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003139 // Check for an empty-set condition.
3140 if (Lower == Upper)
3141 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003142 break;
3143 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003144 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003145 // Check for an empty-set condition.
3146 if (Lower == Upper)
3147 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003148 break;
3149 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003150 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003151 // Check for a full-set condition.
3152 if (Lower == Upper)
3153 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003154 break;
3155 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003156 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003157 // Check for a full-set condition.
3158 if (Lower == Upper)
3159 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003160 break;
3161 case ICmpInst::ICMP_UGE:
3162 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003163 // Check for a full-set condition.
3164 if (Lower == Upper)
3165 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003166 break;
3167 case ICmpInst::ICMP_SGE:
3168 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003169 // Check for a full-set condition.
3170 if (Lower == Upper)
3171 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003172 break;
3173 }
3174 return ConstantRange(Lower, Upper);
3175}
3176
Dan Gohman4e724382008-05-31 02:47:54 +00003177CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003178 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003179 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003180 case ICMP_EQ: case ICMP_NE:
3181 return pred;
3182 case ICMP_SGT: return ICMP_SLT;
3183 case ICMP_SLT: return ICMP_SGT;
3184 case ICMP_SGE: return ICMP_SLE;
3185 case ICMP_SLE: return ICMP_SGE;
3186 case ICMP_UGT: return ICMP_ULT;
3187 case ICMP_ULT: return ICMP_UGT;
3188 case ICMP_UGE: return ICMP_ULE;
3189 case ICMP_ULE: return ICMP_UGE;
3190
Reid Spencerd9436b62006-11-20 01:22:35 +00003191 case FCMP_FALSE: case FCMP_TRUE:
3192 case FCMP_OEQ: case FCMP_ONE:
3193 case FCMP_UEQ: case FCMP_UNE:
3194 case FCMP_ORD: case FCMP_UNO:
3195 return pred;
3196 case FCMP_OGT: return FCMP_OLT;
3197 case FCMP_OLT: return FCMP_OGT;
3198 case FCMP_OGE: return FCMP_OLE;
3199 case FCMP_OLE: return FCMP_OGE;
3200 case FCMP_UGT: return FCMP_ULT;
3201 case FCMP_ULT: return FCMP_UGT;
3202 case FCMP_UGE: return FCMP_ULE;
3203 case FCMP_ULE: return FCMP_UGE;
3204 }
3205}
3206
Reid Spencer266e42b2006-12-23 06:05:41 +00003207bool CmpInst::isUnsigned(unsigned short predicate) {
3208 switch (predicate) {
3209 default: return false;
3210 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3211 case ICmpInst::ICMP_UGE: return true;
3212 }
3213}
3214
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003215bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003216 switch (predicate) {
3217 default: return false;
3218 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3219 case ICmpInst::ICMP_SGE: return true;
3220 }
3221}
3222
3223bool CmpInst::isOrdered(unsigned short predicate) {
3224 switch (predicate) {
3225 default: return false;
3226 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3227 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3228 case FCmpInst::FCMP_ORD: return true;
3229 }
3230}
3231
3232bool CmpInst::isUnordered(unsigned short predicate) {
3233 switch (predicate) {
3234 default: return false;
3235 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3236 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3237 case FCmpInst::FCMP_UNO: return true;
3238 }
3239}
3240
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003241bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3242 switch(predicate) {
3243 default: return false;
3244 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3245 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3246 }
3247}
3248
3249bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3250 switch(predicate) {
3251 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3252 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3253 default: return false;
3254 }
3255}
3256
3257
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003258//===----------------------------------------------------------------------===//
3259// SwitchInst Implementation
3260//===----------------------------------------------------------------------===//
3261
Chris Lattnerbaf00152010-11-17 05:41:46 +00003262void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3263 assert(Value && Default && NumReserved);
3264 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003265 NumOperands = 2;
Pete Cooper3fc30402015-06-10 22:38:46 +00003266 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003267
Pete Coopereb31b682015-05-21 22:48:54 +00003268 Op<0>() = Value;
3269 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003270}
3271
Chris Lattner2195fc42007-02-24 00:55:48 +00003272/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3273/// switch on and a default destination. The number of additional cases can
3274/// be specified here to make memory allocation more efficient. This
3275/// constructor can also autoinsert before another instruction.
3276SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3277 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003278 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003279 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003280 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003281}
3282
3283/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3284/// switch on and a default destination. The number of additional cases can
3285/// be specified here to make memory allocation more efficient. This
3286/// constructor also autoinserts at the end of the specified BasicBlock.
3287SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3288 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003289 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003290 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003291 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003292}
3293
Misha Brukmanb1c93172005-04-21 23:48:37 +00003294SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003295 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003296 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3297 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003298 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003299 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003300 OL[i] = InOL[i];
3301 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003302 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003303 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003304}
3305
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003306
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003307/// addCase - Add an entry to the switch instruction...
3308///
Chris Lattner47ac1872005-02-24 05:32:09 +00003309void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003310 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003311 unsigned OpNo = NumOperands;
3312 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003313 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003314 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003315 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003316 NumOperands = OpNo+2;
Bob Wilsone4077362013-09-09 19:14:35 +00003317 CaseIt Case(this, NewCaseIdx);
3318 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003319 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003320}
3321
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003322/// removeCase - This method removes the specified case and its successor
3323/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003324void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003325 unsigned idx = i.getCaseIndex();
3326
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003327 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003328
3329 unsigned NumOps = getNumOperands();
3330 Use *OL = OperandList;
3331
Jay Foad14277722011-02-01 09:22:34 +00003332 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003333 if (2 + (idx + 1) * 2 != NumOps) {
3334 OL[2 + idx * 2] = OL[NumOps - 2];
3335 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003336 }
3337
3338 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003339 OL[NumOps-2].set(nullptr);
3340 OL[NumOps-2+1].set(nullptr);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003341 NumOperands = NumOps-2;
3342}
3343
Jay Foade98f29d2011-04-01 08:00:58 +00003344/// growOperands - grow operands - This grows the operand list in response
3345/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003346///
Jay Foade98f29d2011-04-01 08:00:58 +00003347void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003348 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003349 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003350
3351 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003352 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003353}
3354
3355
3356BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3357 return getSuccessor(idx);
3358}
3359unsigned SwitchInst::getNumSuccessorsV() const {
3360 return getNumSuccessors();
3361}
3362void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3363 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003364}
Chris Lattnerf22be932004-10-15 23:52:53 +00003365
Chris Lattner3ed871f2009-10-27 19:13:16 +00003366//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003367// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368//===----------------------------------------------------------------------===//
3369
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003370void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003371 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003372 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003373 ReservedSpace = 1+NumDests;
3374 NumOperands = 1;
Pete Cooper3fc30402015-06-10 22:38:46 +00003375 allocHungoffUses(ReservedSpace);
3376
Pete Coopereb31b682015-05-21 22:48:54 +00003377 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003378}
3379
3380
Jay Foade98f29d2011-04-01 08:00:58 +00003381/// growOperands - grow operands - This grows the operand list in response
3382/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003383///
Jay Foade98f29d2011-04-01 08:00:58 +00003384void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003385 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003386 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003387
3388 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003389 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003390}
3391
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003392IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3393 Instruction *InsertBefore)
3394: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003395 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003396 init(Address, NumCases);
3397}
3398
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003399IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3400 BasicBlock *InsertAtEnd)
3401: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003402 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003403 init(Address, NumCases);
3404}
3405
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003406IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003407 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3408 nullptr, IBI.getNumOperands()) {
3409 allocHungoffUses(IBI.getNumOperands());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003410 Use *OL = OperandList, *InOL = IBI.OperandList;
3411 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3412 OL[i] = InOL[i];
3413 SubclassOptionalData = IBI.SubclassOptionalData;
3414}
3415
Chris Lattner3ed871f2009-10-27 19:13:16 +00003416/// addDestination - Add a destination.
3417///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003418void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003419 unsigned OpNo = NumOperands;
3420 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003421 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003422 // Initialize some new operands.
3423 assert(OpNo < ReservedSpace && "Growing didn't work!");
3424 NumOperands = OpNo+1;
3425 OperandList[OpNo] = DestBB;
3426}
3427
3428/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003429/// indirectbr instruction.
3430void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003431 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3432
3433 unsigned NumOps = getNumOperands();
3434 Use *OL = OperandList;
3435
3436 // Replace this value with the last one.
3437 OL[idx+1] = OL[NumOps-1];
3438
3439 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003440 OL[NumOps-1].set(nullptr);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003441 NumOperands = NumOps-1;
3442}
3443
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003444BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003445 return getSuccessor(idx);
3446}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003447unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003448 return getNumSuccessors();
3449}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003450void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003451 setSuccessor(idx, B);
3452}
3453
3454//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003455// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003456//===----------------------------------------------------------------------===//
3457
Chris Lattnerf22be932004-10-15 23:52:53 +00003458// Define these methods here so vtables don't get emitted into every translation
3459// unit that uses these classes.
3460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3462 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003463}
3464
Devang Patel11cf3f42009-10-27 22:16:29 +00003465BinaryOperator *BinaryOperator::clone_impl() const {
3466 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469FCmpInst* FCmpInst::clone_impl() const {
3470 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003471}
3472
Devang Patel11cf3f42009-10-27 22:16:29 +00003473ICmpInst* ICmpInst::clone_impl() const {
3474 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003475}
3476
Devang Patel11cf3f42009-10-27 22:16:29 +00003477ExtractValueInst *ExtractValueInst::clone_impl() const {
3478 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479}
3480
Devang Patel11cf3f42009-10-27 22:16:29 +00003481InsertValueInst *InsertValueInst::clone_impl() const {
3482 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003483}
3484
Devang Patel11cf3f42009-10-27 22:16:29 +00003485AllocaInst *AllocaInst::clone_impl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003486 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3487 (Value *)getOperand(0), getAlignment());
3488 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3489 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003490}
3491
Devang Patel11cf3f42009-10-27 22:16:29 +00003492LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003493 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3494 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003495}
3496
Devang Patel11cf3f42009-10-27 22:16:29 +00003497StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003498 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003499 getAlignment(), getOrdering(), getSynchScope());
3500
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003501}
3502
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003503AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3504 AtomicCmpXchgInst *Result =
3505 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003506 getSuccessOrdering(), getFailureOrdering(),
3507 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003508 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003509 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003510 return Result;
3511}
3512
3513AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3514 AtomicRMWInst *Result =
3515 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3516 getOrdering(), getSynchScope());
3517 Result->setVolatile(isVolatile());
3518 return Result;
3519}
3520
Eli Friedmanfee02c62011-07-25 23:16:38 +00003521FenceInst *FenceInst::clone_impl() const {
3522 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3523}
3524
Devang Patel11cf3f42009-10-27 22:16:29 +00003525TruncInst *TruncInst::clone_impl() const {
3526 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003527}
3528
Devang Patel11cf3f42009-10-27 22:16:29 +00003529ZExtInst *ZExtInst::clone_impl() const {
3530 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003531}
3532
Devang Patel11cf3f42009-10-27 22:16:29 +00003533SExtInst *SExtInst::clone_impl() const {
3534 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003535}
3536
Devang Patel11cf3f42009-10-27 22:16:29 +00003537FPTruncInst *FPTruncInst::clone_impl() const {
3538 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003539}
3540
Devang Patel11cf3f42009-10-27 22:16:29 +00003541FPExtInst *FPExtInst::clone_impl() const {
3542 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003543}
3544
Devang Patel11cf3f42009-10-27 22:16:29 +00003545UIToFPInst *UIToFPInst::clone_impl() const {
3546 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003547}
3548
Devang Patel11cf3f42009-10-27 22:16:29 +00003549SIToFPInst *SIToFPInst::clone_impl() const {
3550 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003551}
3552
Devang Patel11cf3f42009-10-27 22:16:29 +00003553FPToUIInst *FPToUIInst::clone_impl() const {
3554 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003555}
3556
Devang Patel11cf3f42009-10-27 22:16:29 +00003557FPToSIInst *FPToSIInst::clone_impl() const {
3558 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003559}
3560
Devang Patel11cf3f42009-10-27 22:16:29 +00003561PtrToIntInst *PtrToIntInst::clone_impl() const {
3562 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003563}
3564
Devang Patel11cf3f42009-10-27 22:16:29 +00003565IntToPtrInst *IntToPtrInst::clone_impl() const {
3566 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003567}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003568
Devang Patel11cf3f42009-10-27 22:16:29 +00003569BitCastInst *BitCastInst::clone_impl() const {
3570 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003571}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003572
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003573AddrSpaceCastInst *AddrSpaceCastInst::clone_impl() const {
3574 return new AddrSpaceCastInst(getOperand(0), getType());
3575}
3576
Devang Patel11cf3f42009-10-27 22:16:29 +00003577CallInst *CallInst::clone_impl() const {
3578 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003579}
3580
Devang Patel11cf3f42009-10-27 22:16:29 +00003581SelectInst *SelectInst::clone_impl() const {
3582 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003583}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003584
Devang Patel11cf3f42009-10-27 22:16:29 +00003585VAArgInst *VAArgInst::clone_impl() const {
3586 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003587}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003588
Devang Patel11cf3f42009-10-27 22:16:29 +00003589ExtractElementInst *ExtractElementInst::clone_impl() const {
3590 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003591}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003592
Devang Patel11cf3f42009-10-27 22:16:29 +00003593InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003594 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003595}
3596
Devang Patel11cf3f42009-10-27 22:16:29 +00003597ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003598 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003599}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003600
Devang Patel11cf3f42009-10-27 22:16:29 +00003601PHINode *PHINode::clone_impl() const {
3602 return new PHINode(*this);
3603}
3604
Bill Wendlingfae14752011-08-12 20:24:12 +00003605LandingPadInst *LandingPadInst::clone_impl() const {
3606 return new LandingPadInst(*this);
3607}
3608
Devang Patel11cf3f42009-10-27 22:16:29 +00003609ReturnInst *ReturnInst::clone_impl() const {
3610 return new(getNumOperands()) ReturnInst(*this);
3611}
3612
3613BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003614 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003615}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003616
Devang Patel11cf3f42009-10-27 22:16:29 +00003617SwitchInst *SwitchInst::clone_impl() const {
3618 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003619}
3620
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003621IndirectBrInst *IndirectBrInst::clone_impl() const {
3622 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003623}
3624
3625
Devang Patel11cf3f42009-10-27 22:16:29 +00003626InvokeInst *InvokeInst::clone_impl() const {
3627 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003628}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003629
Bill Wendlingf891bf82011-07-31 06:30:59 +00003630ResumeInst *ResumeInst::clone_impl() const {
3631 return new(1) ResumeInst(*this);
3632}
3633
Devang Patel11cf3f42009-10-27 22:16:29 +00003634UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003635 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003636 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003637}