blob: f7bb4b264ed855ef393d6cd184fcd4ee69121ed2 [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
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
Chris Lattner229907c2011-07-18 04:54:35 +000065 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000066 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000069 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000070 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000090 std::copy(PN.op_begin(), PN.op_end(), op_begin());
91 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000092 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000093}
94
Gordon Henriksen14a55692007-12-10 02:14:30 +000095PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000096 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Jay Foad61ea0e42011-06-23 09:09:15 +000099Use *PHINode::allocHungoffUses(unsigned N) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
104 + N * sizeof(BasicBlock*);
105 Use *Begin = static_cast<Use*>(::operator new(size));
106 Use *End = Begin + N;
107 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
108 return Use::initTags(Begin, End);
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111// removeIncomingValue - Remove an incoming value. This is useful if a
112// predecessor basic block is deleted.
113Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // Move everything after this operand down.
117 //
118 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123
124 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000125 Op<-1>().set(0);
126 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127
128 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000129 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000131 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 eraseFromParent();
133 }
134 return Removed;
135}
136
Jay Foade98f29d2011-04-01 08:00:58 +0000137/// growOperands - grow operands - This grows the operand list in response
138/// to a push_back style of operation. This grows the number of ops by 1.5
139/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140///
Jay Foade98f29d2011-04-01 08:00:58 +0000141void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 unsigned NumOps = e + e / 2;
144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
145
146 Use *OldOps = op_begin();
147 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 OperandList = allocHungoffUses(ReservedSpace);
151
152 std::copy(OldOps, OldOps + e, op_begin());
153 std::copy(OldBlocks, OldBlocks + e, block_begin());
154
Jay Foadbbb91f22011-01-16 15:30:52 +0000155 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156}
157
Nate Begemanb3923212005-08-04 23:24:19 +0000158/// hasConstantValue - If the specified PHI node always merges together the same
159/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000160Value *PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value *ConstantValue = getIncomingValue(0);
163 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000164 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
165 if (ConstantValue != this)
166 return 0; // Incoming values not all the same.
167 // The case where the first value is this PHI.
168 ConstantValue = getIncomingValue(i);
169 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000170 if (ConstantValue == this)
171 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
179LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
180 unsigned NumReservedValues, const Twine &NameStr,
181 Instruction *InsertBefore)
182 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
183 init(PersonalityFn, 1 + NumReservedValues, NameStr);
184}
185
186LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
187 unsigned NumReservedValues, const Twine &NameStr,
188 BasicBlock *InsertAtEnd)
189 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
190 init(PersonalityFn, 1 + NumReservedValues, NameStr);
191}
192
193LandingPadInst::LandingPadInst(const LandingPadInst &LP)
194 : Instruction(LP.getType(), Instruction::LandingPad,
195 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
196 ReservedSpace(LP.getNumOperands()) {
197 Use *OL = OperandList, *InOL = LP.OperandList;
198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
204LandingPadInst::~LandingPadInst() {
205 dropHungoffUses();
206}
207
208LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
209 unsigned NumReservedClauses,
210 const Twine &NameStr,
211 Instruction *InsertBefore) {
212 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
213 InsertBefore);
214}
215
216LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
217 unsigned NumReservedClauses,
218 const Twine &NameStr,
219 BasicBlock *InsertAtEnd) {
220 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
221 InsertAtEnd);
222}
223
224void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
225 const Twine &NameStr) {
226 ReservedSpace = NumReservedValues;
227 NumOperands = 1;
228 OperandList = allocHungoffUses(ReservedSpace);
229 OperandList[0] = PersFn;
230 setName(NameStr);
231 setCleanup(false);
232}
233
234/// growOperands - grow operands - This grows the operand list in response to a
235/// push_back style of operation. This grows the number of ops by 2 times.
236void LandingPadInst::growOperands(unsigned Size) {
237 unsigned e = getNumOperands();
238 if (ReservedSpace >= e + Size) return;
239 ReservedSpace = (e + Size / 2) * 2;
240
241 Use *NewOps = allocHungoffUses(ReservedSpace);
242 Use *OldOps = OperandList;
243 for (unsigned i = 0; i != e; ++i)
244 NewOps[i] = OldOps[i];
245
246 OperandList = NewOps;
247 Use::zap(OldOps, OldOps + e, true);
248}
249
250void LandingPadInst::addClause(Value *Val) {
251 unsigned OpNo = getNumOperands();
252 growOperands(1);
253 assert(OpNo < ReservedSpace && "Growing didn't work!");
254 ++NumOperands;
255 OperandList[OpNo] = Val;
256}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257
258//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259// CallInst Implementation
260//===----------------------------------------------------------------------===//
261
Gordon Henriksen14a55692007-12-10 02:14:30 +0000262CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263}
264
Jay Foad5bd375a2011-07-15 08:37:34 +0000265void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
266 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000267 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268
Jay Foad5bd375a2011-07-15 08:37:34 +0000269#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000270 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
272
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 assert((Args.size() == FTy->getNumParams() ||
274 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000275 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000276
277 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000278 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000279 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000280 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000281#endif
282
283 std::copy(Args.begin(), Args.end(), op_begin());
284 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Jay Foad5bd375a2011-07-15 08:37:34 +0000287void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000288 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000292 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000296#endif
297
298 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Daniel Dunbar4975db62009-07-25 04:41:11 +0000301CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 Instruction *InsertBefore)
303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000305 Instruction::Call,
306 OperandTraits<CallInst>::op_end(this) - 1,
307 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Daniel Dunbar4975db62009-07-25 04:41:11 +0000311CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 Instruction::Call,
316 OperandTraits<CallInst>::op_end(this) - 1,
317 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000318 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 : Instruction(CI.getType(), Instruction::Call,
323 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000324 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000325 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000326 setTailCall(CI.isTailCall());
327 setCallingConv(CI.getCallingConv());
328
Jay Foad5bd375a2011-07-15 08:37:34 +0000329 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000330 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333void CallInst::addAttribute(unsigned i, Attributes attr) {
334 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000335 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000337}
338
Devang Patel4c758ea2008-09-25 21:00:45 +0000339void CallInst::removeAttribute(unsigned i, Attributes attr) {
340 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000341 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000343}
344
Bill Wendling375eb1f2012-10-09 00:28:54 +0000345bool CallInst::fnHasNoAliasAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000346 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoAlias))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000347 return true;
348 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000349 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoAlias);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000350 return false;
351}
352bool CallInst::fnHasNoInlineAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000353 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoInline))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000354 return true;
355 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000356 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoInline);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000357 return false;
358}
359bool CallInst::fnHasNoReturnAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000360 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoReturn))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000361 return true;
362 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000363 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoReturn);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000364 return false;
365}
366bool CallInst::fnHasNoUnwindAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000367 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000368 return true;
369 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000370 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000371 return false;
372}
373bool CallInst::fnHasReadNoneAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000374 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadNone))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000375 return true;
376 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000377 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadNone);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000378 return false;
379}
380bool CallInst::fnHasReadOnlyAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000381 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000382 return true;
383 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000384 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000385 return false;
386}
387bool CallInst::fnHasReturnsTwiceAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000388 if (AttributeList.getParamAttributes(~0U).
389 hasAttribute(Attributes::ReturnsTwice))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000390 return true;
391 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000392 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReturnsTwice);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000393 return false;
394}
395
Bill Wendling8baa61d2012-10-03 17:54:26 +0000396bool CallInst::paramHasSExtAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000397 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::SExt))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000398 return true;
399 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000400 return F->getParamAttributes(i).hasAttribute(Attributes::SExt);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000401 return false;
402}
403
404bool CallInst::paramHasZExtAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000405 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ZExt))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000406 return true;
407 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000408 return F->getParamAttributes(i).hasAttribute(Attributes::ZExt);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000409 return false;
410}
411
412bool CallInst::paramHasInRegAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000413 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::InReg))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000414 return true;
415 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000416 return F->getParamAttributes(i).hasAttribute(Attributes::InReg);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000417 return false;
418}
419
420bool CallInst::paramHasStructRetAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000421 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::StructRet))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000422 return true;
423 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000424 return F->getParamAttributes(i).hasAttribute(Attributes::StructRet);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000425 return false;
426}
427
428bool CallInst::paramHasNestAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000429 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::Nest))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000430 return true;
431 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000432 return F->getParamAttributes(i).hasAttribute(Attributes::Nest);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000433 return false;
434}
435
436bool CallInst::paramHasByValAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000437 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ByVal))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000438 return true;
439 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000440 return F->getParamAttributes(i).hasAttribute(Attributes::ByVal);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000441 return false;
442}
443
Bill Wendlingd7773982012-10-04 06:52:09 +0000444bool CallInst::paramHasNoAliasAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000445 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoAlias))
Bill Wendlingd7773982012-10-04 06:52:09 +0000446 return true;
447 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000448 return F->getParamAttributes(i).hasAttribute(Attributes::NoAlias);
Bill Wendlingd7773982012-10-04 06:52:09 +0000449 return false;
450}
451
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000452bool CallInst::paramHasNoCaptureAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000453 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoCapture))
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000454 return true;
455 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000456 return F->getParamAttributes(i).hasAttribute(Attributes::NoCapture);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000457 return false;
458}
459
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000460/// IsConstantOne - Return true only if val is constant int 1
461static bool IsConstantOne(Value *val) {
462 assert(val && "IsConstantOne does not work with NULL val");
463 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
464}
465
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000466static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000467 BasicBlock *InsertAtEnd, Type *IntPtrTy,
468 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Value *ArraySize, Function *MallocF,
470 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000471 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000472 "createMalloc needs either InsertBefore or InsertAtEnd");
473
474 // malloc(type) becomes:
475 // bitcast (i8* malloc(typeSize)) to type*
476 // malloc(type, arraySize) becomes:
477 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000478 if (!ArraySize)
479 ArraySize = ConstantInt::get(IntPtrTy, 1);
480 else if (ArraySize->getType() != IntPtrTy) {
481 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000482 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
483 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000484 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000485 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
486 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000487 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000488
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000489 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490 if (IsConstantOne(AllocSize)) {
491 AllocSize = ArraySize; // Operand * 1 = Operand
492 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
493 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
494 false /*ZExt*/);
495 // Malloc arg is constant product of type size and array size
496 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
497 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000498 // Multiply type size by the array size...
499 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000500 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
501 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000503 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
504 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000505 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000506 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000507
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 // Create the call to Malloc.
510 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
511 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000512 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000513 Value *MallocFunc = MallocF;
514 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000515 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000516 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000517 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000518 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000519 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000520 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000521 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000522 Result = MCall;
523 if (Result->getType() != AllocPtrType)
524 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000525 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000526 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000527 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000528 Result = MCall;
529 if (Result->getType() != AllocPtrType) {
530 InsertAtEnd->getInstList().push_back(MCall);
531 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000532 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000533 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000534 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000535 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000536 if (Function *F = dyn_cast<Function>(MallocFunc)) {
537 MCall->setCallingConv(F->getCallingConv());
538 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
539 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000540 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000541
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000542 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000543}
544
545/// CreateMalloc - Generate the IR for a call to malloc:
546/// 1. Compute the malloc call's argument as the specified type's size,
547/// possibly multiplied by the array size if the array size is not
548/// constant 1.
549/// 2. Call malloc with that argument.
550/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000551Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000552 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000553 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000554 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000555 const Twine &Name) {
556 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000557 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000558}
559
560/// CreateMalloc - Generate the IR for a call to malloc:
561/// 1. Compute the malloc call's argument as the specified type's size,
562/// possibly multiplied by the array size if the array size is not
563/// constant 1.
564/// 2. Call malloc with that argument.
565/// 3. Bitcast the result of the malloc call to the specified type.
566/// Note: This function does not add the bitcast to the basic block, that is the
567/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000568Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000569 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000570 Value *AllocSize, Value *ArraySize,
571 Function *MallocF, const Twine &Name) {
572 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000573 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000574}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000575
Victor Hernandeze2971492009-10-24 04:23:03 +0000576static Instruction* createFree(Value* Source, Instruction *InsertBefore,
577 BasicBlock *InsertAtEnd) {
578 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
579 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000580 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000581 "Can not free something of nonpointer type!");
582
583 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
584 Module* M = BB->getParent()->getParent();
585
Chris Lattner229907c2011-07-18 04:54:35 +0000586 Type *VoidTy = Type::getVoidTy(M->getContext());
587 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000588 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000589 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000590 CallInst* Result = NULL;
591 Value *PtrCast = Source;
592 if (InsertBefore) {
593 if (Source->getType() != IntPtrTy)
594 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
595 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
596 } else {
597 if (Source->getType() != IntPtrTy)
598 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
599 Result = CallInst::Create(FreeFunc, PtrCast, "");
600 }
601 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000602 if (Function *F = dyn_cast<Function>(FreeFunc))
603 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000604
605 return Result;
606}
607
608/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000609Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
610 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000611}
612
613/// CreateFree - Generate the IR for a call to the builtin free function.
614/// Note: This function does not add the call to the basic block, that is the
615/// responsibility of the caller.
616Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
617 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
618 assert(FreeCall && "CreateFree did not create a CallInst");
619 return FreeCall;
620}
621
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622//===----------------------------------------------------------------------===//
623// InvokeInst Implementation
624//===----------------------------------------------------------------------===//
625
626void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000627 ArrayRef<Value *> Args, const Twine &NameStr) {
628 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000629 Op<-3>() = Fn;
630 Op<-2>() = IfNormal;
631 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000632
633#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000634 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000636
Jay Foad5bd375a2011-07-15 08:37:34 +0000637 assert(((Args.size() == FTy->getNumParams()) ||
638 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000639 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000640
Jay Foad5bd375a2011-07-15 08:37:34 +0000641 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000642 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000643 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000644 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000645#endif
646
647 std::copy(Args.begin(), Args.end(), op_begin());
648 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649}
650
Misha Brukmanb1c93172005-04-21 23:48:37 +0000651InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000653 OperandTraits<InvokeInst>::op_end(this)
654 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000655 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000656 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000657 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000658 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000659 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660}
661
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
663 return getSuccessor(idx);
664}
665unsigned InvokeInst::getNumSuccessorsV() const {
666 return getNumSuccessors();
667}
668void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
669 return setSuccessor(idx, B);
670}
671
Bill Wendling375eb1f2012-10-09 00:28:54 +0000672bool InvokeInst::fnHasNoAliasAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000673 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoAlias))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000674 return true;
675 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000676 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoAlias);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000677 return false;
678}
679bool InvokeInst::fnHasNoInlineAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000680 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoInline))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000681 return true;
682 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000683 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoInline);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000684 return false;
685}
686bool InvokeInst::fnHasNoReturnAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000687 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoReturn))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000688 return true;
689 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000690 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoReturn);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000691 return false;
692}
693bool InvokeInst::fnHasNoUnwindAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000694 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000695 return true;
696 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000697 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000698 return false;
699}
700bool InvokeInst::fnHasReadNoneAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000701 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadNone))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000702 return true;
703 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000704 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadNone);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000705 return false;
706}
707bool InvokeInst::fnHasReadOnlyAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000708 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000709 return true;
710 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000711 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000712 return false;
713}
714bool InvokeInst::fnHasReturnsTwiceAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000715 if (AttributeList.getParamAttributes(~0U).
716 hasAttribute(Attributes::ReturnsTwice))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000717 return true;
718 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000719 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReturnsTwice);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000720 return false;
721}
722
Bill Wendling8baa61d2012-10-03 17:54:26 +0000723bool InvokeInst::paramHasSExtAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000724 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::SExt))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000725 return true;
726 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000727 return F->getParamAttributes(i).hasAttribute(Attributes::SExt);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000728 return false;
729}
730
731bool InvokeInst::paramHasZExtAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000732 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ZExt))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000733 return true;
734 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000735 return F->getParamAttributes(i).hasAttribute(Attributes::ZExt);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000736 return false;
737}
738
739bool InvokeInst::paramHasInRegAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000740 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::InReg))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000741 return true;
742 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000743 return F->getParamAttributes(i).hasAttribute(Attributes::InReg);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000744 return false;
745}
746
747bool InvokeInst::paramHasStructRetAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000748 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::StructRet))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000749 return true;
750 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000751 return F->getParamAttributes(i).hasAttribute(Attributes::StructRet);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000752 return false;
753}
754
755bool InvokeInst::paramHasNestAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000756 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::Nest))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000757 return true;
758 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000759 return F->getParamAttributes(i).hasAttribute(Attributes::Nest);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000760 return false;
761}
762
763bool InvokeInst::paramHasByValAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000764 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ByVal))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000765 return true;
766 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000767 return F->getParamAttributes(i).hasAttribute(Attributes::ByVal);
Bill Wendling8baa61d2012-10-03 17:54:26 +0000768 return false;
769}
770
Bill Wendlingd7773982012-10-04 06:52:09 +0000771bool InvokeInst::paramHasNoAliasAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000772 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoAlias))
Bill Wendlingd7773982012-10-04 06:52:09 +0000773 return true;
774 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000775 return F->getParamAttributes(i).hasAttribute(Attributes::NoAlias);
Bill Wendlingd7773982012-10-04 06:52:09 +0000776 return false;
777}
778
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000779bool InvokeInst::paramHasNoCaptureAttr(unsigned i) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000780 if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoCapture))
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000781 return true;
782 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000783 return F->getParamAttributes(i).hasAttribute(Attributes::NoCapture);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000784 return false;
785}
786
Devang Patel4c758ea2008-09-25 21:00:45 +0000787void InvokeInst::addAttribute(unsigned i, Attributes attr) {
788 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000789 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000790 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000791}
792
Devang Patel4c758ea2008-09-25 21:00:45 +0000793void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
794 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000795 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000796 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000797}
798
Bill Wendlingfae14752011-08-12 20:24:12 +0000799LandingPadInst *InvokeInst::getLandingPadInst() const {
800 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
801}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803//===----------------------------------------------------------------------===//
804// ReturnInst Implementation
805//===----------------------------------------------------------------------===//
806
Chris Lattner2195fc42007-02-24 00:55:48 +0000807ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000808 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000809 OperandTraits<ReturnInst>::op_end(this) -
810 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000811 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000812 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000813 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000814 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000815}
816
Owen Anderson55f1c092009-08-13 21:58:54 +0000817ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
818 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000819 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
820 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000821 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000822 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000823}
Owen Anderson55f1c092009-08-13 21:58:54 +0000824ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
825 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000826 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
827 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000828 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000829 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000830}
Owen Anderson55f1c092009-08-13 21:58:54 +0000831ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
832 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000833 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000834}
835
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836unsigned ReturnInst::getNumSuccessorsV() const {
837 return getNumSuccessors();
838}
839
Devang Patelae682fb2008-02-26 17:56:20 +0000840/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
841/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000843 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000847 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848}
849
Devang Patel59643e52008-02-23 00:35:18 +0000850ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000851}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000854// ResumeInst Implementation
855//===----------------------------------------------------------------------===//
856
857ResumeInst::ResumeInst(const ResumeInst &RI)
858 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
859 OperandTraits<ResumeInst>::op_begin(this), 1) {
860 Op<0>() = RI.Op<0>();
861}
862
863ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
864 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
865 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
866 Op<0>() = Exn;
867}
868
869ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
870 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
871 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
872 Op<0>() = Exn;
873}
874
875unsigned ResumeInst::getNumSuccessorsV() const {
876 return getNumSuccessors();
877}
878
879void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
880 llvm_unreachable("ResumeInst has no successors!");
881}
882
883BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
884 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000885}
886
887//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000888// UnreachableInst Implementation
889//===----------------------------------------------------------------------===//
890
Owen Anderson55f1c092009-08-13 21:58:54 +0000891UnreachableInst::UnreachableInst(LLVMContext &Context,
892 Instruction *InsertBefore)
893 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
894 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000895}
Owen Anderson55f1c092009-08-13 21:58:54 +0000896UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
897 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
898 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000899}
900
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901unsigned UnreachableInst::getNumSuccessorsV() const {
902 return getNumSuccessors();
903}
904
905void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000906 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907}
908
909BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000910 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000911}
912
913//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914// BranchInst Implementation
915//===----------------------------------------------------------------------===//
916
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917void BranchInst::AssertOK() {
918 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000919 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921}
922
Chris Lattner2195fc42007-02-24 00:55:48 +0000923BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000924 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000925 OperandTraits<BranchInst>::op_end(this) - 1,
926 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000927 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000928 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000929}
930BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
931 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000932 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000933 OperandTraits<BranchInst>::op_end(this) - 3,
934 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000935 Op<-1>() = IfTrue;
936 Op<-2>() = IfFalse;
937 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000938#ifndef NDEBUG
939 AssertOK();
940#endif
941}
942
943BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000944 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000945 OperandTraits<BranchInst>::op_end(this) - 1,
946 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000948 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000949}
950
951BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
952 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000953 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000954 OperandTraits<BranchInst>::op_end(this) - 3,
955 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000956 Op<-1>() = IfTrue;
957 Op<-2>() = IfFalse;
958 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000959#ifndef NDEBUG
960 AssertOK();
961#endif
962}
963
964
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000966 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000967 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
968 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000969 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 if (BI.getNumOperands() != 1) {
971 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000972 Op<-3>() = BI.Op<-3>();
973 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000975 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976}
977
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000978void BranchInst::swapSuccessors() {
979 assert(isConditional() &&
980 "Cannot swap successors of an unconditional branch");
981 Op<-1>().swap(Op<-2>());
982
983 // Update profile metadata if present and it matches our structural
984 // expectations.
985 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
986 if (!ProfileData || ProfileData->getNumOperands() != 3)
987 return;
988
989 // The first operand is the name. Fetch them backwards and build a new one.
990 Value *Ops[] = {
991 ProfileData->getOperand(0),
992 ProfileData->getOperand(2),
993 ProfileData->getOperand(1)
994 };
995 setMetadata(LLVMContext::MD_prof,
996 MDNode::get(ProfileData->getContext(), Ops));
997}
998
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1000 return getSuccessor(idx);
1001}
1002unsigned BranchInst::getNumSuccessorsV() const {
1003 return getNumSuccessors();
1004}
1005void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1006 setSuccessor(idx, B);
1007}
1008
1009
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001010//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001011// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012//===----------------------------------------------------------------------===//
1013
Owen Andersonb6b25302009-07-14 23:09:55 +00001014static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001015 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001016 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001017 else {
1018 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001019 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001020 assert(Amt->getType()->isIntegerTy() &&
1021 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001022 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001023 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024}
1025
Chris Lattner229907c2011-07-18 04:54:35 +00001026AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001027 const Twine &Name, Instruction *InsertBefore)
1028 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1029 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
1030 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001031 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001032 setName(Name);
1033}
1034
Chris Lattner229907c2011-07-18 04:54:35 +00001035AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001036 const Twine &Name, BasicBlock *InsertAtEnd)
1037 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1038 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
1039 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001040 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001041 setName(Name);
1042}
1043
Chris Lattner229907c2011-07-18 04:54:35 +00001044AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001045 Instruction *InsertBefore)
1046 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1047 getAISize(Ty->getContext(), 0), InsertBefore) {
1048 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001049 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001050 setName(Name);
1051}
1052
Chris Lattner229907c2011-07-18 04:54:35 +00001053AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001054 BasicBlock *InsertAtEnd)
1055 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1056 getAISize(Ty->getContext(), 0), InsertAtEnd) {
1057 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001058 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001059 setName(Name);
1060}
1061
Chris Lattner229907c2011-07-18 04:54:35 +00001062AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001063 const Twine &Name, Instruction *InsertBefore)
1064 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +00001065 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001066 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001067 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001068 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069}
1070
Chris Lattner229907c2011-07-18 04:54:35 +00001071AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001072 const Twine &Name, BasicBlock *InsertAtEnd)
1073 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +00001074 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001075 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001076 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001077 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001078}
1079
Gordon Henriksen14a55692007-12-10 02:14:30 +00001080// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001081AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001082}
1083
Victor Hernandez8acf2952009-10-23 21:09:37 +00001084void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001085 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001086 assert(Align <= MaximumAlignment &&
1087 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001088 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +00001089 assert(getAlignment() == Align && "Alignment representation error!");
1090}
1091
Victor Hernandez8acf2952009-10-23 21:09:37 +00001092bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001093 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001094 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001095 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001096}
1097
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001098Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001099 return getType()->getElementType();
1100}
1101
Chris Lattner8b291e62008-11-26 02:54:17 +00001102/// isStaticAlloca - Return true if this alloca is in the entry block of the
1103/// function and is a constant size. If so, the code generator will fold it
1104/// into the prolog/epilog code, so it is basically free.
1105bool AllocaInst::isStaticAlloca() const {
1106 // Must be constant size.
1107 if (!isa<ConstantInt>(getArraySize())) return false;
1108
1109 // Must be in the entry block.
1110 const BasicBlock *Parent = getParent();
1111 return Parent == &Parent->getParent()->front();
1112}
1113
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001114//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115// LoadInst Implementation
1116//===----------------------------------------------------------------------===//
1117
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001118void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001119 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001121 assert(!(isAtomic() && getAlignment() == 0) &&
1122 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001123}
1124
Daniel Dunbar4975db62009-07-25 04:41:11 +00001125LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001126 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001128 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001129 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001130 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001131 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001132 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001133}
1134
Daniel Dunbar4975db62009-07-25 04:41:11 +00001135LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001136 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001137 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001138 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001139 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001140 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001141 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001142 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143}
1144
Daniel Dunbar4975db62009-07-25 04:41:11 +00001145LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001146 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001147 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001148 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001149 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001150 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001151 setAtomic(NotAtomic);
1152 AssertOK();
1153 setName(Name);
1154}
1155
1156LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1157 BasicBlock *InsertAE)
1158 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1159 Load, Ptr, InsertAE) {
1160 setVolatile(isVolatile);
1161 setAlignment(0);
1162 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001163 AssertOK();
1164 setName(Name);
1165}
1166
Daniel Dunbar4975db62009-07-25 04:41:11 +00001167LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001168 unsigned Align, Instruction *InsertBef)
1169 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1170 Load, Ptr, InsertBef) {
1171 setVolatile(isVolatile);
1172 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001173 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001174 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001175 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001176}
1177
Daniel Dunbar4975db62009-07-25 04:41:11 +00001178LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001179 unsigned Align, BasicBlock *InsertAE)
1180 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1181 Load, Ptr, InsertAE) {
1182 setVolatile(isVolatile);
1183 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001184 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +00001185 AssertOK();
1186 setName(Name);
1187}
1188
Eli Friedman59b66882011-08-09 23:02:53 +00001189LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1190 unsigned Align, AtomicOrdering Order,
1191 SynchronizationScope SynchScope,
1192 Instruction *InsertBef)
1193 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1194 Load, Ptr, InsertBef) {
1195 setVolatile(isVolatile);
1196 setAlignment(Align);
1197 setAtomic(Order, SynchScope);
1198 AssertOK();
1199 setName(Name);
1200}
1201
1202LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1203 unsigned Align, AtomicOrdering Order,
1204 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001205 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001206 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001207 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001208 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001209 setAlignment(Align);
1210 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001211 AssertOK();
1212 setName(Name);
1213}
1214
Daniel Dunbar27096822009-08-11 18:11:15 +00001215LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1216 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1217 Load, Ptr, InsertBef) {
1218 setVolatile(false);
1219 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001220 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001221 AssertOK();
1222 if (Name && Name[0]) setName(Name);
1223}
1224
1225LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1226 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1227 Load, Ptr, InsertAE) {
1228 setVolatile(false);
1229 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001230 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001231 AssertOK();
1232 if (Name && Name[0]) setName(Name);
1233}
1234
1235LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1236 Instruction *InsertBef)
1237: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1238 Load, Ptr, InsertBef) {
1239 setVolatile(isVolatile);
1240 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001241 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001242 AssertOK();
1243 if (Name && Name[0]) setName(Name);
1244}
1245
1246LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1247 BasicBlock *InsertAE)
1248 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1249 Load, Ptr, InsertAE) {
1250 setVolatile(isVolatile);
1251 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001252 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001253 AssertOK();
1254 if (Name && Name[0]) setName(Name);
1255}
1256
Christopher Lamb84485702007-04-22 19:24:39 +00001257void LoadInst::setAlignment(unsigned Align) {
1258 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001259 assert(Align <= MaximumAlignment &&
1260 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001261 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001262 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001263 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001264}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265
1266//===----------------------------------------------------------------------===//
1267// StoreInst Implementation
1268//===----------------------------------------------------------------------===//
1269
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001270void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001271 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001272 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001273 "Ptr must have pointer type!");
1274 assert(getOperand(0)->getType() ==
1275 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001276 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001277 assert(!(isAtomic() && getAlignment() == 0) &&
1278 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001279}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001280
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281
1282StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001283 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001284 OperandTraits<StoreInst>::op_begin(this),
1285 OperandTraits<StoreInst>::operands(this),
1286 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001287 Op<0>() = val;
1288 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001289 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001290 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001291 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001292 AssertOK();
1293}
1294
1295StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001296 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001297 OperandTraits<StoreInst>::op_begin(this),
1298 OperandTraits<StoreInst>::operands(this),
1299 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001300 Op<0>() = val;
1301 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001302 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001303 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001304 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001305 AssertOK();
1306}
1307
Misha Brukmanb1c93172005-04-21 23:48:37 +00001308StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001309 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001310 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001311 OperandTraits<StoreInst>::op_begin(this),
1312 OperandTraits<StoreInst>::operands(this),
1313 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001314 Op<0>() = val;
1315 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001316 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001317 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001318 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001319 AssertOK();
1320}
1321
1322StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1323 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001324 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001325 OperandTraits<StoreInst>::op_begin(this),
1326 OperandTraits<StoreInst>::operands(this),
1327 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001328 Op<0>() = val;
1329 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001330 setVolatile(isVolatile);
1331 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001332 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001333 AssertOK();
1334}
1335
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001337 unsigned Align, AtomicOrdering Order,
1338 SynchronizationScope SynchScope,
1339 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001340 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001341 OperandTraits<StoreInst>::op_begin(this),
1342 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001343 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001344 Op<0>() = val;
1345 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001346 setVolatile(isVolatile);
1347 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001348 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001349 AssertOK();
1350}
1351
1352StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001353 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001354 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001355 OperandTraits<StoreInst>::op_begin(this),
1356 OperandTraits<StoreInst>::operands(this),
1357 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001358 Op<0>() = val;
1359 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001360 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001361 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001362 setAtomic(NotAtomic);
1363 AssertOK();
1364}
1365
1366StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1367 unsigned Align, BasicBlock *InsertAtEnd)
1368 : Instruction(Type::getVoidTy(val->getContext()), Store,
1369 OperandTraits<StoreInst>::op_begin(this),
1370 OperandTraits<StoreInst>::operands(this),
1371 InsertAtEnd) {
1372 Op<0>() = val;
1373 Op<1>() = addr;
1374 setVolatile(isVolatile);
1375 setAlignment(Align);
1376 setAtomic(NotAtomic);
1377 AssertOK();
1378}
1379
1380StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1381 unsigned Align, AtomicOrdering Order,
1382 SynchronizationScope SynchScope,
1383 BasicBlock *InsertAtEnd)
1384 : Instruction(Type::getVoidTy(val->getContext()), Store,
1385 OperandTraits<StoreInst>::op_begin(this),
1386 OperandTraits<StoreInst>::operands(this),
1387 InsertAtEnd) {
1388 Op<0>() = val;
1389 Op<1>() = addr;
1390 setVolatile(isVolatile);
1391 setAlignment(Align);
1392 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001393 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001394}
1395
Christopher Lamb84485702007-04-22 19:24:39 +00001396void StoreInst::setAlignment(unsigned Align) {
1397 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001398 assert(Align <= MaximumAlignment &&
1399 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001400 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001401 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001402 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001403}
1404
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001405//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001406// AtomicCmpXchgInst Implementation
1407//===----------------------------------------------------------------------===//
1408
1409void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1410 AtomicOrdering Ordering,
1411 SynchronizationScope SynchScope) {
1412 Op<0>() = Ptr;
1413 Op<1>() = Cmp;
1414 Op<2>() = NewVal;
1415 setOrdering(Ordering);
1416 setSynchScope(SynchScope);
1417
1418 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1419 "All operands must be non-null!");
1420 assert(getOperand(0)->getType()->isPointerTy() &&
1421 "Ptr must have pointer type!");
1422 assert(getOperand(1)->getType() ==
1423 cast<PointerType>(getOperand(0)->getType())->getElementType()
1424 && "Ptr must be a pointer to Cmp type!");
1425 assert(getOperand(2)->getType() ==
1426 cast<PointerType>(getOperand(0)->getType())->getElementType()
1427 && "Ptr must be a pointer to NewVal type!");
1428 assert(Ordering != NotAtomic &&
1429 "AtomicCmpXchg instructions must be atomic!");
1430}
1431
1432AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1433 AtomicOrdering Ordering,
1434 SynchronizationScope SynchScope,
1435 Instruction *InsertBefore)
1436 : Instruction(Cmp->getType(), AtomicCmpXchg,
1437 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1438 OperandTraits<AtomicCmpXchgInst>::operands(this),
1439 InsertBefore) {
1440 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1441}
1442
1443AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1444 AtomicOrdering Ordering,
1445 SynchronizationScope SynchScope,
1446 BasicBlock *InsertAtEnd)
1447 : Instruction(Cmp->getType(), AtomicCmpXchg,
1448 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1449 OperandTraits<AtomicCmpXchgInst>::operands(this),
1450 InsertAtEnd) {
1451 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1452}
1453
1454//===----------------------------------------------------------------------===//
1455// AtomicRMWInst Implementation
1456//===----------------------------------------------------------------------===//
1457
1458void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1459 AtomicOrdering Ordering,
1460 SynchronizationScope SynchScope) {
1461 Op<0>() = Ptr;
1462 Op<1>() = Val;
1463 setOperation(Operation);
1464 setOrdering(Ordering);
1465 setSynchScope(SynchScope);
1466
1467 assert(getOperand(0) && getOperand(1) &&
1468 "All operands must be non-null!");
1469 assert(getOperand(0)->getType()->isPointerTy() &&
1470 "Ptr must have pointer type!");
1471 assert(getOperand(1)->getType() ==
1472 cast<PointerType>(getOperand(0)->getType())->getElementType()
1473 && "Ptr must be a pointer to Val type!");
1474 assert(Ordering != NotAtomic &&
1475 "AtomicRMW instructions must be atomic!");
1476}
1477
1478AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1479 AtomicOrdering Ordering,
1480 SynchronizationScope SynchScope,
1481 Instruction *InsertBefore)
1482 : Instruction(Val->getType(), AtomicRMW,
1483 OperandTraits<AtomicRMWInst>::op_begin(this),
1484 OperandTraits<AtomicRMWInst>::operands(this),
1485 InsertBefore) {
1486 Init(Operation, Ptr, Val, Ordering, SynchScope);
1487}
1488
1489AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1490 AtomicOrdering Ordering,
1491 SynchronizationScope SynchScope,
1492 BasicBlock *InsertAtEnd)
1493 : Instruction(Val->getType(), AtomicRMW,
1494 OperandTraits<AtomicRMWInst>::op_begin(this),
1495 OperandTraits<AtomicRMWInst>::operands(this),
1496 InsertAtEnd) {
1497 Init(Operation, Ptr, Val, Ordering, SynchScope);
1498}
1499
1500//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001501// FenceInst Implementation
1502//===----------------------------------------------------------------------===//
1503
1504FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1505 SynchronizationScope SynchScope,
1506 Instruction *InsertBefore)
1507 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1508 setOrdering(Ordering);
1509 setSynchScope(SynchScope);
1510}
1511
1512FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1513 SynchronizationScope SynchScope,
1514 BasicBlock *InsertAtEnd)
1515 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1516 setOrdering(Ordering);
1517 setSynchScope(SynchScope);
1518}
1519
1520//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521// GetElementPtrInst Implementation
1522//===----------------------------------------------------------------------===//
1523
Jay Foadd1b78492011-07-25 09:48:08 +00001524void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001525 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001526 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1527 OperandList[0] = Ptr;
1528 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001529 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001530}
1531
Gabor Greiff6caff662008-05-10 08:32:32 +00001532GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001533 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001534 OperandTraits<GetElementPtrInst>::op_end(this)
1535 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001536 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001537 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001538 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001539}
1540
Chris Lattner6090a422009-03-09 04:46:40 +00001541/// getIndexedType - Returns the type of the element that would be accessed with
1542/// a gep instruction with the specified parameters.
1543///
1544/// The Idxs pointer should point to a continuous piece of memory containing the
1545/// indices, either as Value* or uint64_t.
1546///
1547/// A null type is returned if the indices are invalid for the specified
1548/// pointer type.
1549///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001550template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001551static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001552 if (Ptr->isVectorTy()) {
1553 assert(IdxList.size() == 1 &&
1554 "GEP with vector pointers must have a single index");
1555 PointerType *PTy = dyn_cast<PointerType>(
1556 cast<VectorType>(Ptr)->getElementType());
1557 assert(PTy && "Gep with invalid vector pointer found");
1558 return PTy->getElementType();
1559 }
1560
Chris Lattner229907c2011-07-18 04:54:35 +00001561 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001562 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001563 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564
Chris Lattner6090a422009-03-09 04:46:40 +00001565 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001566 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001567 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001568
Chris Lattner6090a422009-03-09 04:46:40 +00001569 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001570 // it cannot be 'stepped over'.
1571 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001572 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001573
Dan Gohman1ecaf452008-05-31 00:58:22 +00001574 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001575 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001576 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001577 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001578 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001579 if (!CT->indexValid(Index)) return 0;
1580 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001581 }
Jay Foadd1b78492011-07-25 09:48:08 +00001582 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001583}
1584
Jay Foadd1b78492011-07-25 09:48:08 +00001585Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1586 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001587}
1588
Chris Lattner229907c2011-07-18 04:54:35 +00001589Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001590 ArrayRef<Constant *> IdxList) {
1591 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001592}
1593
Jay Foadd1b78492011-07-25 09:48:08 +00001594Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1595 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001596}
1597
Nadav Rotem3924cb02011-12-05 06:29:09 +00001598unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1599 Type *Ty = Ptr->getType();
1600
1601 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1602 Ty = VTy->getElementType();
1603
1604 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1605 return PTy->getAddressSpace();
1606
Craig Topperc514b542012-02-05 22:14:15 +00001607 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001608}
1609
Chris Lattner45f15572007-04-14 00:12:57 +00001610/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1611/// zeros. If so, the result pointer and the first operand have the same
1612/// value, just potentially different types.
1613bool GetElementPtrInst::hasAllZeroIndices() const {
1614 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1615 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1616 if (!CI->isZero()) return false;
1617 } else {
1618 return false;
1619 }
1620 }
1621 return true;
1622}
1623
Chris Lattner27058292007-04-27 20:35:56 +00001624/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1625/// constant integers. If so, the result pointer and the first operand have
1626/// a constant offset between them.
1627bool GetElementPtrInst::hasAllConstantIndices() const {
1628 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1629 if (!isa<ConstantInt>(getOperand(i)))
1630 return false;
1631 }
1632 return true;
1633}
1634
Dan Gohman1b849082009-09-07 23:54:19 +00001635void GetElementPtrInst::setIsInBounds(bool B) {
1636 cast<GEPOperator>(this)->setIsInBounds(B);
1637}
Chris Lattner45f15572007-04-14 00:12:57 +00001638
Nick Lewycky28a5f252009-09-27 21:33:04 +00001639bool GetElementPtrInst::isInBounds() const {
1640 return cast<GEPOperator>(this)->isInBounds();
1641}
1642
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001644// ExtractElementInst Implementation
1645//===----------------------------------------------------------------------===//
1646
1647ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001648 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001649 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001650 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001651 ExtractElement,
1652 OperandTraits<ExtractElementInst>::op_begin(this),
1653 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001654 assert(isValidOperands(Val, Index) &&
1655 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001656 Op<0>() = Val;
1657 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001658 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001659}
1660
1661ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001662 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001663 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001664 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001665 ExtractElement,
1666 OperandTraits<ExtractElementInst>::op_begin(this),
1667 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001668 assert(isValidOperands(Val, Index) &&
1669 "Invalid extractelement instruction operands!");
1670
Gabor Greif2d3024d2008-05-26 21:33:52 +00001671 Op<0>() = Val;
1672 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001673 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001674}
1675
Chris Lattner65511ff2006-10-05 06:24:58 +00001676
Chris Lattner54865b32006-04-08 04:05:48 +00001677bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001678 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001679 return false;
1680 return true;
1681}
1682
1683
Robert Bocchino23004482006-01-10 19:05:34 +00001684//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001685// InsertElementInst Implementation
1686//===----------------------------------------------------------------------===//
1687
Chris Lattner54865b32006-04-08 04:05:48 +00001688InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001689 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001690 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001691 : Instruction(Vec->getType(), InsertElement,
1692 OperandTraits<InsertElementInst>::op_begin(this),
1693 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001694 assert(isValidOperands(Vec, Elt, Index) &&
1695 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001696 Op<0>() = Vec;
1697 Op<1>() = Elt;
1698 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001699 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001700}
1701
Chris Lattner54865b32006-04-08 04:05:48 +00001702InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001703 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001704 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001705 : Instruction(Vec->getType(), InsertElement,
1706 OperandTraits<InsertElementInst>::op_begin(this),
1707 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001708 assert(isValidOperands(Vec, Elt, Index) &&
1709 "Invalid insertelement instruction operands!");
1710
Gabor Greif2d3024d2008-05-26 21:33:52 +00001711 Op<0>() = Vec;
1712 Op<1>() = Elt;
1713 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001714 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001715}
1716
Chris Lattner54865b32006-04-08 04:05:48 +00001717bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1718 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001719 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001720 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001721
Reid Spencerd84d35b2007-02-15 02:26:10 +00001722 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001723 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001724
Duncan Sands9dff9be2010-02-15 16:12:20 +00001725 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001726 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001727 return true;
1728}
1729
1730
Robert Bocchinoca27f032006-01-17 20:07:22 +00001731//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001732// ShuffleVectorInst Implementation
1733//===----------------------------------------------------------------------===//
1734
1735ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001736 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001737 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001738: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001739 cast<VectorType>(Mask->getType())->getNumElements()),
1740 ShuffleVector,
1741 OperandTraits<ShuffleVectorInst>::op_begin(this),
1742 OperandTraits<ShuffleVectorInst>::operands(this),
1743 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001744 assert(isValidOperands(V1, V2, Mask) &&
1745 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001746 Op<0>() = V1;
1747 Op<1>() = V2;
1748 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001749 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001750}
1751
1752ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001753 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001754 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001755: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1756 cast<VectorType>(Mask->getType())->getNumElements()),
1757 ShuffleVector,
1758 OperandTraits<ShuffleVectorInst>::op_begin(this),
1759 OperandTraits<ShuffleVectorInst>::operands(this),
1760 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001761 assert(isValidOperands(V1, V2, Mask) &&
1762 "Invalid shuffle vector instruction operands!");
1763
Gabor Greif2d3024d2008-05-26 21:33:52 +00001764 Op<0>() = V1;
1765 Op<1>() = V2;
1766 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001767 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001768}
1769
Mon P Wang25f01062008-11-10 04:46:22 +00001770bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001771 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001772 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001773 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001774 return false;
1775
Chris Lattner1dcb6542012-01-25 23:49:49 +00001776 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001777 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001778 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001779 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001780
1781 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001782 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1783 return true;
1784
Nate Begeman60a31c32010-08-13 00:16:46 +00001785 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001786 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001787 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001788 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1789 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001790 return false;
1791 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1792 return false;
1793 }
1794 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001795 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001796 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001797
1798 if (const ConstantDataSequential *CDS =
1799 dyn_cast<ConstantDataSequential>(Mask)) {
1800 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1801 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1802 if (CDS->getElementAsInteger(i) >= V1Size*2)
1803 return false;
1804 return true;
1805 }
1806
1807 // The bitcode reader can create a place holder for a forward reference
1808 // used as the shuffle mask. When this occurs, the shuffle mask will
1809 // fall into this case and fail. To avoid this error, do this bit of
1810 // ugliness to allow such a mask pass.
1811 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1812 if (CE->getOpcode() == Instruction::UserOp1)
1813 return true;
1814
1815 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001816}
1817
Chris Lattnerf724e342008-03-02 05:28:33 +00001818/// getMaskValue - Return the index from the shuffle mask for the specified
1819/// output result. This is either -1 if the element is undef or a number less
1820/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001821int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1822 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1823 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001824 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001825 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001826 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001827 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001828 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001829}
1830
Chris Lattner1dcb6542012-01-25 23:49:49 +00001831/// getShuffleMask - Return the full mask for this instruction, where each
1832/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001833void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1834 SmallVectorImpl<int> &Result) {
1835 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001836
Chris Lattnercf129702012-01-26 02:51:13 +00001837 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001838 for (unsigned i = 0; i != NumElts; ++i)
1839 Result.push_back(CDS->getElementAsInteger(i));
1840 return;
1841 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001842 for (unsigned i = 0; i != NumElts; ++i) {
1843 Constant *C = Mask->getAggregateElement(i);
1844 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001845 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001846 }
1847}
1848
1849
Dan Gohman12fce772008-05-15 19:50:34 +00001850//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001851// InsertValueInst Class
1852//===----------------------------------------------------------------------===//
1853
Jay Foad57aa6362011-07-13 10:26:04 +00001854void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1855 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001856 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001857
1858 // There's no fundamental reason why we require at least one index
1859 // (other than weirdness with &*IdxBegin being invalid; see
1860 // getelementptr's init routine for example). But there's no
1861 // present need to support it.
1862 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1863
1864 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001865 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001866 Op<0>() = Agg;
1867 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001868
Jay Foad57aa6362011-07-13 10:26:04 +00001869 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001870 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001871}
1872
1873InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001874 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001875 OperandTraits<InsertValueInst>::op_begin(this), 2),
1876 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001877 Op<0>() = IVI.getOperand(0);
1878 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001879 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001880}
1881
1882//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001883// ExtractValueInst Class
1884//===----------------------------------------------------------------------===//
1885
Jay Foad57aa6362011-07-13 10:26:04 +00001886void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001887 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001888
Jay Foad57aa6362011-07-13 10:26:04 +00001889 // There's no fundamental reason why we require at least one index.
1890 // But there's no present need to support it.
1891 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001892
Jay Foad57aa6362011-07-13 10:26:04 +00001893 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001894 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001895}
1896
1897ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001898 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001899 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001900 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001901}
1902
Dan Gohman12fce772008-05-15 19:50:34 +00001903// getIndexedType - Returns the type of the element that would be extracted
1904// with an extractvalue instruction with the specified parameters.
1905//
1906// A null type is returned if the indices are invalid for the specified
1907// pointer type.
1908//
Chris Lattner229907c2011-07-18 04:54:35 +00001909Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001910 ArrayRef<unsigned> Idxs) {
1911 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001912 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001913 // We can't use CompositeType::indexValid(Index) here.
1914 // indexValid() always returns true for arrays because getelementptr allows
1915 // out-of-bounds indices. Since we don't allow those for extractvalue and
1916 // insertvalue we need to check array indexing manually.
1917 // Since the only other types we can index into are struct types it's just
1918 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001919 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001920 if (Index >= AT->getNumElements())
1921 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001922 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001923 if (Index >= ST->getNumElements())
1924 return 0;
1925 } else {
1926 // Not a valid type to index into.
1927 return 0;
1928 }
1929
1930 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001931 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001932 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001933}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001934
1935//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001936// BinaryOperator Class
1937//===----------------------------------------------------------------------===//
1938
Chris Lattner2195fc42007-02-24 00:55:48 +00001939BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001940 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001941 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001942 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001943 OperandTraits<BinaryOperator>::op_begin(this),
1944 OperandTraits<BinaryOperator>::operands(this),
1945 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001946 Op<0>() = S1;
1947 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001948 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001949 setName(Name);
1950}
1951
1952BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001953 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001954 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001955 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001956 OperandTraits<BinaryOperator>::op_begin(this),
1957 OperandTraits<BinaryOperator>::operands(this),
1958 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001959 Op<0>() = S1;
1960 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001961 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001962 setName(Name);
1963}
1964
1965
1966void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001967 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001968 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001969 assert(LHS->getType() == RHS->getType() &&
1970 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001971#ifndef NDEBUG
1972 switch (iType) {
1973 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001974 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001975 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001976 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001977 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001978 "Tried to create an integer operation on a non-integer type!");
1979 break;
1980 case FAdd: case FSub:
1981 case FMul:
1982 assert(getType() == LHS->getType() &&
1983 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001984 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001985 "Tried to create a floating-point operation on a "
1986 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001988 case UDiv:
1989 case SDiv:
1990 assert(getType() == LHS->getType() &&
1991 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001992 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001993 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001994 "Incorrect operand type (not integer) for S/UDIV");
1995 break;
1996 case FDiv:
1997 assert(getType() == LHS->getType() &&
1998 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001999 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002000 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002001 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002002 case URem:
2003 case SRem:
2004 assert(getType() == LHS->getType() &&
2005 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002006 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002007 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002008 "Incorrect operand type (not integer) for S/UREM");
2009 break;
2010 case FRem:
2011 assert(getType() == LHS->getType() &&
2012 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002013 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002014 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002015 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002016 case Shl:
2017 case LShr:
2018 case AShr:
2019 assert(getType() == LHS->getType() &&
2020 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002021 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002022 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002023 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002024 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002025 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 case And: case Or:
2027 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002028 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002029 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002030 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002031 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002032 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002033 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002034 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002035 default:
2036 break;
2037 }
2038#endif
2039}
2040
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002041BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002042 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002043 Instruction *InsertBefore) {
2044 assert(S1->getType() == S2->getType() &&
2045 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002046 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002047}
2048
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002050 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002053 InsertAtEnd->getInstList().push_back(Res);
2054 return Res;
2055}
2056
Dan Gohman5476cfd2009-08-12 16:23:25 +00002057BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002058 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002059 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002060 return new BinaryOperator(Instruction::Sub,
2061 zero, Op,
2062 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002063}
2064
Dan Gohman5476cfd2009-08-12 16:23:25 +00002065BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002066 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002067 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002068 return new BinaryOperator(Instruction::Sub,
2069 zero, Op,
2070 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002071}
2072
Dan Gohman4ab44202009-12-18 02:58:50 +00002073BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2074 Instruction *InsertBefore) {
2075 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2076 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2077}
2078
2079BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2080 BasicBlock *InsertAtEnd) {
2081 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2082 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2083}
2084
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002085BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2086 Instruction *InsertBefore) {
2087 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2088 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2089}
2090
2091BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2092 BasicBlock *InsertAtEnd) {
2093 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2094 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2095}
2096
Dan Gohman5476cfd2009-08-12 16:23:25 +00002097BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002098 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002099 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002100 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002101 Op->getType(), Name, InsertBefore);
2102}
2103
Dan Gohman5476cfd2009-08-12 16:23:25 +00002104BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002105 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002106 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002107 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002108 Op->getType(), Name, InsertAtEnd);
2109}
2110
Dan Gohman5476cfd2009-08-12 16:23:25 +00002111BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002113 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002114 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 Op->getType(), Name, InsertBefore);
2116}
2117
Dan Gohman5476cfd2009-08-12 16:23:25 +00002118BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002119 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002120 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002121 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002122 Op->getType(), Name, InsertAtEnd);
2123}
2124
2125
2126// isConstantAllOnes - Helper function for several functions below
2127static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002128 if (const Constant *C = dyn_cast<Constant>(V))
2129 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002130 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002131}
2132
Owen Andersonbb2501b2009-07-13 22:18:28 +00002133bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2135 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002136 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2137 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002138 return false;
2139}
2140
Owen Andersonbb2501b2009-07-13 22:18:28 +00002141bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002142 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2143 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002144 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00002145 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00002146 return false;
2147}
2148
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002149bool BinaryOperator::isNot(const Value *V) {
2150 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2151 return (Bop->getOpcode() == Instruction::Xor &&
2152 (isConstantAllOnes(Bop->getOperand(1)) ||
2153 isConstantAllOnes(Bop->getOperand(0))));
2154 return false;
2155}
2156
Chris Lattner2c7d1772005-04-24 07:28:37 +00002157Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002158 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002159}
2160
Chris Lattner2c7d1772005-04-24 07:28:37 +00002161const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2162 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002163}
2164
Dan Gohmana5b96452009-06-04 22:49:04 +00002165Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002166 return cast<BinaryOperator>(BinOp)->getOperand(1);
2167}
2168
2169const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2170 return getFNegArgument(const_cast<Value*>(BinOp));
2171}
2172
Chris Lattner2c7d1772005-04-24 07:28:37 +00002173Value *BinaryOperator::getNotArgument(Value *BinOp) {
2174 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2175 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2176 Value *Op0 = BO->getOperand(0);
2177 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002178 if (isConstantAllOnes(Op0)) return Op1;
2179
2180 assert(isConstantAllOnes(Op1));
2181 return Op0;
2182}
2183
Chris Lattner2c7d1772005-04-24 07:28:37 +00002184const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2185 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002186}
2187
2188
2189// swapOperands - Exchange the two operands to this instruction. This
2190// instruction is safe to use on any binary instruction and does not
2191// modify the semantics of the instruction. If the instruction is
2192// order dependent (SetLT f.e.) the opcode is changed.
2193//
2194bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002195 if (!isCommutative())
2196 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002197 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198 return false;
2199}
2200
Dan Gohman1b849082009-09-07 23:54:19 +00002201void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2202 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2203}
2204
2205void BinaryOperator::setHasNoSignedWrap(bool b) {
2206 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2207}
2208
2209void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002210 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002211}
2212
Nick Lewycky28a5f252009-09-27 21:33:04 +00002213bool BinaryOperator::hasNoUnsignedWrap() const {
2214 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2215}
2216
2217bool BinaryOperator::hasNoSignedWrap() const {
2218 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2219}
2220
2221bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002222 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002223}
2224
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002225//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002226// FPMathOperator Class
2227//===----------------------------------------------------------------------===//
2228
2229/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2230/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002231/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002232float FPMathOperator::getFPAccuracy() const {
2233 const MDNode *MD =
2234 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2235 if (!MD)
2236 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002237 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2238 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002239}
2240
2241
2242//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002243// CastInst Class
2244//===----------------------------------------------------------------------===//
2245
David Blaikie3a15e142011-12-01 08:00:17 +00002246void CastInst::anchor() {}
2247
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248// Just determine if this cast only deals with integral->integral conversion.
2249bool CastInst::isIntegerCast() const {
2250 switch (getOpcode()) {
2251 default: return false;
2252 case Instruction::ZExt:
2253 case Instruction::SExt:
2254 case Instruction::Trunc:
2255 return true;
2256 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002257 return getOperand(0)->getType()->isIntegerTy() &&
2258 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002260}
2261
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262bool CastInst::isLosslessCast() const {
2263 // Only BitCast can be lossless, exit fast if we're not BitCast
2264 if (getOpcode() != Instruction::BitCast)
2265 return false;
2266
2267 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002268 Type* SrcTy = getOperand(0)->getType();
2269 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 if (SrcTy == DstTy)
2271 return true;
2272
Reid Spencer8d9336d2006-12-31 05:26:44 +00002273 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002274 if (SrcTy->isPointerTy())
2275 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276 return false; // Other types have no identity values
2277}
2278
2279/// This function determines if the CastInst does not require any bits to be
2280/// changed in order to effect the cast. Essentially, it identifies cases where
2281/// no code gen is necessary for the cast, hence the name no-op cast. For
2282/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002283/// # bitcast i32* %x to i8*
2284/// # bitcast <2 x i32> %x to <4 x i16>
2285/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002286/// @brief Determine if the described cast is a no-op.
2287bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002288 Type *SrcTy,
2289 Type *DestTy,
2290 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002291 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002292 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 case Instruction::Trunc:
2294 case Instruction::ZExt:
2295 case Instruction::SExt:
2296 case Instruction::FPTrunc:
2297 case Instruction::FPExt:
2298 case Instruction::UIToFP:
2299 case Instruction::SIToFP:
2300 case Instruction::FPToUI:
2301 case Instruction::FPToSI:
2302 return false; // These always modify bits
2303 case Instruction::BitCast:
2304 return true; // BitCast never modifies bits.
2305 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002306 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002307 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002309 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002310 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 }
2312}
2313
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002314/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002315bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002316 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2317}
2318
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319/// This function determines if a pair of casts can be eliminated and what
2320/// opcode should be used in the elimination. This assumes that there are two
2321/// instructions like this:
2322/// * %F = firstOpcode SrcTy %x to MidTy
2323/// * %S = secondOpcode MidTy %F to DstTy
2324/// The function returns a resultOpcode so these two casts can be replaced with:
2325/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2326/// If no such cast is permited, the function returns 0.
2327unsigned CastInst::isEliminableCastPair(
2328 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002329 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 // Define the 144 possibilities for these two cast instructions. The values
2331 // in this matrix determine what to do in a given situation and select the
2332 // case in the switch below. The rows correspond to firstOp, the columns
2333 // correspond to secondOp. In looking at the table below, keep in mind
2334 // the following cast properties:
2335 //
2336 // Size Compare Source Destination
2337 // Operator Src ? Size Type Sign Type Sign
2338 // -------- ------------ ------------------- ---------------------
2339 // TRUNC > Integer Any Integral Any
2340 // ZEXT < Integral Unsigned Integer Any
2341 // SEXT < Integral Signed Integer Any
2342 // FPTOUI n/a FloatPt n/a Integral Unsigned
2343 // FPTOSI n/a FloatPt n/a Integral Signed
2344 // UITOFP n/a Integral Unsigned FloatPt n/a
2345 // SITOFP n/a Integral Signed FloatPt n/a
2346 // FPTRUNC > FloatPt n/a FloatPt n/a
2347 // FPEXT < FloatPt n/a FloatPt n/a
2348 // PTRTOINT n/a Pointer n/a Integral Unsigned
2349 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002350 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002351 //
2352 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002353 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2354 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002355 // of the produced value (we no longer know the top-part is all zeros).
2356 // Further this conversion is often much more expensive for typical hardware,
2357 // and causes issues when building libgcc. We disallow fptosi+sext for the
2358 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 const unsigned numCastOps =
2360 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2361 static const uint8_t CastResults[numCastOps][numCastOps] = {
2362 // T F F U S F F P I B -+
2363 // R Z S P P I I T P 2 N T |
2364 // U E E 2 2 2 2 R E I T C +- secondOp
2365 // N X X U S F F N X N 2 V |
2366 // C T T I I P P C T T P T -+
2367 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2368 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2369 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002370 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2371 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2373 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2374 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2375 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2376 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2377 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2378 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2379 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002380
2381 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002382 // merging. However, bitcast of A->B->A are allowed.
2383 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2384 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2385 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2386
2387 // Check if any of the bitcasts convert scalars<->vectors.
2388 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2389 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2390 // Unless we are bitcasing to the original type, disallow optimizations.
2391 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392
2393 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2394 [secondOp-Instruction::CastOpsBegin];
2395 switch (ElimCase) {
2396 case 0:
2397 // categorically disallowed
2398 return 0;
2399 case 1:
2400 // allowed, use first cast's opcode
2401 return firstOp;
2402 case 2:
2403 // allowed, use second cast's opcode
2404 return secondOp;
2405 case 3:
2406 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002407 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002408 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002409 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 return firstOp;
2411 return 0;
2412 case 4:
2413 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002414 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002415 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 return firstOp;
2417 return 0;
2418 case 5:
2419 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002420 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002421 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return secondOp;
2423 return 0;
2424 case 6:
2425 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002426 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002427 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return secondOp;
2429 return 0;
2430 case 7: {
2431 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002432 if (!IntPtrTy)
2433 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002434 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2435 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 if (MidSize >= PtrSize)
2437 return Instruction::BitCast;
2438 return 0;
2439 }
2440 case 8: {
2441 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2442 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2443 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002444 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2445 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 if (SrcSize == DstSize)
2447 return Instruction::BitCast;
2448 else if (SrcSize < DstSize)
2449 return firstOp;
2450 return secondOp;
2451 }
2452 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2453 return Instruction::ZExt;
2454 case 10:
2455 // fpext followed by ftrunc is allowed if the bit size returned to is
2456 // the same as the original, in which case its just a bitcast
2457 if (SrcTy == DstTy)
2458 return Instruction::BitCast;
2459 return 0; // If the types are not the same we can't eliminate it.
2460 case 11:
2461 // bitcast followed by ptrtoint is allowed as long as the bitcast
2462 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002463 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 return secondOp;
2465 return 0;
2466 case 12:
2467 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002468 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 return firstOp;
2470 return 0;
2471 case 13: {
2472 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002473 if (!IntPtrTy)
2474 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002475 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2476 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2477 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 if (SrcSize <= PtrSize && SrcSize == DstSize)
2479 return Instruction::BitCast;
2480 return 0;
2481 }
2482 case 99:
2483 // cast combination can't happen (error in input). This is for all cases
2484 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002485 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 default:
Craig Topperc514b542012-02-05 22:14:15 +00002487 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489}
2490
Chris Lattner229907c2011-07-18 04:54:35 +00002491CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002492 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002493 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 // Construct and return the appropriate CastInst subclass
2495 switch (op) {
2496 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2497 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2498 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2499 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2500 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2501 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2502 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2503 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2504 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2505 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2506 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2507 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002508 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510}
2511
Chris Lattner229907c2011-07-18 04:54:35 +00002512CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002513 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002514 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515 // Construct and return the appropriate CastInst subclass
2516 switch (op) {
2517 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2518 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2519 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2520 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2521 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2522 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2523 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2524 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2525 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2526 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2527 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2528 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002529 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531}
2532
Chris Lattner229907c2011-07-18 04:54:35 +00002533CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002534 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002535 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002536 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002537 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2538 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002539}
2540
Chris Lattner229907c2011-07-18 04:54:35 +00002541CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002542 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002543 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002544 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002545 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2546 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002547}
2548
Chris Lattner229907c2011-07-18 04:54:35 +00002549CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002550 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002551 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002552 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002553 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2554 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002555}
2556
Chris Lattner229907c2011-07-18 04:54:35 +00002557CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002558 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002559 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002560 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002561 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2562 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002563}
2564
Chris Lattner229907c2011-07-18 04:54:35 +00002565CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002566 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002567 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002568 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002569 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2570 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002571}
2572
Chris Lattner229907c2011-07-18 04:54:35 +00002573CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002574 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002575 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002576 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002577 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2578 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002579}
2580
Chris Lattner229907c2011-07-18 04:54:35 +00002581CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002582 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002583 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002584 assert(S->getType()->isPointerTy() && "Invalid cast");
2585 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002586 "Invalid cast");
2587
Duncan Sands9dff9be2010-02-15 16:12:20 +00002588 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002589 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2590 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002591}
2592
2593/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002594CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002596 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002597 assert(S->getType()->isPointerTy() && "Invalid cast");
2598 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002599 "Invalid cast");
2600
Duncan Sands9dff9be2010-02-15 16:12:20 +00002601 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002602 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2603 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002604}
2605
Chris Lattner229907c2011-07-18 04:54:35 +00002606CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002608 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002609 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002610 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002611 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2612 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002613 Instruction::CastOps opcode =
2614 (SrcBits == DstBits ? Instruction::BitCast :
2615 (SrcBits > DstBits ? Instruction::Trunc :
2616 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002617 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002618}
2619
Chris Lattner229907c2011-07-18 04:54:35 +00002620CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002622 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002623 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002624 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002625 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2626 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002627 Instruction::CastOps opcode =
2628 (SrcBits == DstBits ? Instruction::BitCast :
2629 (SrcBits > DstBits ? Instruction::Trunc :
2630 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002631 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002632}
2633
Chris Lattner229907c2011-07-18 04:54:35 +00002634CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002636 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002637 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002638 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2640 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002641 Instruction::CastOps opcode =
2642 (SrcBits == DstBits ? Instruction::BitCast :
2643 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002644 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002645}
2646
Chris Lattner229907c2011-07-18 04:54:35 +00002647CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002648 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002649 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002650 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002651 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002652 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2653 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002654 Instruction::CastOps opcode =
2655 (SrcBits == DstBits ? Instruction::BitCast :
2656 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002657 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002658}
2659
Duncan Sands55e50902008-01-06 10:12:28 +00002660// Check whether it is valid to call getCastOpcode for these types.
2661// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002662bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002663 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2664 return false;
2665
2666 if (SrcTy == DestTy)
2667 return true;
2668
Chris Lattner229907c2011-07-18 04:54:35 +00002669 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2670 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002671 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2672 // An element by element cast. Valid if casting the elements is valid.
2673 SrcTy = SrcVecTy->getElementType();
2674 DestTy = DestVecTy->getElementType();
2675 }
2676
Duncan Sands55e50902008-01-06 10:12:28 +00002677 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002678 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2679 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002680
2681 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002682 if (DestTy->isIntegerTy()) { // Casting to integral
2683 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002684 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002685 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002686 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002687 } else if (SrcTy->isVectorTy()) { // Casting from vector
2688 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002689 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002690 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002691 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002692 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2693 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002694 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002695 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002696 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002697 } else if (SrcTy->isVectorTy()) { // Casting from vector
2698 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002699 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002700 return false;
2701 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002702 } else if (DestTy->isVectorTy()) { // Casting to vector
2703 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002704 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002705 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002706 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002707 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002708 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002709 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002710 return false;
2711 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002712 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002713 if (SrcTy->isVectorTy()) {
2714 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002715 } else {
2716 return false;
2717 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002718 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002719 return false;
2720 }
2721}
2722
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723// Provide a way to get a "cast" where the cast opcode is inferred from the
2724// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002725// logic in the castIsValid function below. This axiom should hold:
2726// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2727// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002729// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002731CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002732 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2733 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734
Duncan Sands55e50902008-01-06 10:12:28 +00002735 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2736 "Only first class types are castable!");
2737
Duncan Sandsa8514532011-05-18 07:13:41 +00002738 if (SrcTy == DestTy)
2739 return BitCast;
2740
Chris Lattner229907c2011-07-18 04:54:35 +00002741 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2742 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002743 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2744 // An element by element cast. Find the appropriate opcode based on the
2745 // element types.
2746 SrcTy = SrcVecTy->getElementType();
2747 DestTy = DestVecTy->getElementType();
2748 }
2749
2750 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002751 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2752 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002753
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002754 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002755 if (DestTy->isIntegerTy()) { // Casting to integral
2756 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757 if (DestBits < SrcBits)
2758 return Trunc; // int -> smaller int
2759 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002760 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761 return SExt; // signed -> SEXT
2762 else
2763 return ZExt; // unsigned -> ZEXT
2764 } else {
2765 return BitCast; // Same size, No-op cast
2766 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002767 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002768 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769 return FPToSI; // FP -> sint
2770 else
2771 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002772 } else if (SrcTy->isVectorTy()) {
2773 assert(DestBits == SrcBits &&
2774 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775 return BitCast; // Same size, no-op cast
2776 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002777 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002778 "Casting from a value that is not first-class type");
2779 return PtrToInt; // ptr -> int
2780 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002781 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2782 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002783 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784 return SIToFP; // sint -> FP
2785 else
2786 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002787 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788 if (DestBits < SrcBits) {
2789 return FPTrunc; // FP -> smaller FP
2790 } else if (DestBits > SrcBits) {
2791 return FPExt; // FP -> larger FP
2792 } else {
2793 return BitCast; // same size, no-op cast
2794 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002795 } else if (SrcTy->isVectorTy()) {
2796 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002797 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002798 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002799 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002800 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002801 } else if (DestTy->isVectorTy()) {
2802 assert(DestBits == SrcBits &&
2803 "Illegal cast to vector (wrong type or size)");
2804 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002805 } else if (DestTy->isPointerTy()) {
2806 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002808 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002809 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002810 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002811 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002812 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002813 if (SrcTy->isVectorTy()) {
2814 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002815 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002816 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002817 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002819 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002820}
2821
2822//===----------------------------------------------------------------------===//
2823// CastInst SubClass Constructors
2824//===----------------------------------------------------------------------===//
2825
2826/// Check that the construction parameters for a CastInst are correct. This
2827/// could be broken out into the separate constructors but it is useful to have
2828/// it in one place and to eliminate the redundant code for getting the sizes
2829/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002830bool
Chris Lattner229907c2011-07-18 04:54:35 +00002831CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002832
2833 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002835 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2836 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837 return false;
2838
2839 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002840 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2841 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002842
Duncan Sands7f646562011-05-18 09:21:57 +00002843 // If these are vector types, get the lengths of the vectors (using zero for
2844 // scalar types means that checking that vector lengths match also checks that
2845 // scalars are not being converted to vectors or vectors to scalars).
2846 unsigned SrcLength = SrcTy->isVectorTy() ?
2847 cast<VectorType>(SrcTy)->getNumElements() : 0;
2848 unsigned DstLength = DstTy->isVectorTy() ?
2849 cast<VectorType>(DstTy)->getNumElements() : 0;
2850
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851 // Switch on the opcode provided
2852 switch (op) {
2853 default: return false; // This is an input error
2854 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002855 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2856 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002857 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002858 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2859 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002860 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002861 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2862 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002863 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002864 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2865 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002866 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002867 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2868 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002870 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002871 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2872 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002874 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002875 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2876 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002878 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002879 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002880 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2881 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2882 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002883 return SrcTy->getScalarType()->isPointerTy() &&
2884 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002886 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002887 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002888 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2889 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2890 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002891 return SrcTy->getScalarType()->isIntegerTy() &&
2892 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893 case Instruction::BitCast:
2894 // BitCast implies a no-op cast of type only. No bits change.
2895 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002896 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002897 return false;
2898
Duncan Sands55e50902008-01-06 10:12:28 +00002899 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002900 // these cases, the cast is okay if the source and destination bit widths
2901 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002902 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903 }
2904}
2905
2906TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002907 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002908) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002909 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910}
2911
2912TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002913 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002915 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002916}
2917
2918ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002919 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002920) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002921 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922}
2923
2924ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002925 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002927 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928}
2929SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002930 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002932 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933}
2934
Jeff Cohencc08c832006-12-02 02:22:01 +00002935SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002936 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002938 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939}
2940
2941FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002942 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002944 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945}
2946
2947FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002948 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002950 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951}
2952
2953FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002954 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002956 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002957}
2958
2959FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002960 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002962 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002963}
2964
2965UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002966 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002967) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002968 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969}
2970
2971UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002972 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002974 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975}
2976
2977SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002978 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002980 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981}
2982
2983SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002984 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002985) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002986 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987}
2988
2989FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002990 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002992 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002993}
2994
2995FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002996 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002998 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999}
3000
3001FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003002 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003003) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003004 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003005}
3006
3007FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003008 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003009) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003010 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011}
3012
3013PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003014 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003015) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003016 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017}
3018
3019PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003020 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003022 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023}
3024
3025IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003026 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003028 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029}
3030
3031IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003032 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003034 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035}
3036
3037BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003038 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003039) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003040 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041}
3042
3043BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003044 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003046 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003048
3049//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003050// CmpInst Classes
3051//===----------------------------------------------------------------------===//
3052
Craig Topper3186c012012-09-23 02:12:10 +00003053void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003054
Chris Lattner229907c2011-07-18 04:54:35 +00003055CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003056 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003057 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003058 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003059 OperandTraits<CmpInst>::op_begin(this),
3060 OperandTraits<CmpInst>::operands(this),
3061 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003062 Op<0>() = LHS;
3063 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003064 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003065 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003066}
Gabor Greiff6caff662008-05-10 08:32:32 +00003067
Chris Lattner229907c2011-07-18 04:54:35 +00003068CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003069 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003070 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003071 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003072 OperandTraits<CmpInst>::op_begin(this),
3073 OperandTraits<CmpInst>::operands(this),
3074 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003075 Op<0>() = LHS;
3076 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003077 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003078 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003079}
3080
3081CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003082CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003083 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003084 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003085 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003086 if (InsertBefore)
3087 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3088 S1, S2, Name);
3089 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003090 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003091 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003092 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003093
3094 if (InsertBefore)
3095 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3096 S1, S2, Name);
3097 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003098 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003099 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003100}
3101
3102CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003103CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003104 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003105 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003106 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3107 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003108 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003109 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3110 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003111}
3112
3113void CmpInst::swapOperands() {
3114 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3115 IC->swapOperands();
3116 else
3117 cast<FCmpInst>(this)->swapOperands();
3118}
3119
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003120bool CmpInst::isCommutative() const {
3121 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003122 return IC->isCommutative();
3123 return cast<FCmpInst>(this)->isCommutative();
3124}
3125
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003126bool CmpInst::isEquality() const {
3127 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003128 return IC->isEquality();
3129 return cast<FCmpInst>(this)->isEquality();
3130}
3131
3132
Dan Gohman4e724382008-05-31 02:47:54 +00003133CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003134 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003135 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003136 case ICMP_EQ: return ICMP_NE;
3137 case ICMP_NE: return ICMP_EQ;
3138 case ICMP_UGT: return ICMP_ULE;
3139 case ICMP_ULT: return ICMP_UGE;
3140 case ICMP_UGE: return ICMP_ULT;
3141 case ICMP_ULE: return ICMP_UGT;
3142 case ICMP_SGT: return ICMP_SLE;
3143 case ICMP_SLT: return ICMP_SGE;
3144 case ICMP_SGE: return ICMP_SLT;
3145 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003146
Dan Gohman4e724382008-05-31 02:47:54 +00003147 case FCMP_OEQ: return FCMP_UNE;
3148 case FCMP_ONE: return FCMP_UEQ;
3149 case FCMP_OGT: return FCMP_ULE;
3150 case FCMP_OLT: return FCMP_UGE;
3151 case FCMP_OGE: return FCMP_ULT;
3152 case FCMP_OLE: return FCMP_UGT;
3153 case FCMP_UEQ: return FCMP_ONE;
3154 case FCMP_UNE: return FCMP_OEQ;
3155 case FCMP_UGT: return FCMP_OLE;
3156 case FCMP_ULT: return FCMP_OGE;
3157 case FCMP_UGE: return FCMP_OLT;
3158 case FCMP_ULE: return FCMP_OGT;
3159 case FCMP_ORD: return FCMP_UNO;
3160 case FCMP_UNO: return FCMP_ORD;
3161 case FCMP_TRUE: return FCMP_FALSE;
3162 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003163 }
3164}
3165
Reid Spencer266e42b2006-12-23 06:05:41 +00003166ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3167 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003168 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003169 case ICMP_EQ: case ICMP_NE:
3170 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3171 return pred;
3172 case ICMP_UGT: return ICMP_SGT;
3173 case ICMP_ULT: return ICMP_SLT;
3174 case ICMP_UGE: return ICMP_SGE;
3175 case ICMP_ULE: return ICMP_SLE;
3176 }
3177}
3178
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003179ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3180 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003181 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003182 case ICMP_EQ: case ICMP_NE:
3183 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3184 return pred;
3185 case ICMP_SGT: return ICMP_UGT;
3186 case ICMP_SLT: return ICMP_ULT;
3187 case ICMP_SGE: return ICMP_UGE;
3188 case ICMP_SLE: return ICMP_ULE;
3189 }
3190}
3191
Reid Spencer0286bc12007-02-28 22:00:54 +00003192/// Initialize a set of values that all satisfy the condition with C.
3193///
3194ConstantRange
3195ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3196 APInt Lower(C);
3197 APInt Upper(C);
3198 uint32_t BitWidth = C.getBitWidth();
3199 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003200 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003201 case ICmpInst::ICMP_EQ: Upper++; break;
3202 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003203 case ICmpInst::ICMP_ULT:
3204 Lower = APInt::getMinValue(BitWidth);
3205 // Check for an empty-set condition.
3206 if (Lower == Upper)
3207 return ConstantRange(BitWidth, /*isFullSet=*/false);
3208 break;
3209 case ICmpInst::ICMP_SLT:
3210 Lower = APInt::getSignedMinValue(BitWidth);
3211 // Check for an empty-set condition.
3212 if (Lower == Upper)
3213 return ConstantRange(BitWidth, /*isFullSet=*/false);
3214 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003215 case ICmpInst::ICMP_UGT:
3216 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003217 // Check for an empty-set condition.
3218 if (Lower == Upper)
3219 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003220 break;
3221 case ICmpInst::ICMP_SGT:
3222 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003223 // Check for an empty-set condition.
3224 if (Lower == Upper)
3225 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003226 break;
3227 case ICmpInst::ICMP_ULE:
3228 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003229 // Check for a full-set condition.
3230 if (Lower == Upper)
3231 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003232 break;
3233 case ICmpInst::ICMP_SLE:
3234 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003235 // Check for a full-set condition.
3236 if (Lower == Upper)
3237 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003238 break;
3239 case ICmpInst::ICMP_UGE:
3240 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003241 // Check for a full-set condition.
3242 if (Lower == Upper)
3243 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003244 break;
3245 case ICmpInst::ICMP_SGE:
3246 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003247 // Check for a full-set condition.
3248 if (Lower == Upper)
3249 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003250 break;
3251 }
3252 return ConstantRange(Lower, Upper);
3253}
3254
Dan Gohman4e724382008-05-31 02:47:54 +00003255CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003256 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003257 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003258 case ICMP_EQ: case ICMP_NE:
3259 return pred;
3260 case ICMP_SGT: return ICMP_SLT;
3261 case ICMP_SLT: return ICMP_SGT;
3262 case ICMP_SGE: return ICMP_SLE;
3263 case ICMP_SLE: return ICMP_SGE;
3264 case ICMP_UGT: return ICMP_ULT;
3265 case ICMP_ULT: return ICMP_UGT;
3266 case ICMP_UGE: return ICMP_ULE;
3267 case ICMP_ULE: return ICMP_UGE;
3268
Reid Spencerd9436b62006-11-20 01:22:35 +00003269 case FCMP_FALSE: case FCMP_TRUE:
3270 case FCMP_OEQ: case FCMP_ONE:
3271 case FCMP_UEQ: case FCMP_UNE:
3272 case FCMP_ORD: case FCMP_UNO:
3273 return pred;
3274 case FCMP_OGT: return FCMP_OLT;
3275 case FCMP_OLT: return FCMP_OGT;
3276 case FCMP_OGE: return FCMP_OLE;
3277 case FCMP_OLE: return FCMP_OGE;
3278 case FCMP_UGT: return FCMP_ULT;
3279 case FCMP_ULT: return FCMP_UGT;
3280 case FCMP_UGE: return FCMP_ULE;
3281 case FCMP_ULE: return FCMP_UGE;
3282 }
3283}
3284
Reid Spencer266e42b2006-12-23 06:05:41 +00003285bool CmpInst::isUnsigned(unsigned short predicate) {
3286 switch (predicate) {
3287 default: return false;
3288 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3289 case ICmpInst::ICMP_UGE: return true;
3290 }
3291}
3292
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003293bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003294 switch (predicate) {
3295 default: return false;
3296 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3297 case ICmpInst::ICMP_SGE: return true;
3298 }
3299}
3300
3301bool CmpInst::isOrdered(unsigned short predicate) {
3302 switch (predicate) {
3303 default: return false;
3304 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3305 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3306 case FCmpInst::FCMP_ORD: return true;
3307 }
3308}
3309
3310bool CmpInst::isUnordered(unsigned short predicate) {
3311 switch (predicate) {
3312 default: return false;
3313 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3314 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3315 case FCmpInst::FCMP_UNO: return true;
3316 }
3317}
3318
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003319bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3320 switch(predicate) {
3321 default: return false;
3322 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3323 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3324 }
3325}
3326
3327bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3328 switch(predicate) {
3329 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3330 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3331 default: return false;
3332 }
3333}
3334
3335
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003336//===----------------------------------------------------------------------===//
3337// SwitchInst Implementation
3338//===----------------------------------------------------------------------===//
3339
Chris Lattnerbaf00152010-11-17 05:41:46 +00003340void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3341 assert(Value && Default && NumReserved);
3342 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003343 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003344 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003345
Gabor Greif2d3024d2008-05-26 21:33:52 +00003346 OperandList[0] = Value;
3347 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003348}
3349
Chris Lattner2195fc42007-02-24 00:55:48 +00003350/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3351/// switch on and a default destination. The number of additional cases can
3352/// be specified here to make memory allocation more efficient. This
3353/// constructor can also autoinsert before another instruction.
3354SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3355 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003356 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3357 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003358 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003359}
3360
3361/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3362/// switch on and a default destination. The number of additional cases can
3363/// be specified here to make memory allocation more efficient. This
3364/// constructor also autoinserts at the end of the specified BasicBlock.
3365SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3366 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003367 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3368 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003369 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003370}
3371
Misha Brukmanb1c93172005-04-21 23:48:37 +00003372SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003373 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3374 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3375 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003376 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003377 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003378 OL[i] = InOL[i];
3379 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003380 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003381 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003382 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003383}
3384
Gordon Henriksen14a55692007-12-10 02:14:30 +00003385SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003386 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003387}
3388
3389
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003390/// addCase - Add an entry to the switch instruction...
3391///
Chris Lattner47ac1872005-02-24 05:32:09 +00003392void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003393 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003394
3395 // FIXME: Currently we work with ConstantInt based cases.
3396 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003397 Mapping.add(IntItem::fromConstantInt(OnVal));
3398 IntegersSubset CaseRanges = Mapping.getCase();
3399 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003400}
3401
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003402void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003403 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003404 unsigned OpNo = NumOperands;
3405 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003406 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003407 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003408 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003409 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003410
3411 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3412
3413 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3414 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003415 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003416}
3417
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003418/// removeCase - This method removes the specified case and its successor
3419/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003420void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003421 unsigned idx = i.getCaseIndex();
3422
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003423 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003424
3425 unsigned NumOps = getNumOperands();
3426 Use *OL = OperandList;
3427
Jay Foad14277722011-02-01 09:22:34 +00003428 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003429 if (2 + (idx + 1) * 2 != NumOps) {
3430 OL[2 + idx * 2] = OL[NumOps - 2];
3431 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003432 }
3433
3434 // Nuke the last value.
3435 OL[NumOps-2].set(0);
3436 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003437
3438 // Do the same with TheCases collection:
3439 if (i.SubsetIt != --TheSubsets.end()) {
3440 *i.SubsetIt = TheSubsets.back();
3441 TheSubsets.pop_back();
3442 } else {
3443 TheSubsets.pop_back();
3444 i.SubsetIt = TheSubsets.end();
3445 }
3446
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003447 NumOperands = NumOps-2;
3448}
3449
Jay Foade98f29d2011-04-01 08:00:58 +00003450/// growOperands - grow operands - This grows the operand list in response
3451/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003452///
Jay Foade98f29d2011-04-01 08:00:58 +00003453void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003454 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003455 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003456
3457 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003458 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003459 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003460 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003461 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003462 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003463 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003464 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003465}
3466
3467
3468BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3469 return getSuccessor(idx);
3470}
3471unsigned SwitchInst::getNumSuccessorsV() const {
3472 return getNumSuccessors();
3473}
3474void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3475 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003476}
Chris Lattnerf22be932004-10-15 23:52:53 +00003477
Chris Lattner3ed871f2009-10-27 19:13:16 +00003478//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003479// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003480//===----------------------------------------------------------------------===//
3481
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003482void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003483 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003484 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003485 ReservedSpace = 1+NumDests;
3486 NumOperands = 1;
3487 OperandList = allocHungoffUses(ReservedSpace);
3488
3489 OperandList[0] = Address;
3490}
3491
3492
Jay Foade98f29d2011-04-01 08:00:58 +00003493/// growOperands - grow operands - This grows the operand list in response
3494/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003495///
Jay Foade98f29d2011-04-01 08:00:58 +00003496void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003497 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003498 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003499
3500 ReservedSpace = NumOps;
3501 Use *NewOps = allocHungoffUses(NumOps);
3502 Use *OldOps = OperandList;
3503 for (unsigned i = 0; i != e; ++i)
3504 NewOps[i] = OldOps[i];
3505 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003506 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003507}
3508
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003509IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3510 Instruction *InsertBefore)
3511: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003512 0, 0, InsertBefore) {
3513 init(Address, NumCases);
3514}
3515
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003516IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3517 BasicBlock *InsertAtEnd)
3518: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003519 0, 0, InsertAtEnd) {
3520 init(Address, NumCases);
3521}
3522
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003523IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3524 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003525 allocHungoffUses(IBI.getNumOperands()),
3526 IBI.getNumOperands()) {
3527 Use *OL = OperandList, *InOL = IBI.OperandList;
3528 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3529 OL[i] = InOL[i];
3530 SubclassOptionalData = IBI.SubclassOptionalData;
3531}
3532
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003533IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003534 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003535}
3536
3537/// addDestination - Add a destination.
3538///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003539void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003540 unsigned OpNo = NumOperands;
3541 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003542 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003543 // Initialize some new operands.
3544 assert(OpNo < ReservedSpace && "Growing didn't work!");
3545 NumOperands = OpNo+1;
3546 OperandList[OpNo] = DestBB;
3547}
3548
3549/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003550/// indirectbr instruction.
3551void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003552 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3553
3554 unsigned NumOps = getNumOperands();
3555 Use *OL = OperandList;
3556
3557 // Replace this value with the last one.
3558 OL[idx+1] = OL[NumOps-1];
3559
3560 // Nuke the last value.
3561 OL[NumOps-1].set(0);
3562 NumOperands = NumOps-1;
3563}
3564
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003565BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003566 return getSuccessor(idx);
3567}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003568unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003569 return getNumSuccessors();
3570}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003571void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003572 setSuccessor(idx, B);
3573}
3574
3575//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003576// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003577//===----------------------------------------------------------------------===//
3578
Chris Lattnerf22be932004-10-15 23:52:53 +00003579// Define these methods here so vtables don't get emitted into every translation
3580// unit that uses these classes.
3581
Devang Patel11cf3f42009-10-27 22:16:29 +00003582GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3583 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003584}
3585
Devang Patel11cf3f42009-10-27 22:16:29 +00003586BinaryOperator *BinaryOperator::clone_impl() const {
3587 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003588}
3589
Devang Patel11cf3f42009-10-27 22:16:29 +00003590FCmpInst* FCmpInst::clone_impl() const {
3591 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003592}
3593
Devang Patel11cf3f42009-10-27 22:16:29 +00003594ICmpInst* ICmpInst::clone_impl() const {
3595 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003596}
3597
Devang Patel11cf3f42009-10-27 22:16:29 +00003598ExtractValueInst *ExtractValueInst::clone_impl() const {
3599 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003600}
3601
Devang Patel11cf3f42009-10-27 22:16:29 +00003602InsertValueInst *InsertValueInst::clone_impl() const {
3603 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003604}
3605
Devang Patel11cf3f42009-10-27 22:16:29 +00003606AllocaInst *AllocaInst::clone_impl() const {
3607 return new AllocaInst(getAllocatedType(),
3608 (Value*)getOperand(0),
3609 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003610}
3611
Devang Patel11cf3f42009-10-27 22:16:29 +00003612LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003613 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3614 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003615}
3616
Devang Patel11cf3f42009-10-27 22:16:29 +00003617StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003618 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003619 getAlignment(), getOrdering(), getSynchScope());
3620
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003621}
3622
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003623AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3624 AtomicCmpXchgInst *Result =
3625 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3626 getOrdering(), getSynchScope());
3627 Result->setVolatile(isVolatile());
3628 return Result;
3629}
3630
3631AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3632 AtomicRMWInst *Result =
3633 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3634 getOrdering(), getSynchScope());
3635 Result->setVolatile(isVolatile());
3636 return Result;
3637}
3638
Eli Friedmanfee02c62011-07-25 23:16:38 +00003639FenceInst *FenceInst::clone_impl() const {
3640 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3641}
3642
Devang Patel11cf3f42009-10-27 22:16:29 +00003643TruncInst *TruncInst::clone_impl() const {
3644 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003645}
3646
Devang Patel11cf3f42009-10-27 22:16:29 +00003647ZExtInst *ZExtInst::clone_impl() const {
3648 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003649}
3650
Devang Patel11cf3f42009-10-27 22:16:29 +00003651SExtInst *SExtInst::clone_impl() const {
3652 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003653}
3654
Devang Patel11cf3f42009-10-27 22:16:29 +00003655FPTruncInst *FPTruncInst::clone_impl() const {
3656 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003657}
3658
Devang Patel11cf3f42009-10-27 22:16:29 +00003659FPExtInst *FPExtInst::clone_impl() const {
3660 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003661}
3662
Devang Patel11cf3f42009-10-27 22:16:29 +00003663UIToFPInst *UIToFPInst::clone_impl() const {
3664 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003665}
3666
Devang Patel11cf3f42009-10-27 22:16:29 +00003667SIToFPInst *SIToFPInst::clone_impl() const {
3668 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003669}
3670
Devang Patel11cf3f42009-10-27 22:16:29 +00003671FPToUIInst *FPToUIInst::clone_impl() const {
3672 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003673}
3674
Devang Patel11cf3f42009-10-27 22:16:29 +00003675FPToSIInst *FPToSIInst::clone_impl() const {
3676 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003677}
3678
Devang Patel11cf3f42009-10-27 22:16:29 +00003679PtrToIntInst *PtrToIntInst::clone_impl() const {
3680 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003681}
3682
Devang Patel11cf3f42009-10-27 22:16:29 +00003683IntToPtrInst *IntToPtrInst::clone_impl() const {
3684 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003685}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003686
Devang Patel11cf3f42009-10-27 22:16:29 +00003687BitCastInst *BitCastInst::clone_impl() const {
3688 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003689}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003690
Devang Patel11cf3f42009-10-27 22:16:29 +00003691CallInst *CallInst::clone_impl() const {
3692 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003693}
3694
Devang Patel11cf3f42009-10-27 22:16:29 +00003695SelectInst *SelectInst::clone_impl() const {
3696 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003697}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003698
Devang Patel11cf3f42009-10-27 22:16:29 +00003699VAArgInst *VAArgInst::clone_impl() const {
3700 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003701}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003702
Devang Patel11cf3f42009-10-27 22:16:29 +00003703ExtractElementInst *ExtractElementInst::clone_impl() const {
3704 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003705}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003706
Devang Patel11cf3f42009-10-27 22:16:29 +00003707InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003708 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003709}
3710
Devang Patel11cf3f42009-10-27 22:16:29 +00003711ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003712 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003713}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003714
Devang Patel11cf3f42009-10-27 22:16:29 +00003715PHINode *PHINode::clone_impl() const {
3716 return new PHINode(*this);
3717}
3718
Bill Wendlingfae14752011-08-12 20:24:12 +00003719LandingPadInst *LandingPadInst::clone_impl() const {
3720 return new LandingPadInst(*this);
3721}
3722
Devang Patel11cf3f42009-10-27 22:16:29 +00003723ReturnInst *ReturnInst::clone_impl() const {
3724 return new(getNumOperands()) ReturnInst(*this);
3725}
3726
3727BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003728 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003729}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003730
Devang Patel11cf3f42009-10-27 22:16:29 +00003731SwitchInst *SwitchInst::clone_impl() const {
3732 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003733}
3734
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003735IndirectBrInst *IndirectBrInst::clone_impl() const {
3736 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737}
3738
3739
Devang Patel11cf3f42009-10-27 22:16:29 +00003740InvokeInst *InvokeInst::clone_impl() const {
3741 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003742}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003743
Bill Wendlingf891bf82011-07-31 06:30:59 +00003744ResumeInst *ResumeInst::clone_impl() const {
3745 return new(1) ResumeInst(*this);
3746}
3747
Devang Patel11cf3f42009-10-27 22:16:29 +00003748UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003749 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003750 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003751}