blob: 0a097b8be201acfe4b4ae0a059fc9b1e4c5859c5 [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 Wendling8ccd6ca2012-10-09 21:38:14 +0000396bool CallInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
397 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000398 return true;
399 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000400 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000401 return false;
402}
403
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404/// IsConstantOne - Return true only if val is constant int 1
405static bool IsConstantOne(Value *val) {
406 assert(val && "IsConstantOne does not work with NULL val");
407 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
408}
409
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000410static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000411 BasicBlock *InsertAtEnd, Type *IntPtrTy,
412 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000413 Value *ArraySize, Function *MallocF,
414 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000415 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000416 "createMalloc needs either InsertBefore or InsertAtEnd");
417
418 // malloc(type) becomes:
419 // bitcast (i8* malloc(typeSize)) to type*
420 // malloc(type, arraySize) becomes:
421 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000422 if (!ArraySize)
423 ArraySize = ConstantInt::get(IntPtrTy, 1);
424 else if (ArraySize->getType() != IntPtrTy) {
425 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000426 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
427 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000428 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000429 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
430 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000432
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000433 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000434 if (IsConstantOne(AllocSize)) {
435 AllocSize = ArraySize; // Operand * 1 = Operand
436 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
437 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
438 false /*ZExt*/);
439 // Malloc arg is constant product of type size and array size
440 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
441 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000442 // Multiply type size by the array size...
443 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
445 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000446 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000447 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
448 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000449 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000450 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453 // Create the call to Malloc.
454 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
455 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000456 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000457 Value *MallocFunc = MallocF;
458 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000459 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000460 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000461 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000462 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000463 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000464 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000465 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000466 Result = MCall;
467 if (Result->getType() != AllocPtrType)
468 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000469 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000470 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000471 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000472 Result = MCall;
473 if (Result->getType() != AllocPtrType) {
474 InsertAtEnd->getInstList().push_back(MCall);
475 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000476 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000477 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000478 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000479 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000480 if (Function *F = dyn_cast<Function>(MallocFunc)) {
481 MCall->setCallingConv(F->getCallingConv());
482 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
483 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000484 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000485
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000486 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000487}
488
489/// CreateMalloc - Generate the IR for a call to malloc:
490/// 1. Compute the malloc call's argument as the specified type's size,
491/// possibly multiplied by the array size if the array size is not
492/// constant 1.
493/// 2. Call malloc with that argument.
494/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000495Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000496 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000497 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000498 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000499 const Twine &Name) {
500 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000501 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502}
503
504/// CreateMalloc - Generate the IR for a call to malloc:
505/// 1. Compute the malloc call's argument as the specified type's size,
506/// possibly multiplied by the array size if the array size is not
507/// constant 1.
508/// 2. Call malloc with that argument.
509/// 3. Bitcast the result of the malloc call to the specified type.
510/// Note: This function does not add the bitcast to the basic block, that is the
511/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000512Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000513 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000514 Value *AllocSize, Value *ArraySize,
515 Function *MallocF, const Twine &Name) {
516 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000517 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000518}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000519
Victor Hernandeze2971492009-10-24 04:23:03 +0000520static Instruction* createFree(Value* Source, Instruction *InsertBefore,
521 BasicBlock *InsertAtEnd) {
522 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
523 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000524 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000525 "Can not free something of nonpointer type!");
526
527 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
528 Module* M = BB->getParent()->getParent();
529
Chris Lattner229907c2011-07-18 04:54:35 +0000530 Type *VoidTy = Type::getVoidTy(M->getContext());
531 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000532 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000533 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000534 CallInst* Result = NULL;
535 Value *PtrCast = Source;
536 if (InsertBefore) {
537 if (Source->getType() != IntPtrTy)
538 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
539 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
540 } else {
541 if (Source->getType() != IntPtrTy)
542 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
543 Result = CallInst::Create(FreeFunc, PtrCast, "");
544 }
545 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000546 if (Function *F = dyn_cast<Function>(FreeFunc))
547 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000548
549 return Result;
550}
551
552/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000553Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
554 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000555}
556
557/// CreateFree - Generate the IR for a call to the builtin free function.
558/// Note: This function does not add the call to the basic block, that is the
559/// responsibility of the caller.
560Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
561 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
562 assert(FreeCall && "CreateFree did not create a CallInst");
563 return FreeCall;
564}
565
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566//===----------------------------------------------------------------------===//
567// InvokeInst Implementation
568//===----------------------------------------------------------------------===//
569
570void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000571 ArrayRef<Value *> Args, const Twine &NameStr) {
572 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000573 Op<-3>() = Fn;
574 Op<-2>() = IfNormal;
575 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000576
577#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000578 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000580
Jay Foad5bd375a2011-07-15 08:37:34 +0000581 assert(((Args.size() == FTy->getNumParams()) ||
582 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000583 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000584
Jay Foad5bd375a2011-07-15 08:37:34 +0000585 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000586 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000587 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000588 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000589#endif
590
591 std::copy(Args.begin(), Args.end(), op_begin());
592 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593}
594
Misha Brukmanb1c93172005-04-21 23:48:37 +0000595InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000597 OperandTraits<InvokeInst>::op_end(this)
598 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000599 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000600 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000601 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000602 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000603 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
607 return getSuccessor(idx);
608}
609unsigned InvokeInst::getNumSuccessorsV() const {
610 return getNumSuccessors();
611}
612void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
613 return setSuccessor(idx, B);
614}
615
Bill Wendling375eb1f2012-10-09 00:28:54 +0000616bool InvokeInst::fnHasNoAliasAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000617 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoAlias))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000618 return true;
619 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000620 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoAlias);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 return false;
622}
623bool InvokeInst::fnHasNoInlineAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000624 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoInline))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000625 return true;
626 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000627 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoInline);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000628 return false;
629}
630bool InvokeInst::fnHasNoReturnAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000631 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoReturn))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000632 return true;
633 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000634 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoReturn);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000635 return false;
636}
637bool InvokeInst::fnHasNoUnwindAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000638 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000639 return true;
640 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000641 return F->getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000642 return false;
643}
644bool InvokeInst::fnHasReadNoneAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000645 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadNone))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000646 return true;
647 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000648 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadNone);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000649 return false;
650}
651bool InvokeInst::fnHasReadOnlyAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000652 if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000653 return true;
654 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000655 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000656 return false;
657}
658bool InvokeInst::fnHasReturnsTwiceAttr() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000659 if (AttributeList.getParamAttributes(~0U).
660 hasAttribute(Attributes::ReturnsTwice))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000661 return true;
662 if (const Function *F = getCalledFunction())
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000663 return F->getParamAttributes(~0U).hasAttribute(Attributes::ReturnsTwice);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000664 return false;
665}
666
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000667bool InvokeInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
668 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000669 return true;
670 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000671 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000672 return false;
673}
674
Devang Patel4c758ea2008-09-25 21:00:45 +0000675void InvokeInst::addAttribute(unsigned i, Attributes attr) {
676 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000677 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000678 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000679}
680
Devang Patel4c758ea2008-09-25 21:00:45 +0000681void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
682 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000683 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000684 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000685}
686
Bill Wendlingfae14752011-08-12 20:24:12 +0000687LandingPadInst *InvokeInst::getLandingPadInst() const {
688 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
689}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000690
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000691//===----------------------------------------------------------------------===//
692// ReturnInst Implementation
693//===----------------------------------------------------------------------===//
694
Chris Lattner2195fc42007-02-24 00:55:48 +0000695ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000696 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000697 OperandTraits<ReturnInst>::op_end(this) -
698 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000699 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000700 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000701 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000702 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000703}
704
Owen Anderson55f1c092009-08-13 21:58:54 +0000705ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
706 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
708 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000709 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000710 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000711}
Owen Anderson55f1c092009-08-13 21:58:54 +0000712ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
713 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000714 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
715 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000716 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000717 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000718}
Owen Anderson55f1c092009-08-13 21:58:54 +0000719ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
720 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000721 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000722}
723
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724unsigned ReturnInst::getNumSuccessorsV() const {
725 return getNumSuccessors();
726}
727
Devang Patelae682fb2008-02-26 17:56:20 +0000728/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
729/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000731 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000735 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736}
737
Devang Patel59643e52008-02-23 00:35:18 +0000738ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000739}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000742// ResumeInst Implementation
743//===----------------------------------------------------------------------===//
744
745ResumeInst::ResumeInst(const ResumeInst &RI)
746 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
747 OperandTraits<ResumeInst>::op_begin(this), 1) {
748 Op<0>() = RI.Op<0>();
749}
750
751ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
752 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
753 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
754 Op<0>() = Exn;
755}
756
757ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
758 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
759 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
760 Op<0>() = Exn;
761}
762
763unsigned ResumeInst::getNumSuccessorsV() const {
764 return getNumSuccessors();
765}
766
767void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
768 llvm_unreachable("ResumeInst has no successors!");
769}
770
771BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
772 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000773}
774
775//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000776// UnreachableInst Implementation
777//===----------------------------------------------------------------------===//
778
Owen Anderson55f1c092009-08-13 21:58:54 +0000779UnreachableInst::UnreachableInst(LLVMContext &Context,
780 Instruction *InsertBefore)
781 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
782 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000783}
Owen Anderson55f1c092009-08-13 21:58:54 +0000784UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
785 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
786 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000787}
788
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789unsigned UnreachableInst::getNumSuccessorsV() const {
790 return getNumSuccessors();
791}
792
793void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000794 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795}
796
797BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000798 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000799}
800
801//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802// BranchInst Implementation
803//===----------------------------------------------------------------------===//
804
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805void BranchInst::AssertOK() {
806 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000807 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809}
810
Chris Lattner2195fc42007-02-24 00:55:48 +0000811BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000812 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000813 OperandTraits<BranchInst>::op_end(this) - 1,
814 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000815 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000816 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000817}
818BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
819 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000820 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000821 OperandTraits<BranchInst>::op_end(this) - 3,
822 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000823 Op<-1>() = IfTrue;
824 Op<-2>() = IfFalse;
825 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000826#ifndef NDEBUG
827 AssertOK();
828#endif
829}
830
831BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000832 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000833 OperandTraits<BranchInst>::op_end(this) - 1,
834 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000835 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000836 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000837}
838
839BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
840 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000841 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000842 OperandTraits<BranchInst>::op_end(this) - 3,
843 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000844 Op<-1>() = IfTrue;
845 Op<-2>() = IfFalse;
846 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000847#ifndef NDEBUG
848 AssertOK();
849#endif
850}
851
852
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000853BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000854 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000855 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
856 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000857 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 if (BI.getNumOperands() != 1) {
859 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000860 Op<-3>() = BI.Op<-3>();
861 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000862 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000863 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864}
865
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000866void BranchInst::swapSuccessors() {
867 assert(isConditional() &&
868 "Cannot swap successors of an unconditional branch");
869 Op<-1>().swap(Op<-2>());
870
871 // Update profile metadata if present and it matches our structural
872 // expectations.
873 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
874 if (!ProfileData || ProfileData->getNumOperands() != 3)
875 return;
876
877 // The first operand is the name. Fetch them backwards and build a new one.
878 Value *Ops[] = {
879 ProfileData->getOperand(0),
880 ProfileData->getOperand(2),
881 ProfileData->getOperand(1)
882 };
883 setMetadata(LLVMContext::MD_prof,
884 MDNode::get(ProfileData->getContext(), Ops));
885}
886
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
888 return getSuccessor(idx);
889}
890unsigned BranchInst::getNumSuccessorsV() const {
891 return getNumSuccessors();
892}
893void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
894 setSuccessor(idx, B);
895}
896
897
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000899// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900//===----------------------------------------------------------------------===//
901
Owen Andersonb6b25302009-07-14 23:09:55 +0000902static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000904 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000905 else {
906 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000907 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000908 assert(Amt->getType()->isIntegerTy() &&
909 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000910 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000911 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912}
913
Chris Lattner229907c2011-07-18 04:54:35 +0000914AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000915 const Twine &Name, Instruction *InsertBefore)
916 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
917 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
918 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000919 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000920 setName(Name);
921}
922
Chris Lattner229907c2011-07-18 04:54:35 +0000923AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000924 const Twine &Name, BasicBlock *InsertAtEnd)
925 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
926 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
927 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000928 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000929 setName(Name);
930}
931
Chris Lattner229907c2011-07-18 04:54:35 +0000932AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000933 Instruction *InsertBefore)
934 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
935 getAISize(Ty->getContext(), 0), InsertBefore) {
936 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000937 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000938 setName(Name);
939}
940
Chris Lattner229907c2011-07-18 04:54:35 +0000941AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000942 BasicBlock *InsertAtEnd)
943 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
944 getAISize(Ty->getContext(), 0), InsertAtEnd) {
945 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000946 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000947 setName(Name);
948}
949
Chris Lattner229907c2011-07-18 04:54:35 +0000950AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000951 const Twine &Name, Instruction *InsertBefore)
952 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000953 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000954 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000955 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000956 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957}
958
Chris Lattner229907c2011-07-18 04:54:35 +0000959AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000960 const Twine &Name, BasicBlock *InsertAtEnd)
961 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000962 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000963 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000964 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000965 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966}
967
Gordon Henriksen14a55692007-12-10 02:14:30 +0000968// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000969AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000970}
971
Victor Hernandez8acf2952009-10-23 21:09:37 +0000972void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000973 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000974 assert(Align <= MaximumAlignment &&
975 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000976 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000977 assert(getAlignment() == Align && "Alignment representation error!");
978}
979
Victor Hernandez8acf2952009-10-23 21:09:37 +0000980bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000981 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000982 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000983 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984}
985
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000986Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987 return getType()->getElementType();
988}
989
Chris Lattner8b291e62008-11-26 02:54:17 +0000990/// isStaticAlloca - Return true if this alloca is in the entry block of the
991/// function and is a constant size. If so, the code generator will fold it
992/// into the prolog/epilog code, so it is basically free.
993bool AllocaInst::isStaticAlloca() const {
994 // Must be constant size.
995 if (!isa<ConstantInt>(getArraySize())) return false;
996
997 // Must be in the entry block.
998 const BasicBlock *Parent = getParent();
999 return Parent == &Parent->getParent()->front();
1000}
1001
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003// LoadInst Implementation
1004//===----------------------------------------------------------------------===//
1005
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001006void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001007 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001009 assert(!(isAtomic() && getAlignment() == 0) &&
1010 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011}
1012
Daniel Dunbar4975db62009-07-25 04:41:11 +00001013LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001015 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001016 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001017 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001018 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001020 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021}
1022
Daniel Dunbar4975db62009-07-25 04:41:11 +00001023LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001024 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001025 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001026 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001027 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001028 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001030 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031}
1032
Daniel Dunbar4975db62009-07-25 04:41:11 +00001033LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001035 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001036 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001037 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001038 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001039 setAtomic(NotAtomic);
1040 AssertOK();
1041 setName(Name);
1042}
1043
1044LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1045 BasicBlock *InsertAE)
1046 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1047 Load, Ptr, InsertAE) {
1048 setVolatile(isVolatile);
1049 setAlignment(0);
1050 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001051 AssertOK();
1052 setName(Name);
1053}
1054
Daniel Dunbar4975db62009-07-25 04:41:11 +00001055LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001056 unsigned Align, Instruction *InsertBef)
1057 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1058 Load, Ptr, InsertBef) {
1059 setVolatile(isVolatile);
1060 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001061 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001063 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064}
1065
Daniel Dunbar4975db62009-07-25 04:41:11 +00001066LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001067 unsigned Align, BasicBlock *InsertAE)
1068 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1069 Load, Ptr, InsertAE) {
1070 setVolatile(isVolatile);
1071 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001072 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +00001073 AssertOK();
1074 setName(Name);
1075}
1076
Eli Friedman59b66882011-08-09 23:02:53 +00001077LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1078 unsigned Align, AtomicOrdering Order,
1079 SynchronizationScope SynchScope,
1080 Instruction *InsertBef)
1081 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1082 Load, Ptr, InsertBef) {
1083 setVolatile(isVolatile);
1084 setAlignment(Align);
1085 setAtomic(Order, SynchScope);
1086 AssertOK();
1087 setName(Name);
1088}
1089
1090LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1091 unsigned Align, AtomicOrdering Order,
1092 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001093 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001095 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001096 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001097 setAlignment(Align);
1098 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001099 AssertOK();
1100 setName(Name);
1101}
1102
Daniel Dunbar27096822009-08-11 18:11:15 +00001103LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1104 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1105 Load, Ptr, InsertBef) {
1106 setVolatile(false);
1107 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001108 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001109 AssertOK();
1110 if (Name && Name[0]) setName(Name);
1111}
1112
1113LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1114 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1115 Load, Ptr, InsertAE) {
1116 setVolatile(false);
1117 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001118 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001119 AssertOK();
1120 if (Name && Name[0]) setName(Name);
1121}
1122
1123LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1124 Instruction *InsertBef)
1125: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1126 Load, Ptr, InsertBef) {
1127 setVolatile(isVolatile);
1128 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001129 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001130 AssertOK();
1131 if (Name && Name[0]) setName(Name);
1132}
1133
1134LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1135 BasicBlock *InsertAE)
1136 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1137 Load, Ptr, InsertAE) {
1138 setVolatile(isVolatile);
1139 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001140 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001141 AssertOK();
1142 if (Name && Name[0]) setName(Name);
1143}
1144
Christopher Lamb84485702007-04-22 19:24:39 +00001145void LoadInst::setAlignment(unsigned Align) {
1146 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001147 assert(Align <= MaximumAlignment &&
1148 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001149 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001150 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001151 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001152}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001153
1154//===----------------------------------------------------------------------===//
1155// StoreInst Implementation
1156//===----------------------------------------------------------------------===//
1157
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001158void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001159 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001160 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001161 "Ptr must have pointer type!");
1162 assert(getOperand(0)->getType() ==
1163 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001164 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001165 assert(!(isAtomic() && getAlignment() == 0) &&
1166 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001167}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001168
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001169
1170StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001171 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001172 OperandTraits<StoreInst>::op_begin(this),
1173 OperandTraits<StoreInst>::operands(this),
1174 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001175 Op<0>() = val;
1176 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001177 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001178 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001179 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001180 AssertOK();
1181}
1182
1183StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001184 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001185 OperandTraits<StoreInst>::op_begin(this),
1186 OperandTraits<StoreInst>::operands(this),
1187 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001188 Op<0>() = val;
1189 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001190 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001191 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001192 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193 AssertOK();
1194}
1195
Misha Brukmanb1c93172005-04-21 23:48:37 +00001196StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001197 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001198 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001199 OperandTraits<StoreInst>::op_begin(this),
1200 OperandTraits<StoreInst>::operands(this),
1201 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001202 Op<0>() = val;
1203 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001204 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001205 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001206 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001207 AssertOK();
1208}
1209
1210StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1211 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001212 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001213 OperandTraits<StoreInst>::op_begin(this),
1214 OperandTraits<StoreInst>::operands(this),
1215 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001216 Op<0>() = val;
1217 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001218 setVolatile(isVolatile);
1219 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001220 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001221 AssertOK();
1222}
1223
Misha Brukmanb1c93172005-04-21 23:48:37 +00001224StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001225 unsigned Align, AtomicOrdering Order,
1226 SynchronizationScope SynchScope,
1227 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001228 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001229 OperandTraits<StoreInst>::op_begin(this),
1230 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001231 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001232 Op<0>() = val;
1233 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001234 setVolatile(isVolatile);
1235 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001236 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001237 AssertOK();
1238}
1239
1240StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001241 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001242 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001243 OperandTraits<StoreInst>::op_begin(this),
1244 OperandTraits<StoreInst>::operands(this),
1245 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001246 Op<0>() = val;
1247 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001248 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001249 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001250 setAtomic(NotAtomic);
1251 AssertOK();
1252}
1253
1254StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1255 unsigned Align, BasicBlock *InsertAtEnd)
1256 : Instruction(Type::getVoidTy(val->getContext()), Store,
1257 OperandTraits<StoreInst>::op_begin(this),
1258 OperandTraits<StoreInst>::operands(this),
1259 InsertAtEnd) {
1260 Op<0>() = val;
1261 Op<1>() = addr;
1262 setVolatile(isVolatile);
1263 setAlignment(Align);
1264 setAtomic(NotAtomic);
1265 AssertOK();
1266}
1267
1268StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1269 unsigned Align, AtomicOrdering Order,
1270 SynchronizationScope SynchScope,
1271 BasicBlock *InsertAtEnd)
1272 : Instruction(Type::getVoidTy(val->getContext()), Store,
1273 OperandTraits<StoreInst>::op_begin(this),
1274 OperandTraits<StoreInst>::operands(this),
1275 InsertAtEnd) {
1276 Op<0>() = val;
1277 Op<1>() = addr;
1278 setVolatile(isVolatile);
1279 setAlignment(Align);
1280 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001282}
1283
Christopher Lamb84485702007-04-22 19:24:39 +00001284void StoreInst::setAlignment(unsigned Align) {
1285 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001286 assert(Align <= MaximumAlignment &&
1287 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001288 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001289 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001290 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001291}
1292
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001294// AtomicCmpXchgInst Implementation
1295//===----------------------------------------------------------------------===//
1296
1297void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1298 AtomicOrdering Ordering,
1299 SynchronizationScope SynchScope) {
1300 Op<0>() = Ptr;
1301 Op<1>() = Cmp;
1302 Op<2>() = NewVal;
1303 setOrdering(Ordering);
1304 setSynchScope(SynchScope);
1305
1306 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1307 "All operands must be non-null!");
1308 assert(getOperand(0)->getType()->isPointerTy() &&
1309 "Ptr must have pointer type!");
1310 assert(getOperand(1)->getType() ==
1311 cast<PointerType>(getOperand(0)->getType())->getElementType()
1312 && "Ptr must be a pointer to Cmp type!");
1313 assert(getOperand(2)->getType() ==
1314 cast<PointerType>(getOperand(0)->getType())->getElementType()
1315 && "Ptr must be a pointer to NewVal type!");
1316 assert(Ordering != NotAtomic &&
1317 "AtomicCmpXchg instructions must be atomic!");
1318}
1319
1320AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1321 AtomicOrdering Ordering,
1322 SynchronizationScope SynchScope,
1323 Instruction *InsertBefore)
1324 : Instruction(Cmp->getType(), AtomicCmpXchg,
1325 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1326 OperandTraits<AtomicCmpXchgInst>::operands(this),
1327 InsertBefore) {
1328 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1329}
1330
1331AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1332 AtomicOrdering Ordering,
1333 SynchronizationScope SynchScope,
1334 BasicBlock *InsertAtEnd)
1335 : Instruction(Cmp->getType(), AtomicCmpXchg,
1336 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1337 OperandTraits<AtomicCmpXchgInst>::operands(this),
1338 InsertAtEnd) {
1339 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1340}
1341
1342//===----------------------------------------------------------------------===//
1343// AtomicRMWInst Implementation
1344//===----------------------------------------------------------------------===//
1345
1346void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1347 AtomicOrdering Ordering,
1348 SynchronizationScope SynchScope) {
1349 Op<0>() = Ptr;
1350 Op<1>() = Val;
1351 setOperation(Operation);
1352 setOrdering(Ordering);
1353 setSynchScope(SynchScope);
1354
1355 assert(getOperand(0) && getOperand(1) &&
1356 "All operands must be non-null!");
1357 assert(getOperand(0)->getType()->isPointerTy() &&
1358 "Ptr must have pointer type!");
1359 assert(getOperand(1)->getType() ==
1360 cast<PointerType>(getOperand(0)->getType())->getElementType()
1361 && "Ptr must be a pointer to Val type!");
1362 assert(Ordering != NotAtomic &&
1363 "AtomicRMW instructions must be atomic!");
1364}
1365
1366AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1367 AtomicOrdering Ordering,
1368 SynchronizationScope SynchScope,
1369 Instruction *InsertBefore)
1370 : Instruction(Val->getType(), AtomicRMW,
1371 OperandTraits<AtomicRMWInst>::op_begin(this),
1372 OperandTraits<AtomicRMWInst>::operands(this),
1373 InsertBefore) {
1374 Init(Operation, Ptr, Val, Ordering, SynchScope);
1375}
1376
1377AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1378 AtomicOrdering Ordering,
1379 SynchronizationScope SynchScope,
1380 BasicBlock *InsertAtEnd)
1381 : Instruction(Val->getType(), AtomicRMW,
1382 OperandTraits<AtomicRMWInst>::op_begin(this),
1383 OperandTraits<AtomicRMWInst>::operands(this),
1384 InsertAtEnd) {
1385 Init(Operation, Ptr, Val, Ordering, SynchScope);
1386}
1387
1388//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001389// FenceInst Implementation
1390//===----------------------------------------------------------------------===//
1391
1392FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1393 SynchronizationScope SynchScope,
1394 Instruction *InsertBefore)
1395 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1396 setOrdering(Ordering);
1397 setSynchScope(SynchScope);
1398}
1399
1400FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1401 SynchronizationScope SynchScope,
1402 BasicBlock *InsertAtEnd)
1403 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1404 setOrdering(Ordering);
1405 setSynchScope(SynchScope);
1406}
1407
1408//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001409// GetElementPtrInst Implementation
1410//===----------------------------------------------------------------------===//
1411
Jay Foadd1b78492011-07-25 09:48:08 +00001412void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001413 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001414 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1415 OperandList[0] = Ptr;
1416 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001417 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001418}
1419
Gabor Greiff6caff662008-05-10 08:32:32 +00001420GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001421 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001422 OperandTraits<GetElementPtrInst>::op_end(this)
1423 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001424 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001425 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001426 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001427}
1428
Chris Lattner6090a422009-03-09 04:46:40 +00001429/// getIndexedType - Returns the type of the element that would be accessed with
1430/// a gep instruction with the specified parameters.
1431///
1432/// The Idxs pointer should point to a continuous piece of memory containing the
1433/// indices, either as Value* or uint64_t.
1434///
1435/// A null type is returned if the indices are invalid for the specified
1436/// pointer type.
1437///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001438template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001439static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001440 if (Ptr->isVectorTy()) {
1441 assert(IdxList.size() == 1 &&
1442 "GEP with vector pointers must have a single index");
1443 PointerType *PTy = dyn_cast<PointerType>(
1444 cast<VectorType>(Ptr)->getElementType());
1445 assert(PTy && "Gep with invalid vector pointer found");
1446 return PTy->getElementType();
1447 }
1448
Chris Lattner229907c2011-07-18 04:54:35 +00001449 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001450 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001451 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001452
Chris Lattner6090a422009-03-09 04:46:40 +00001453 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001454 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001455 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001456
Chris Lattner6090a422009-03-09 04:46:40 +00001457 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001458 // it cannot be 'stepped over'.
1459 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001460 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001461
Dan Gohman1ecaf452008-05-31 00:58:22 +00001462 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001463 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001464 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001465 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001466 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001467 if (!CT->indexValid(Index)) return 0;
1468 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001469 }
Jay Foadd1b78492011-07-25 09:48:08 +00001470 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001471}
1472
Jay Foadd1b78492011-07-25 09:48:08 +00001473Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1474 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001475}
1476
Chris Lattner229907c2011-07-18 04:54:35 +00001477Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001478 ArrayRef<Constant *> IdxList) {
1479 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001480}
1481
Jay Foadd1b78492011-07-25 09:48:08 +00001482Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1483 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001484}
1485
Nadav Rotem3924cb02011-12-05 06:29:09 +00001486unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1487 Type *Ty = Ptr->getType();
1488
1489 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1490 Ty = VTy->getElementType();
1491
1492 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1493 return PTy->getAddressSpace();
1494
Craig Topperc514b542012-02-05 22:14:15 +00001495 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001496}
1497
Chris Lattner45f15572007-04-14 00:12:57 +00001498/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1499/// zeros. If so, the result pointer and the first operand have the same
1500/// value, just potentially different types.
1501bool GetElementPtrInst::hasAllZeroIndices() const {
1502 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1503 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1504 if (!CI->isZero()) return false;
1505 } else {
1506 return false;
1507 }
1508 }
1509 return true;
1510}
1511
Chris Lattner27058292007-04-27 20:35:56 +00001512/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1513/// constant integers. If so, the result pointer and the first operand have
1514/// a constant offset between them.
1515bool GetElementPtrInst::hasAllConstantIndices() const {
1516 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1517 if (!isa<ConstantInt>(getOperand(i)))
1518 return false;
1519 }
1520 return true;
1521}
1522
Dan Gohman1b849082009-09-07 23:54:19 +00001523void GetElementPtrInst::setIsInBounds(bool B) {
1524 cast<GEPOperator>(this)->setIsInBounds(B);
1525}
Chris Lattner45f15572007-04-14 00:12:57 +00001526
Nick Lewycky28a5f252009-09-27 21:33:04 +00001527bool GetElementPtrInst::isInBounds() const {
1528 return cast<GEPOperator>(this)->isInBounds();
1529}
1530
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001531//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001532// ExtractElementInst Implementation
1533//===----------------------------------------------------------------------===//
1534
1535ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001536 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001537 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001538 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001539 ExtractElement,
1540 OperandTraits<ExtractElementInst>::op_begin(this),
1541 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001542 assert(isValidOperands(Val, Index) &&
1543 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001544 Op<0>() = Val;
1545 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001546 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001547}
1548
1549ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001550 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001552 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001553 ExtractElement,
1554 OperandTraits<ExtractElementInst>::op_begin(this),
1555 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001556 assert(isValidOperands(Val, Index) &&
1557 "Invalid extractelement instruction operands!");
1558
Gabor Greif2d3024d2008-05-26 21:33:52 +00001559 Op<0>() = Val;
1560 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001561 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001562}
1563
Chris Lattner65511ff2006-10-05 06:24:58 +00001564
Chris Lattner54865b32006-04-08 04:05:48 +00001565bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001566 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001567 return false;
1568 return true;
1569}
1570
1571
Robert Bocchino23004482006-01-10 19:05:34 +00001572//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001573// InsertElementInst Implementation
1574//===----------------------------------------------------------------------===//
1575
Chris Lattner54865b32006-04-08 04:05:48 +00001576InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001577 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001578 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001579 : Instruction(Vec->getType(), InsertElement,
1580 OperandTraits<InsertElementInst>::op_begin(this),
1581 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001582 assert(isValidOperands(Vec, Elt, Index) &&
1583 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001584 Op<0>() = Vec;
1585 Op<1>() = Elt;
1586 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001588}
1589
Chris Lattner54865b32006-04-08 04:05:48 +00001590InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001591 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001592 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001593 : Instruction(Vec->getType(), InsertElement,
1594 OperandTraits<InsertElementInst>::op_begin(this),
1595 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001596 assert(isValidOperands(Vec, Elt, Index) &&
1597 "Invalid insertelement instruction operands!");
1598
Gabor Greif2d3024d2008-05-26 21:33:52 +00001599 Op<0>() = Vec;
1600 Op<1>() = Elt;
1601 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001602 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001603}
1604
Chris Lattner54865b32006-04-08 04:05:48 +00001605bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1606 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001607 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001608 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001609
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001611 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001612
Duncan Sands9dff9be2010-02-15 16:12:20 +00001613 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001614 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001615 return true;
1616}
1617
1618
Robert Bocchinoca27f032006-01-17 20:07:22 +00001619//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001620// ShuffleVectorInst Implementation
1621//===----------------------------------------------------------------------===//
1622
1623ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001624 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001625 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001626: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001627 cast<VectorType>(Mask->getType())->getNumElements()),
1628 ShuffleVector,
1629 OperandTraits<ShuffleVectorInst>::op_begin(this),
1630 OperandTraits<ShuffleVectorInst>::operands(this),
1631 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001632 assert(isValidOperands(V1, V2, Mask) &&
1633 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001634 Op<0>() = V1;
1635 Op<1>() = V2;
1636 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001637 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001638}
1639
1640ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001641 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001642 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001643: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1644 cast<VectorType>(Mask->getType())->getNumElements()),
1645 ShuffleVector,
1646 OperandTraits<ShuffleVectorInst>::op_begin(this),
1647 OperandTraits<ShuffleVectorInst>::operands(this),
1648 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001649 assert(isValidOperands(V1, V2, Mask) &&
1650 "Invalid shuffle vector instruction operands!");
1651
Gabor Greif2d3024d2008-05-26 21:33:52 +00001652 Op<0>() = V1;
1653 Op<1>() = V2;
1654 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001655 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001656}
1657
Mon P Wang25f01062008-11-10 04:46:22 +00001658bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001659 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001660 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001661 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001662 return false;
1663
Chris Lattner1dcb6542012-01-25 23:49:49 +00001664 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001665 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001666 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001667 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001668
1669 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001670 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1671 return true;
1672
Nate Begeman60a31c32010-08-13 00:16:46 +00001673 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001674 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001675 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001676 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1677 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001678 return false;
1679 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1680 return false;
1681 }
1682 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001683 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001684 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001685
1686 if (const ConstantDataSequential *CDS =
1687 dyn_cast<ConstantDataSequential>(Mask)) {
1688 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1689 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1690 if (CDS->getElementAsInteger(i) >= V1Size*2)
1691 return false;
1692 return true;
1693 }
1694
1695 // The bitcode reader can create a place holder for a forward reference
1696 // used as the shuffle mask. When this occurs, the shuffle mask will
1697 // fall into this case and fail. To avoid this error, do this bit of
1698 // ugliness to allow such a mask pass.
1699 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1700 if (CE->getOpcode() == Instruction::UserOp1)
1701 return true;
1702
1703 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001704}
1705
Chris Lattnerf724e342008-03-02 05:28:33 +00001706/// getMaskValue - Return the index from the shuffle mask for the specified
1707/// output result. This is either -1 if the element is undef or a number less
1708/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001709int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1710 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1711 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001712 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001713 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001714 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001715 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001716 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001717}
1718
Chris Lattner1dcb6542012-01-25 23:49:49 +00001719/// getShuffleMask - Return the full mask for this instruction, where each
1720/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001721void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1722 SmallVectorImpl<int> &Result) {
1723 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001724
Chris Lattnercf129702012-01-26 02:51:13 +00001725 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001726 for (unsigned i = 0; i != NumElts; ++i)
1727 Result.push_back(CDS->getElementAsInteger(i));
1728 return;
1729 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001730 for (unsigned i = 0; i != NumElts; ++i) {
1731 Constant *C = Mask->getAggregateElement(i);
1732 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001733 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001734 }
1735}
1736
1737
Dan Gohman12fce772008-05-15 19:50:34 +00001738//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001739// InsertValueInst Class
1740//===----------------------------------------------------------------------===//
1741
Jay Foad57aa6362011-07-13 10:26:04 +00001742void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1743 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001744 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001745
1746 // There's no fundamental reason why we require at least one index
1747 // (other than weirdness with &*IdxBegin being invalid; see
1748 // getelementptr's init routine for example). But there's no
1749 // present need to support it.
1750 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1751
1752 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001753 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001754 Op<0>() = Agg;
1755 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001756
Jay Foad57aa6362011-07-13 10:26:04 +00001757 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001758 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001759}
1760
1761InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001762 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001763 OperandTraits<InsertValueInst>::op_begin(this), 2),
1764 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001765 Op<0>() = IVI.getOperand(0);
1766 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001767 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001768}
1769
1770//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001771// ExtractValueInst Class
1772//===----------------------------------------------------------------------===//
1773
Jay Foad57aa6362011-07-13 10:26:04 +00001774void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001775 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001776
Jay Foad57aa6362011-07-13 10:26:04 +00001777 // There's no fundamental reason why we require at least one index.
1778 // But there's no present need to support it.
1779 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001780
Jay Foad57aa6362011-07-13 10:26:04 +00001781 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001782 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001783}
1784
1785ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001786 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001787 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001788 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001789}
1790
Dan Gohman12fce772008-05-15 19:50:34 +00001791// getIndexedType - Returns the type of the element that would be extracted
1792// with an extractvalue instruction with the specified parameters.
1793//
1794// A null type is returned if the indices are invalid for the specified
1795// pointer type.
1796//
Chris Lattner229907c2011-07-18 04:54:35 +00001797Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001798 ArrayRef<unsigned> Idxs) {
1799 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001800 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001801 // We can't use CompositeType::indexValid(Index) here.
1802 // indexValid() always returns true for arrays because getelementptr allows
1803 // out-of-bounds indices. Since we don't allow those for extractvalue and
1804 // insertvalue we need to check array indexing manually.
1805 // Since the only other types we can index into are struct types it's just
1806 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001807 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001808 if (Index >= AT->getNumElements())
1809 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001810 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001811 if (Index >= ST->getNumElements())
1812 return 0;
1813 } else {
1814 // Not a valid type to index into.
1815 return 0;
1816 }
1817
1818 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001819 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001820 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001821}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001822
1823//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824// BinaryOperator Class
1825//===----------------------------------------------------------------------===//
1826
Chris Lattner2195fc42007-02-24 00:55:48 +00001827BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001828 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001829 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001830 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001831 OperandTraits<BinaryOperator>::op_begin(this),
1832 OperandTraits<BinaryOperator>::operands(this),
1833 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001834 Op<0>() = S1;
1835 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001836 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001837 setName(Name);
1838}
1839
1840BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001841 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001842 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001843 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001844 OperandTraits<BinaryOperator>::op_begin(this),
1845 OperandTraits<BinaryOperator>::operands(this),
1846 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001847 Op<0>() = S1;
1848 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001849 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001850 setName(Name);
1851}
1852
1853
1854void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001855 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001856 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001857 assert(LHS->getType() == RHS->getType() &&
1858 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001859#ifndef NDEBUG
1860 switch (iType) {
1861 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001862 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001863 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001864 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001865 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001866 "Tried to create an integer operation on a non-integer type!");
1867 break;
1868 case FAdd: case FSub:
1869 case FMul:
1870 assert(getType() == LHS->getType() &&
1871 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001872 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001873 "Tried to create a floating-point operation on a "
1874 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001875 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001876 case UDiv:
1877 case SDiv:
1878 assert(getType() == LHS->getType() &&
1879 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001880 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001881 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001882 "Incorrect operand type (not integer) for S/UDIV");
1883 break;
1884 case FDiv:
1885 assert(getType() == LHS->getType() &&
1886 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001887 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001888 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001889 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001890 case URem:
1891 case SRem:
1892 assert(getType() == LHS->getType() &&
1893 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001894 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001895 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001896 "Incorrect operand type (not integer) for S/UREM");
1897 break;
1898 case FRem:
1899 assert(getType() == LHS->getType() &&
1900 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001901 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001902 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001903 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001904 case Shl:
1905 case LShr:
1906 case AShr:
1907 assert(getType() == LHS->getType() &&
1908 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001909 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001910 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001911 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001912 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001913 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001914 case And: case Or:
1915 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001916 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001917 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001918 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001919 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001920 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001921 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001922 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001923 default:
1924 break;
1925 }
1926#endif
1927}
1928
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001929BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001930 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001931 Instruction *InsertBefore) {
1932 assert(S1->getType() == S2->getType() &&
1933 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001934 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001935}
1936
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001937BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001938 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001939 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001940 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001941 InsertAtEnd->getInstList().push_back(Res);
1942 return Res;
1943}
1944
Dan Gohman5476cfd2009-08-12 16:23:25 +00001945BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001946 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001947 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001948 return new BinaryOperator(Instruction::Sub,
1949 zero, Op,
1950 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001951}
1952
Dan Gohman5476cfd2009-08-12 16:23:25 +00001953BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001954 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001955 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001956 return new BinaryOperator(Instruction::Sub,
1957 zero, Op,
1958 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001959}
1960
Dan Gohman4ab44202009-12-18 02:58:50 +00001961BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1962 Instruction *InsertBefore) {
1963 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1964 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1965}
1966
1967BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1968 BasicBlock *InsertAtEnd) {
1969 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1970 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1971}
1972
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001973BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1974 Instruction *InsertBefore) {
1975 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1976 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1977}
1978
1979BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1980 BasicBlock *InsertAtEnd) {
1981 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1982 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1983}
1984
Dan Gohman5476cfd2009-08-12 16:23:25 +00001985BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001986 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001987 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001988 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001989 Op->getType(), Name, InsertBefore);
1990}
1991
Dan Gohman5476cfd2009-08-12 16:23:25 +00001992BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001993 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001994 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001995 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001996 Op->getType(), Name, InsertAtEnd);
1997}
1998
Dan Gohman5476cfd2009-08-12 16:23:25 +00001999BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002000 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002001 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002002 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002003 Op->getType(), Name, InsertBefore);
2004}
2005
Dan Gohman5476cfd2009-08-12 16:23:25 +00002006BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002007 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002008 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002009 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002010 Op->getType(), Name, InsertAtEnd);
2011}
2012
2013
2014// isConstantAllOnes - Helper function for several functions below
2015static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002016 if (const Constant *C = dyn_cast<Constant>(V))
2017 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002018 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002019}
2020
Owen Andersonbb2501b2009-07-13 22:18:28 +00002021bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002022 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2023 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002024 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2025 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 return false;
2027}
2028
Owen Andersonbb2501b2009-07-13 22:18:28 +00002029bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002030 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2031 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002032 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00002033 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00002034 return false;
2035}
2036
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002037bool BinaryOperator::isNot(const Value *V) {
2038 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2039 return (Bop->getOpcode() == Instruction::Xor &&
2040 (isConstantAllOnes(Bop->getOperand(1)) ||
2041 isConstantAllOnes(Bop->getOperand(0))));
2042 return false;
2043}
2044
Chris Lattner2c7d1772005-04-24 07:28:37 +00002045Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002046 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002047}
2048
Chris Lattner2c7d1772005-04-24 07:28:37 +00002049const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2050 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051}
2052
Dan Gohmana5b96452009-06-04 22:49:04 +00002053Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002054 return cast<BinaryOperator>(BinOp)->getOperand(1);
2055}
2056
2057const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2058 return getFNegArgument(const_cast<Value*>(BinOp));
2059}
2060
Chris Lattner2c7d1772005-04-24 07:28:37 +00002061Value *BinaryOperator::getNotArgument(Value *BinOp) {
2062 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2063 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2064 Value *Op0 = BO->getOperand(0);
2065 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002066 if (isConstantAllOnes(Op0)) return Op1;
2067
2068 assert(isConstantAllOnes(Op1));
2069 return Op0;
2070}
2071
Chris Lattner2c7d1772005-04-24 07:28:37 +00002072const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2073 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002074}
2075
2076
2077// swapOperands - Exchange the two operands to this instruction. This
2078// instruction is safe to use on any binary instruction and does not
2079// modify the semantics of the instruction. If the instruction is
2080// order dependent (SetLT f.e.) the opcode is changed.
2081//
2082bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002083 if (!isCommutative())
2084 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002085 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002086 return false;
2087}
2088
Dan Gohman1b849082009-09-07 23:54:19 +00002089void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2090 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2091}
2092
2093void BinaryOperator::setHasNoSignedWrap(bool b) {
2094 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2095}
2096
2097void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002098 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002099}
2100
Nick Lewycky28a5f252009-09-27 21:33:04 +00002101bool BinaryOperator::hasNoUnsignedWrap() const {
2102 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2103}
2104
2105bool BinaryOperator::hasNoSignedWrap() const {
2106 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2107}
2108
2109bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002110 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002111}
2112
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002113//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002114// FPMathOperator Class
2115//===----------------------------------------------------------------------===//
2116
2117/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2118/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002119/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002120float FPMathOperator::getFPAccuracy() const {
2121 const MDNode *MD =
2122 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2123 if (!MD)
2124 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002125 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2126 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002127}
2128
2129
2130//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002131// CastInst Class
2132//===----------------------------------------------------------------------===//
2133
David Blaikie3a15e142011-12-01 08:00:17 +00002134void CastInst::anchor() {}
2135
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002136// Just determine if this cast only deals with integral->integral conversion.
2137bool CastInst::isIntegerCast() const {
2138 switch (getOpcode()) {
2139 default: return false;
2140 case Instruction::ZExt:
2141 case Instruction::SExt:
2142 case Instruction::Trunc:
2143 return true;
2144 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002145 return getOperand(0)->getType()->isIntegerTy() &&
2146 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002148}
2149
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002150bool CastInst::isLosslessCast() const {
2151 // Only BitCast can be lossless, exit fast if we're not BitCast
2152 if (getOpcode() != Instruction::BitCast)
2153 return false;
2154
2155 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002156 Type* SrcTy = getOperand(0)->getType();
2157 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158 if (SrcTy == DstTy)
2159 return true;
2160
Reid Spencer8d9336d2006-12-31 05:26:44 +00002161 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002162 if (SrcTy->isPointerTy())
2163 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 return false; // Other types have no identity values
2165}
2166
2167/// This function determines if the CastInst does not require any bits to be
2168/// changed in order to effect the cast. Essentially, it identifies cases where
2169/// no code gen is necessary for the cast, hence the name no-op cast. For
2170/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002171/// # bitcast i32* %x to i8*
2172/// # bitcast <2 x i32> %x to <4 x i16>
2173/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002174/// @brief Determine if the described cast is a no-op.
2175bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002176 Type *SrcTy,
2177 Type *DestTy,
2178 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002179 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002180 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 case Instruction::Trunc:
2182 case Instruction::ZExt:
2183 case Instruction::SExt:
2184 case Instruction::FPTrunc:
2185 case Instruction::FPExt:
2186 case Instruction::UIToFP:
2187 case Instruction::SIToFP:
2188 case Instruction::FPToUI:
2189 case Instruction::FPToSI:
2190 return false; // These always modify bits
2191 case Instruction::BitCast:
2192 return true; // BitCast never modifies bits.
2193 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002194 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002195 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002197 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002198 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 }
2200}
2201
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002202/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002203bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002204 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2205}
2206
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207/// This function determines if a pair of casts can be eliminated and what
2208/// opcode should be used in the elimination. This assumes that there are two
2209/// instructions like this:
2210/// * %F = firstOpcode SrcTy %x to MidTy
2211/// * %S = secondOpcode MidTy %F to DstTy
2212/// The function returns a resultOpcode so these two casts can be replaced with:
2213/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2214/// If no such cast is permited, the function returns 0.
2215unsigned CastInst::isEliminableCastPair(
2216 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002217 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218 // Define the 144 possibilities for these two cast instructions. The values
2219 // in this matrix determine what to do in a given situation and select the
2220 // case in the switch below. The rows correspond to firstOp, the columns
2221 // correspond to secondOp. In looking at the table below, keep in mind
2222 // the following cast properties:
2223 //
2224 // Size Compare Source Destination
2225 // Operator Src ? Size Type Sign Type Sign
2226 // -------- ------------ ------------------- ---------------------
2227 // TRUNC > Integer Any Integral Any
2228 // ZEXT < Integral Unsigned Integer Any
2229 // SEXT < Integral Signed Integer Any
2230 // FPTOUI n/a FloatPt n/a Integral Unsigned
2231 // FPTOSI n/a FloatPt n/a Integral Signed
2232 // UITOFP n/a Integral Unsigned FloatPt n/a
2233 // SITOFP n/a Integral Signed FloatPt n/a
2234 // FPTRUNC > FloatPt n/a FloatPt n/a
2235 // FPEXT < FloatPt n/a FloatPt n/a
2236 // PTRTOINT n/a Pointer n/a Integral Unsigned
2237 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002238 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002239 //
2240 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002241 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2242 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002243 // of the produced value (we no longer know the top-part is all zeros).
2244 // Further this conversion is often much more expensive for typical hardware,
2245 // and causes issues when building libgcc. We disallow fptosi+sext for the
2246 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 const unsigned numCastOps =
2248 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2249 static const uint8_t CastResults[numCastOps][numCastOps] = {
2250 // T F F U S F F P I B -+
2251 // R Z S P P I I T P 2 N T |
2252 // U E E 2 2 2 2 R E I T C +- secondOp
2253 // N X X U S F F N X N 2 V |
2254 // C T T I I P P C T T P T -+
2255 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2256 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2257 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002258 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2259 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2261 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2262 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2263 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2264 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2265 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2266 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2267 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002268
2269 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002270 // merging. However, bitcast of A->B->A are allowed.
2271 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2272 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2273 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2274
2275 // Check if any of the bitcasts convert scalars<->vectors.
2276 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2277 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2278 // Unless we are bitcasing to the original type, disallow optimizations.
2279 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280
2281 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2282 [secondOp-Instruction::CastOpsBegin];
2283 switch (ElimCase) {
2284 case 0:
2285 // categorically disallowed
2286 return 0;
2287 case 1:
2288 // allowed, use first cast's opcode
2289 return firstOp;
2290 case 2:
2291 // allowed, use second cast's opcode
2292 return secondOp;
2293 case 3:
2294 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002295 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002296 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002297 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 return firstOp;
2299 return 0;
2300 case 4:
2301 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002302 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002303 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 return firstOp;
2305 return 0;
2306 case 5:
2307 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002308 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002309 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 return secondOp;
2311 return 0;
2312 case 6:
2313 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002314 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002315 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 return secondOp;
2317 return 0;
2318 case 7: {
2319 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002320 if (!IntPtrTy)
2321 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002322 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2323 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 if (MidSize >= PtrSize)
2325 return Instruction::BitCast;
2326 return 0;
2327 }
2328 case 8: {
2329 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2330 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2331 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002332 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2333 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 if (SrcSize == DstSize)
2335 return Instruction::BitCast;
2336 else if (SrcSize < DstSize)
2337 return firstOp;
2338 return secondOp;
2339 }
2340 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2341 return Instruction::ZExt;
2342 case 10:
2343 // fpext followed by ftrunc is allowed if the bit size returned to is
2344 // the same as the original, in which case its just a bitcast
2345 if (SrcTy == DstTy)
2346 return Instruction::BitCast;
2347 return 0; // If the types are not the same we can't eliminate it.
2348 case 11:
2349 // bitcast followed by ptrtoint is allowed as long as the bitcast
2350 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002351 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 return secondOp;
2353 return 0;
2354 case 12:
2355 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002356 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 return firstOp;
2358 return 0;
2359 case 13: {
2360 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002361 if (!IntPtrTy)
2362 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002363 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2364 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2365 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366 if (SrcSize <= PtrSize && SrcSize == DstSize)
2367 return Instruction::BitCast;
2368 return 0;
2369 }
2370 case 99:
2371 // cast combination can't happen (error in input). This is for all cases
2372 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002373 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374 default:
Craig Topperc514b542012-02-05 22:14:15 +00002375 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377}
2378
Chris Lattner229907c2011-07-18 04:54:35 +00002379CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002380 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002381 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 // Construct and return the appropriate CastInst subclass
2383 switch (op) {
2384 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2385 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2386 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2387 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2388 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2389 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2390 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2391 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2392 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2393 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2394 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2395 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002396 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398}
2399
Chris Lattner229907c2011-07-18 04:54:35 +00002400CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002401 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002402 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 // Construct and return the appropriate CastInst subclass
2404 switch (op) {
2405 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2406 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2407 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2408 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2409 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2410 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2411 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2412 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2413 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2414 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2415 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2416 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002417 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419}
2420
Chris Lattner229907c2011-07-18 04:54:35 +00002421CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002422 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002423 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002424 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002425 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2426 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002427}
2428
Chris Lattner229907c2011-07-18 04:54:35 +00002429CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002430 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002431 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002432 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002433 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2434 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002435}
2436
Chris Lattner229907c2011-07-18 04:54:35 +00002437CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002438 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002439 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002440 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002441 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2442 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002443}
2444
Chris Lattner229907c2011-07-18 04:54:35 +00002445CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002446 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002447 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002448 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002449 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2450 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002451}
2452
Chris Lattner229907c2011-07-18 04:54:35 +00002453CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002454 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002455 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002456 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002457 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2458 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002459}
2460
Chris Lattner229907c2011-07-18 04:54:35 +00002461CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002462 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002463 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002464 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002465 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2466 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002467}
2468
Chris Lattner229907c2011-07-18 04:54:35 +00002469CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002470 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002471 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002472 assert(S->getType()->isPointerTy() && "Invalid cast");
2473 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002474 "Invalid cast");
2475
Duncan Sands9dff9be2010-02-15 16:12:20 +00002476 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002477 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2478 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002479}
2480
2481/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002482CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002483 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002484 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002485 assert(S->getType()->isPointerTy() && "Invalid cast");
2486 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002487 "Invalid cast");
2488
Duncan Sands9dff9be2010-02-15 16:12:20 +00002489 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002490 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2491 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002492}
2493
Chris Lattner229907c2011-07-18 04:54:35 +00002494CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002495 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002496 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002497 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002498 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002499 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2500 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002501 Instruction::CastOps opcode =
2502 (SrcBits == DstBits ? Instruction::BitCast :
2503 (SrcBits > DstBits ? Instruction::Trunc :
2504 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002505 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002506}
2507
Chris Lattner229907c2011-07-18 04:54:35 +00002508CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002509 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002510 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002511 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002512 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002513 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2514 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002515 Instruction::CastOps opcode =
2516 (SrcBits == DstBits ? Instruction::BitCast :
2517 (SrcBits > DstBits ? Instruction::Trunc :
2518 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002519 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002520}
2521
Chris Lattner229907c2011-07-18 04:54:35 +00002522CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002523 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002524 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002525 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002526 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002527 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2528 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002529 Instruction::CastOps opcode =
2530 (SrcBits == DstBits ? Instruction::BitCast :
2531 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002532 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002533}
2534
Chris Lattner229907c2011-07-18 04:54:35 +00002535CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002536 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002537 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002538 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002539 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002540 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2541 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002542 Instruction::CastOps opcode =
2543 (SrcBits == DstBits ? Instruction::BitCast :
2544 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002545 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002546}
2547
Duncan Sands55e50902008-01-06 10:12:28 +00002548// Check whether it is valid to call getCastOpcode for these types.
2549// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002550bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002551 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2552 return false;
2553
2554 if (SrcTy == DestTy)
2555 return true;
2556
Chris Lattner229907c2011-07-18 04:54:35 +00002557 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2558 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002559 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2560 // An element by element cast. Valid if casting the elements is valid.
2561 SrcTy = SrcVecTy->getElementType();
2562 DestTy = DestVecTy->getElementType();
2563 }
2564
Duncan Sands55e50902008-01-06 10:12:28 +00002565 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002566 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2567 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002568
2569 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002570 if (DestTy->isIntegerTy()) { // Casting to integral
2571 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002572 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002573 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002574 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002575 } else if (SrcTy->isVectorTy()) { // Casting from vector
2576 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002577 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002578 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002579 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002580 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2581 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002582 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002583 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002584 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002585 } else if (SrcTy->isVectorTy()) { // Casting from vector
2586 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002587 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002588 return false;
2589 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002590 } else if (DestTy->isVectorTy()) { // Casting to vector
2591 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002592 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002593 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002594 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002595 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002596 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002597 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002598 return false;
2599 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002600 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002601 if (SrcTy->isVectorTy()) {
2602 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002603 } else {
2604 return false;
2605 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002606 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002607 return false;
2608 }
2609}
2610
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611// Provide a way to get a "cast" where the cast opcode is inferred from the
2612// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002613// logic in the castIsValid function below. This axiom should hold:
2614// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2615// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002617// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002619CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002620 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2621 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622
Duncan Sands55e50902008-01-06 10:12:28 +00002623 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2624 "Only first class types are castable!");
2625
Duncan Sandsa8514532011-05-18 07:13:41 +00002626 if (SrcTy == DestTy)
2627 return BitCast;
2628
Chris Lattner229907c2011-07-18 04:54:35 +00002629 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2630 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002631 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2632 // An element by element cast. Find the appropriate opcode based on the
2633 // element types.
2634 SrcTy = SrcVecTy->getElementType();
2635 DestTy = DestVecTy->getElementType();
2636 }
2637
2638 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002639 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2640 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002641
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002643 if (DestTy->isIntegerTy()) { // Casting to integral
2644 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645 if (DestBits < SrcBits)
2646 return Trunc; // int -> smaller int
2647 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002648 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649 return SExt; // signed -> SEXT
2650 else
2651 return ZExt; // unsigned -> ZEXT
2652 } else {
2653 return BitCast; // Same size, No-op cast
2654 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002655 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002656 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 return FPToSI; // FP -> sint
2658 else
2659 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002660 } else if (SrcTy->isVectorTy()) {
2661 assert(DestBits == SrcBits &&
2662 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663 return BitCast; // Same size, no-op cast
2664 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002665 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666 "Casting from a value that is not first-class type");
2667 return PtrToInt; // ptr -> int
2668 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002669 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2670 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002671 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672 return SIToFP; // sint -> FP
2673 else
2674 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002675 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676 if (DestBits < SrcBits) {
2677 return FPTrunc; // FP -> smaller FP
2678 } else if (DestBits > SrcBits) {
2679 return FPExt; // FP -> larger FP
2680 } else {
2681 return BitCast; // same size, no-op cast
2682 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002683 } else if (SrcTy->isVectorTy()) {
2684 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002685 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002686 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002688 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002689 } else if (DestTy->isVectorTy()) {
2690 assert(DestBits == SrcBits &&
2691 "Illegal cast to vector (wrong type or size)");
2692 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002693 } else if (DestTy->isPointerTy()) {
2694 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002696 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002697 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002699 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002700 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002701 if (SrcTy->isVectorTy()) {
2702 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002703 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002704 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002705 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002707 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708}
2709
2710//===----------------------------------------------------------------------===//
2711// CastInst SubClass Constructors
2712//===----------------------------------------------------------------------===//
2713
2714/// Check that the construction parameters for a CastInst are correct. This
2715/// could be broken out into the separate constructors but it is useful to have
2716/// it in one place and to eliminate the redundant code for getting the sizes
2717/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002718bool
Chris Lattner229907c2011-07-18 04:54:35 +00002719CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720
2721 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002722 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002723 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2724 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725 return false;
2726
2727 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002728 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2729 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730
Duncan Sands7f646562011-05-18 09:21:57 +00002731 // If these are vector types, get the lengths of the vectors (using zero for
2732 // scalar types means that checking that vector lengths match also checks that
2733 // scalars are not being converted to vectors or vectors to scalars).
2734 unsigned SrcLength = SrcTy->isVectorTy() ?
2735 cast<VectorType>(SrcTy)->getNumElements() : 0;
2736 unsigned DstLength = DstTy->isVectorTy() ?
2737 cast<VectorType>(DstTy)->getNumElements() : 0;
2738
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739 // Switch on the opcode provided
2740 switch (op) {
2741 default: return false; // This is an input error
2742 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002743 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2744 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002746 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2747 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002749 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2750 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002752 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2753 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002754 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002755 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2756 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002759 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2760 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002763 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2764 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002766 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002767 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002768 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2769 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2770 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002771 return SrcTy->getScalarType()->isPointerTy() &&
2772 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002773 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002774 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002775 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002776 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2777 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2778 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002779 return SrcTy->getScalarType()->isIntegerTy() &&
2780 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781 case Instruction::BitCast:
2782 // BitCast implies a no-op cast of type only. No bits change.
2783 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002784 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002785 return false;
2786
Duncan Sands55e50902008-01-06 10:12:28 +00002787 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788 // these cases, the cast is okay if the source and destination bit widths
2789 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002790 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002791 }
2792}
2793
2794TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002795 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002797 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002798}
2799
2800TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002801 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002802) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002803 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002804}
2805
2806ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002807 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002808) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002809 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002810}
2811
2812ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002813 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002815 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002816}
2817SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002818 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002820 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002821}
2822
Jeff Cohencc08c832006-12-02 02:22:01 +00002823SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002824 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002827}
2828
2829FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002830 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002833}
2834
2835FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002836 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002838 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002839}
2840
2841FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002842 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002844 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002845}
2846
2847FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002848 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002850 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851}
2852
2853UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002854 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002855) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002856 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002857}
2858
2859UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002860 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002861) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002862 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002863}
2864
2865SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002866 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002867) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002868 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869}
2870
2871SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002872 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002874 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002875}
2876
2877FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002878 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002879) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002880 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002881}
2882
2883FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002884 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002886 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002887}
2888
2889FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002890 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002892 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893}
2894
2895FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002896 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002897) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002898 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002899}
2900
2901PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002902 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002904 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905}
2906
2907PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002908 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002909) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002910 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911}
2912
2913IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002914 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002916 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917}
2918
2919IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002920 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002921) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002922 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002923}
2924
2925BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002926 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002928 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929}
2930
2931BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002932 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002934 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002936
2937//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002938// CmpInst Classes
2939//===----------------------------------------------------------------------===//
2940
Craig Topper3186c012012-09-23 02:12:10 +00002941void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002942
Chris Lattner229907c2011-07-18 04:54:35 +00002943CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002944 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002945 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002946 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002947 OperandTraits<CmpInst>::op_begin(this),
2948 OperandTraits<CmpInst>::operands(this),
2949 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002950 Op<0>() = LHS;
2951 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002952 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002953 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002954}
Gabor Greiff6caff662008-05-10 08:32:32 +00002955
Chris Lattner229907c2011-07-18 04:54:35 +00002956CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002957 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002958 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002959 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002960 OperandTraits<CmpInst>::op_begin(this),
2961 OperandTraits<CmpInst>::operands(this),
2962 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002963 Op<0>() = LHS;
2964 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002965 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002966 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002967}
2968
2969CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002970CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002971 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002972 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002973 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002974 if (InsertBefore)
2975 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2976 S1, S2, Name);
2977 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002978 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002979 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002980 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002981
2982 if (InsertBefore)
2983 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2984 S1, S2, Name);
2985 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002986 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002987 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002988}
2989
2990CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002991CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002992 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002993 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002994 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2995 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002996 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2998 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002999}
3000
3001void CmpInst::swapOperands() {
3002 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3003 IC->swapOperands();
3004 else
3005 cast<FCmpInst>(this)->swapOperands();
3006}
3007
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003008bool CmpInst::isCommutative() const {
3009 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003010 return IC->isCommutative();
3011 return cast<FCmpInst>(this)->isCommutative();
3012}
3013
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003014bool CmpInst::isEquality() const {
3015 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003016 return IC->isEquality();
3017 return cast<FCmpInst>(this)->isEquality();
3018}
3019
3020
Dan Gohman4e724382008-05-31 02:47:54 +00003021CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003022 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003023 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003024 case ICMP_EQ: return ICMP_NE;
3025 case ICMP_NE: return ICMP_EQ;
3026 case ICMP_UGT: return ICMP_ULE;
3027 case ICMP_ULT: return ICMP_UGE;
3028 case ICMP_UGE: return ICMP_ULT;
3029 case ICMP_ULE: return ICMP_UGT;
3030 case ICMP_SGT: return ICMP_SLE;
3031 case ICMP_SLT: return ICMP_SGE;
3032 case ICMP_SGE: return ICMP_SLT;
3033 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003034
Dan Gohman4e724382008-05-31 02:47:54 +00003035 case FCMP_OEQ: return FCMP_UNE;
3036 case FCMP_ONE: return FCMP_UEQ;
3037 case FCMP_OGT: return FCMP_ULE;
3038 case FCMP_OLT: return FCMP_UGE;
3039 case FCMP_OGE: return FCMP_ULT;
3040 case FCMP_OLE: return FCMP_UGT;
3041 case FCMP_UEQ: return FCMP_ONE;
3042 case FCMP_UNE: return FCMP_OEQ;
3043 case FCMP_UGT: return FCMP_OLE;
3044 case FCMP_ULT: return FCMP_OGE;
3045 case FCMP_UGE: return FCMP_OLT;
3046 case FCMP_ULE: return FCMP_OGT;
3047 case FCMP_ORD: return FCMP_UNO;
3048 case FCMP_UNO: return FCMP_ORD;
3049 case FCMP_TRUE: return FCMP_FALSE;
3050 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003051 }
3052}
3053
Reid Spencer266e42b2006-12-23 06:05:41 +00003054ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3055 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003056 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003057 case ICMP_EQ: case ICMP_NE:
3058 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3059 return pred;
3060 case ICMP_UGT: return ICMP_SGT;
3061 case ICMP_ULT: return ICMP_SLT;
3062 case ICMP_UGE: return ICMP_SGE;
3063 case ICMP_ULE: return ICMP_SLE;
3064 }
3065}
3066
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003067ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3068 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003069 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003070 case ICMP_EQ: case ICMP_NE:
3071 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3072 return pred;
3073 case ICMP_SGT: return ICMP_UGT;
3074 case ICMP_SLT: return ICMP_ULT;
3075 case ICMP_SGE: return ICMP_UGE;
3076 case ICMP_SLE: return ICMP_ULE;
3077 }
3078}
3079
Reid Spencer0286bc12007-02-28 22:00:54 +00003080/// Initialize a set of values that all satisfy the condition with C.
3081///
3082ConstantRange
3083ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3084 APInt Lower(C);
3085 APInt Upper(C);
3086 uint32_t BitWidth = C.getBitWidth();
3087 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003088 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003089 case ICmpInst::ICMP_EQ: Upper++; break;
3090 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003091 case ICmpInst::ICMP_ULT:
3092 Lower = APInt::getMinValue(BitWidth);
3093 // Check for an empty-set condition.
3094 if (Lower == Upper)
3095 return ConstantRange(BitWidth, /*isFullSet=*/false);
3096 break;
3097 case ICmpInst::ICMP_SLT:
3098 Lower = APInt::getSignedMinValue(BitWidth);
3099 // Check for an empty-set condition.
3100 if (Lower == Upper)
3101 return ConstantRange(BitWidth, /*isFullSet=*/false);
3102 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003103 case ICmpInst::ICMP_UGT:
3104 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003105 // Check for an empty-set condition.
3106 if (Lower == Upper)
3107 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003108 break;
3109 case ICmpInst::ICMP_SGT:
3110 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003111 // Check for an empty-set condition.
3112 if (Lower == Upper)
3113 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003114 break;
3115 case ICmpInst::ICMP_ULE:
3116 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003117 // Check for a full-set condition.
3118 if (Lower == Upper)
3119 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003120 break;
3121 case ICmpInst::ICMP_SLE:
3122 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003123 // Check for a full-set condition.
3124 if (Lower == Upper)
3125 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003126 break;
3127 case ICmpInst::ICMP_UGE:
3128 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003129 // Check for a full-set condition.
3130 if (Lower == Upper)
3131 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003132 break;
3133 case ICmpInst::ICMP_SGE:
3134 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003135 // Check for a full-set condition.
3136 if (Lower == Upper)
3137 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003138 break;
3139 }
3140 return ConstantRange(Lower, Upper);
3141}
3142
Dan Gohman4e724382008-05-31 02:47:54 +00003143CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003144 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003145 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003146 case ICMP_EQ: case ICMP_NE:
3147 return pred;
3148 case ICMP_SGT: return ICMP_SLT;
3149 case ICMP_SLT: return ICMP_SGT;
3150 case ICMP_SGE: return ICMP_SLE;
3151 case ICMP_SLE: return ICMP_SGE;
3152 case ICMP_UGT: return ICMP_ULT;
3153 case ICMP_ULT: return ICMP_UGT;
3154 case ICMP_UGE: return ICMP_ULE;
3155 case ICMP_ULE: return ICMP_UGE;
3156
Reid Spencerd9436b62006-11-20 01:22:35 +00003157 case FCMP_FALSE: case FCMP_TRUE:
3158 case FCMP_OEQ: case FCMP_ONE:
3159 case FCMP_UEQ: case FCMP_UNE:
3160 case FCMP_ORD: case FCMP_UNO:
3161 return pred;
3162 case FCMP_OGT: return FCMP_OLT;
3163 case FCMP_OLT: return FCMP_OGT;
3164 case FCMP_OGE: return FCMP_OLE;
3165 case FCMP_OLE: return FCMP_OGE;
3166 case FCMP_UGT: return FCMP_ULT;
3167 case FCMP_ULT: return FCMP_UGT;
3168 case FCMP_UGE: return FCMP_ULE;
3169 case FCMP_ULE: return FCMP_UGE;
3170 }
3171}
3172
Reid Spencer266e42b2006-12-23 06:05:41 +00003173bool CmpInst::isUnsigned(unsigned short predicate) {
3174 switch (predicate) {
3175 default: return false;
3176 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3177 case ICmpInst::ICMP_UGE: return true;
3178 }
3179}
3180
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003181bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003182 switch (predicate) {
3183 default: return false;
3184 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3185 case ICmpInst::ICMP_SGE: return true;
3186 }
3187}
3188
3189bool CmpInst::isOrdered(unsigned short predicate) {
3190 switch (predicate) {
3191 default: return false;
3192 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3193 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3194 case FCmpInst::FCMP_ORD: return true;
3195 }
3196}
3197
3198bool CmpInst::isUnordered(unsigned short predicate) {
3199 switch (predicate) {
3200 default: return false;
3201 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3202 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3203 case FCmpInst::FCMP_UNO: return true;
3204 }
3205}
3206
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003207bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3208 switch(predicate) {
3209 default: return false;
3210 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3211 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3212 }
3213}
3214
3215bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3216 switch(predicate) {
3217 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3218 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3219 default: return false;
3220 }
3221}
3222
3223
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003224//===----------------------------------------------------------------------===//
3225// SwitchInst Implementation
3226//===----------------------------------------------------------------------===//
3227
Chris Lattnerbaf00152010-11-17 05:41:46 +00003228void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3229 assert(Value && Default && NumReserved);
3230 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003231 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003232 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003233
Gabor Greif2d3024d2008-05-26 21:33:52 +00003234 OperandList[0] = Value;
3235 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003236}
3237
Chris Lattner2195fc42007-02-24 00:55:48 +00003238/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3239/// switch on and a default destination. The number of additional cases can
3240/// be specified here to make memory allocation more efficient. This
3241/// constructor can also autoinsert before another instruction.
3242SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3243 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003244 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3245 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003246 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003247}
3248
3249/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3250/// switch on and a default destination. The number of additional cases can
3251/// be specified here to make memory allocation more efficient. This
3252/// constructor also autoinserts at the end of the specified BasicBlock.
3253SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3254 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003255 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3256 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003257 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003258}
3259
Misha Brukmanb1c93172005-04-21 23:48:37 +00003260SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003261 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3262 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3263 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003264 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003265 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003266 OL[i] = InOL[i];
3267 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003268 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003269 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003270 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003271}
3272
Gordon Henriksen14a55692007-12-10 02:14:30 +00003273SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003274 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003275}
3276
3277
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003278/// addCase - Add an entry to the switch instruction...
3279///
Chris Lattner47ac1872005-02-24 05:32:09 +00003280void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003281 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003282
3283 // FIXME: Currently we work with ConstantInt based cases.
3284 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003285 Mapping.add(IntItem::fromConstantInt(OnVal));
3286 IntegersSubset CaseRanges = Mapping.getCase();
3287 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003288}
3289
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003290void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003291 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003292 unsigned OpNo = NumOperands;
3293 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003294 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003295 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003296 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003297 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003298
3299 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3300
3301 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3302 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003303 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003304}
3305
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003306/// removeCase - This method removes the specified case and its successor
3307/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003308void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003309 unsigned idx = i.getCaseIndex();
3310
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003311 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003312
3313 unsigned NumOps = getNumOperands();
3314 Use *OL = OperandList;
3315
Jay Foad14277722011-02-01 09:22:34 +00003316 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003317 if (2 + (idx + 1) * 2 != NumOps) {
3318 OL[2 + idx * 2] = OL[NumOps - 2];
3319 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003320 }
3321
3322 // Nuke the last value.
3323 OL[NumOps-2].set(0);
3324 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003325
3326 // Do the same with TheCases collection:
3327 if (i.SubsetIt != --TheSubsets.end()) {
3328 *i.SubsetIt = TheSubsets.back();
3329 TheSubsets.pop_back();
3330 } else {
3331 TheSubsets.pop_back();
3332 i.SubsetIt = TheSubsets.end();
3333 }
3334
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003335 NumOperands = NumOps-2;
3336}
3337
Jay Foade98f29d2011-04-01 08:00:58 +00003338/// growOperands - grow operands - This grows the operand list in response
3339/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003340///
Jay Foade98f29d2011-04-01 08:00:58 +00003341void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003342 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003343 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003344
3345 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003346 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003347 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003348 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003349 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003350 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003351 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003352 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003353}
3354
3355
3356BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3357 return getSuccessor(idx);
3358}
3359unsigned SwitchInst::getNumSuccessorsV() const {
3360 return getNumSuccessors();
3361}
3362void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3363 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003364}
Chris Lattnerf22be932004-10-15 23:52:53 +00003365
Chris Lattner3ed871f2009-10-27 19:13:16 +00003366//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003367// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368//===----------------------------------------------------------------------===//
3369
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003370void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003371 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003372 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003373 ReservedSpace = 1+NumDests;
3374 NumOperands = 1;
3375 OperandList = allocHungoffUses(ReservedSpace);
3376
3377 OperandList[0] = Address;
3378}
3379
3380
Jay Foade98f29d2011-04-01 08:00:58 +00003381/// growOperands - grow operands - This grows the operand list in response
3382/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003383///
Jay Foade98f29d2011-04-01 08:00:58 +00003384void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003385 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003386 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003387
3388 ReservedSpace = NumOps;
3389 Use *NewOps = allocHungoffUses(NumOps);
3390 Use *OldOps = OperandList;
3391 for (unsigned i = 0; i != e; ++i)
3392 NewOps[i] = OldOps[i];
3393 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003394 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003395}
3396
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003397IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3398 Instruction *InsertBefore)
3399: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003400 0, 0, InsertBefore) {
3401 init(Address, NumCases);
3402}
3403
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003404IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3405 BasicBlock *InsertAtEnd)
3406: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003407 0, 0, InsertAtEnd) {
3408 init(Address, NumCases);
3409}
3410
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003411IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3412 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003413 allocHungoffUses(IBI.getNumOperands()),
3414 IBI.getNumOperands()) {
3415 Use *OL = OperandList, *InOL = IBI.OperandList;
3416 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3417 OL[i] = InOL[i];
3418 SubclassOptionalData = IBI.SubclassOptionalData;
3419}
3420
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003421IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003422 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003423}
3424
3425/// addDestination - Add a destination.
3426///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003427void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003428 unsigned OpNo = NumOperands;
3429 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003430 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003431 // Initialize some new operands.
3432 assert(OpNo < ReservedSpace && "Growing didn't work!");
3433 NumOperands = OpNo+1;
3434 OperandList[OpNo] = DestBB;
3435}
3436
3437/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003438/// indirectbr instruction.
3439void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003440 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3441
3442 unsigned NumOps = getNumOperands();
3443 Use *OL = OperandList;
3444
3445 // Replace this value with the last one.
3446 OL[idx+1] = OL[NumOps-1];
3447
3448 // Nuke the last value.
3449 OL[NumOps-1].set(0);
3450 NumOperands = NumOps-1;
3451}
3452
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003453BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003454 return getSuccessor(idx);
3455}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003456unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003457 return getNumSuccessors();
3458}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003459void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003460 setSuccessor(idx, B);
3461}
3462
3463//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003464// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003465//===----------------------------------------------------------------------===//
3466
Chris Lattnerf22be932004-10-15 23:52:53 +00003467// Define these methods here so vtables don't get emitted into every translation
3468// unit that uses these classes.
3469
Devang Patel11cf3f42009-10-27 22:16:29 +00003470GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3471 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003472}
3473
Devang Patel11cf3f42009-10-27 22:16:29 +00003474BinaryOperator *BinaryOperator::clone_impl() const {
3475 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003476}
3477
Devang Patel11cf3f42009-10-27 22:16:29 +00003478FCmpInst* FCmpInst::clone_impl() const {
3479 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003480}
3481
Devang Patel11cf3f42009-10-27 22:16:29 +00003482ICmpInst* ICmpInst::clone_impl() const {
3483 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003484}
3485
Devang Patel11cf3f42009-10-27 22:16:29 +00003486ExtractValueInst *ExtractValueInst::clone_impl() const {
3487 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003488}
3489
Devang Patel11cf3f42009-10-27 22:16:29 +00003490InsertValueInst *InsertValueInst::clone_impl() const {
3491 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003492}
3493
Devang Patel11cf3f42009-10-27 22:16:29 +00003494AllocaInst *AllocaInst::clone_impl() const {
3495 return new AllocaInst(getAllocatedType(),
3496 (Value*)getOperand(0),
3497 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003498}
3499
Devang Patel11cf3f42009-10-27 22:16:29 +00003500LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003501 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3502 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003503}
3504
Devang Patel11cf3f42009-10-27 22:16:29 +00003505StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003506 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003507 getAlignment(), getOrdering(), getSynchScope());
3508
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003509}
3510
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003511AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3512 AtomicCmpXchgInst *Result =
3513 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3514 getOrdering(), getSynchScope());
3515 Result->setVolatile(isVolatile());
3516 return Result;
3517}
3518
3519AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3520 AtomicRMWInst *Result =
3521 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3522 getOrdering(), getSynchScope());
3523 Result->setVolatile(isVolatile());
3524 return Result;
3525}
3526
Eli Friedmanfee02c62011-07-25 23:16:38 +00003527FenceInst *FenceInst::clone_impl() const {
3528 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3529}
3530
Devang Patel11cf3f42009-10-27 22:16:29 +00003531TruncInst *TruncInst::clone_impl() const {
3532 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003533}
3534
Devang Patel11cf3f42009-10-27 22:16:29 +00003535ZExtInst *ZExtInst::clone_impl() const {
3536 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003537}
3538
Devang Patel11cf3f42009-10-27 22:16:29 +00003539SExtInst *SExtInst::clone_impl() const {
3540 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003541}
3542
Devang Patel11cf3f42009-10-27 22:16:29 +00003543FPTruncInst *FPTruncInst::clone_impl() const {
3544 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003545}
3546
Devang Patel11cf3f42009-10-27 22:16:29 +00003547FPExtInst *FPExtInst::clone_impl() const {
3548 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003549}
3550
Devang Patel11cf3f42009-10-27 22:16:29 +00003551UIToFPInst *UIToFPInst::clone_impl() const {
3552 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003553}
3554
Devang Patel11cf3f42009-10-27 22:16:29 +00003555SIToFPInst *SIToFPInst::clone_impl() const {
3556 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003557}
3558
Devang Patel11cf3f42009-10-27 22:16:29 +00003559FPToUIInst *FPToUIInst::clone_impl() const {
3560 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003561}
3562
Devang Patel11cf3f42009-10-27 22:16:29 +00003563FPToSIInst *FPToSIInst::clone_impl() const {
3564 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003565}
3566
Devang Patel11cf3f42009-10-27 22:16:29 +00003567PtrToIntInst *PtrToIntInst::clone_impl() const {
3568 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003569}
3570
Devang Patel11cf3f42009-10-27 22:16:29 +00003571IntToPtrInst *IntToPtrInst::clone_impl() const {
3572 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003573}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003574
Devang Patel11cf3f42009-10-27 22:16:29 +00003575BitCastInst *BitCastInst::clone_impl() const {
3576 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003577}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003578
Devang Patel11cf3f42009-10-27 22:16:29 +00003579CallInst *CallInst::clone_impl() const {
3580 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003581}
3582
Devang Patel11cf3f42009-10-27 22:16:29 +00003583SelectInst *SelectInst::clone_impl() const {
3584 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003585}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003586
Devang Patel11cf3f42009-10-27 22:16:29 +00003587VAArgInst *VAArgInst::clone_impl() const {
3588 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003589}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003590
Devang Patel11cf3f42009-10-27 22:16:29 +00003591ExtractElementInst *ExtractElementInst::clone_impl() const {
3592 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003593}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003594
Devang Patel11cf3f42009-10-27 22:16:29 +00003595InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003596 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003597}
3598
Devang Patel11cf3f42009-10-27 22:16:29 +00003599ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003600 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003601}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003602
Devang Patel11cf3f42009-10-27 22:16:29 +00003603PHINode *PHINode::clone_impl() const {
3604 return new PHINode(*this);
3605}
3606
Bill Wendlingfae14752011-08-12 20:24:12 +00003607LandingPadInst *LandingPadInst::clone_impl() const {
3608 return new LandingPadInst(*this);
3609}
3610
Devang Patel11cf3f42009-10-27 22:16:29 +00003611ReturnInst *ReturnInst::clone_impl() const {
3612 return new(getNumOperands()) ReturnInst(*this);
3613}
3614
3615BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003616 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003617}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003618
Devang Patel11cf3f42009-10-27 22:16:29 +00003619SwitchInst *SwitchInst::clone_impl() const {
3620 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003621}
3622
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003623IndirectBrInst *IndirectBrInst::clone_impl() const {
3624 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003625}
3626
3627
Devang Patel11cf3f42009-10-27 22:16:29 +00003628InvokeInst *InvokeInst::clone_impl() const {
3629 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003630}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003631
Bill Wendlingf891bf82011-07-31 06:30:59 +00003632ResumeInst *ResumeInst::clone_impl() const {
3633 return new(1) ResumeInst(*this);
3634}
3635
Devang Patel11cf3f42009-10-27 22:16:29 +00003636UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003637 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003638 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003639}