blob: 1d732687236c1c4ceb7d64148ec625cd95be4bea [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 {
346 if (AttributeList.getParamAttributes(~0U).hasNoAliasAttr())
347 return true;
348 if (const Function *F = getCalledFunction())
349 return F->getParamAttributes(~0U).hasNoAliasAttr();
350 return false;
351}
352bool CallInst::fnHasNoInlineAttr() const {
353 if (AttributeList.getParamAttributes(~0U).hasNoInlineAttr())
354 return true;
355 if (const Function *F = getCalledFunction())
356 return F->getParamAttributes(~0U).hasNoInlineAttr();
357 return false;
358}
359bool CallInst::fnHasNoReturnAttr() const {
360 if (AttributeList.getParamAttributes(~0U).hasNoReturnAttr())
361 return true;
362 if (const Function *F = getCalledFunction())
363 return F->getParamAttributes(~0U).hasNoReturnAttr();
364 return false;
365}
366bool CallInst::fnHasNoUnwindAttr() const {
367 if (AttributeList.getParamAttributes(~0U).hasNoUnwindAttr())
368 return true;
369 if (const Function *F = getCalledFunction())
370 return F->getParamAttributes(~0U).hasNoUnwindAttr();
371 return false;
372}
373bool CallInst::fnHasReadNoneAttr() const {
374 if (AttributeList.getParamAttributes(~0U).hasReadNoneAttr())
375 return true;
376 if (const Function *F = getCalledFunction())
377 return F->getParamAttributes(~0U).hasReadNoneAttr();
378 return false;
379}
380bool CallInst::fnHasReadOnlyAttr() const {
381 if (AttributeList.getParamAttributes(~0U).hasReadOnlyAttr())
382 return true;
383 if (const Function *F = getCalledFunction())
384 return F->getParamAttributes(~0U).hasReadOnlyAttr();
385 return false;
386}
387bool CallInst::fnHasReturnsTwiceAttr() const {
388 if (AttributeList.getParamAttributes(~0U).hasReturnsTwiceAttr())
389 return true;
390 if (const Function *F = getCalledFunction())
391 return F->getParamAttributes(~0U).hasReturnsTwiceAttr();
392 return false;
393}
394
Bill Wendling8baa61d2012-10-03 17:54:26 +0000395bool CallInst::paramHasSExtAttr(unsigned i) const {
396 if (AttributeList.getParamAttributes(i).hasSExtAttr())
397 return true;
398 if (const Function *F = getCalledFunction())
399 return F->getParamAttributes(i).hasSExtAttr();
400 return false;
401}
402
403bool CallInst::paramHasZExtAttr(unsigned i) const {
404 if (AttributeList.getParamAttributes(i).hasZExtAttr())
405 return true;
406 if (const Function *F = getCalledFunction())
407 return F->getParamAttributes(i).hasZExtAttr();
408 return false;
409}
410
411bool CallInst::paramHasInRegAttr(unsigned i) const {
412 if (AttributeList.getParamAttributes(i).hasInRegAttr())
413 return true;
414 if (const Function *F = getCalledFunction())
415 return F->getParamAttributes(i).hasInRegAttr();
416 return false;
417}
418
419bool CallInst::paramHasStructRetAttr(unsigned i) const {
420 if (AttributeList.getParamAttributes(i).hasStructRetAttr())
421 return true;
422 if (const Function *F = getCalledFunction())
423 return F->getParamAttributes(i).hasStructRetAttr();
424 return false;
425}
426
427bool CallInst::paramHasNestAttr(unsigned i) const {
428 if (AttributeList.getParamAttributes(i).hasNestAttr())
429 return true;
430 if (const Function *F = getCalledFunction())
431 return F->getParamAttributes(i).hasNestAttr();
432 return false;
433}
434
435bool CallInst::paramHasByValAttr(unsigned i) const {
436 if (AttributeList.getParamAttributes(i).hasByValAttr())
437 return true;
438 if (const Function *F = getCalledFunction())
439 return F->getParamAttributes(i).hasByValAttr();
440 return false;
441}
442
Bill Wendlingd7773982012-10-04 06:52:09 +0000443bool CallInst::paramHasNoAliasAttr(unsigned i) const {
444 if (AttributeList.getParamAttributes(i).hasNoAliasAttr())
445 return true;
446 if (const Function *F = getCalledFunction())
447 return F->getParamAttributes(i).hasNoAliasAttr();
448 return false;
449}
450
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000451bool CallInst::paramHasNoCaptureAttr(unsigned i) const {
452 if (AttributeList.getParamAttributes(i).hasNoCaptureAttr())
453 return true;
454 if (const Function *F = getCalledFunction())
455 return F->getParamAttributes(i).hasNoCaptureAttr();
456 return false;
457}
458
Devang Patelba3fa6c2008-09-23 23:03:40 +0000459bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000460 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000461 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000462 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000463 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000464 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000465}
466
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467/// IsConstantOne - Return true only if val is constant int 1
468static bool IsConstantOne(Value *val) {
469 assert(val && "IsConstantOne does not work with NULL val");
470 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
471}
472
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000473static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000474 BasicBlock *InsertAtEnd, Type *IntPtrTy,
475 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000476 Value *ArraySize, Function *MallocF,
477 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000478 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000479 "createMalloc needs either InsertBefore or InsertAtEnd");
480
481 // malloc(type) becomes:
482 // bitcast (i8* malloc(typeSize)) to type*
483 // malloc(type, arraySize) becomes:
484 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000485 if (!ArraySize)
486 ArraySize = ConstantInt::get(IntPtrTy, 1);
487 else if (ArraySize->getType() != IntPtrTy) {
488 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000489 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
490 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000491 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000492 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
493 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000494 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000496 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497 if (IsConstantOne(AllocSize)) {
498 AllocSize = ArraySize; // Operand * 1 = Operand
499 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
500 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
501 false /*ZExt*/);
502 // Malloc arg is constant product of type size and array size
503 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
504 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000505 // Multiply type size by the array size...
506 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000507 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
508 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000510 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
511 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000512 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000513 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000514
Victor Hernandez788eaab2009-09-18 19:20:02 +0000515 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000516 // Create the call to Malloc.
517 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
518 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000519 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000520 Value *MallocFunc = MallocF;
521 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000522 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000523 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000524 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000525 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000526 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000527 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000528 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000529 Result = MCall;
530 if (Result->getType() != AllocPtrType)
531 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000532 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000533 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000534 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000535 Result = MCall;
536 if (Result->getType() != AllocPtrType) {
537 InsertAtEnd->getInstList().push_back(MCall);
538 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000539 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000540 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000541 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000542 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000543 if (Function *F = dyn_cast<Function>(MallocFunc)) {
544 MCall->setCallingConv(F->getCallingConv());
545 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
546 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000547 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000548
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000549 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000550}
551
552/// CreateMalloc - Generate the IR for a call to malloc:
553/// 1. Compute the malloc call's argument as the specified type's size,
554/// possibly multiplied by the array size if the array size is not
555/// constant 1.
556/// 2. Call malloc with that argument.
557/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000558Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000559 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000560 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000561 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000562 const Twine &Name) {
563 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000564 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000565}
566
567/// CreateMalloc - Generate the IR for a call to malloc:
568/// 1. Compute the malloc call's argument as the specified type's size,
569/// possibly multiplied by the array size if the array size is not
570/// constant 1.
571/// 2. Call malloc with that argument.
572/// 3. Bitcast the result of the malloc call to the specified type.
573/// Note: This function does not add the bitcast to the basic block, that is the
574/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000575Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000576 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000577 Value *AllocSize, Value *ArraySize,
578 Function *MallocF, const Twine &Name) {
579 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000580 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000581}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000582
Victor Hernandeze2971492009-10-24 04:23:03 +0000583static Instruction* createFree(Value* Source, Instruction *InsertBefore,
584 BasicBlock *InsertAtEnd) {
585 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
586 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000587 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000588 "Can not free something of nonpointer type!");
589
590 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
591 Module* M = BB->getParent()->getParent();
592
Chris Lattner229907c2011-07-18 04:54:35 +0000593 Type *VoidTy = Type::getVoidTy(M->getContext());
594 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000595 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000596 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000597 CallInst* Result = NULL;
598 Value *PtrCast = Source;
599 if (InsertBefore) {
600 if (Source->getType() != IntPtrTy)
601 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
602 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
603 } else {
604 if (Source->getType() != IntPtrTy)
605 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
606 Result = CallInst::Create(FreeFunc, PtrCast, "");
607 }
608 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000609 if (Function *F = dyn_cast<Function>(FreeFunc))
610 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000611
612 return Result;
613}
614
615/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000616Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
617 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000618}
619
620/// CreateFree - Generate the IR for a call to the builtin free function.
621/// Note: This function does not add the call to the basic block, that is the
622/// responsibility of the caller.
623Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
624 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
625 assert(FreeCall && "CreateFree did not create a CallInst");
626 return FreeCall;
627}
628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629//===----------------------------------------------------------------------===//
630// InvokeInst Implementation
631//===----------------------------------------------------------------------===//
632
633void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000634 ArrayRef<Value *> Args, const Twine &NameStr) {
635 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000636 Op<-3>() = Fn;
637 Op<-2>() = IfNormal;
638 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000639
640#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000641 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000642 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000643
Jay Foad5bd375a2011-07-15 08:37:34 +0000644 assert(((Args.size() == FTy->getNumParams()) ||
645 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000646 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000647
Jay Foad5bd375a2011-07-15 08:37:34 +0000648 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000649 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000650 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000651 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000652#endif
653
654 std::copy(Args.begin(), Args.end(), op_begin());
655 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656}
657
Misha Brukmanb1c93172005-04-21 23:48:37 +0000658InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000660 OperandTraits<InvokeInst>::op_end(this)
661 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000662 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000663 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000664 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000665 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000666 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667}
668
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
670 return getSuccessor(idx);
671}
672unsigned InvokeInst::getNumSuccessorsV() const {
673 return getNumSuccessors();
674}
675void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
676 return setSuccessor(idx, B);
677}
678
Bill Wendling375eb1f2012-10-09 00:28:54 +0000679bool InvokeInst::fnHasNoAliasAttr() const {
680 if (AttributeList.getParamAttributes(~0U).hasNoAliasAttr())
681 return true;
682 if (const Function *F = getCalledFunction())
683 return F->getParamAttributes(~0U).hasNoAliasAttr();
684 return false;
685}
686bool InvokeInst::fnHasNoInlineAttr() const {
687 if (AttributeList.getParamAttributes(~0U).hasNoInlineAttr())
688 return true;
689 if (const Function *F = getCalledFunction())
690 return F->getParamAttributes(~0U).hasNoInlineAttr();
691 return false;
692}
693bool InvokeInst::fnHasNoReturnAttr() const {
694 if (AttributeList.getParamAttributes(~0U).hasNoReturnAttr())
695 return true;
696 if (const Function *F = getCalledFunction())
697 return F->getParamAttributes(~0U).hasNoReturnAttr();
698 return false;
699}
700bool InvokeInst::fnHasNoUnwindAttr() const {
701 if (AttributeList.getParamAttributes(~0U).hasNoUnwindAttr())
702 return true;
703 if (const Function *F = getCalledFunction())
704 return F->getParamAttributes(~0U).hasNoUnwindAttr();
705 return false;
706}
707bool InvokeInst::fnHasReadNoneAttr() const {
708 if (AttributeList.getParamAttributes(~0U).hasReadNoneAttr())
709 return true;
710 if (const Function *F = getCalledFunction())
711 return F->getParamAttributes(~0U).hasReadNoneAttr();
712 return false;
713}
714bool InvokeInst::fnHasReadOnlyAttr() const {
715 if (AttributeList.getParamAttributes(~0U).hasReadOnlyAttr())
716 return true;
717 if (const Function *F = getCalledFunction())
718 return F->getParamAttributes(~0U).hasReadOnlyAttr();
719 return false;
720}
721bool InvokeInst::fnHasReturnsTwiceAttr() const {
722 if (AttributeList.getParamAttributes(~0U).hasReturnsTwiceAttr())
723 return true;
724 if (const Function *F = getCalledFunction())
725 return F->getParamAttributes(~0U).hasReturnsTwiceAttr();
726 return false;
727}
728
Bill Wendling8baa61d2012-10-03 17:54:26 +0000729bool InvokeInst::paramHasSExtAttr(unsigned i) const {
730 if (AttributeList.getParamAttributes(i).hasSExtAttr())
731 return true;
732 if (const Function *F = getCalledFunction())
733 return F->getParamAttributes(i).hasSExtAttr();
734 return false;
735}
736
737bool InvokeInst::paramHasZExtAttr(unsigned i) const {
738 if (AttributeList.getParamAttributes(i).hasZExtAttr())
739 return true;
740 if (const Function *F = getCalledFunction())
741 return F->getParamAttributes(i).hasZExtAttr();
742 return false;
743}
744
745bool InvokeInst::paramHasInRegAttr(unsigned i) const {
746 if (AttributeList.getParamAttributes(i).hasInRegAttr())
747 return true;
748 if (const Function *F = getCalledFunction())
749 return F->getParamAttributes(i).hasInRegAttr();
750 return false;
751}
752
753bool InvokeInst::paramHasStructRetAttr(unsigned i) const {
754 if (AttributeList.getParamAttributes(i).hasStructRetAttr())
755 return true;
756 if (const Function *F = getCalledFunction())
757 return F->getParamAttributes(i).hasStructRetAttr();
758 return false;
759}
760
761bool InvokeInst::paramHasNestAttr(unsigned i) const {
762 if (AttributeList.getParamAttributes(i).hasNestAttr())
763 return true;
764 if (const Function *F = getCalledFunction())
765 return F->getParamAttributes(i).hasNestAttr();
766 return false;
767}
768
769bool InvokeInst::paramHasByValAttr(unsigned i) const {
770 if (AttributeList.getParamAttributes(i).hasByValAttr())
771 return true;
772 if (const Function *F = getCalledFunction())
773 return F->getParamAttributes(i).hasByValAttr();
774 return false;
775}
776
Bill Wendlingd7773982012-10-04 06:52:09 +0000777bool InvokeInst::paramHasNoAliasAttr(unsigned i) const {
778 if (AttributeList.getParamAttributes(i).hasNoAliasAttr())
779 return true;
780 if (const Function *F = getCalledFunction())
781 return F->getParamAttributes(i).hasNoAliasAttr();
782 return false;
783}
784
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000785bool InvokeInst::paramHasNoCaptureAttr(unsigned i) const {
786 if (AttributeList.getParamAttributes(i).hasNoCaptureAttr())
787 return true;
788 if (const Function *F = getCalledFunction())
789 return F->getParamAttributes(i).hasNoCaptureAttr();
790 return false;
791}
792
Devang Patelba3fa6c2008-09-23 23:03:40 +0000793bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000794 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000795 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000796 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000797 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000798 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000799}
800
Devang Patel4c758ea2008-09-25 21:00:45 +0000801void InvokeInst::addAttribute(unsigned i, Attributes attr) {
802 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000803 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000804 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000805}
806
Devang Patel4c758ea2008-09-25 21:00:45 +0000807void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
808 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000809 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000810 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000811}
812
Bill Wendlingfae14752011-08-12 20:24:12 +0000813LandingPadInst *InvokeInst::getLandingPadInst() const {
814 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
815}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000816
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817//===----------------------------------------------------------------------===//
818// ReturnInst Implementation
819//===----------------------------------------------------------------------===//
820
Chris Lattner2195fc42007-02-24 00:55:48 +0000821ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000822 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000823 OperandTraits<ReturnInst>::op_end(this) -
824 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000825 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000826 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000827 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000828 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000829}
830
Owen Anderson55f1c092009-08-13 21:58:54 +0000831ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
832 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000833 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
834 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000835 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000836 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000837}
Owen Anderson55f1c092009-08-13 21:58:54 +0000838ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
839 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000840 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
841 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000842 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000843 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000844}
Owen Anderson55f1c092009-08-13 21:58:54 +0000845ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
846 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000847 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000848}
849
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000850unsigned ReturnInst::getNumSuccessorsV() const {
851 return getNumSuccessors();
852}
853
Devang Patelae682fb2008-02-26 17:56:20 +0000854/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
855/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000856void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000857 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858}
859
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000860BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000861 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000862}
863
Devang Patel59643e52008-02-23 00:35:18 +0000864ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000865}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000866
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000867//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000868// ResumeInst Implementation
869//===----------------------------------------------------------------------===//
870
871ResumeInst::ResumeInst(const ResumeInst &RI)
872 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
873 OperandTraits<ResumeInst>::op_begin(this), 1) {
874 Op<0>() = RI.Op<0>();
875}
876
877ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
878 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
879 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
880 Op<0>() = Exn;
881}
882
883ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
884 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
885 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
886 Op<0>() = Exn;
887}
888
889unsigned ResumeInst::getNumSuccessorsV() const {
890 return getNumSuccessors();
891}
892
893void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
894 llvm_unreachable("ResumeInst has no successors!");
895}
896
897BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
898 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000899}
900
901//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000902// UnreachableInst Implementation
903//===----------------------------------------------------------------------===//
904
Owen Anderson55f1c092009-08-13 21:58:54 +0000905UnreachableInst::UnreachableInst(LLVMContext &Context,
906 Instruction *InsertBefore)
907 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
908 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000909}
Owen Anderson55f1c092009-08-13 21:58:54 +0000910UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
911 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
912 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000913}
914
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915unsigned UnreachableInst::getNumSuccessorsV() const {
916 return getNumSuccessors();
917}
918
919void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000920 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921}
922
923BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000924 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000925}
926
927//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928// BranchInst Implementation
929//===----------------------------------------------------------------------===//
930
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931void BranchInst::AssertOK() {
932 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000933 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935}
936
Chris Lattner2195fc42007-02-24 00:55:48 +0000937BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000938 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000939 OperandTraits<BranchInst>::op_end(this) - 1,
940 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000941 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000942 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000943}
944BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
945 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000946 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000947 OperandTraits<BranchInst>::op_end(this) - 3,
948 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000949 Op<-1>() = IfTrue;
950 Op<-2>() = IfFalse;
951 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000952#ifndef NDEBUG
953 AssertOK();
954#endif
955}
956
957BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000958 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000959 OperandTraits<BranchInst>::op_end(this) - 1,
960 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000961 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000962 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000963}
964
965BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
966 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000967 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000968 OperandTraits<BranchInst>::op_end(this) - 3,
969 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000970 Op<-1>() = IfTrue;
971 Op<-2>() = IfFalse;
972 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000973#ifndef NDEBUG
974 AssertOK();
975#endif
976}
977
978
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000980 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000981 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
982 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000983 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984 if (BI.getNumOperands() != 1) {
985 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000986 Op<-3>() = BI.Op<-3>();
987 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000989 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000990}
991
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000992void BranchInst::swapSuccessors() {
993 assert(isConditional() &&
994 "Cannot swap successors of an unconditional branch");
995 Op<-1>().swap(Op<-2>());
996
997 // Update profile metadata if present and it matches our structural
998 // expectations.
999 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
1000 if (!ProfileData || ProfileData->getNumOperands() != 3)
1001 return;
1002
1003 // The first operand is the name. Fetch them backwards and build a new one.
1004 Value *Ops[] = {
1005 ProfileData->getOperand(0),
1006 ProfileData->getOperand(2),
1007 ProfileData->getOperand(1)
1008 };
1009 setMetadata(LLVMContext::MD_prof,
1010 MDNode::get(ProfileData->getContext(), Ops));
1011}
1012
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1014 return getSuccessor(idx);
1015}
1016unsigned BranchInst::getNumSuccessorsV() const {
1017 return getNumSuccessors();
1018}
1019void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1020 setSuccessor(idx, B);
1021}
1022
1023
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001025// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026//===----------------------------------------------------------------------===//
1027
Owen Andersonb6b25302009-07-14 23:09:55 +00001028static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001030 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001031 else {
1032 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001033 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001034 assert(Amt->getType()->isIntegerTy() &&
1035 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001036 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001037 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001038}
1039
Chris Lattner229907c2011-07-18 04:54:35 +00001040AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001041 const Twine &Name, Instruction *InsertBefore)
1042 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1043 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
1044 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001045 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001046 setName(Name);
1047}
1048
Chris Lattner229907c2011-07-18 04:54:35 +00001049AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001050 const Twine &Name, BasicBlock *InsertAtEnd)
1051 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1052 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
1053 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001054 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001055 setName(Name);
1056}
1057
Chris Lattner229907c2011-07-18 04:54:35 +00001058AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001059 Instruction *InsertBefore)
1060 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1061 getAISize(Ty->getContext(), 0), InsertBefore) {
1062 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001063 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001064 setName(Name);
1065}
1066
Chris Lattner229907c2011-07-18 04:54:35 +00001067AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001068 BasicBlock *InsertAtEnd)
1069 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1070 getAISize(Ty->getContext(), 0), InsertAtEnd) {
1071 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001072 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +00001073 setName(Name);
1074}
1075
Chris Lattner229907c2011-07-18 04:54:35 +00001076AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001077 const Twine &Name, Instruction *InsertBefore)
1078 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +00001079 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001080 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001081 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001082 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083}
1084
Chris Lattner229907c2011-07-18 04:54:35 +00001085AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001086 const Twine &Name, BasicBlock *InsertAtEnd)
1087 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +00001088 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001089 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001090 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001091 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001092}
1093
Gordon Henriksen14a55692007-12-10 02:14:30 +00001094// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001095AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001096}
1097
Victor Hernandez8acf2952009-10-23 21:09:37 +00001098void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001099 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001100 assert(Align <= MaximumAlignment &&
1101 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001102 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +00001103 assert(getAlignment() == Align && "Alignment representation error!");
1104}
1105
Victor Hernandez8acf2952009-10-23 21:09:37 +00001106bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001107 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001108 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110}
1111
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001112Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001113 return getType()->getElementType();
1114}
1115
Chris Lattner8b291e62008-11-26 02:54:17 +00001116/// isStaticAlloca - Return true if this alloca is in the entry block of the
1117/// function and is a constant size. If so, the code generator will fold it
1118/// into the prolog/epilog code, so it is basically free.
1119bool AllocaInst::isStaticAlloca() const {
1120 // Must be constant size.
1121 if (!isa<ConstantInt>(getArraySize())) return false;
1122
1123 // Must be in the entry block.
1124 const BasicBlock *Parent = getParent();
1125 return Parent == &Parent->getParent()->front();
1126}
1127
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001128//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129// LoadInst Implementation
1130//===----------------------------------------------------------------------===//
1131
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001132void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001133 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001134 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001135 assert(!(isAtomic() && getAlignment() == 0) &&
1136 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001137}
1138
Daniel Dunbar4975db62009-07-25 04:41:11 +00001139LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001140 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001141 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001142 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001143 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001144 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001145 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001146 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001147}
1148
Daniel Dunbar4975db62009-07-25 04:41:11 +00001149LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001150 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001151 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001152 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001153 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001154 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001155 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001156 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001157}
1158
Daniel Dunbar4975db62009-07-25 04:41:11 +00001159LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001160 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001161 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001163 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001164 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001165 setAtomic(NotAtomic);
1166 AssertOK();
1167 setName(Name);
1168}
1169
1170LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1171 BasicBlock *InsertAE)
1172 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1173 Load, Ptr, InsertAE) {
1174 setVolatile(isVolatile);
1175 setAlignment(0);
1176 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001177 AssertOK();
1178 setName(Name);
1179}
1180
Daniel Dunbar4975db62009-07-25 04:41:11 +00001181LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001182 unsigned Align, Instruction *InsertBef)
1183 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1184 Load, Ptr, InsertBef) {
1185 setVolatile(isVolatile);
1186 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001187 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001188 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001189 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190}
1191
Daniel Dunbar4975db62009-07-25 04:41:11 +00001192LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001193 unsigned Align, BasicBlock *InsertAE)
1194 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1195 Load, Ptr, InsertAE) {
1196 setVolatile(isVolatile);
1197 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001198 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +00001199 AssertOK();
1200 setName(Name);
1201}
1202
Eli Friedman59b66882011-08-09 23:02:53 +00001203LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1204 unsigned Align, AtomicOrdering Order,
1205 SynchronizationScope SynchScope,
1206 Instruction *InsertBef)
1207 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1208 Load, Ptr, InsertBef) {
1209 setVolatile(isVolatile);
1210 setAlignment(Align);
1211 setAtomic(Order, SynchScope);
1212 AssertOK();
1213 setName(Name);
1214}
1215
1216LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1217 unsigned Align, AtomicOrdering Order,
1218 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001219 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001220 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001221 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001222 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001223 setAlignment(Align);
1224 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001225 AssertOK();
1226 setName(Name);
1227}
1228
Daniel Dunbar27096822009-08-11 18:11:15 +00001229LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1230 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1231 Load, Ptr, InsertBef) {
1232 setVolatile(false);
1233 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001234 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001235 AssertOK();
1236 if (Name && Name[0]) setName(Name);
1237}
1238
1239LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1240 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1241 Load, Ptr, InsertAE) {
1242 setVolatile(false);
1243 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001244 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001245 AssertOK();
1246 if (Name && Name[0]) setName(Name);
1247}
1248
1249LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1250 Instruction *InsertBef)
1251: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1252 Load, Ptr, InsertBef) {
1253 setVolatile(isVolatile);
1254 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001255 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001256 AssertOK();
1257 if (Name && Name[0]) setName(Name);
1258}
1259
1260LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1261 BasicBlock *InsertAE)
1262 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1263 Load, Ptr, InsertAE) {
1264 setVolatile(isVolatile);
1265 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001266 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001267 AssertOK();
1268 if (Name && Name[0]) setName(Name);
1269}
1270
Christopher Lamb84485702007-04-22 19:24:39 +00001271void LoadInst::setAlignment(unsigned Align) {
1272 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001273 assert(Align <= MaximumAlignment &&
1274 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001275 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001276 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001277 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001278}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279
1280//===----------------------------------------------------------------------===//
1281// StoreInst Implementation
1282//===----------------------------------------------------------------------===//
1283
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001284void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001285 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001286 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001287 "Ptr must have pointer type!");
1288 assert(getOperand(0)->getType() ==
1289 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001290 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001291 assert(!(isAtomic() && getAlignment() == 0) &&
1292 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001293}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001295
1296StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001297 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001298 OperandTraits<StoreInst>::op_begin(this),
1299 OperandTraits<StoreInst>::operands(this),
1300 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001301 Op<0>() = val;
1302 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001303 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001304 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001305 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001306 AssertOK();
1307}
1308
1309StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001310 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001311 OperandTraits<StoreInst>::op_begin(this),
1312 OperandTraits<StoreInst>::operands(this),
1313 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001314 Op<0>() = val;
1315 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001316 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001317 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001318 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001319 AssertOK();
1320}
1321
Misha Brukmanb1c93172005-04-21 23:48:37 +00001322StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001323 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001324 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001325 OperandTraits<StoreInst>::op_begin(this),
1326 OperandTraits<StoreInst>::operands(this),
1327 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001328 Op<0>() = val;
1329 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001330 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001331 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001332 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001333 AssertOK();
1334}
1335
1336StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1337 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001338 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001339 OperandTraits<StoreInst>::op_begin(this),
1340 OperandTraits<StoreInst>::operands(this),
1341 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001342 Op<0>() = val;
1343 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001344 setVolatile(isVolatile);
1345 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001346 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001347 AssertOK();
1348}
1349
Misha Brukmanb1c93172005-04-21 23:48:37 +00001350StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001351 unsigned Align, AtomicOrdering Order,
1352 SynchronizationScope SynchScope,
1353 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001354 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001355 OperandTraits<StoreInst>::op_begin(this),
1356 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001357 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001358 Op<0>() = val;
1359 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001360 setVolatile(isVolatile);
1361 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001362 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001363 AssertOK();
1364}
1365
1366StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001367 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001368 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001369 OperandTraits<StoreInst>::op_begin(this),
1370 OperandTraits<StoreInst>::operands(this),
1371 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001372 Op<0>() = val;
1373 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001374 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001375 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001376 setAtomic(NotAtomic);
1377 AssertOK();
1378}
1379
1380StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1381 unsigned Align, BasicBlock *InsertAtEnd)
1382 : Instruction(Type::getVoidTy(val->getContext()), Store,
1383 OperandTraits<StoreInst>::op_begin(this),
1384 OperandTraits<StoreInst>::operands(this),
1385 InsertAtEnd) {
1386 Op<0>() = val;
1387 Op<1>() = addr;
1388 setVolatile(isVolatile);
1389 setAlignment(Align);
1390 setAtomic(NotAtomic);
1391 AssertOK();
1392}
1393
1394StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1395 unsigned Align, AtomicOrdering Order,
1396 SynchronizationScope SynchScope,
1397 BasicBlock *InsertAtEnd)
1398 : Instruction(Type::getVoidTy(val->getContext()), Store,
1399 OperandTraits<StoreInst>::op_begin(this),
1400 OperandTraits<StoreInst>::operands(this),
1401 InsertAtEnd) {
1402 Op<0>() = val;
1403 Op<1>() = addr;
1404 setVolatile(isVolatile);
1405 setAlignment(Align);
1406 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001407 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001408}
1409
Christopher Lamb84485702007-04-22 19:24:39 +00001410void StoreInst::setAlignment(unsigned Align) {
1411 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001412 assert(Align <= MaximumAlignment &&
1413 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001414 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001415 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001416 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001417}
1418
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001419//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001420// AtomicCmpXchgInst Implementation
1421//===----------------------------------------------------------------------===//
1422
1423void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1424 AtomicOrdering Ordering,
1425 SynchronizationScope SynchScope) {
1426 Op<0>() = Ptr;
1427 Op<1>() = Cmp;
1428 Op<2>() = NewVal;
1429 setOrdering(Ordering);
1430 setSynchScope(SynchScope);
1431
1432 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1433 "All operands must be non-null!");
1434 assert(getOperand(0)->getType()->isPointerTy() &&
1435 "Ptr must have pointer type!");
1436 assert(getOperand(1)->getType() ==
1437 cast<PointerType>(getOperand(0)->getType())->getElementType()
1438 && "Ptr must be a pointer to Cmp type!");
1439 assert(getOperand(2)->getType() ==
1440 cast<PointerType>(getOperand(0)->getType())->getElementType()
1441 && "Ptr must be a pointer to NewVal type!");
1442 assert(Ordering != NotAtomic &&
1443 "AtomicCmpXchg instructions must be atomic!");
1444}
1445
1446AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1447 AtomicOrdering Ordering,
1448 SynchronizationScope SynchScope,
1449 Instruction *InsertBefore)
1450 : Instruction(Cmp->getType(), AtomicCmpXchg,
1451 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1452 OperandTraits<AtomicCmpXchgInst>::operands(this),
1453 InsertBefore) {
1454 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1455}
1456
1457AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1458 AtomicOrdering Ordering,
1459 SynchronizationScope SynchScope,
1460 BasicBlock *InsertAtEnd)
1461 : Instruction(Cmp->getType(), AtomicCmpXchg,
1462 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1463 OperandTraits<AtomicCmpXchgInst>::operands(this),
1464 InsertAtEnd) {
1465 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1466}
1467
1468//===----------------------------------------------------------------------===//
1469// AtomicRMWInst Implementation
1470//===----------------------------------------------------------------------===//
1471
1472void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1473 AtomicOrdering Ordering,
1474 SynchronizationScope SynchScope) {
1475 Op<0>() = Ptr;
1476 Op<1>() = Val;
1477 setOperation(Operation);
1478 setOrdering(Ordering);
1479 setSynchScope(SynchScope);
1480
1481 assert(getOperand(0) && getOperand(1) &&
1482 "All operands must be non-null!");
1483 assert(getOperand(0)->getType()->isPointerTy() &&
1484 "Ptr must have pointer type!");
1485 assert(getOperand(1)->getType() ==
1486 cast<PointerType>(getOperand(0)->getType())->getElementType()
1487 && "Ptr must be a pointer to Val type!");
1488 assert(Ordering != NotAtomic &&
1489 "AtomicRMW instructions must be atomic!");
1490}
1491
1492AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1493 AtomicOrdering Ordering,
1494 SynchronizationScope SynchScope,
1495 Instruction *InsertBefore)
1496 : Instruction(Val->getType(), AtomicRMW,
1497 OperandTraits<AtomicRMWInst>::op_begin(this),
1498 OperandTraits<AtomicRMWInst>::operands(this),
1499 InsertBefore) {
1500 Init(Operation, Ptr, Val, Ordering, SynchScope);
1501}
1502
1503AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1504 AtomicOrdering Ordering,
1505 SynchronizationScope SynchScope,
1506 BasicBlock *InsertAtEnd)
1507 : Instruction(Val->getType(), AtomicRMW,
1508 OperandTraits<AtomicRMWInst>::op_begin(this),
1509 OperandTraits<AtomicRMWInst>::operands(this),
1510 InsertAtEnd) {
1511 Init(Operation, Ptr, Val, Ordering, SynchScope);
1512}
1513
1514//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001515// FenceInst Implementation
1516//===----------------------------------------------------------------------===//
1517
1518FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1519 SynchronizationScope SynchScope,
1520 Instruction *InsertBefore)
1521 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1522 setOrdering(Ordering);
1523 setSynchScope(SynchScope);
1524}
1525
1526FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1527 SynchronizationScope SynchScope,
1528 BasicBlock *InsertAtEnd)
1529 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1530 setOrdering(Ordering);
1531 setSynchScope(SynchScope);
1532}
1533
1534//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001535// GetElementPtrInst Implementation
1536//===----------------------------------------------------------------------===//
1537
Jay Foadd1b78492011-07-25 09:48:08 +00001538void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001539 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001540 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1541 OperandList[0] = Ptr;
1542 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001543 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001544}
1545
Gabor Greiff6caff662008-05-10 08:32:32 +00001546GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001547 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001548 OperandTraits<GetElementPtrInst>::op_end(this)
1549 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001550 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001551 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001552 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001553}
1554
Chris Lattner6090a422009-03-09 04:46:40 +00001555/// getIndexedType - Returns the type of the element that would be accessed with
1556/// a gep instruction with the specified parameters.
1557///
1558/// The Idxs pointer should point to a continuous piece of memory containing the
1559/// indices, either as Value* or uint64_t.
1560///
1561/// A null type is returned if the indices are invalid for the specified
1562/// pointer type.
1563///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001564template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001565static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001566 if (Ptr->isVectorTy()) {
1567 assert(IdxList.size() == 1 &&
1568 "GEP with vector pointers must have a single index");
1569 PointerType *PTy = dyn_cast<PointerType>(
1570 cast<VectorType>(Ptr)->getElementType());
1571 assert(PTy && "Gep with invalid vector pointer found");
1572 return PTy->getElementType();
1573 }
1574
Chris Lattner229907c2011-07-18 04:54:35 +00001575 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001576 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001577 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001578
Chris Lattner6090a422009-03-09 04:46:40 +00001579 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001580 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001581 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001582
Chris Lattner6090a422009-03-09 04:46:40 +00001583 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001584 // it cannot be 'stepped over'.
1585 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001586 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001587
Dan Gohman1ecaf452008-05-31 00:58:22 +00001588 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001589 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001590 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001591 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001592 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001593 if (!CT->indexValid(Index)) return 0;
1594 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001595 }
Jay Foadd1b78492011-07-25 09:48:08 +00001596 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Jay Foadd1b78492011-07-25 09:48:08 +00001599Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1600 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001601}
1602
Chris Lattner229907c2011-07-18 04:54:35 +00001603Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001604 ArrayRef<Constant *> IdxList) {
1605 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001606}
1607
Jay Foadd1b78492011-07-25 09:48:08 +00001608Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1609 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001610}
1611
Nadav Rotem3924cb02011-12-05 06:29:09 +00001612unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1613 Type *Ty = Ptr->getType();
1614
1615 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1616 Ty = VTy->getElementType();
1617
1618 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1619 return PTy->getAddressSpace();
1620
Craig Topperc514b542012-02-05 22:14:15 +00001621 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001622}
1623
Chris Lattner45f15572007-04-14 00:12:57 +00001624/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1625/// zeros. If so, the result pointer and the first operand have the same
1626/// value, just potentially different types.
1627bool GetElementPtrInst::hasAllZeroIndices() const {
1628 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1629 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1630 if (!CI->isZero()) return false;
1631 } else {
1632 return false;
1633 }
1634 }
1635 return true;
1636}
1637
Chris Lattner27058292007-04-27 20:35:56 +00001638/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1639/// constant integers. If so, the result pointer and the first operand have
1640/// a constant offset between them.
1641bool GetElementPtrInst::hasAllConstantIndices() const {
1642 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1643 if (!isa<ConstantInt>(getOperand(i)))
1644 return false;
1645 }
1646 return true;
1647}
1648
Dan Gohman1b849082009-09-07 23:54:19 +00001649void GetElementPtrInst::setIsInBounds(bool B) {
1650 cast<GEPOperator>(this)->setIsInBounds(B);
1651}
Chris Lattner45f15572007-04-14 00:12:57 +00001652
Nick Lewycky28a5f252009-09-27 21:33:04 +00001653bool GetElementPtrInst::isInBounds() const {
1654 return cast<GEPOperator>(this)->isInBounds();
1655}
1656
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001658// ExtractElementInst Implementation
1659//===----------------------------------------------------------------------===//
1660
1661ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001662 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001663 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001664 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001665 ExtractElement,
1666 OperandTraits<ExtractElementInst>::op_begin(this),
1667 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001668 assert(isValidOperands(Val, Index) &&
1669 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001670 Op<0>() = Val;
1671 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001672 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001673}
1674
1675ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001676 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001677 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001678 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001679 ExtractElement,
1680 OperandTraits<ExtractElementInst>::op_begin(this),
1681 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001682 assert(isValidOperands(Val, Index) &&
1683 "Invalid extractelement instruction operands!");
1684
Gabor Greif2d3024d2008-05-26 21:33:52 +00001685 Op<0>() = Val;
1686 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001687 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001688}
1689
Chris Lattner65511ff2006-10-05 06:24:58 +00001690
Chris Lattner54865b32006-04-08 04:05:48 +00001691bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001692 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001693 return false;
1694 return true;
1695}
1696
1697
Robert Bocchino23004482006-01-10 19:05:34 +00001698//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001699// InsertElementInst Implementation
1700//===----------------------------------------------------------------------===//
1701
Chris Lattner54865b32006-04-08 04:05:48 +00001702InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001703 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001704 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001705 : Instruction(Vec->getType(), InsertElement,
1706 OperandTraits<InsertElementInst>::op_begin(this),
1707 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001708 assert(isValidOperands(Vec, Elt, Index) &&
1709 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001710 Op<0>() = Vec;
1711 Op<1>() = Elt;
1712 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001713 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001714}
1715
Chris Lattner54865b32006-04-08 04:05:48 +00001716InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001717 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001718 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001719 : Instruction(Vec->getType(), InsertElement,
1720 OperandTraits<InsertElementInst>::op_begin(this),
1721 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001722 assert(isValidOperands(Vec, Elt, Index) &&
1723 "Invalid insertelement instruction operands!");
1724
Gabor Greif2d3024d2008-05-26 21:33:52 +00001725 Op<0>() = Vec;
1726 Op<1>() = Elt;
1727 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001728 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001729}
1730
Chris Lattner54865b32006-04-08 04:05:48 +00001731bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1732 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001733 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001734 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001735
Reid Spencerd84d35b2007-02-15 02:26:10 +00001736 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001737 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001738
Duncan Sands9dff9be2010-02-15 16:12:20 +00001739 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001740 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001741 return true;
1742}
1743
1744
Robert Bocchinoca27f032006-01-17 20:07:22 +00001745//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001746// ShuffleVectorInst Implementation
1747//===----------------------------------------------------------------------===//
1748
1749ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001750 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001751 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001752: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001753 cast<VectorType>(Mask->getType())->getNumElements()),
1754 ShuffleVector,
1755 OperandTraits<ShuffleVectorInst>::op_begin(this),
1756 OperandTraits<ShuffleVectorInst>::operands(this),
1757 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001758 assert(isValidOperands(V1, V2, Mask) &&
1759 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001760 Op<0>() = V1;
1761 Op<1>() = V2;
1762 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001763 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001764}
1765
1766ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001767 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001768 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001769: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1770 cast<VectorType>(Mask->getType())->getNumElements()),
1771 ShuffleVector,
1772 OperandTraits<ShuffleVectorInst>::op_begin(this),
1773 OperandTraits<ShuffleVectorInst>::operands(this),
1774 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001775 assert(isValidOperands(V1, V2, Mask) &&
1776 "Invalid shuffle vector instruction operands!");
1777
Gabor Greif2d3024d2008-05-26 21:33:52 +00001778 Op<0>() = V1;
1779 Op<1>() = V2;
1780 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001781 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001782}
1783
Mon P Wang25f01062008-11-10 04:46:22 +00001784bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001785 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001786 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001787 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001788 return false;
1789
Chris Lattner1dcb6542012-01-25 23:49:49 +00001790 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001791 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001792 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001793 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001794
1795 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001796 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1797 return true;
1798
Nate Begeman60a31c32010-08-13 00:16:46 +00001799 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001800 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001801 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001802 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1803 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001804 return false;
1805 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1806 return false;
1807 }
1808 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001809 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001810 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001811
1812 if (const ConstantDataSequential *CDS =
1813 dyn_cast<ConstantDataSequential>(Mask)) {
1814 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1815 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1816 if (CDS->getElementAsInteger(i) >= V1Size*2)
1817 return false;
1818 return true;
1819 }
1820
1821 // The bitcode reader can create a place holder for a forward reference
1822 // used as the shuffle mask. When this occurs, the shuffle mask will
1823 // fall into this case and fail. To avoid this error, do this bit of
1824 // ugliness to allow such a mask pass.
1825 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1826 if (CE->getOpcode() == Instruction::UserOp1)
1827 return true;
1828
1829 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001830}
1831
Chris Lattnerf724e342008-03-02 05:28:33 +00001832/// getMaskValue - Return the index from the shuffle mask for the specified
1833/// output result. This is either -1 if the element is undef or a number less
1834/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001835int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1836 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1837 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001838 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001839 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001840 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001841 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001842 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001843}
1844
Chris Lattner1dcb6542012-01-25 23:49:49 +00001845/// getShuffleMask - Return the full mask for this instruction, where each
1846/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001847void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1848 SmallVectorImpl<int> &Result) {
1849 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001850
Chris Lattnercf129702012-01-26 02:51:13 +00001851 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001852 for (unsigned i = 0; i != NumElts; ++i)
1853 Result.push_back(CDS->getElementAsInteger(i));
1854 return;
1855 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001856 for (unsigned i = 0; i != NumElts; ++i) {
1857 Constant *C = Mask->getAggregateElement(i);
1858 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001859 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001860 }
1861}
1862
1863
Dan Gohman12fce772008-05-15 19:50:34 +00001864//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001865// InsertValueInst Class
1866//===----------------------------------------------------------------------===//
1867
Jay Foad57aa6362011-07-13 10:26:04 +00001868void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1869 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001870 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001871
1872 // There's no fundamental reason why we require at least one index
1873 // (other than weirdness with &*IdxBegin being invalid; see
1874 // getelementptr's init routine for example). But there's no
1875 // present need to support it.
1876 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1877
1878 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001879 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001880 Op<0>() = Agg;
1881 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001882
Jay Foad57aa6362011-07-13 10:26:04 +00001883 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001884 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001885}
1886
1887InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001888 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001889 OperandTraits<InsertValueInst>::op_begin(this), 2),
1890 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001891 Op<0>() = IVI.getOperand(0);
1892 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001893 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001894}
1895
1896//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001897// ExtractValueInst Class
1898//===----------------------------------------------------------------------===//
1899
Jay Foad57aa6362011-07-13 10:26:04 +00001900void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001901 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001902
Jay Foad57aa6362011-07-13 10:26:04 +00001903 // There's no fundamental reason why we require at least one index.
1904 // But there's no present need to support it.
1905 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001906
Jay Foad57aa6362011-07-13 10:26:04 +00001907 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001908 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001909}
1910
1911ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001912 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001913 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001914 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001915}
1916
Dan Gohman12fce772008-05-15 19:50:34 +00001917// getIndexedType - Returns the type of the element that would be extracted
1918// with an extractvalue instruction with the specified parameters.
1919//
1920// A null type is returned if the indices are invalid for the specified
1921// pointer type.
1922//
Chris Lattner229907c2011-07-18 04:54:35 +00001923Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001924 ArrayRef<unsigned> Idxs) {
1925 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001926 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001927 // We can't use CompositeType::indexValid(Index) here.
1928 // indexValid() always returns true for arrays because getelementptr allows
1929 // out-of-bounds indices. Since we don't allow those for extractvalue and
1930 // insertvalue we need to check array indexing manually.
1931 // Since the only other types we can index into are struct types it's just
1932 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001933 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001934 if (Index >= AT->getNumElements())
1935 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001936 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001937 if (Index >= ST->getNumElements())
1938 return 0;
1939 } else {
1940 // Not a valid type to index into.
1941 return 0;
1942 }
1943
1944 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001945 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001946 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001947}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001948
1949//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001950// BinaryOperator Class
1951//===----------------------------------------------------------------------===//
1952
Chris Lattner2195fc42007-02-24 00:55:48 +00001953BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001954 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001955 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001956 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001957 OperandTraits<BinaryOperator>::op_begin(this),
1958 OperandTraits<BinaryOperator>::operands(this),
1959 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001960 Op<0>() = S1;
1961 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001962 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001963 setName(Name);
1964}
1965
1966BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001967 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001968 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001969 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001970 OperandTraits<BinaryOperator>::op_begin(this),
1971 OperandTraits<BinaryOperator>::operands(this),
1972 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001973 Op<0>() = S1;
1974 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001975 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001976 setName(Name);
1977}
1978
1979
1980void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001981 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001982 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001983 assert(LHS->getType() == RHS->getType() &&
1984 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001985#ifndef NDEBUG
1986 switch (iType) {
1987 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001988 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001989 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001990 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001991 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001992 "Tried to create an integer operation on a non-integer type!");
1993 break;
1994 case FAdd: case FSub:
1995 case FMul:
1996 assert(getType() == LHS->getType() &&
1997 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001998 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001999 "Tried to create a floating-point operation on a "
2000 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002001 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002002 case UDiv:
2003 case SDiv:
2004 assert(getType() == LHS->getType() &&
2005 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002006 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002007 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002008 "Incorrect operand type (not integer) for S/UDIV");
2009 break;
2010 case FDiv:
2011 assert(getType() == LHS->getType() &&
2012 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002013 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002014 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002015 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002016 case URem:
2017 case SRem:
2018 assert(getType() == LHS->getType() &&
2019 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002020 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002021 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002022 "Incorrect operand type (not integer) for S/UREM");
2023 break;
2024 case FRem:
2025 assert(getType() == LHS->getType() &&
2026 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002027 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002028 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002029 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002030 case Shl:
2031 case LShr:
2032 case AShr:
2033 assert(getType() == LHS->getType() &&
2034 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002035 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002036 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002037 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002038 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002039 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002040 case And: case Or:
2041 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002042 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002043 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002044 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002045 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002046 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002047 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002048 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002049 default:
2050 break;
2051 }
2052#endif
2053}
2054
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002055BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002056 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002057 Instruction *InsertBefore) {
2058 assert(S1->getType() == S2->getType() &&
2059 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002060 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002061}
2062
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002064 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002065 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002067 InsertAtEnd->getInstList().push_back(Res);
2068 return Res;
2069}
2070
Dan Gohman5476cfd2009-08-12 16:23:25 +00002071BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002072 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002073 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002074 return new BinaryOperator(Instruction::Sub,
2075 zero, Op,
2076 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002077}
2078
Dan Gohman5476cfd2009-08-12 16:23:25 +00002079BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002080 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002081 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002082 return new BinaryOperator(Instruction::Sub,
2083 zero, Op,
2084 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002085}
2086
Dan Gohman4ab44202009-12-18 02:58:50 +00002087BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2088 Instruction *InsertBefore) {
2089 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2090 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2091}
2092
2093BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2094 BasicBlock *InsertAtEnd) {
2095 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2096 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2097}
2098
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002099BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2100 Instruction *InsertBefore) {
2101 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2102 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2103}
2104
2105BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2106 BasicBlock *InsertAtEnd) {
2107 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2108 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2109}
2110
Dan Gohman5476cfd2009-08-12 16:23:25 +00002111BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002112 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002113 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002114 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002115 Op->getType(), Name, InsertBefore);
2116}
2117
Dan Gohman5476cfd2009-08-12 16:23:25 +00002118BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002119 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002120 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002121 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002122 Op->getType(), Name, InsertAtEnd);
2123}
2124
Dan Gohman5476cfd2009-08-12 16:23:25 +00002125BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002127 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002128 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129 Op->getType(), Name, InsertBefore);
2130}
2131
Dan Gohman5476cfd2009-08-12 16:23:25 +00002132BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002133 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002134 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002135 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002136 Op->getType(), Name, InsertAtEnd);
2137}
2138
2139
2140// isConstantAllOnes - Helper function for several functions below
2141static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002142 if (const Constant *C = dyn_cast<Constant>(V))
2143 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002144 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002145}
2146
Owen Andersonbb2501b2009-07-13 22:18:28 +00002147bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002148 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2149 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002150 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2151 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152 return false;
2153}
2154
Owen Andersonbb2501b2009-07-13 22:18:28 +00002155bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002156 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2157 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002158 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00002159 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00002160 return false;
2161}
2162
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002163bool BinaryOperator::isNot(const Value *V) {
2164 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2165 return (Bop->getOpcode() == Instruction::Xor &&
2166 (isConstantAllOnes(Bop->getOperand(1)) ||
2167 isConstantAllOnes(Bop->getOperand(0))));
2168 return false;
2169}
2170
Chris Lattner2c7d1772005-04-24 07:28:37 +00002171Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002172 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173}
2174
Chris Lattner2c7d1772005-04-24 07:28:37 +00002175const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2176 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177}
2178
Dan Gohmana5b96452009-06-04 22:49:04 +00002179Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002180 return cast<BinaryOperator>(BinOp)->getOperand(1);
2181}
2182
2183const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2184 return getFNegArgument(const_cast<Value*>(BinOp));
2185}
2186
Chris Lattner2c7d1772005-04-24 07:28:37 +00002187Value *BinaryOperator::getNotArgument(Value *BinOp) {
2188 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2189 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2190 Value *Op0 = BO->getOperand(0);
2191 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002192 if (isConstantAllOnes(Op0)) return Op1;
2193
2194 assert(isConstantAllOnes(Op1));
2195 return Op0;
2196}
2197
Chris Lattner2c7d1772005-04-24 07:28:37 +00002198const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2199 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002200}
2201
2202
2203// swapOperands - Exchange the two operands to this instruction. This
2204// instruction is safe to use on any binary instruction and does not
2205// modify the semantics of the instruction. If the instruction is
2206// order dependent (SetLT f.e.) the opcode is changed.
2207//
2208bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002209 if (!isCommutative())
2210 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002211 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002212 return false;
2213}
2214
Dan Gohman1b849082009-09-07 23:54:19 +00002215void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2216 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2217}
2218
2219void BinaryOperator::setHasNoSignedWrap(bool b) {
2220 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2221}
2222
2223void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002224 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002225}
2226
Nick Lewycky28a5f252009-09-27 21:33:04 +00002227bool BinaryOperator::hasNoUnsignedWrap() const {
2228 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2229}
2230
2231bool BinaryOperator::hasNoSignedWrap() const {
2232 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2233}
2234
2235bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002236 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002237}
2238
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002239//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002240// FPMathOperator Class
2241//===----------------------------------------------------------------------===//
2242
2243/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2244/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002245/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002246float FPMathOperator::getFPAccuracy() const {
2247 const MDNode *MD =
2248 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2249 if (!MD)
2250 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002251 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2252 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002253}
2254
2255
2256//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002257// CastInst Class
2258//===----------------------------------------------------------------------===//
2259
David Blaikie3a15e142011-12-01 08:00:17 +00002260void CastInst::anchor() {}
2261
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262// Just determine if this cast only deals with integral->integral conversion.
2263bool CastInst::isIntegerCast() const {
2264 switch (getOpcode()) {
2265 default: return false;
2266 case Instruction::ZExt:
2267 case Instruction::SExt:
2268 case Instruction::Trunc:
2269 return true;
2270 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002271 return getOperand(0)->getType()->isIntegerTy() &&
2272 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002274}
2275
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276bool CastInst::isLosslessCast() const {
2277 // Only BitCast can be lossless, exit fast if we're not BitCast
2278 if (getOpcode() != Instruction::BitCast)
2279 return false;
2280
2281 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002282 Type* SrcTy = getOperand(0)->getType();
2283 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284 if (SrcTy == DstTy)
2285 return true;
2286
Reid Spencer8d9336d2006-12-31 05:26:44 +00002287 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002288 if (SrcTy->isPointerTy())
2289 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 return false; // Other types have no identity values
2291}
2292
2293/// This function determines if the CastInst does not require any bits to be
2294/// changed in order to effect the cast. Essentially, it identifies cases where
2295/// no code gen is necessary for the cast, hence the name no-op cast. For
2296/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002297/// # bitcast i32* %x to i8*
2298/// # bitcast <2 x i32> %x to <4 x i16>
2299/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002300/// @brief Determine if the described cast is a no-op.
2301bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002302 Type *SrcTy,
2303 Type *DestTy,
2304 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002305 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002306 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 case Instruction::Trunc:
2308 case Instruction::ZExt:
2309 case Instruction::SExt:
2310 case Instruction::FPTrunc:
2311 case Instruction::FPExt:
2312 case Instruction::UIToFP:
2313 case Instruction::SIToFP:
2314 case Instruction::FPToUI:
2315 case Instruction::FPToSI:
2316 return false; // These always modify bits
2317 case Instruction::BitCast:
2318 return true; // BitCast never modifies bits.
2319 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002320 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002321 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002323 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002324 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325 }
2326}
2327
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002328/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002329bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002330 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2331}
2332
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333/// This function determines if a pair of casts can be eliminated and what
2334/// opcode should be used in the elimination. This assumes that there are two
2335/// instructions like this:
2336/// * %F = firstOpcode SrcTy %x to MidTy
2337/// * %S = secondOpcode MidTy %F to DstTy
2338/// The function returns a resultOpcode so these two casts can be replaced with:
2339/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2340/// If no such cast is permited, the function returns 0.
2341unsigned CastInst::isEliminableCastPair(
2342 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002343 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 // Define the 144 possibilities for these two cast instructions. The values
2345 // in this matrix determine what to do in a given situation and select the
2346 // case in the switch below. The rows correspond to firstOp, the columns
2347 // correspond to secondOp. In looking at the table below, keep in mind
2348 // the following cast properties:
2349 //
2350 // Size Compare Source Destination
2351 // Operator Src ? Size Type Sign Type Sign
2352 // -------- ------------ ------------------- ---------------------
2353 // TRUNC > Integer Any Integral Any
2354 // ZEXT < Integral Unsigned Integer Any
2355 // SEXT < Integral Signed Integer Any
2356 // FPTOUI n/a FloatPt n/a Integral Unsigned
2357 // FPTOSI n/a FloatPt n/a Integral Signed
2358 // UITOFP n/a Integral Unsigned FloatPt n/a
2359 // SITOFP n/a Integral Signed FloatPt n/a
2360 // FPTRUNC > FloatPt n/a FloatPt n/a
2361 // FPEXT < FloatPt n/a FloatPt n/a
2362 // PTRTOINT n/a Pointer n/a Integral Unsigned
2363 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002364 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002365 //
2366 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002367 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2368 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002369 // of the produced value (we no longer know the top-part is all zeros).
2370 // Further this conversion is often much more expensive for typical hardware,
2371 // and causes issues when building libgcc. We disallow fptosi+sext for the
2372 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 const unsigned numCastOps =
2374 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2375 static const uint8_t CastResults[numCastOps][numCastOps] = {
2376 // T F F U S F F P I B -+
2377 // R Z S P P I I T P 2 N T |
2378 // U E E 2 2 2 2 R E I T C +- secondOp
2379 // N X X U S F F N X N 2 V |
2380 // C T T I I P P C T T P T -+
2381 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2382 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2383 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002384 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2385 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2387 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2388 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2389 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2390 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2391 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2392 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2393 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002394
2395 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002396 // merging. However, bitcast of A->B->A are allowed.
2397 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2398 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2399 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2400
2401 // Check if any of the bitcasts convert scalars<->vectors.
2402 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2403 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2404 // Unless we are bitcasing to the original type, disallow optimizations.
2405 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406
2407 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2408 [secondOp-Instruction::CastOpsBegin];
2409 switch (ElimCase) {
2410 case 0:
2411 // categorically disallowed
2412 return 0;
2413 case 1:
2414 // allowed, use first cast's opcode
2415 return firstOp;
2416 case 2:
2417 // allowed, use second cast's opcode
2418 return secondOp;
2419 case 3:
2420 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002421 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002422 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002423 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 return firstOp;
2425 return 0;
2426 case 4:
2427 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002428 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002429 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 return firstOp;
2431 return 0;
2432 case 5:
2433 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002434 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002435 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 return secondOp;
2437 return 0;
2438 case 6:
2439 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002440 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002441 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 return secondOp;
2443 return 0;
2444 case 7: {
2445 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002446 if (!IntPtrTy)
2447 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002448 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2449 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 if (MidSize >= PtrSize)
2451 return Instruction::BitCast;
2452 return 0;
2453 }
2454 case 8: {
2455 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2456 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2457 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002458 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2459 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 if (SrcSize == DstSize)
2461 return Instruction::BitCast;
2462 else if (SrcSize < DstSize)
2463 return firstOp;
2464 return secondOp;
2465 }
2466 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2467 return Instruction::ZExt;
2468 case 10:
2469 // fpext followed by ftrunc is allowed if the bit size returned to is
2470 // the same as the original, in which case its just a bitcast
2471 if (SrcTy == DstTy)
2472 return Instruction::BitCast;
2473 return 0; // If the types are not the same we can't eliminate it.
2474 case 11:
2475 // bitcast followed by ptrtoint is allowed as long as the bitcast
2476 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002477 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 return secondOp;
2479 return 0;
2480 case 12:
2481 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002482 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 return firstOp;
2484 return 0;
2485 case 13: {
2486 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002487 if (!IntPtrTy)
2488 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002489 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2490 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2491 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 if (SrcSize <= PtrSize && SrcSize == DstSize)
2493 return Instruction::BitCast;
2494 return 0;
2495 }
2496 case 99:
2497 // cast combination can't happen (error in input). This is for all cases
2498 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002499 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500 default:
Craig Topperc514b542012-02-05 22:14:15 +00002501 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
Chris Lattner229907c2011-07-18 04:54:35 +00002505CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002507 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 // Construct and return the appropriate CastInst subclass
2509 switch (op) {
2510 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2511 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2512 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2513 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2514 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2515 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2516 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2517 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2518 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2519 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2520 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2521 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002522 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524}
2525
Chris Lattner229907c2011-07-18 04:54:35 +00002526CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002527 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002528 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529 // Construct and return the appropriate CastInst subclass
2530 switch (op) {
2531 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2532 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2533 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2534 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2535 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2536 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2537 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2538 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2539 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2540 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2541 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2542 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002543 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545}
2546
Chris Lattner229907c2011-07-18 04:54:35 +00002547CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002548 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002549 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002550 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002551 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2552 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002553}
2554
Chris Lattner229907c2011-07-18 04:54:35 +00002555CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002556 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002557 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002558 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002559 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2560 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002561}
2562
Chris Lattner229907c2011-07-18 04:54:35 +00002563CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002564 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002565 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002566 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002567 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2568 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002569}
2570
Chris Lattner229907c2011-07-18 04:54:35 +00002571CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002573 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002574 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002575 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2576 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002577}
2578
Chris Lattner229907c2011-07-18 04:54:35 +00002579CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002581 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002582 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002583 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2584 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002585}
2586
Chris Lattner229907c2011-07-18 04:54:35 +00002587CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002588 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002589 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002590 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002591 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2592 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002593}
2594
Chris Lattner229907c2011-07-18 04:54:35 +00002595CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002596 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002597 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002598 assert(S->getType()->isPointerTy() && "Invalid cast");
2599 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002600 "Invalid cast");
2601
Duncan Sands9dff9be2010-02-15 16:12:20 +00002602 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002603 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2604 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002605}
2606
2607/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002608CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002610 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002611 assert(S->getType()->isPointerTy() && "Invalid cast");
2612 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002613 "Invalid cast");
2614
Duncan Sands9dff9be2010-02-15 16:12:20 +00002615 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002616 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2617 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002618}
2619
Chris Lattner229907c2011-07-18 04:54:35 +00002620CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002622 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002623 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002624 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002625 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2626 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002627 Instruction::CastOps opcode =
2628 (SrcBits == DstBits ? Instruction::BitCast :
2629 (SrcBits > DstBits ? Instruction::Trunc :
2630 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002631 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002632}
2633
Chris Lattner229907c2011-07-18 04:54:35 +00002634CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002636 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002637 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002638 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2640 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002641 Instruction::CastOps opcode =
2642 (SrcBits == DstBits ? Instruction::BitCast :
2643 (SrcBits > DstBits ? Instruction::Trunc :
2644 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002645 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002646}
2647
Chris Lattner229907c2011-07-18 04:54:35 +00002648CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002649 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002650 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002651 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002652 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002653 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2654 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002655 Instruction::CastOps opcode =
2656 (SrcBits == DstBits ? Instruction::BitCast :
2657 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002658 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002659}
2660
Chris Lattner229907c2011-07-18 04:54:35 +00002661CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002662 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002663 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002664 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002665 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002666 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2667 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002668 Instruction::CastOps opcode =
2669 (SrcBits == DstBits ? Instruction::BitCast :
2670 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002671 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002672}
2673
Duncan Sands55e50902008-01-06 10:12:28 +00002674// Check whether it is valid to call getCastOpcode for these types.
2675// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002676bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002677 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2678 return false;
2679
2680 if (SrcTy == DestTy)
2681 return true;
2682
Chris Lattner229907c2011-07-18 04:54:35 +00002683 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2684 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002685 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2686 // An element by element cast. Valid if casting the elements is valid.
2687 SrcTy = SrcVecTy->getElementType();
2688 DestTy = DestVecTy->getElementType();
2689 }
2690
Duncan Sands55e50902008-01-06 10:12:28 +00002691 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002692 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2693 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002694
2695 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002696 if (DestTy->isIntegerTy()) { // Casting to integral
2697 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002698 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002699 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002700 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002701 } else if (SrcTy->isVectorTy()) { // Casting from vector
2702 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002703 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002704 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002705 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002706 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2707 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002708 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002709 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002710 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002711 } else if (SrcTy->isVectorTy()) { // Casting from vector
2712 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002713 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002714 return false;
2715 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002716 } else if (DestTy->isVectorTy()) { // Casting to vector
2717 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002718 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002719 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002720 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002721 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002722 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002723 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002724 return false;
2725 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002726 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002727 if (SrcTy->isVectorTy()) {
2728 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002729 } else {
2730 return false;
2731 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002732 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002733 return false;
2734 }
2735}
2736
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737// Provide a way to get a "cast" where the cast opcode is inferred from the
2738// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002739// logic in the castIsValid function below. This axiom should hold:
2740// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2741// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002743// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002744Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002745CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002746 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2747 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748
Duncan Sands55e50902008-01-06 10:12:28 +00002749 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2750 "Only first class types are castable!");
2751
Duncan Sandsa8514532011-05-18 07:13:41 +00002752 if (SrcTy == DestTy)
2753 return BitCast;
2754
Chris Lattner229907c2011-07-18 04:54:35 +00002755 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2756 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002757 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2758 // An element by element cast. Find the appropriate opcode based on the
2759 // element types.
2760 SrcTy = SrcVecTy->getElementType();
2761 DestTy = DestVecTy->getElementType();
2762 }
2763
2764 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002765 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2766 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002767
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002768 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002769 if (DestTy->isIntegerTy()) { // Casting to integral
2770 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 if (DestBits < SrcBits)
2772 return Trunc; // int -> smaller int
2773 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002774 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775 return SExt; // signed -> SEXT
2776 else
2777 return ZExt; // unsigned -> ZEXT
2778 } else {
2779 return BitCast; // Same size, No-op cast
2780 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002781 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002782 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783 return FPToSI; // FP -> sint
2784 else
2785 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002786 } else if (SrcTy->isVectorTy()) {
2787 assert(DestBits == SrcBits &&
2788 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002789 return BitCast; // Same size, no-op cast
2790 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002791 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002792 "Casting from a value that is not first-class type");
2793 return PtrToInt; // ptr -> int
2794 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002795 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2796 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002797 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002798 return SIToFP; // sint -> FP
2799 else
2800 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002801 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002802 if (DestBits < SrcBits) {
2803 return FPTrunc; // FP -> smaller FP
2804 } else if (DestBits > SrcBits) {
2805 return FPExt; // FP -> larger FP
2806 } else {
2807 return BitCast; // same size, no-op cast
2808 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002809 } else if (SrcTy->isVectorTy()) {
2810 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002811 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002812 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002814 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002815 } else if (DestTy->isVectorTy()) {
2816 assert(DestBits == SrcBits &&
2817 "Illegal cast to vector (wrong type or size)");
2818 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002819 } else if (DestTy->isPointerTy()) {
2820 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002821 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002822 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002825 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002826 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002827 if (SrcTy->isVectorTy()) {
2828 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002829 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002830 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002831 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002832 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002833 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002834}
2835
2836//===----------------------------------------------------------------------===//
2837// CastInst SubClass Constructors
2838//===----------------------------------------------------------------------===//
2839
2840/// Check that the construction parameters for a CastInst are correct. This
2841/// could be broken out into the separate constructors but it is useful to have
2842/// it in one place and to eliminate the redundant code for getting the sizes
2843/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002844bool
Chris Lattner229907c2011-07-18 04:54:35 +00002845CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002846
2847 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002848 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002849 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2850 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851 return false;
2852
2853 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002854 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2855 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002856
Duncan Sands7f646562011-05-18 09:21:57 +00002857 // If these are vector types, get the lengths of the vectors (using zero for
2858 // scalar types means that checking that vector lengths match also checks that
2859 // scalars are not being converted to vectors or vectors to scalars).
2860 unsigned SrcLength = SrcTy->isVectorTy() ?
2861 cast<VectorType>(SrcTy)->getNumElements() : 0;
2862 unsigned DstLength = DstTy->isVectorTy() ?
2863 cast<VectorType>(DstTy)->getNumElements() : 0;
2864
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865 // Switch on the opcode provided
2866 switch (op) {
2867 default: return false; // This is an input error
2868 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002869 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2870 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002872 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2873 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002874 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002875 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2876 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002878 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2879 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002880 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002881 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2882 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002884 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002885 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2886 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002887 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002888 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002889 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2890 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002892 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002893 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002894 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2895 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2896 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002897 return SrcTy->getScalarType()->isPointerTy() &&
2898 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002899 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002900 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002901 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002902 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2903 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2904 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002905 return SrcTy->getScalarType()->isIntegerTy() &&
2906 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907 case Instruction::BitCast:
2908 // BitCast implies a no-op cast of type only. No bits change.
2909 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002910 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911 return false;
2912
Duncan Sands55e50902008-01-06 10:12:28 +00002913 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914 // these cases, the cast is okay if the source and destination bit widths
2915 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002916 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917 }
2918}
2919
2920TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002921 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002923 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002924}
2925
2926TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002927 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002929 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002930}
2931
2932ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002933 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002934) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002935 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002936}
2937
2938ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002939 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002940) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002941 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002942}
2943SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002944 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002946 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947}
2948
Jeff Cohencc08c832006-12-02 02:22:01 +00002949SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002950 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002952 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953}
2954
2955FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002956 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002957) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002958 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959}
2960
2961FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002962 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002963) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002964 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002965}
2966
2967FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002968 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002970 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971}
2972
2973FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002974 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002976 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977}
2978
2979UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002980 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002982 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983}
2984
2985UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002986 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002988 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002989}
2990
2991SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002992 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002993) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002994 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995}
2996
2997SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002998 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003000 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003001}
3002
3003FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003004 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003005) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003006 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003007}
3008
3009FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003010 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003012 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013}
3014
3015FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003016 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003018 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003019}
3020
3021FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003022 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003024 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003025}
3026
3027PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003028 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003030 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031}
3032
3033PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003034 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003036 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003037}
3038
3039IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003040 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003042 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043}
3044
3045IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003046 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003048 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049}
3050
3051BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003052 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003054 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055}
3056
3057BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003058 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003060 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003062
3063//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003064// CmpInst Classes
3065//===----------------------------------------------------------------------===//
3066
Craig Topper3186c012012-09-23 02:12:10 +00003067void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003068
Chris Lattner229907c2011-07-18 04:54:35 +00003069CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003070 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003071 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003072 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003073 OperandTraits<CmpInst>::op_begin(this),
3074 OperandTraits<CmpInst>::operands(this),
3075 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003076 Op<0>() = LHS;
3077 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003078 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003079 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003080}
Gabor Greiff6caff662008-05-10 08:32:32 +00003081
Chris Lattner229907c2011-07-18 04:54:35 +00003082CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003083 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003084 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003085 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003086 OperandTraits<CmpInst>::op_begin(this),
3087 OperandTraits<CmpInst>::operands(this),
3088 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003089 Op<0>() = LHS;
3090 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003091 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003092 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003093}
3094
3095CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003096CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003097 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003098 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003099 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003100 if (InsertBefore)
3101 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3102 S1, S2, Name);
3103 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003104 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003105 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003106 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003107
3108 if (InsertBefore)
3109 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3110 S1, S2, Name);
3111 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003112 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003113 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003114}
3115
3116CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003117CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003118 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003119 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003120 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3121 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003122 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003123 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3124 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003125}
3126
3127void CmpInst::swapOperands() {
3128 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3129 IC->swapOperands();
3130 else
3131 cast<FCmpInst>(this)->swapOperands();
3132}
3133
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003134bool CmpInst::isCommutative() const {
3135 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003136 return IC->isCommutative();
3137 return cast<FCmpInst>(this)->isCommutative();
3138}
3139
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003140bool CmpInst::isEquality() const {
3141 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003142 return IC->isEquality();
3143 return cast<FCmpInst>(this)->isEquality();
3144}
3145
3146
Dan Gohman4e724382008-05-31 02:47:54 +00003147CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003148 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003149 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003150 case ICMP_EQ: return ICMP_NE;
3151 case ICMP_NE: return ICMP_EQ;
3152 case ICMP_UGT: return ICMP_ULE;
3153 case ICMP_ULT: return ICMP_UGE;
3154 case ICMP_UGE: return ICMP_ULT;
3155 case ICMP_ULE: return ICMP_UGT;
3156 case ICMP_SGT: return ICMP_SLE;
3157 case ICMP_SLT: return ICMP_SGE;
3158 case ICMP_SGE: return ICMP_SLT;
3159 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003160
Dan Gohman4e724382008-05-31 02:47:54 +00003161 case FCMP_OEQ: return FCMP_UNE;
3162 case FCMP_ONE: return FCMP_UEQ;
3163 case FCMP_OGT: return FCMP_ULE;
3164 case FCMP_OLT: return FCMP_UGE;
3165 case FCMP_OGE: return FCMP_ULT;
3166 case FCMP_OLE: return FCMP_UGT;
3167 case FCMP_UEQ: return FCMP_ONE;
3168 case FCMP_UNE: return FCMP_OEQ;
3169 case FCMP_UGT: return FCMP_OLE;
3170 case FCMP_ULT: return FCMP_OGE;
3171 case FCMP_UGE: return FCMP_OLT;
3172 case FCMP_ULE: return FCMP_OGT;
3173 case FCMP_ORD: return FCMP_UNO;
3174 case FCMP_UNO: return FCMP_ORD;
3175 case FCMP_TRUE: return FCMP_FALSE;
3176 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003177 }
3178}
3179
Reid Spencer266e42b2006-12-23 06:05:41 +00003180ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3181 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003182 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003183 case ICMP_EQ: case ICMP_NE:
3184 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3185 return pred;
3186 case ICMP_UGT: return ICMP_SGT;
3187 case ICMP_ULT: return ICMP_SLT;
3188 case ICMP_UGE: return ICMP_SGE;
3189 case ICMP_ULE: return ICMP_SLE;
3190 }
3191}
3192
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003193ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3194 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003195 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003196 case ICMP_EQ: case ICMP_NE:
3197 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3198 return pred;
3199 case ICMP_SGT: return ICMP_UGT;
3200 case ICMP_SLT: return ICMP_ULT;
3201 case ICMP_SGE: return ICMP_UGE;
3202 case ICMP_SLE: return ICMP_ULE;
3203 }
3204}
3205
Reid Spencer0286bc12007-02-28 22:00:54 +00003206/// Initialize a set of values that all satisfy the condition with C.
3207///
3208ConstantRange
3209ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3210 APInt Lower(C);
3211 APInt Upper(C);
3212 uint32_t BitWidth = C.getBitWidth();
3213 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003214 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003215 case ICmpInst::ICMP_EQ: Upper++; break;
3216 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003217 case ICmpInst::ICMP_ULT:
3218 Lower = APInt::getMinValue(BitWidth);
3219 // Check for an empty-set condition.
3220 if (Lower == Upper)
3221 return ConstantRange(BitWidth, /*isFullSet=*/false);
3222 break;
3223 case ICmpInst::ICMP_SLT:
3224 Lower = APInt::getSignedMinValue(BitWidth);
3225 // Check for an empty-set condition.
3226 if (Lower == Upper)
3227 return ConstantRange(BitWidth, /*isFullSet=*/false);
3228 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003229 case ICmpInst::ICMP_UGT:
3230 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003231 // Check for an empty-set condition.
3232 if (Lower == Upper)
3233 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003234 break;
3235 case ICmpInst::ICMP_SGT:
3236 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003237 // Check for an empty-set condition.
3238 if (Lower == Upper)
3239 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003240 break;
3241 case ICmpInst::ICMP_ULE:
3242 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003243 // Check for a full-set condition.
3244 if (Lower == Upper)
3245 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003246 break;
3247 case ICmpInst::ICMP_SLE:
3248 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003249 // Check for a full-set condition.
3250 if (Lower == Upper)
3251 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003252 break;
3253 case ICmpInst::ICMP_UGE:
3254 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003255 // Check for a full-set condition.
3256 if (Lower == Upper)
3257 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003258 break;
3259 case ICmpInst::ICMP_SGE:
3260 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003261 // Check for a full-set condition.
3262 if (Lower == Upper)
3263 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003264 break;
3265 }
3266 return ConstantRange(Lower, Upper);
3267}
3268
Dan Gohman4e724382008-05-31 02:47:54 +00003269CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003270 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003271 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003272 case ICMP_EQ: case ICMP_NE:
3273 return pred;
3274 case ICMP_SGT: return ICMP_SLT;
3275 case ICMP_SLT: return ICMP_SGT;
3276 case ICMP_SGE: return ICMP_SLE;
3277 case ICMP_SLE: return ICMP_SGE;
3278 case ICMP_UGT: return ICMP_ULT;
3279 case ICMP_ULT: return ICMP_UGT;
3280 case ICMP_UGE: return ICMP_ULE;
3281 case ICMP_ULE: return ICMP_UGE;
3282
Reid Spencerd9436b62006-11-20 01:22:35 +00003283 case FCMP_FALSE: case FCMP_TRUE:
3284 case FCMP_OEQ: case FCMP_ONE:
3285 case FCMP_UEQ: case FCMP_UNE:
3286 case FCMP_ORD: case FCMP_UNO:
3287 return pred;
3288 case FCMP_OGT: return FCMP_OLT;
3289 case FCMP_OLT: return FCMP_OGT;
3290 case FCMP_OGE: return FCMP_OLE;
3291 case FCMP_OLE: return FCMP_OGE;
3292 case FCMP_UGT: return FCMP_ULT;
3293 case FCMP_ULT: return FCMP_UGT;
3294 case FCMP_UGE: return FCMP_ULE;
3295 case FCMP_ULE: return FCMP_UGE;
3296 }
3297}
3298
Reid Spencer266e42b2006-12-23 06:05:41 +00003299bool CmpInst::isUnsigned(unsigned short predicate) {
3300 switch (predicate) {
3301 default: return false;
3302 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3303 case ICmpInst::ICMP_UGE: return true;
3304 }
3305}
3306
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003307bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003308 switch (predicate) {
3309 default: return false;
3310 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3311 case ICmpInst::ICMP_SGE: return true;
3312 }
3313}
3314
3315bool CmpInst::isOrdered(unsigned short predicate) {
3316 switch (predicate) {
3317 default: return false;
3318 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3319 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3320 case FCmpInst::FCMP_ORD: return true;
3321 }
3322}
3323
3324bool CmpInst::isUnordered(unsigned short predicate) {
3325 switch (predicate) {
3326 default: return false;
3327 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3328 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3329 case FCmpInst::FCMP_UNO: return true;
3330 }
3331}
3332
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003333bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3334 switch(predicate) {
3335 default: return false;
3336 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3337 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3338 }
3339}
3340
3341bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3342 switch(predicate) {
3343 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3344 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3345 default: return false;
3346 }
3347}
3348
3349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003350//===----------------------------------------------------------------------===//
3351// SwitchInst Implementation
3352//===----------------------------------------------------------------------===//
3353
Chris Lattnerbaf00152010-11-17 05:41:46 +00003354void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3355 assert(Value && Default && NumReserved);
3356 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003357 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003358 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003359
Gabor Greif2d3024d2008-05-26 21:33:52 +00003360 OperandList[0] = Value;
3361 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003362}
3363
Chris Lattner2195fc42007-02-24 00:55:48 +00003364/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3365/// switch on and a default destination. The number of additional cases can
3366/// be specified here to make memory allocation more efficient. This
3367/// constructor can also autoinsert before another instruction.
3368SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3369 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003370 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3371 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003372 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003373}
3374
3375/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3376/// switch on and a default destination. The number of additional cases can
3377/// be specified here to make memory allocation more efficient. This
3378/// constructor also autoinserts at the end of the specified BasicBlock.
3379SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3380 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003381 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3382 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003383 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003384}
3385
Misha Brukmanb1c93172005-04-21 23:48:37 +00003386SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003387 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3388 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3389 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003390 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003391 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003392 OL[i] = InOL[i];
3393 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003394 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003395 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003396 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003397}
3398
Gordon Henriksen14a55692007-12-10 02:14:30 +00003399SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003400 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003401}
3402
3403
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003404/// addCase - Add an entry to the switch instruction...
3405///
Chris Lattner47ac1872005-02-24 05:32:09 +00003406void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003407 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003408
3409 // FIXME: Currently we work with ConstantInt based cases.
3410 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003411 Mapping.add(IntItem::fromConstantInt(OnVal));
3412 IntegersSubset CaseRanges = Mapping.getCase();
3413 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003414}
3415
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003416void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003417 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003418 unsigned OpNo = NumOperands;
3419 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003420 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003421 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003422 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003423 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003424
3425 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3426
3427 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3428 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003429 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003430}
3431
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003432/// removeCase - This method removes the specified case and its successor
3433/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003434void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003435 unsigned idx = i.getCaseIndex();
3436
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003437 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003438
3439 unsigned NumOps = getNumOperands();
3440 Use *OL = OperandList;
3441
Jay Foad14277722011-02-01 09:22:34 +00003442 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003443 if (2 + (idx + 1) * 2 != NumOps) {
3444 OL[2 + idx * 2] = OL[NumOps - 2];
3445 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003446 }
3447
3448 // Nuke the last value.
3449 OL[NumOps-2].set(0);
3450 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003451
3452 // Do the same with TheCases collection:
3453 if (i.SubsetIt != --TheSubsets.end()) {
3454 *i.SubsetIt = TheSubsets.back();
3455 TheSubsets.pop_back();
3456 } else {
3457 TheSubsets.pop_back();
3458 i.SubsetIt = TheSubsets.end();
3459 }
3460
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003461 NumOperands = NumOps-2;
3462}
3463
Jay Foade98f29d2011-04-01 08:00:58 +00003464/// growOperands - grow operands - This grows the operand list in response
3465/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003466///
Jay Foade98f29d2011-04-01 08:00:58 +00003467void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003468 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003469 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003470
3471 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003472 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003473 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003474 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003475 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003476 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003477 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003478 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003479}
3480
3481
3482BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3483 return getSuccessor(idx);
3484}
3485unsigned SwitchInst::getNumSuccessorsV() const {
3486 return getNumSuccessors();
3487}
3488void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3489 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003490}
Chris Lattnerf22be932004-10-15 23:52:53 +00003491
Chris Lattner3ed871f2009-10-27 19:13:16 +00003492//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003493// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003494//===----------------------------------------------------------------------===//
3495
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003496void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003497 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003498 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003499 ReservedSpace = 1+NumDests;
3500 NumOperands = 1;
3501 OperandList = allocHungoffUses(ReservedSpace);
3502
3503 OperandList[0] = Address;
3504}
3505
3506
Jay Foade98f29d2011-04-01 08:00:58 +00003507/// growOperands - grow operands - This grows the operand list in response
3508/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003509///
Jay Foade98f29d2011-04-01 08:00:58 +00003510void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003511 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003512 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003513
3514 ReservedSpace = NumOps;
3515 Use *NewOps = allocHungoffUses(NumOps);
3516 Use *OldOps = OperandList;
3517 for (unsigned i = 0; i != e; ++i)
3518 NewOps[i] = OldOps[i];
3519 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003520 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003521}
3522
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003523IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3524 Instruction *InsertBefore)
3525: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003526 0, 0, InsertBefore) {
3527 init(Address, NumCases);
3528}
3529
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003530IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3531 BasicBlock *InsertAtEnd)
3532: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003533 0, 0, InsertAtEnd) {
3534 init(Address, NumCases);
3535}
3536
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003537IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3538 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003539 allocHungoffUses(IBI.getNumOperands()),
3540 IBI.getNumOperands()) {
3541 Use *OL = OperandList, *InOL = IBI.OperandList;
3542 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3543 OL[i] = InOL[i];
3544 SubclassOptionalData = IBI.SubclassOptionalData;
3545}
3546
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003547IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003548 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003549}
3550
3551/// addDestination - Add a destination.
3552///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003553void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003554 unsigned OpNo = NumOperands;
3555 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003556 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003557 // Initialize some new operands.
3558 assert(OpNo < ReservedSpace && "Growing didn't work!");
3559 NumOperands = OpNo+1;
3560 OperandList[OpNo] = DestBB;
3561}
3562
3563/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003564/// indirectbr instruction.
3565void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003566 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3567
3568 unsigned NumOps = getNumOperands();
3569 Use *OL = OperandList;
3570
3571 // Replace this value with the last one.
3572 OL[idx+1] = OL[NumOps-1];
3573
3574 // Nuke the last value.
3575 OL[NumOps-1].set(0);
3576 NumOperands = NumOps-1;
3577}
3578
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003579BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003580 return getSuccessor(idx);
3581}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003582unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003583 return getNumSuccessors();
3584}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003585void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003586 setSuccessor(idx, B);
3587}
3588
3589//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003590// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003591//===----------------------------------------------------------------------===//
3592
Chris Lattnerf22be932004-10-15 23:52:53 +00003593// Define these methods here so vtables don't get emitted into every translation
3594// unit that uses these classes.
3595
Devang Patel11cf3f42009-10-27 22:16:29 +00003596GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3597 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003598}
3599
Devang Patel11cf3f42009-10-27 22:16:29 +00003600BinaryOperator *BinaryOperator::clone_impl() const {
3601 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003602}
3603
Devang Patel11cf3f42009-10-27 22:16:29 +00003604FCmpInst* FCmpInst::clone_impl() const {
3605 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003606}
3607
Devang Patel11cf3f42009-10-27 22:16:29 +00003608ICmpInst* ICmpInst::clone_impl() const {
3609 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003610}
3611
Devang Patel11cf3f42009-10-27 22:16:29 +00003612ExtractValueInst *ExtractValueInst::clone_impl() const {
3613 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003614}
3615
Devang Patel11cf3f42009-10-27 22:16:29 +00003616InsertValueInst *InsertValueInst::clone_impl() const {
3617 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003618}
3619
Devang Patel11cf3f42009-10-27 22:16:29 +00003620AllocaInst *AllocaInst::clone_impl() const {
3621 return new AllocaInst(getAllocatedType(),
3622 (Value*)getOperand(0),
3623 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003624}
3625
Devang Patel11cf3f42009-10-27 22:16:29 +00003626LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003627 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3628 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003629}
3630
Devang Patel11cf3f42009-10-27 22:16:29 +00003631StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003632 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003633 getAlignment(), getOrdering(), getSynchScope());
3634
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003635}
3636
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003637AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3638 AtomicCmpXchgInst *Result =
3639 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3640 getOrdering(), getSynchScope());
3641 Result->setVolatile(isVolatile());
3642 return Result;
3643}
3644
3645AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3646 AtomicRMWInst *Result =
3647 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3648 getOrdering(), getSynchScope());
3649 Result->setVolatile(isVolatile());
3650 return Result;
3651}
3652
Eli Friedmanfee02c62011-07-25 23:16:38 +00003653FenceInst *FenceInst::clone_impl() const {
3654 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3655}
3656
Devang Patel11cf3f42009-10-27 22:16:29 +00003657TruncInst *TruncInst::clone_impl() const {
3658 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003659}
3660
Devang Patel11cf3f42009-10-27 22:16:29 +00003661ZExtInst *ZExtInst::clone_impl() const {
3662 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003663}
3664
Devang Patel11cf3f42009-10-27 22:16:29 +00003665SExtInst *SExtInst::clone_impl() const {
3666 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003667}
3668
Devang Patel11cf3f42009-10-27 22:16:29 +00003669FPTruncInst *FPTruncInst::clone_impl() const {
3670 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003671}
3672
Devang Patel11cf3f42009-10-27 22:16:29 +00003673FPExtInst *FPExtInst::clone_impl() const {
3674 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003675}
3676
Devang Patel11cf3f42009-10-27 22:16:29 +00003677UIToFPInst *UIToFPInst::clone_impl() const {
3678 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003679}
3680
Devang Patel11cf3f42009-10-27 22:16:29 +00003681SIToFPInst *SIToFPInst::clone_impl() const {
3682 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003683}
3684
Devang Patel11cf3f42009-10-27 22:16:29 +00003685FPToUIInst *FPToUIInst::clone_impl() const {
3686 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003687}
3688
Devang Patel11cf3f42009-10-27 22:16:29 +00003689FPToSIInst *FPToSIInst::clone_impl() const {
3690 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003691}
3692
Devang Patel11cf3f42009-10-27 22:16:29 +00003693PtrToIntInst *PtrToIntInst::clone_impl() const {
3694 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003695}
3696
Devang Patel11cf3f42009-10-27 22:16:29 +00003697IntToPtrInst *IntToPtrInst::clone_impl() const {
3698 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003699}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003700
Devang Patel11cf3f42009-10-27 22:16:29 +00003701BitCastInst *BitCastInst::clone_impl() const {
3702 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003703}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003704
Devang Patel11cf3f42009-10-27 22:16:29 +00003705CallInst *CallInst::clone_impl() const {
3706 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003707}
3708
Devang Patel11cf3f42009-10-27 22:16:29 +00003709SelectInst *SelectInst::clone_impl() const {
3710 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003711}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003712
Devang Patel11cf3f42009-10-27 22:16:29 +00003713VAArgInst *VAArgInst::clone_impl() const {
3714 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003715}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003716
Devang Patel11cf3f42009-10-27 22:16:29 +00003717ExtractElementInst *ExtractElementInst::clone_impl() const {
3718 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003719}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003720
Devang Patel11cf3f42009-10-27 22:16:29 +00003721InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003722 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003723}
3724
Devang Patel11cf3f42009-10-27 22:16:29 +00003725ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003726 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003727}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003728
Devang Patel11cf3f42009-10-27 22:16:29 +00003729PHINode *PHINode::clone_impl() const {
3730 return new PHINode(*this);
3731}
3732
Bill Wendlingfae14752011-08-12 20:24:12 +00003733LandingPadInst *LandingPadInst::clone_impl() const {
3734 return new LandingPadInst(*this);
3735}
3736
Devang Patel11cf3f42009-10-27 22:16:29 +00003737ReturnInst *ReturnInst::clone_impl() const {
3738 return new(getNumOperands()) ReturnInst(*this);
3739}
3740
3741BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003742 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003743}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003744
Devang Patel11cf3f42009-10-27 22:16:29 +00003745SwitchInst *SwitchInst::clone_impl() const {
3746 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003747}
3748
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003749IndirectBrInst *IndirectBrInst::clone_impl() const {
3750 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751}
3752
3753
Devang Patel11cf3f42009-10-27 22:16:29 +00003754InvokeInst *InvokeInst::clone_impl() const {
3755 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003756}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003757
Bill Wendlingf891bf82011-07-31 06:30:59 +00003758ResumeInst *ResumeInst::clone_impl() const {
3759 return new(1) ResumeInst(*this);
3760}
3761
Devang Patel11cf3f42009-10-27 22:16:29 +00003762UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003763 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003764 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003765}